TYPO3 CMS  TYPO3_6-2
ExtendedFileUtility.php
Go to the documentation of this file.
1 <?php
3 
24 
54 
55  // External static variables:
56  // Notice; some of these are overridden in the start() method with values from $GLOBALS['TYPO3_CONF_VARS']['BE']
57  // Path to unzip-program (with trailing '/')
61  public $unzipPath = '';
62 
63  // If set, the uploaded files will overwrite existing files.
67  public $dontCheckForUnique = 0;
68 
74  public $actionPerms = array(
75  // File permissions
76  'addFile' => FALSE,
77  'readFile' => FALSE,
78  'writeFile' => FALSE,
79  'copyFile' => FALSE,
80  'moveFile' => FALSE,
81  'renameFile' => FALSE,
82  'unzipFile' => FALSE,
83  'deleteFile' => FALSE,
84  // Folder permissions
85  'addFolder' => FALSE,
86  'readFolder' => FALSE,
87  'writeFolder' => FALSE,
88  'copyFolder' => FALSE,
89  'moveFolder' => FALSE,
90  'renameFolder' => FALSE,
91  'deleteFolder' => FALSE,
92  'recursivedeleteFolder' => FALSE
93  );
94 
95  // This is regarded to be the recycler folder
99  public $recyclerFN = '_recycler_';
100 
108  public $useRecycler = 1;
109 
110  // Internal, dynamic
111  // Will contain map between upload ID and the final filename
115  public $internalUploadMap = array();
116 
120  public $lastError = '';
121 
127  protected $errorMessages = array();
128 
132  protected $fileCmdMap;
133 
139  protected $fileFactory;
140 
141 
149  public function start($fileCmds) {
150  $unzipPath = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['unzip_path']);
151  if (substr($unzipPath, -1) !== '/' && is_dir($unzipPath)) {
152  // Make sure the path ends with a slash
153  $unzipPath .= '/';
154  }
155  $this->unzipPath = $unzipPath;
156  // Initialize Object Factory
158  // Initializing file processing commands:
159  $this->fileCmdMap = $fileCmds;
160  }
161 
169  public function init_actionPerms() {
171  $this->setActionPermissions();
172  }
173 
180  public function setActionPermissions(array $permissions = array()) {
181  if (empty($permissions)) {
182  $permissions = $GLOBALS['BE_USER']->getFilePermissions();
183  }
184  $this->actionPerms = $permissions;
185  }
186 
194  public function processData() {
195  $result = array();
196  if (!$this->isInit) {
197  return FALSE;
198  }
199  if (is_array($this->fileCmdMap)) {
200  // Check if there were uploads expected, but no one made
201  if ($this->fileCmdMap['upload']) {
202  $uploads = $this->fileCmdMap['upload'];
203  foreach ($uploads as $upload) {
204  if (empty($_FILES[('upload_' . $upload['data'])]['name'])
205  || (is_array($_FILES[('upload_' . $upload['data'])]['name'])
206  && empty($_FILES[('upload_' . $upload['data'])]['name'][0])
207  )
208  ) {
209  unset($this->fileCmdMap['upload'][$upload['data']]);
210  }
211  }
212  if (count($this->fileCmdMap['upload']) == 0) {
213  $this->writelog(1, 1, 108, 'No file was uploaded!', '');
214  }
215  }
216 
217  // Check if there were new folder names expected, but non given
218  if ($this->fileCmdMap['newfolder']) {
219  foreach ($this->fileCmdMap['newfolder'] as $key => $cmdArr) {
220  if (empty($cmdArr['data'])) {
221  unset($this->fileCmdMap['newfolder'][$key]);
222  }
223  }
224  if (count($this->fileCmdMap['newfolder']) === 0) {
225  $this->writeLog(6, 1, 108, 'No name for new folder given!', '');
226  }
227  }
228 
229  // Traverse each set of actions
230  foreach ($this->fileCmdMap as $action => $actionData) {
231  // Traverse all action data. More than one file might be affected at the same time.
232  if (is_array($actionData)) {
233  $result[$action] = array();
234  foreach ($actionData as $cmdArr) {
235  // Clear file stats
236  clearstatcache();
237  // Branch out based on command:
238  switch ($action) {
239  case 'delete':
240  $result[$action][] = $this->func_delete($cmdArr);
241  break;
242  case 'copy':
243  $result[$action][] = $this->func_copy($cmdArr);
244  break;
245  case 'move':
246  $result[$action][] = $this->func_move($cmdArr);
247  break;
248  case 'rename':
249  $result[$action][] = $this->func_rename($cmdArr);
250  break;
251  case 'newfolder':
252  $result[$action][] = $this->func_newfolder($cmdArr);
253  break;
254  case 'newfile':
255  $result[$action][] = $this->func_newfile($cmdArr);
256  break;
257  case 'editfile':
258  $result[$action][] = $this->func_edit($cmdArr);
259  break;
260  case 'upload':
261  $result[$action][] = $this->func_upload($cmdArr);
262  break;
263  case 'unzip':
264  $result[$action][] = $this->func_unzip($cmdArr);
265  break;
266  }
267  // Hook for post-processing the action
268  if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'])) {
269  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'] as $classRef) {
270  $hookObject = GeneralUtility::getUserObj($classRef);
271  if (!$hookObject instanceof \TYPO3\CMS\Core\Utility\File\ExtendedFileUtilityProcessDataHookInterface) {
272  throw new \UnexpectedValueException('$hookObject must implement interface TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtilityProcessDataHookInterface', 1279719168);
273  }
274  $hookObject->processData_postProcessAction($action, $cmdArr, $result[$action], $this);
275  }
276  }
277  }
278  }
279  }
280  }
281  return $result;
282  }
283 
291  public function printLogErrorMessages($redirect = '') {
294  }
295 
302  foreach ($this->getErrorMessages() as $msg) {
303  $flashMessage = GeneralUtility::makeInstance(
304  'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
305  htmlspecialchars($msg),
306  '',
307  \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
308  TRUE
309  );
310  $this->addFlashMessage($flashMessage);
311  }
312  }
313 
319  public function getErrorMessages() {
320  return $this->errorMessages;
321  }
322 
333  public function findRecycler($theFile) {
335  if (GeneralUtility::validPathStr($theFile)) {
337  $fI = GeneralUtility::split_fileref($theFile);
338  $c = 0;
339  // !!! Method has been put in the storage, can be saftely removed
340  $rDir = $fI['path'] . $this->recyclerFN;
341  while ($this->checkPathAgainstMounts($fI['path']) && $c < 20) {
342  if (@is_dir($rDir) && $this->recyclerFN != $fI['file']) {
343  return $rDir;
344  }
345  $theFile = $fI['path'];
347  $fI = GeneralUtility::split_fileref($theFile);
348  $c++;
349  }
350  }
351  }
352 
364  public function writeLog($action, $error, $details_nr, $details, $data) {
365  // Type value for tce_file.php
366  $type = 2;
367  if (is_object($this->getBackendUser())) {
368  $this->getBackendUser()->writelog($type, $action, $error, $details_nr, $details, $data);
369  }
370  if ($error > 0) {
371  $this->lastError = vsprintf($details, $data);
372  $this->errorMessages[] = $this->lastError;
373  }
374  }
375 
376  /*************************************
377  *
378  * File operation functions
379  *
380  **************************************/
388  public function func_delete(array $cmds) {
389  $result = FALSE;
390  if (!$this->isInit) {
391  return $result;
392  }
393  // Example indentifier for $cmds['data'] => "4:mypath/tomyfolder/myfile.jpg"
394  // for backwards compatibility: the combined file identifier was the path+filename
395  try {
396  $fileObject = $this->getFileObject($cmds['data']);
397  } catch (ResourceDoesNotExistException $e) {
398  $flashMessage = GeneralUtility::makeInstance(
399  'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
400  sprintf( $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileNotFound'), $cmds['data']),
401  $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileNotFound'),
403  TRUE
404  );
405  $this->addFlashMessage($flashMessage);
406 
407  return FALSE;
408  }
409  // @todo implement the recycler feature which has been removed from the original implementation
410  // checks to delete the file
411  if ($fileObject instanceof File) {
412  // check if the file still has references
413  // Exclude sys_file_metadata records as these are no use references
414  $databaseConnection = $this->getDatabaseConnection();
415  $table = 'sys_refindex';
416  $refIndexRecords = $databaseConnection->exec_SELECTgetRows(
417  '*',
418  $table,
419  'deleted=0 AND ref_table=' . $databaseConnection->fullQuoteStr('sys_file', $table)
420  . ' AND ref_uid=' . (int)$fileObject->getUid()
421  . ' AND tablename != ' . $databaseConnection->fullQuoteStr('sys_file_metadata', $table)
422  );
423  $deleteFile = TRUE;
424  if (count($refIndexRecords) > 0) {
425  $shortcutContent = array();
426  $brokenReferences = array();
427 
428  foreach ($refIndexRecords as $fileReferenceRow) {
429  if ($fileReferenceRow['tablename'] === 'sys_file_reference') {
430  $row = $this->transformFileReferenceToRecordReference($fileReferenceRow);
431  $shortcutRecord = BackendUtility::getRecord($row['tablename'], $row['recuid']);
432 
433  if ($shortcutRecord) {
434  $icon = IconUtility::getSpriteIconForRecord($row['tablename'], $shortcutRecord);
435  $onClick = 'Clickmenu.show("' . $row['tablename'] . '", "' . $row['recuid'] . '", "1", "+info,history,edit", "|", "");return false;';
436  $shortcutContent[] = '<a href="#" oncontextmenu="this.click();return false;" onclick="' . htmlspecialchars($onClick) . '">' . $icon . '</a>' . htmlspecialchars((BackendUtility::getRecordTitle($row['tablename'], $shortcutRecord) . ' [' . BackendUtility::getRecordPath($shortcutRecord['pid'], '', 80) . ']'));
437  } else {
438  $brokenReferences[] = $fileReferenceRow['ref_uid'];
439  }
440  }
441  }
442  if (!empty($brokenReferences)) {
443  // render a message that the file has broken references
444  $flashMessage = GeneralUtility::makeInstance(
445  '\\TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
446  sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileHasBrokenReferences'), count($brokenReferences)),
447  $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileHasBrokenReferences'),
449  TRUE
450  );
451  $this->addFlashMessage($flashMessage);
452  }
453  if (!empty($shortcutContent)) {
454  // render a message that the file could not be deleted
455  $flashMessage = GeneralUtility::makeInstance(
456  '\\TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
457  sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileNotDeletedHasReferences'), $fileObject->getName()) . '<br />' . implode('<br />', $shortcutContent),
458  $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileNotDeletedHasReferences'),
460  TRUE
461  );
462  $this->addFlashMessage($flashMessage);
463  $deleteFile = FALSE;
464  }
465  }
466 
467  if ($deleteFile) {
468  try {
469  $result = $fileObject->delete();
470 
471  // show the user that the file was deleted
472  $flashMessage = GeneralUtility::makeInstance(
473  '\\TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
474  sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileDeleted'), $fileObject->getName()),
475  $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileDeleted'),
477  TRUE
478  );
479  $this->addFlashMessage($flashMessage);
480  // Log success
481  $this->writelog(4, 0, 1, 'File "%s" deleted', array($fileObject->getIdentifier()));
482  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
483  $this->writelog(4, 1, 112, 'You are not allowed to access the file', array($fileObject->getIdentifier()));
484  } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
485  $this->writelog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
486  } catch (\RuntimeException $e) {
487  $this->writelog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', array($fileObject->getIdentifier()));
488  }
489  }
490  } else {
492  if (!$this->folderHasFilesInUse($fileObject)) {
493  try {
494  $result = $fileObject->delete(TRUE);
495  if ($result) {
496  // notify the user that the folder was deleted
498  $flashMessage = GeneralUtility::makeInstance(
499  'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
500  sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderDeleted'), $fileObject->getName()),
501  $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderDeleted'),
503  TRUE
504  );
505  $this->addFlashMessage($flashMessage);
506  // Log success
507  $this->writelog(4, 0, 3, 'Directory "%s" deleted', array($fileObject->getIdentifier()));
508  }
509  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
510  $this->writelog(4, 1, 120, 'Could not delete directory! Is directory "%s" empty? (You are not allowed to delete directories recursively).', array($fileObject->getIdentifier()));
511  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
512  $this->writelog(4, 1, 123, 'You are not allowed to access the directory', array($fileObject->getIdentifier()));
513  } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
514  $this->writelog(4, 1, 121, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
515  } catch (\TYPO3\CMS\Core\Resource\Exception\FileOperationErrorException $e) {
516  $this->writelog(4, 1, 120, 'Could not delete directory "%s"! Write-permission problem?', array($fileObject->getIdentifier()));
517  }
518  }
519  }
520 
521  return $result;
522  }
523 
532  public function folderHasFilesInUse(\TYPO3\CMS\Core\Resource\Folder $folder) {
533  $files = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, TRUE);
534  if (empty($files)) {
535  return FALSE;
536  }
537 
539  $fileUids = array();
540  foreach ($files as $file) {
541  $fileUids[] = $file->getUid();
542  }
543  $numberOfReferences = $this->getDatabaseConnection()->exec_SELECTcountRows(
544  '*',
545  'sys_refindex',
546  'deleted=0 AND ref_table="sys_file" AND ref_uid IN (' . implode(',', $fileUids) . ') AND tablename<>"sys_file_metadata"'
547  );
548 
549  $hasReferences = $numberOfReferences > 0;
550  if ($hasReferences) {
552  $flashMessage = GeneralUtility::makeInstance(
553  'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
554  $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences'),
555  $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences'),
557  TRUE
558  );
559  $this->addFlashMessage($flashMessage);
560  }
561 
562  return $hasReferences;
563  }
564 
572  protected function transformFileReferenceToRecordReference(array $referenceRecord) {
573  $fileReference = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
574  '*',
575  'sys_file_reference',
576  'uid=' . (int)$referenceRecord['recuid']
577  );
578  return array(
579  'recuid' => $fileReference['uid_foreign'],
580  'tablename' => $fileReference['tablenames'],
581  'field' => $fileReference['fieldname'],
582  'flexpointer' => '',
583  'softref_key' => '',
584  'sorting' => $fileReference['sorting_foreign']
585  );
586  }
587 
595  protected function getFileObject($identifier) {
596  $object = $this->fileFactory->retrieveFileOrFolderObject($identifier);
597  if (!is_object($object)) {
598  throw new \TYPO3\CMS\Core\Resource\Exception\InvalidFileException('The item ' . $identifier . ' was not a file or directory!!', 1320122453);
599  }
600  if ($object->getStorage()->getUid() === 0) {
601  throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889830);
602  }
603  return $object;
604  }
605 
619  protected function func_copy($cmds) {
620  if (!$this->isInit) {
621  return FALSE;
622  }
623  $sourceFileObject = $this->getFileObject($cmds['data']);
625  $targetFolderObject = $this->getFileObject($cmds['target']);
626  // Basic check
627  if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
628  $this->writelog(2, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
629  return FALSE;
630  }
631  // If this is TRUE, we append _XX to the file name if
632  $appendSuffixOnConflict = (string) $cmds['altName'];
633  $resultObject = NULL;
634  // Copying the file
635  if ($sourceFileObject instanceof File) {
636  try {
637  $conflictMode = $appendSuffixOnConflict !== '' ? 'renameNewFile' : 'cancel';
638  $resultObject = $sourceFileObject->copyTo($targetFolderObject, NULL, $conflictMode);
639  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
640  $this->writelog(2, 1, 114, 'You are not allowed to copy files', '');
641  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
642  $this->writelog(2, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
643  } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
644  $this->writelog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
645  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
646  $this->writelog(2, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
647  } catch (\BadMethodCallException $e) {
648  $this->writelog(3, 1, 128, 'The function to copy a file between storages is not yet implemented', array());
649  } catch (\RuntimeException $e) {
650  $this->writelog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
651  }
652  if ($resultObject) {
653  $this->writelog(2, 0, 1, 'File "%s" copied to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
654  }
655  } else {
656  // Else means this is a Folder
657  $sourceFolderObject = $sourceFileObject;
658  try {
659  $conflictMode = $appendSuffixOnConflict !== '' ? 'renameNewFile' : 'cancel';
660  $resultObject = $sourceFolderObject->copyTo($targetFolderObject, NULL, $conflictMode);
661  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
662  $this->writelog(2, 1, 125, 'You are not allowed to copy directories', '');
663  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
664  $this->writelog(2, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
665  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
666  $this->writelog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
667  } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
668  $this->writelog(2, 1, 122, 'Cannot copy folder "%s" into target folder "%s", because the target folder is already within the folder to be copied!', array($sourceFolderObject->getName(), $targetFolderObject->getName()));
669  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
670  $this->writelog(2, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
671  } catch (\BadMethodCallException $e) {
672  $this->writelog(3, 1, 129, 'The function to copy a folder between storages is not yet implemented', array());
673  } catch (\RuntimeException $e) {
674  $this->writelog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
675  }
676  if ($resultObject) {
677  $this->writelog(2, 0, 2, 'Directory "%s" copied to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
678  }
679  }
680  return $resultObject;
681  }
682 
696  protected function func_move($cmds) {
697  if (!$this->isInit) {
698  return FALSE;
699  }
700  $sourceFileObject = $this->getFileObject($cmds['data']);
701  $targetFolderObject = $this->getFileObject($cmds['target']);
702  // Basic check
703  if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
704  $this->writelog(3, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
705  return FALSE;
706  }
707  $alternativeName = (string) $cmds['altName'];
708  $resultObject = NULL;
709  // Moving the file
710  if ($sourceFileObject instanceof File) {
711  try {
712  if ($alternativeName !== '') {
713  // Don't allow overwriting existing files, but find a new name
714  $resultObject = $sourceFileObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
715  } else {
716  // Don't allow overwriting existing files
717  $resultObject = $sourceFileObject->moveTo($targetFolderObject, NULL, 'cancel');
718  }
719  $this->writelog(3, 0, 1, 'File "%s" moved to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
720  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
721  $this->writelog(3, 1, 114, 'You are not allowed to move files', '');
722  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
723  $this->writelog(3, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
724  } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
725  $this->writelog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
726  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
727  $this->writelog(3, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
728  } catch (\BadMethodCallException $e) {
729  $this->writelog(3, 1, 126, 'The function to move a file between storages is not yet implemented', array());
730  } catch (\RuntimeException $e) {
731  $this->writelog(3, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
732  }
733  } else {
734  // Else means this is a Folder
735  $sourceFolderObject = $sourceFileObject;
736  try {
737  if ($alternativeName !== '') {
738  // Don't allow overwriting existing files, but find a new name
739  $resultObject = $sourceFolderObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
740  } else {
741  // Don't allow overwriting existing files
742  $resultObject = $sourceFolderObject->moveTo($targetFolderObject, NULL, 'renameNewFile');
743  }
744  $this->writelog(3, 0, 2, 'Directory "%s" moved to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
745  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
746  $this->writelog(3, 1, 125, 'You are not allowed to move directories', '');
747  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
748  $this->writelog(3, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
749  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
750  $this->writelog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
751  } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
752  $this->writelog(3, 1, 122, 'Cannot move folder "%s" into target folder "%s", because the target folder is already within the folder to be moved!', array($sourceFolderObject->getName(), $targetFolderObject->getName()));
753  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
754  $this->writelog(3, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
755  } catch (\BadMethodCallException $e) {
756  $this->writelog(3, 1, 127, 'The function to move a folder between storages is not yet implemented', array());
757  } catch (\RuntimeException $e) {
758  $this->writelog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
759  }
760  }
761  return $resultObject;
762  }
763 
776  public function func_rename($cmds) {
777  if (!$this->isInit) {
778  return FALSE;
779  }
780  $sourceFileObject = $this->getFileObject($cmds['data']);
781  $targetFile = $cmds['target'];
782  $resultObject = NULL;
783  if ($sourceFileObject instanceof File) {
784  try {
785  // Try to rename the File
786  $resultObject = $sourceFileObject->rename($targetFile);
787  $this->writelog(5, 0, 1, 'File renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
788  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
789  $this->writelog(5, 1, 102, 'You are not allowed to rename files!', '');
790  } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
791  $this->writelog(5, 1, 101, 'Extension of file name "%s" or "%s" was not allowed!', array($sourceFileObject->getName(), $targetFile));
792  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
793  $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
794  } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
795  $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
796  } catch (\RuntimeException $e) {
797  $this->writelog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
798  }
799  } else {
800  // Else means this is a Folder
801  try {
802  // Try to rename the Folder
803  $resultObject = $sourceFileObject->rename($targetFile);
804  $this->writelog(5, 0, 2, 'Directory renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
805  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
806  $this->writelog(5, 1, 111, 'You are not allowed to rename directories!', '');
807  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
808  $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
809  } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
810  $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
811  } catch (\RuntimeException $e) {
812  $this->writelog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
813  }
814  }
815  return $resultObject;
816  }
817 
829  public function func_newfolder($cmds) {
830  if (!$this->isInit) {
831  return FALSE;
832  }
833  $targetFolderObject = $this->getFileObject($cmds['target']);
834  if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
835  $this->writelog(6, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
836  return FALSE;
837  }
838  $resultObject = NULL;
839  try {
840  $folderName = $cmds['data'];
841  $resultObject = $targetFolderObject->createFolder($folderName);
842  $this->writelog(6, 0, 1, 'Directory "%s" created in "%s"', array($folderName, $targetFolderObject->getIdentifier() . '/'));
843  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
844  $this->writelog(6, 1, 103, 'You are not allowed to create directories!', '');
845  } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
846  $this->writelog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier() . '/'));
847  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
848  $this->writelog(6, 1, 101, 'File or directory "%s" existed already!', array($folderName));
849  } catch (\RuntimeException $e) {
850  $this->writelog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', array($folderName, $targetFolderObject->getIdentifier() . '/'));
851  }
852  return $resultObject;
853  }
854 
865  public function func_newfile($cmds) {
866  if (!$this->isInit) {
867  return FALSE;
868  }
869  $targetFolderObject = $this->getFileObject($cmds['target']);
870  if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
871  $this->writelog(8, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
872  return FALSE;
873  }
874  $resultObject = NULL;
875  try {
876  $fileName = $cmds['data'];
877  $resultObject = $targetFolderObject->createFile($fileName);
878  $this->writelog(8, 0, 1, 'File created: "%s"', array($fileName));
879  } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
880  $this->writeLog(8, 1, 106, 'Extension of file "%s" was not allowed!', array($fileName));
881  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
882  $this->writelog(8, 1, 103, 'You are not allowed to create files!', '');
883  } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
884  $this->writelog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
885  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
886  $this->writelog(8, 1, 101, 'File existed already in "%s"!', array($targetFolderObject->getIdentifier()));
887  } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException $e) {
888  $this->writelog(8, 1, 106, 'File name "%s" was not allowed!', $fileName);
889  } catch (\RuntimeException $e) {
890  $this->writelog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', array($fileName, $targetFolderObject->getIdentifier()));
891  }
892  return $resultObject;
893  }
894 
902  public function func_edit($cmds) {
903  if (!$this->isInit) {
904  return FALSE;
905  }
906  // Example indentifier for $cmds['target'] => "4:mypath/tomyfolder/myfile.jpg"
907  // for backwards compatibility: the combined file identifier was the path+filename
908  $fileIdentifier = $cmds['target'];
909  $fileObject = $this->getFileObject($fileIdentifier);
910  // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
911  $content = $cmds['data'];
912  if (!$fileObject instanceof File) {
913  $this->writelog(9, 2, 123, 'Target "%s" was not a file!', array($fileIdentifier));
914  return FALSE;
915  }
916  $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
917  if (!GeneralUtility::inList($extList, $fileObject->getExtension())) {
918  $this->writelog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', array($fileObject->getExtension(), $extList));
919  return FALSE;
920  }
921  try {
922  $fileObject->setContents($content);
923  clearstatcache();
924  $this->writelog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', array($fileObject->getIdentifier(), $fileObject->getSize(), md5($content)));
925  return TRUE;
926  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
927  $this->writelog(9, 1, 104, 'You are not allowed to edit files!', '');
928  return FALSE;
929  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileWritePermissionsException $e) {
930  $this->writelog(9, 1, 100, 'File "%s" was not saved! Write-permission problem?', array($fileObject->getIdentifier()));
931  return FALSE;
932  } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
933  $this->writelog(9, 1, 100, 'File "%s" was not saved! File extension rejected!', array($fileObject->getIdentifier()));
934  return FALSE;
935  }
936  }
937 
967  public function func_upload($cmds) {
968  if (!$this->isInit) {
969  return FALSE;
970  }
971  $uploadPosition = $cmds['data'];
972  $uploadedFileData = $_FILES['upload_' . $uploadPosition];
973  if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
974  $this->writelog(1, 2, 108, 'No file was uploaded!', '');
975  return FALSE;
976  }
977  // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
978  $targetFolderObject = $this->getFileObject($cmds['target']);
979  // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
980  if (!is_array($uploadedFileData['name'])) {
981  $uploadedFileData = array(
982  'name' => array($uploadedFileData['name']),
983  'type' => array($uploadedFileData['type']),
984  'tmp_name' => array($uploadedFileData['tmp_name']),
985  'size' => array($uploadedFileData['size'])
986  );
987  }
988  $resultObjects = array();
989  $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
990  // Loop through all uploaded files
991  for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
992  $fileInfo = array(
993  'name' => $uploadedFileData['name'][$i],
994  'type' => $uploadedFileData['type'][$i],
995  'tmp_name' => $uploadedFileData['tmp_name'][$i],
996  'size' => $uploadedFileData['size'][$i]
997  );
998  try {
999  // @todo can be improved towards conflict mode naming
1000  if ($this->dontCheckForUnique) {
1001  $conflictMode = 'replace';
1002  } else {
1003  $conflictMode = 'cancel';
1004  }
1006  $fileObject = $targetFolderObject->addUploadedFile($fileInfo, $conflictMode);
1007  $fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
1008  if ($conflictMode === 'replace') {
1009  $this->getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
1010  }
1011  $resultObjects[] = $fileObject;
1012  $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
1013  $this->writelog(1, 0, 1, 'Uploading file "%s" to "%s"', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
1014  } catch (\TYPO3\CMS\Core\Resource\Exception\UploadException $e) {
1015  $this->writelog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
1016  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
1017  $this->writelog(1, 1, 105, 'You are not allowed to upload files!', '');
1018  } catch (\TYPO3\CMS\Core\Resource\Exception\UploadSizeException $e) {
1019  $this->writelog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', array($fileInfo['name']));
1020  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
1021  $this->writelog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
1022  } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
1023  $this->writelog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
1024  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
1025  $this->writelog(1, 1, 101, 'No unique filename available in "%s"!', array($targetFolderObject->getIdentifier()));
1026  } catch (\RuntimeException $e) {
1027  $this->writelog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', array($targetFolderObject->getIdentifier()));
1028  }
1029  }
1030 
1031  return $resultObjects;
1032  }
1033 
1042  public function func_unzip($cmds) {
1043  if (!$this->isInit || $this->dont_use_exec_commands) {
1044  return FALSE;
1045  }
1046  $theFile = $cmds['data'];
1047  if (!@is_file($theFile)) {
1048  $this->writelog(7, 2, 105, 'The file "%s" did not exist!', array($theFile));
1049  return FALSE;
1050  }
1051  $fI = GeneralUtility::split_fileref($theFile);
1052  if (!isset($cmds['target'])) {
1053  $cmds['target'] = $fI['path'];
1054  }
1055  // Clean up destination directory
1056  // !!! Method has been put in the local driver, can be saftely removed
1057  $theDest = $this->is_directory($cmds['target']);
1058  if (!$theDest) {
1059  $this->writelog(7, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
1060  return FALSE;
1061  }
1062  if (!$this->actionPerms['unzipFile']) {
1063  $this->writelog(7, 1, 103, 'You are not allowed to unzip files', '');
1064  return FALSE;
1065  }
1066  if ($fI['fileext'] != 'zip') {
1067  $this->writelog(7, 1, 102, 'File extension is not "zip"', '');
1068  return FALSE;
1069  }
1070  if (!$this->checkIfFullAccess($theDest)) {
1071  $this->writelog(7, 1, 101, 'You don\'t have full access to the destination directory "%s"!', array($theDest));
1072  return FALSE;
1073  }
1074  // !!! Method has been put in the sotrage driver, can be saftely removed
1075  if ($this->checkPathAgainstMounts($theFile) && $this->checkPathAgainstMounts($theDest . '/')) {
1076  // No way to do this under windows.
1077  $cmd = $this->unzipPath . 'unzip -qq ' . escapeshellarg($theFile) . ' -d ' . escapeshellarg($theDest);
1079  $this->writelog(7, 0, 1, 'Unzipping file "%s" in "%s"', array($theFile, $theDest));
1080  return TRUE;
1081  } else {
1082  $this->writelog(7, 1, 100, 'File "%s" or destination "%s" was not within your mountpoints!', array($theFile, $theDest));
1083  return FALSE;
1084  }
1085  }
1086 
1093  protected function addFlashMessage(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage) {
1095  $flashMessageService = GeneralUtility::makeInstance(
1096  'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
1097  );
1099  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1100  $defaultFlashMessageQueue->enqueue($flashMessage);
1101  }
1102 
1109  protected function getIndexer(\TYPO3\CMS\Core\Resource\ResourceStorage $storage) {
1110  return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\Indexer', $storage);
1111  }
1112 
1118  protected function getDatabaseConnection() {
1119  return $GLOBALS['TYPO3_DB'];
1120  }
1121 
1125  protected function getBackendUser() {
1126  return $GLOBALS['BE_USER'];
1127  }
1128 
1129 }
getIndexer(\TYPO3\CMS\Core\Resource\ResourceStorage $storage)
writeLog($action, $error, $details_nr, $details, $data)
static getUserObj($classRef, $checkPrefix='', $silent=FALSE)
static getRecordTitle($table, $row, $prep=FALSE, $forceResult=TRUE)
static getSpriteIconForRecord($table, array $row, array $options=array())
static split_fileref($fileNameWithPath)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static getRecordPath($uid, $clause, $titleLimit, $fullTitleLimit=0)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static exec($command, &$output=NULL, &$returnValue=0)