ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
8 {
9  const BACKGROUND_IMAGE_NAME = 'background.jpg';
10  const BACKGROUND_THUMBNAIL_IMAGE_NAME = 'background.jpg.thumb.jpg';
11  const BACKGROUND_TEMPORARY_FILENAME = 'background_upload.tmp';
12 
16  private $fileUpload;
17 
22 
26  private $language;
27 
31  private $rootDirectory;
32 
36  private $fileSystem;
37 
41  private $utilHelper;
42 
47 
51  private $clientId;
52 
57 
70  public function __construct(
71  \ILIAS\FileUpload\FileUpload $fileUpload,
72  string $certificatePath,
74  ilLogger $logger,
77  ilCertificateFileUtilsHelper $certificateFileUtilsHelper = null,
79  string $rootDirectory = CLIENT_WEB_DIR,
80  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 
125  public function uploadBackgroundImage(string $imageTempFilename, int $version)
126  {
127  $imagepath = $this->rootDirectory . $this->certificatePath;
128 
129  if (!$this->fileSystem->hasDir($imagepath)) {
130  ilUtil::makeDirParents($imagepath);
131  }
132 
133  $backgroundImageTempFilePath = $this->createBackgroundImageTempfilePath();
134 
135  $this->uploadFile($imageTempFilename, $backgroundImageTempFilePath);
136 
137  $backgroundImagePath = $this->certificatePath . 'background_' . $version . '.jpg';
138 
139  $this->utilHelper->convertImage(
140  $backgroundImageTempFilePath,
141  $this->rootDirectory . $backgroundImagePath,
142  'JPEG'
143  );
144 
145  $backgroundImageThumbnailPath = $this->createBackgroundImageThumbPath();
146 
147  $this->utilHelper->convertImage(
148  $backgroundImageTempFilePath,
149  $backgroundImageThumbnailPath,
150  'JPEG',
151  100
152  );
153 
154  $convert_filename = self::BACKGROUND_IMAGE_NAME;
155 
156  if (!$this->fileSystem->has($backgroundImagePath)) {
157  // something went wrong converting the file. use the original file and hope, that PDF can work with it
158  if (!ilUtil::moveUploadedFile($backgroundImageTempFilePath, $convert_filename, $this->rootDirectory . $backgroundImagePath)) {
159  throw new ilException('Unable to convert the file and the original file');
160  }
161  }
162 
163  $this->fileSystem->delete($this->certificatePath . self::BACKGROUND_TEMPORARY_FILENAME);
164 
165  if ($this->fileSystem->has($backgroundImagePath)) {
166  return $this->certificatePath . 'background_' . $version . '.jpg';
167  }
168 
169  throw new ilException('The given temporary filename is empty');
170  }
171 
179  private function uploadFile(string $temporaryFilename, string $targetFileName)
180  {
181  $targetFilename = basename($targetFileName);
182  $targetFilename = $this->fileUtilsHelper->getValidFilename($targetFilename);
183 
184  $targetFilesystem = $this->getTargetFilesystem($targetFileName);
185  $targetDir = $this->getTargetDir($targetFileName);
186 
187  if (false === $this->fileUpload->hasBeenProcessed()) {
188  $this->fileUpload->process();
189  }
190 
191  if (false === $this->fileUpload->hasUploads()) {
192  throw new ilException($this->language->txt('upload_error_file_not_found'));
193  }
194 
198  $uploadResults = $this->fileUpload->getResults();
199  $uploadResult = $uploadResults[$temporaryFilename];
200 
201  $processingStatus = $uploadResult->getStatus();
202  if ($processingStatus->getCode() === ILIAS\FileUpload\DTO\ProcessingStatus::REJECTED) {
203  throw new ilException($processingStatus->getMessage());
204  }
205 
206  $this->fileUpload->moveOneFileTo(
207  $uploadResult,
208  $targetDir,
209  $targetFilesystem,
210  $targetFilename,
211  true
212  );
213  }
214 
219  private function getTargetFilesystem(string $target)
220  {
221  switch (true) {
222  case strpos($target, $this->rootDirectory . '/' . $this->clientId) === 0:
223  case strpos($target, './' . $this->rootDirectory . '/' . $this->clientId) === 0:
224  case strpos($target, $this->rootDirectory) === 0:
225  $targetFilesystem = \ILIAS\FileUpload\Location::WEB;
226  break;
227  case strpos($target, CLIENT_DATA_DIR . "/temp") === 0:
228  $targetFilesystem = \ILIAS\FileUpload\Location::TEMPORARY;
229  break;
230  case strpos($target, CLIENT_DATA_DIR) === 0:
231  $targetFilesystem = \ILIAS\FileUpload\Location::STORAGE;
232  break;
233  case strpos($target, ILIAS_ABSOLUTE_PATH . '/Customizing') === 0:
234  $targetFilesystem = \ILIAS\FileUpload\Location::CUSTOMIZING;
235  break;
236  default:
237  throw new InvalidArgumentException("Can not move files to \"$target\" because path can not be mapped to web, storage or customizing location.");
238  }
239 
240  return $targetFilesystem;
241  }
242 
247  private function getTargetDir(string $target)
248  {
249  $absTargetDir = dirname($target);
250  $targetDir = $this->legacyPathHelper->createRelativePath($absTargetDir);
251 
252  return $targetDir;
253  }
254 
261  {
262  return $this->rootDirectory . $this->certificatePath . self::BACKGROUND_TEMPORARY_FILENAME;
263  }
264 
271  {
272  return $this->rootDirectory . $this->certificatePath . self::BACKGROUND_THUMBNAIL_IMAGE_NAME;
273  }
274 }
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
global $DIC
Definition: saml.php:7
uploadBackgroundImage(string $imageTempFilename, int $version)
Uploads a background image for the certificate.
Class BaseForm.
$version
Definition: build.php:27
const CUSTOMIZING
The filesystem within the web root where all the skins and plugins are saved.
Definition: Location.php:33
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.
language handling
Component logger with individual log levels by component id.
$target
Definition: test.php:19
Class FlySystemFileAccessTest.
__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)
const WEB
The filesystem within the ilias web root.
Definition: Location.php:23