ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCertificateBackgroundImageUpload.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
7 
12 {
13  const BACKGROUND_IMAGE_NAME = 'background.jpg';
14  const BACKGROUND_THUMBNAIL_IMAGE_NAME = 'background.jpg.thumb.jpg';
15  const BACKGROUND_TEMPORARY_FILENAME = 'background_upload.tmp';
16 
20  private $fileUpload;
21 
26 
30  private $language;
31 
35  private $rootDirectory;
36 
40  private $fileSystem;
41 
45  private $utilHelper;
46 
51 
55  private $clientId;
56 
64  private $logger;
65 
68 
69  public function __construct(
70  \ILIAS\FileUpload\FileUpload $fileUpload,
71  string $certificatePath,
76  ilCertificateFileUtilsHelper $certificateFileUtilsHelper = null,
79  string $clientID = CLIENT_ID,
81  ) {
82  $this->fileUpload = $fileUpload;
83  $this->certificatePath = $certificatePath;
84  $this->language = $language;
85  $this->logger = $logger;
86  $this->rootDirectory = $rootDirectory;
87 
88  if (null === $fileSystem) {
89  global $DIC;
90  $fileSystem = $DIC->filesystem()->web();
91  }
92  $this->fileSystem = $fileSystem;
93 
94  if (null === $utilHelper) {
96  }
97  $this->utilHelper = $utilHelper;
98 
99  if (null === $certificateFileUtilsHelper) {
100  $certificateFileUtilsHelper = new ilCertificateFileUtilsHelper();
101  }
102  $this->fileUtilsHelper = $certificateFileUtilsHelper;
103 
104  if (null === $legacyPathHelper) {
106  }
107  $this->legacyPathHelper = $legacyPathHelper;
108 
109  $this->clientId = $clientID;
110 
111  if (null === $tmp_file_system) {
112  global $DIC;
113  $tmp_file_system = $DIC->filesystem()->temp();
114  }
115  $this->tmp_file_system = $tmp_file_system;
116  }
117 
132  public function uploadBackgroundImage(string $imageTempFilename, int $version, ?array $pending_file = null)
133  {
134  $imagepath = $this->rootDirectory . $this->certificatePath;
135 
136  if (!$this->fileSystem->hasDir($imagepath)) {
137  ilUtil::makeDirParents($imagepath);
138  }
139 
140  $backgroundImageTempFilePath = $this->createBackgroundImageTempfilePath();
141 
142  $this->uploadFile($imageTempFilename, $backgroundImageTempFilePath, $pending_file);
143 
144  $backgroundImagePath = $this->certificatePath . 'background_' . $version . '.jpg';
145 
146  $this->utilHelper->convertImage(
147  $backgroundImageTempFilePath,
148  $this->rootDirectory . $backgroundImagePath,
149  'JPEG'
150  );
151 
152  $backgroundImageThumbnailPath = $this->createBackgroundImageThumbPath();
153 
154  $this->utilHelper->convertImage(
155  $backgroundImageTempFilePath,
156  $backgroundImageThumbnailPath,
157  'JPEG',
158  100
159  );
160 
161  $convert_filename = self::BACKGROUND_IMAGE_NAME;
162 
163  if (!$this->fileSystem->has($backgroundImagePath)) {
164  // something went wrong converting the file. use the original file and hope, that PDF can work with it
165  if (!ilUtil::moveUploadedFile($backgroundImageTempFilePath, $convert_filename, $this->rootDirectory . $backgroundImagePath)) {
166  throw new ilException('Unable to convert the file and the original file');
167  }
168  }
169 
170  $this->fileSystem->delete($this->certificatePath . self::BACKGROUND_TEMPORARY_FILENAME);
171 
172  if ($this->fileSystem->has($backgroundImagePath)) {
173  return $this->certificatePath . 'background_' . $version . '.jpg';
174  }
175 
176  throw new ilException('The given temporary filename is empty');
177  }
178 
187  private function uploadFile(string $temporaryFilename, string $targetFileName, ?array $pending_file = null)
188  {
189  $targetFilename = basename($targetFileName);
190  $targetFilename = $this->fileUtilsHelper->getValidFilename($targetFilename);
191 
192  $targetFilesystem = $this->getTargetFilesystem($targetFileName);
193  $targetDir = $this->getTargetDir($targetFileName);
194 
195  if (false === $this->fileUpload->hasBeenProcessed()) {
196  $this->fileUpload->process();
197  }
198 
199  if (false === $this->fileUpload->hasUploads()) {
200  throw new ilException($this->language->txt('upload_error_file_not_found'));
201  }
202 
206  $uploadResults = $this->fileUpload->getResults();
207  if (isset($uploadResults[$temporaryFilename])) {
208  $uploadResult = $uploadResults[$temporaryFilename];
209  $processingStatus = $uploadResult->getStatus();
210  if ($processingStatus->getCode() === ILIAS\FileUpload\DTO\ProcessingStatus::REJECTED) {
211  throw new ilException($processingStatus->getMessage());
212  }
213 
214  $this->fileUpload->moveOneFileTo(
215  $uploadResult,
216  $targetDir,
217  $targetFilesystem,
218  $targetFilename,
219  true
220  );
221  } elseif ($pending_file !== null && !empty($pending_file)) {
222  $stream = $this->tmp_file_system->readStream(basename($pending_file['tmp_name']));
223  $this->fileSystem->writeStream($targetDir . '/' . $targetFilename, $stream);
224  } else {
225  throw new ilException($this->language->txt('upload_error_file_not_found'));
226  }
227  }
228 
233  private function getTargetFilesystem(string $target)
234  {
235  switch (true) {
236  case strpos($target, $this->rootDirectory . '/' . $this->clientId) === 0:
237  case strpos($target, './' . $this->rootDirectory . '/' . $this->clientId) === 0:
238  case strpos($target, $this->rootDirectory) === 0:
239  $targetFilesystem = \ILIAS\FileUpload\Location::WEB;
240  break;
241  case strpos($target, CLIENT_DATA_DIR . "/temp") === 0:
242  $targetFilesystem = \ILIAS\FileUpload\Location::TEMPORARY;
243  break;
244  case strpos($target, CLIENT_DATA_DIR) === 0:
245  $targetFilesystem = \ILIAS\FileUpload\Location::STORAGE;
246  break;
247  case strpos($target, ILIAS_ABSOLUTE_PATH . '/Customizing') === 0:
248  $targetFilesystem = \ILIAS\FileUpload\Location::CUSTOMIZING;
249  break;
250  default:
251  throw new InvalidArgumentException("Can not move files to \"$target\" because path can not be mapped to web, storage or customizing location.");
252  }
253 
254  return $targetFilesystem;
255  }
256 
261  private function getTargetDir(string $target)
262  {
263  $absTargetDir = dirname($target);
264  $targetDir = $this->legacyPathHelper->createRelativePath($absTargetDir);
265 
266  return $targetDir;
267  }
268 
275  {
276  return $this->rootDirectory . $this->certificatePath . self::BACKGROUND_TEMPORARY_FILENAME;
277  }
278 
285  {
286  return $this->rootDirectory . $this->certificatePath . self::BACKGROUND_THUMBNAIL_IMAGE_NAME;
287  }
288 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
const REJECTED
Upload got rejected by a processor.
const STORAGE
The filesystem outside of the ilias web root.
Definition: Location.php:28
Class ChatMainBarProvider .
__construct(\ILIAS\FileUpload\FileUpload $fileUpload, string $certificatePath, ilLanguage $language, ilLogger $logger, \ILIAS\Filesystem\Filesystem $fileSystem=null, ilCertificateUtilHelper $utilHelper=null, ilCertificateFileUtilsHelper $certificateFileUtilsHelper=null, LegacyPathHelperHelper $legacyPathHelper=null, string $rootDirectory=CLIENT_WEB_DIR, string $clientID=CLIENT_ID, \ILIAS\Filesystem\Filesystem $tmp_file_system=null)
const CLIENT_DATA_DIR
Definition: constants.php:44
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
const CLIENT_ID
Definition: constants.php:39
global $DIC
Definition: goto.php:24
const CUSTOMIZING
The filesystem within the web root where all the skins and plugins are saved.
Definition: Location.php:33
const CLIENT_WEB_DIR
Definition: constants.php:45
uploadBackgroundImage(string $imageTempFilename, int $version, ?array $pending_file=null)
Uploads a background image for the certificate.
createBackgroundImageTempfilePath()
Returns the filesystem path of the background image temp file during upload.
Just a wrapper class to create Unit Test for other classes.
const TEMPORARY
The ILIAS temporary directory.
Definition: Location.php:38
createBackgroundImageThumbPath()
Returns the filesystem path of the background image thumbnail.
Component logger with individual log levels by component id.
language()
Definition: language.php:2
Class FlySystemFileAccessTest disabled disabled disabled.
const WEB
The filesystem within the ilias web root.
Definition: Location.php:23