[TASK] Clean up phpDoc comments in t3lib_extfilefunc
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_extfilefunc.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Extending class to class t3lib_basicFileFunctions
29 *
30 * Revised for TYPO3 3.6 May/2004 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34
35 /**
36 * Contains functions for performing file operations like copying, pasting, uploading, moving, deleting etc. through the TCE
37 * Extending class to class t3lib_basicFileFunctions.
38 *
39 * see basicFileFunctions
40 * see document "TYPO3 Core API" for syntax
41 *
42 * This class contains functions primarily used by tce_file.php (TYPO3 Core Engine for file manipulation)
43 * Functions include copying, moving, deleting, uploading and so on...
44 *
45 * Important internal variables:
46 *
47 * $filemounts (see basicFileFunctions)
48 * $f_ext (see basicFileFunctions)
49 * ... All fileoperations must be within the filemount-paths. Further the fileextension MUST validate TRUE with the f_ext array
50 *
51 * The unzip-function allows unzip only if the destination path has it's f_ext[]['allow'] set to '*'!!
52 * You are allowed to copy/move folders within the same 'space' (web/ftp).
53 * You are allowed to copy/move folders between spaces (web/ftp) IF the destination has it's f_ext[]['allow'] set to '*'!
54 *
55 * Advice:
56 * You should always exclude php-files from the webspace. This will keep people from uploading, copy/moving and renaming files to become executable php scripts.
57 * You should never mount a ftp_space 'below' the webspace so that it reaches into the webspace. This is because if somebody unzips a zip-file in the ftp-space so that it reaches out into the webspace this will be a violation of the safety
58 * For example this is a bad idea: you have an ftp-space that is '/www/' and a web-space that is '/www/htdocs/'
59 *
60 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
61 * @package TYPO3
62 * @subpackage t3lib
63 */
64 class t3lib_extFileFunctions extends t3lib_basicFileFunctions {
65
66 // External static variables:
67 // Notice; some of these are overridden in the start() method with values from $GLOBALS['TYPO3_CONF_VARS']['BE']
68 // Path to unzip-program (with trailing '/')
69 var $unzipPath = '';
70 // If set, the uploaded files will overwrite existing files.
71 var $dontCheckForUnique = 0;
72
73 // This array is self-explaning (look in the class below).
74 // It grants access to the functions. This could be set from outside in order to enabled functions to users.
75 // See also the function init_actionPerms() which takes input directly from the user-record
76 var $actionPerms = array(
77 'deleteFile' => 0, // Deleting files physically
78 'deleteFolder' => 0, // Deleting foldes physically
79 'deleteFolderRecursively' => 0, // normally folders are deleted by the PHP-function rmdir(), but with this option a user deletes with 'rm -Rf ....' which is pretty wild!
80 'moveFile' => 0,
81 'moveFolder' => 0,
82 'copyFile' => 0,
83 'copyFolder' => 0,
84 'newFolder' => 0,
85 'newFile' => 0,
86 'editFile' => 0,
87 'unzipFile' => 0,
88 'uploadFile' => 0,
89 'renameFile' => 0,
90 'renameFolder' => 0
91 );
92
93 // This is regarded to be the recycler folder
94 var $recyclerFN = '_recycler_';
95
96 /**
97 * Whether to use recycler (0 = no, 1 = if available, 2 = always)
98 *
99 * @var integer
100 * @deprecated since TYPO3 6.0
101 */
102 var $useRecycler = 1;
103
104 // Internal, dynamic
105 // Will contain map between upload ID and the final filename
106 var $internalUploadMap = array();
107
108 var $lastError = '';
109
110 /**
111 * @var array
112 */
113 protected $fileCmdMap;
114
115 /**
116 * The File Factory
117 *
118 * @var t3lib_file_Factory
119 */
120 protected $fileFactory;
121
122 /**
123 * Initialization of the class
124 *
125 * @param array $fileCmds Array with the commands to execute. See "TYPO3 Core API" document
126 * @return void
127 */
128 function start($fileCmds) {
129
130 $unzipPath = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['unzip_path']);
131 if (substr($unzipPath, -1) !== '/' && is_dir($unzipPath)) {
132 // Make sure the path ends with a slash
133 $unzipPath.= '/';
134 }
135 $this->unzipPath = $unzipPath;
136
137 // Initialize Object Factory
138 $this->fileFactory = t3lib_file_Factory::getInstance();
139
140 // Initializing file processing commands:
141 $this->fileCmdMap = $fileCmds;
142 }
143
144 /**
145 * Sets up permission to perform file/directory operations.
146 * See below or the be_user-table for the significance of the various bits in $setup.
147 *
148 * @param integer $setup File permission integer from BE_USER OR'ed with permissions of back-end groups this user is a member of
149 * @return void
150 */
151 function init_actionPerms($setup) {
152 // Files: Upload,Copy,Move,Delete,Rename
153 if (($setup & 1) == 1) {
154 $this->actionPerms['uploadFile'] = 1;
155 $this->actionPerms['copyFile'] = 1;
156 $this->actionPerms['moveFile'] = 1;
157 $this->actionPerms['deleteFile'] = 1;
158 $this->actionPerms['renameFile'] = 1;
159 $this->actionPerms['editFile'] = 1;
160 $this->actionPerms['newFile'] = 1;
161 }
162 // Files: Unzip
163 if (($setup & 2) == 2) {
164 $this->actionPerms['unzipFile'] = 1;
165 }
166 // Directory: Move,Delete,Rename,New
167 if (($setup & 4) == 4) {
168 $this->actionPerms['moveFolder'] = 1;
169 $this->actionPerms['deleteFolder'] = 1;
170 $this->actionPerms['renameFolder'] = 1;
171 $this->actionPerms['newFolder'] = 1;
172 }
173 // Directory: Copy
174 if (($setup & 8) == 8) {
175 $this->actionPerms['copyFolder'] = 1;
176 }
177 // Directory: Delete recursively (rm -Rf)
178 if (($setup & 16) == 16) {
179 $this->actionPerms['deleteFolderRecursively'] = 1;
180 }
181 }
182
183 /**
184 * Processing the command array in $this->fileCmdMap
185 *
186 * @return mixed FALSE, if the file functions were not initialized
187 * otherwise returns an array of all the results that are returned
188 * from each command, separated in each action.
189 */
190 function processData() {
191 $result = array();
192 if (!$this->isInit) {
193 return FALSE;
194 }
195
196 if (is_array($this->fileCmdMap)) {
197
198 // Check if there were uploads expected, but no one made
199 if ($this->fileCmdMap['upload']) {
200 $uploads = $this->fileCmdMap['upload'];
201 foreach ($uploads as $upload) {
202 if (!$_FILES['upload_' . $upload['data']]['name']) {
203 unset($this->fileCmdMap['upload'][$upload['data']]);
204 }
205 }
206 if (count($this->fileCmdMap['upload']) == 0) {
207 $this->writelog(1, 1, 108, 'No file was uploaded!', '');
208 }
209 }
210
211 // Traverse each set of actions
212 foreach ($this->fileCmdMap as $action => $actionData) {
213
214 // Traverse all action data. More than one file might be affected at the same time.
215 if (is_array($actionData)) {
216 $result[$action] = array();
217 foreach ($actionData as $cmdArr) {
218
219 // Clear file stats
220 clearstatcache();
221
222 // Branch out based on command:
223 switch ($action) {
224 case 'delete':
225 $result[$action][] = $this->func_delete($cmdArr);
226 break;
227 case 'copy':
228 $result[$action][] = $this->func_copy($cmdArr);
229 break;
230 case 'move':
231 $result[$action][] = $this->func_move($cmdArr);
232 break;
233 case 'rename':
234 $result[$action][] = $this->func_rename($cmdArr);
235 break;
236 case 'newfolder':
237 $result[$action][] = $this->func_newfolder($cmdArr);
238 break;
239 case 'newfile':
240 $result[$action][] = $this->func_newfile($cmdArr);
241 break;
242 case 'editfile':
243 $result[$action][] = $this->func_edit($cmdArr);
244 break;
245 case 'upload':
246 $result[$action][] = $this->func_upload($cmdArr);
247 break;
248 case 'unzip':
249 $result[$action][] = $this->func_unzip($cmdArr);
250 break;
251 }
252
253 // Hook for post-processing the action
254 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'])) {
255 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'] as $classRef) {
256 $hookObject = t3lib_div::getUserObj($classRef);
257
258 if (!($hookObject instanceof t3lib_extFileFunctions_processDataHook)) {
259 throw new UnexpectedValueException('$hookObject must implement interface t3lib_extFileFunctions_processDataHook', 1279719168);
260 }
261
262 $hookObject->processData_postProcessAction($action, $cmdArr, $result[$action], $this);
263 }
264 }
265 }
266 }
267 }
268 }
269 return $result;
270 }
271
272 /**
273 * Adds log error messages from the operations of this script instance to the FlashMessageQueue
274 *
275 * @param string $redirect Redirect URL (for creating link in message)
276 * @return void
277 */
278 function printLogErrorMessages($redirect = '') {
279 $this->getErrorMessages();
280 }
281
282 /**
283 * Adds log error messages from the previous file operations of this script instance
284 * to the FlashMessageQueue
285 *
286 * @return void
287 */
288 function getErrorMessages() {
289 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
290 '*',
291 'sys_log',
292 'type = 2 AND userid = ' . intval($GLOBALS['BE_USER']->user['uid'])
293 . ' AND tstamp=' . intval($GLOBALS['EXEC_TIME'])
294 . ' AND error<>0'
295 );
296 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
297 $logData = unserialize($row['log_data']);
298 $msg = $row['error'] . ': ' . sprintf($row['details'], $logData[0], $logData[1], $logData[2], $logData[3], $logData[4]);
299 $flashMessage = t3lib_div::makeInstance(
300 't3lib_FlashMessage',
301 $msg,
302 '',
303 t3lib_FlashMessage::ERROR,
304 TRUE
305 );
306 t3lib_FlashMessageQueue::addMessage($flashMessage);
307 }
308 $GLOBALS['TYPO3_DB']->sql_free_result($res);
309 }
310
311 /**
312 * Goes back in the path and checks in each directory if a folder named $this->recyclerFN (usually '_recycler_') is present.
313 * If a folder in the tree happens to be a _recycler_-folder (which means that we're deleting something inside a _recycler_-folder) this is ignored
314 *
315 * @param string $theFile Takes a valid Path ($theFile)
316 * @return string Returns the path (without trailing slash) of the closest recycle-folder if found. Else FALSE.
317 *
318 * @todo To be put in Storage with a better concept
319 * @deprecated since TYPO3 6.0, use t3lib_file_Storage method instead
320 */
321 function findRecycler($theFile) {
322 t3lib_div::logDeprecatedFunction();
323
324 if ($this->isPathValid($theFile)) {
325 $theFile = $this->cleanDirectoryName($theFile);
326 $fI = t3lib_div::split_fileref($theFile);
327 $c = 0;
328 // !!! Method has been put in the storage, can be saftely removed
329 $rDir = $fI['path'] . $this->recyclerFN;
330 while ($this->checkPathAgainstMounts($fI['path']) && $c < 20) {
331 if (@is_dir($rDir) && $this->recyclerFN != $fI['file']) {
332 return $rDir;
333 }
334 $theFile = $fI['path'];
335 $theFile = $this->cleanDirectoryName($theFile);
336 $fI = t3lib_div::split_fileref($theFile);
337 $c++;
338 }
339 }
340 }
341
342 /**
343 * Logging file operations
344 *
345 * @param integer $action The action number. See the functions in the class for a hint. Eg. edit is '9', upload is '1' ...
346 * @param integer $error The severity: 0 = message, 1 = error, 2 = System Error, 3 = security notice (admin)
347 * @param integer $details_nr This number is unique for every combination of $type and $action. This is the error-message number, which can later be used to translate error messages.
348 * @param string $details This is the default, raw error message in english
349 * @param array $data Array with special information that may go into $details by "%s" marks / sprintf() when the log is shown
350 * @return void
351 * @see class.t3lib_userauthgroup.php
352 */
353 function writeLog($action, $error, $details_nr, $details, $data) {
354 // Type value for tce_file.php
355 $type = 2;
356 if (is_object($GLOBALS['BE_USER'])) {
357 $GLOBALS['BE_USER']->writelog($type, $action, $error, $details_nr, $details, $data);
358 }
359 $this->lastError = vsprintf($details, $data);
360 }
361
362 /*************************************
363 *
364 * File operation functions
365 *
366 **************************************/
367
368 /**
369 * Deleting files and folders (action=4)
370 *
371 * @param array $cmds $cmds['data'] is the file/folder to delete
372 * @return boolean Returns TRUE upon success
373 */
374 function func_delete($cmds) {
375 $result = FALSE;
376
377 if (!$this->isInit) {
378 return $result;
379 }
380
381 // Example indentifier for $cmds['data'] => "4:mypath/tomyfolder/myfile.jpg"
382 // for backwards compatibility: the combined file identifier was the path+filename
383 $fileObject = $this->getFileObject($cmds['data']);
384
385 // @todo implement the recycler feature which has been removed from the original implementation
386 // $this->writelog(4, 0, 4, 'Item "%s" moved to recycler at "%s"', array($theFile, $recyclerPath));
387
388 // Copies the file
389 if ($fileObject instanceof t3lib_file_File) {
390 $refIndexRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
391 '*',
392 'sys_refindex',
393 "ref_table='sys_file' AND ref_uid=" . $fileObject->getUid()
394 );
395
396 if (count($refIndexRecords) > 0) {
397 $shortcutContent = array();
398
399 foreach ($refIndexRecords as $row) {
400 $shortcutRecord = NULL;
401 $shortcutRecord = t3lib_BEfunc::getRecord($row['tablename'], $row['recuid']);
402 if (is_array($shortcutRecord) && $row['tablename'] !== 'sys_file_reference') {
403 $icon = t3lib_iconWorks::getSpriteIconForRecord($row['tablename'], $shortcutRecord);
404
405 $onClick = 'showClickmenu("' . $row['tablename'] . '", "' . $row['recuid'] . '", "1", "+info,history,edit,delete", "|", "");return false;';
406 $shortcutContent[] = '<a href="#" oncontectmenu="' . htmlspecialchars($onClick) . '" onclick="' . htmlspecialchars($onClick) . '">' . $icon . '</a>' .
407 htmlspecialchars(t3lib_BEfunc::getRecordTitle($row['tablename'], $shortcutRecord) . ' [' . t3lib_BEfunc::getRecordPath($shortcutRecord['pid'], '', 80) . ']');
408 }
409 }
410
411 $out = '<p>The file cannot be deleted since it is still used at the following places:<br />' .
412 implode('<br />', $shortcutContent) . '</p>';
413
414 $flashMessage = t3lib_div::makeInstance(
415 't3lib_flashMessage',
416 $out,
417 'File not deleted',
418 t3lib_FlashMessage::WARNING,
419 TRUE
420 );
421 t3lib_FlashMessageQueue::addMessage($flashMessage);
422 return;
423
424 } else {
425 try {
426 $result = $fileObject->delete();
427 } catch (t3lib_file_exception_InsufficientFileAccessPermissionsException $e) {
428 $this->writelog(4, 1, 112, 'You are not allowed to access the file', array($fileObject->getIdentifier()));
429 } catch (t3lib_file_exception_NotInMountPointException $e) {
430 $this->writelog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
431 } catch (RuntimeException $e) {
432 $this->writelog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', array($fileObject->getIdentifier()));
433 }
434 // Log success
435 $this->writelog(4, 0, 1, 'File "%s" deleted', array($fileObject->getIdentifier()));
436 }
437 // Working on a folder
438 } else {
439 try {
440 /** @var $fileObject t3lib_file_FolderInterface */
441 $result = $fileObject->delete(TRUE);
442 } catch (t3lib_file_exception_InsufficientFileAccessPermissionsException $e) {
443 $this->writelog(4, 1, 123, 'You are not allowed to access the directory', array($fileObject->getIdentifier()));
444 } catch (t3lib_file_exception_NotInMountPointException $e) {
445 $this->writelog(4, 1, 121, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
446 } catch (RuntimeException $e) {
447 $this->writelog(4, 1, 120, 'Could not delete directory! Write-permission problem? Is directory "%s" empty? (You are not allowed to delete directories recursively).', array($fileObject->getIdentifier()));
448 }
449
450 // Log success
451 $this->writelog(4, 0, 3, 'Directory "%s" deleted', array($fileObject->getIdentifier()));
452 }
453
454 return $result;
455 }
456
457 /**
458 * Gets a File or a Folder object from an identifier [storage]:[fileId]
459 *
460 * @param string $identifier
461 * @return t3lib_file_Folder|t3lib_file_File
462 */
463 protected function getFileObject($identifier) {
464 $object = $this->fileFactory->retrieveFileOrFolderObject($identifier);
465
466 if (!is_object($object)) {
467 throw new t3lib_file_exception_InvalidFileException(
468 'The item ' . $identifier . ' was not a file or directory!!',
469 1320122453
470 );
471 }
472
473 return $object;
474 }
475
476 /**
477 * Copying files and folders (action=2)
478 *
479 * $cmds['data'] (string): The file/folder to copy
480 * + example "4:mypath/tomyfolder/myfile.jpg")
481 * + for backwards compatibility: the identifier was the path+filename
482 * $cmds['target'] (string): The path where to copy to.
483 * + example "2:targetpath/targetfolder/"
484 * $cmds['altName'] (string): Use an alternative name if the target already exists
485 *
486 * @param array $cmds Command details as described above
487 * @return t3lib_file_File
488 */
489 protected function func_copy($cmds) {
490 if (!$this->isInit) {
491 return FALSE;
492 }
493
494 $sourceFileObject = $this->getFileObject($cmds['data']);
495 /** @var $targetFolderObject t3lib_file_Folder */
496 $targetFolderObject = $this->getFileObject($cmds['target']);
497
498 // Basic check
499 if (!($targetFolderObject instanceof t3lib_file_Folder)) {
500 $this->writelog(2, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
501 return FALSE;
502 }
503
504 // If this is TRUE, we append _XX to the file name if
505 $appendSuffixOnConflict = (string) $cmds['altName'];
506 $resultObject = NULL;
507
508 // Copying the file
509 if ($sourceFileObject instanceof t3lib_file_File) {
510 try {
511 $conflictMode = ($appendSuffixOnConflict !== '') ? 'renameNewFile' : 'cancel';
512 $resultObject = $sourceFileObject->copyTo($targetFolderObject, NULL, $conflictMode);
513 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
514 $this->writelog(2, 1, 114, 'You are not allowed to copy files', '');
515 } catch (t3lib_file_exception_InsufficientFileAccessPermissionsException $e) {
516 $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()));
517 } catch (t3lib_file_exception_IllegalFileExtensionException $e) {
518 $this->writelog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
519 } catch (t3lib_file_exception_ExistingTargetFileNameException $e) {
520 $this->writelog(2, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
521 } catch (RuntimeException $e) {
522 $this->writelog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
523 }
524
525 $this->writelog(2, 0, 1, 'File "%s" copied to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
526
527 } else { // Else means this is a Folder
528 $sourceFolderObject = $sourceFileObject;
529
530 try {
531 $conflictMode = ($appendSuffixOnConflict !== '') ? 'renameNewFile' : 'cancel';
532 $resultObject = $sourceFolderObject->copyTo($targetFolderObject, NULL, $conflictMode);
533 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
534 $this->writelog(2, 1, 125, 'You are not allowed to copy directories', '');
535 } catch (t3lib_file_exception_InsufficientFileAccessPermissionsException $e) {
536 $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()));
537 } catch (t3lib_file_exception_InsufficientFolderAccessPermissionsException $e) {
538 $this->writelog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
539 } catch (t3lib_file_exception_InvalidTargetFolderException $e) {
540 $this->writelog(2, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', array($targetFolderObject->getIdentifier(), $sourceFolderObject->getIdentifier()));
541 } catch (t3lib_file_exception_ExistingTargetFolderException $e) {
542 $this->writelog(2, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
543 } catch (RuntimeException $e) {
544 $this->writelog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
545 }
546
547 $this->writelog(2, 0, 2, 'Directory "%s" copied to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
548 }
549
550 return $resultObject;
551 }
552
553 /**
554 * Moving files and folders (action=3)
555 *
556 * $cmds['data'] (string): The file/folder to move
557 * + example "4:mypath/tomyfolder/myfile.jpg")
558 * + for backwards compatibility: the identifier was the path+filename
559 * $cmds['target'] (string): The path where to move to.
560 * + example "2:targetpath/targetfolder/"
561 * $cmds['altName'] (string): Use an alternative name if the target already exists
562 *
563 * @param array $cmds Command details as described above
564 * @return t3lib_file_File
565 */
566 protected function func_move($cmds) {
567 if (!$this->isInit) {
568 return FALSE;
569 }
570
571 $sourceFileObject = $this->getFileObject($cmds['data']);
572 $targetFolderObject = $this->getFileObject($cmds['target']);
573
574 // Basic check
575 if (!($targetFolderObject instanceof t3lib_file_Folder)) {
576 $this->writelog(3, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
577 return FALSE;
578 }
579
580 $alternativeName = (string) $cmds['altName'];
581 $resultObject = NULL;
582
583 // Moving the file
584 if ($sourceFileObject instanceof t3lib_file_File) {
585 try {
586 if ($alternativeName !== '') {
587 // Don't allow overwriting existing files, but find a new name
588 $resultObject = $sourceFileObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
589 } else {
590 // Don't allow overwriting existing files
591 $resultObject = $sourceFileObject->moveTo($targetFolderObject, NULL, 'cancel');
592 }
593 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
594 $this->writelog(3, 1, 114, 'You are not allowed to move files', '');
595 } catch (t3lib_file_exception_InsufficientFileAccessPermissionsException $e) {
596 $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()));
597 } catch (t3lib_file_exception_IllegalFileExtensionException $e) {
598 $this->writelog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
599 } catch (t3lib_file_exception_ExistingTargetFileNameException $e) {
600 $this->writelog(3, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
601 } catch (RuntimeException $e) {
602 $this->writelog(3, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
603 }
604
605 $this->writelog(3, 0, 1, 'File "%s" moved to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
606
607 } else { // Else means this is a Folder
608 $sourceFolderObject = $sourceFileObject;
609
610 try {
611 if ($alternativeName !== '') {
612 // Don't allow overwriting existing files, but find a new name
613 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
614 } else {
615 // Don't allow overwriting existing files
616 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, NULL, 'renameNewFile');
617 }
618 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
619 $this->writelog(3, 1, 125, 'You are not allowed to move directories', '');
620 } catch (t3lib_file_exception_InsufficientFileAccessPermissionsException $e) {
621 $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()));
622 } catch (t3lib_file_exception_InsufficientFolderAccessPermissionsException $e) {
623 $this->writelog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
624 } catch (t3lib_file_exception_InvalidTargetFolderException $e) {
625 $this->writelog(3, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', array($targetFolderObject->getIdentifier(), $sourceFolderObject->getIdentifier()));
626 } catch (t3lib_file_exception_ExistingTargetFolderException $e) {
627 $this->writelog(3, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
628 } catch (RuntimeException $e) {
629 $this->writelog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
630 }
631
632 $this->writelog(3, 0, 2, 'Directory "%s" moved to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
633 }
634
635 return $resultObject;
636 }
637
638 /**
639 * Renaming files or foldes (action=5)
640 *
641 * $cmds['data'] (string): The file/folder to copy
642 * + example "4:mypath/tomyfolder/myfile.jpg")
643 * + for backwards compatibility: the identifier was the path+filename
644 * $cmds['target'] (string): New name of the file/folder
645 *
646 * @param array $cmds Command details as described above
647 * @return t3lib_file_File Returns the new file upon success
648 */
649 function func_rename($cmds) {
650 if (!$this->isInit) {
651 return FALSE;
652 }
653
654 $sourceFileObject = $this->getFileObject($cmds['data']);
655 $targetFile = $cmds['target'];
656 $resultObject = NULL;
657
658 if ($sourceFileObject instanceof t3lib_file_File) {
659 try {
660 // Try to rename the File
661 $resultObject = $sourceFileObject->rename($targetFile);
662 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
663 $this->writelog(5, 1, 102, 'You are not allowed to rename files!', '');
664 } catch (t3lib_file_exception_IllegalFileExtensionException $e) {
665 $this->writelog(5, 1, 101, 'Extension of file name "%s" was not allowed!', array($targetFile));
666 } catch (t3lib_file_exception_ExistingTargetFileNameException $e) {
667 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
668 } catch (t3lib_file_exception_NotInMountPointException $e) {
669 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
670 } catch (RuntimeException $e) {
671 $this->writelog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
672 }
673 $this->writelog(5, 0, 1, 'File renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
674
675 } else { // Else means this is a Folder
676 try {
677 // Try to rename the Folder
678 $resultObject = $sourceFileObject->rename($targetFile);
679 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
680 $this->writelog(5, 1, 111, 'You are not allowed to rename directories!', '');
681 } catch (t3lib_file_exception_ExistingTargetFileNameException $e) {
682 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
683 } catch (t3lib_file_exception_NotInMountPointException $e) {
684 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
685 } catch (RuntimeException $e) {
686 $this->writelog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
687 }
688 $this->writelog(5, 0, 2, 'Directory renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
689 }
690
691 return $resultObject;
692 }
693
694 /**
695 * This creates a new folder. (action=6)
696 *
697 * $cmds['data'] (string): The new folder name
698 * $cmds['target'] (string): The path where to copy to.
699 * + example "2:targetpath/targetfolder/"
700 *
701 * @param array $cmds Command details as described above
702 * @return t3lib_file_Folder Returns the new foldername upon success
703 */
704 function func_newfolder($cmds) {
705 if (!$this->isInit) {
706 return FALSE;
707 }
708
709 $targetFolderObject = $this->getFileObject($cmds['target']);
710
711 if (!($targetFolderObject instanceof t3lib_file_Folder)) {
712 $this->writelog(6, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
713 return FALSE;
714 }
715
716 $resultObject = NULL;
717
718 try {
719 $folderName = $cmds['data'];
720 $resultObject = $targetFolderObject->createFolder($folderName);
721 $this->writelog(6, 0, 1, 'Directory "%s" created in "%s"', array($folderName, $targetFolderObject->getIdentifier() . '/'));
722 } catch (t3lib_file_exception_InsufficientFolderWritePermissionsException $e) {
723 $this->writelog(6, 1, 103, 'You are not allowed to create directories!', '');
724 } catch (t3lib_file_exception_NotInMountPointException $e) {
725 $this->writelog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier() . '/'));
726 } catch (t3lib_file_exception_ExistingTargetFolderException $e) {
727 $this->writelog(6, 1, 101, 'File or directory "%s" existed already!', array($folderName));
728 } catch (RuntimeException $e) {
729 $this->writelog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', array($folderName, $targetFolderObject->getIdentifier() . '/'));
730 }
731
732 return $resultObject;
733 }
734
735 /**
736 * This creates a new file. (action=8)
737 * $cmds['data'] (string): The new file name
738 * $cmds['target'] (string): The path where to create it.
739 * + example "2:targetpath/targetfolder/"
740 *
741 * @param array $cmds Command details as described above
742 * @return string Returns the new filename upon success
743 */
744 function func_newfile($cmds) {
745 if (!$this->isInit) {
746 return FALSE;
747 }
748
749 $targetFolderObject = $this->getFileObject($cmds['target']);
750
751 if (!($targetFolderObject instanceof t3lib_file_Folder)) {
752 $this->writelog(8, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
753 return FALSE;
754 }
755
756 $resultObject = NULL;
757
758 try {
759 $fileName = $cmds['data'];
760 $resultObject = $targetFolderObject->createFile($fileName);
761 $this->writelog(8, 0, 1, 'File created: "%s"', array($fileName));
762 } catch (t3lib_file_exception_InsufficientFolderWritePermissionsException $e) {
763 $this->writelog(8, 1, 103, 'You are not allowed to create files!', '');
764 } catch (t3lib_file_exception_NotInMountPointException $e) {
765 $this->writelog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
766 } catch (t3lib_file_exception_ExistingTargetFileNameException $e) {
767 $this->writelog(8, 1, 101, 'File existed already in "%s"!', array($targetFolderObject->getIdentifier()));
768 } catch (t3lib_file_exception_InvalidFileNameException $e) {
769 $this->writelog(8, 1, 106, 'File name "%s" was not allowed!', $fileName);
770 } catch (RuntimeException $e) {
771 $this->writelog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', array($fileName, $targetFolderObject->getIdentifier()));
772 }
773
774 return $resultObject;
775 }
776
777 /**
778 * Editing textfiles or folders (action=9)
779 *
780 * @param array $cmds $cmds['data'] is the new content. $cmds['target'] is the target (file or dir)
781 * @return boolean Returns TRUE on success
782 */
783 function func_edit($cmds) {
784 if (!$this->isInit) {
785 return FALSE;
786 }
787
788 // Example indentifier for $cmds['target'] => "4:mypath/tomyfolder/myfile.jpg"
789 // for backwards compatibility: the combined file identifier was the path+filename
790 $fileIdentifier = $cmds['target'];
791 $fileObject = $this->getFileObject($fileIdentifier);
792
793 // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
794 $content = $cmds['data'];
795
796 if (!$fileObject instanceof t3lib_file_File) {
797 $this->writelog(9, 2, 123, 'Target "%s" was not a file!', array($fileIdentifier));
798 return FALSE;
799 }
800
801 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
802 if (!t3lib_div::inList($extList, $fileObject->getExtension())) {
803 $this->writelog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', array($fileObject->getExtension(), $extList));
804 return FALSE;
805 }
806
807 try {
808 $fileObject->setContents($content);
809 clearstatcache();
810 $this->writelog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', array($fileObject->getIdentifier(), $fileObject->getSize(), md5($content)));
811 return TRUE;
812 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
813 $this->writelog(9, 1, 104, 'You are not allowed to edit files!', '');
814 return FALSE;
815 } catch (t3lib_file_exception_InsufficientFileWritePermissionsException $e) {
816 $this->writelog(9, 1, 100, 'File "%s" was not saved! Write-permission problem?', array($fileObject->getIdentifier()));
817 return FALSE;
818 }
819 }
820
821 /**
822 * Upload of files (action=1)
823 * when having multiple uploads (HTML5-style), the array $_FILES looks like this:
824 * Array(
825 * [upload_1] => Array(
826 * [name] => Array(
827 * [0] => GData - Content-Elemente und Media-Gallery.pdf
828 * [1] => CMS Expo 2011.txt
829 * )
830 * [type] => Array(
831 * [0] => application/pdf
832 * [1] => text/plain
833 * )
834 * [tmp_name] => Array(
835 * [0] => /Applications/MAMP/tmp/php/phpNrOB43
836 * [1] => /Applications/MAMP/tmp/php/phpD2HQAK
837 * )
838 * [size] => Array(
839 * [0] => 373079
840 * [1] => 1291
841 * )
842 * )
843 * )
844 * in HTML you'd need sth like this: <input type="file" name="upload_1[]" multiple="true" />
845 *
846 * @param array $cmds $cmds['data'] is the ID-number (points to the global var that holds the filename-ref ($_FILES['upload_'.$id]['name']). $cmds['target'] is the target directory, $cmds['charset'] is the the character set of the file name (utf-8 is needed for JS-interaction)
847 * @return string Returns the new filename upon success
848 */
849 function func_upload($cmds) {
850 if (!$this->isInit) {
851 return FALSE;
852 }
853
854 $uploadPosition = $cmds['data'];
855 $uploadedFileData = $_FILES['upload_' . $uploadPosition];
856
857 if (empty($uploadedFileData['name']) || (is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0]))) {
858 $this->writelog(1, 2, 108, 'No file was uploaded!', '');
859 return FALSE;
860 }
861
862 // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
863 $targetFolderObject = $this->getFileObject($cmds['target']);
864
865 // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
866 if (!is_array($uploadedFileData['name'])) {
867 $uploadedFileData = array(
868 'name' => array($uploadedFileData['name']),
869 'type' => array($uploadedFileData['type']),
870 'tmp_name' => array($uploadedFileData['tmp_name']),
871 'size' => array($uploadedFileData['size']),
872 );
873 }
874
875 $resultObjects = array();
876
877 $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
878 // Loop through all uploaded files
879 for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
880 $fileInfo = array(
881 'name' => $uploadedFileData['name'][$i],
882 'type' => $uploadedFileData['type'][$i],
883 'tmp_name' => $uploadedFileData['tmp_name'][$i],
884 'size' => $uploadedFileData['size'][$i],
885 );
886 try {
887
888 // @todo can be improved towards conflict mode naming
889 if ($this->dontCheckForUnique) {
890 $conflictMode = 'replace';
891 } else {
892 $conflictMode = 'cancel';
893 }
894 $resultObjects[] = $targetFolderObject->addUploadedFile($fileInfo, $conflictMode);
895 $this->writelog(1, 0, 1, 'Uploading file "%s" to "%s"', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
896
897 } catch (t3lib_file_exception_UploadException $e) {
898 $this->writelog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
899 } catch (t3lib_file_exception_InsufficientUserPermissionsException $e) {
900 $this->writelog(1, 1, 105, 'You are not allowed to upload files!', '');
901 } catch (t3lib_file_exception_UploadSizeException $e) {
902 $this->writelog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', array($fileInfo['name']));
903 } catch (t3lib_file_exception_InsufficientFolderWritePermissionsException $e) {
904 $this->writelog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
905 } catch (t3lib_file_exception_IllegalFileExtensionException $e) {
906 $this->writelog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
907 } catch (t3lib_file_exception_ExistingTargetFileNameException $e) {
908 $this->writelog(1, 1, 101, 'No unique filename available in "%s"!', array($targetFolderObject->getIdentifier()));
909 } catch (RuntimeException $e) {
910 $this->writelog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', array($targetFolderObject->getIdentifier()));
911 }
912 }
913
914 return $resultObjects;
915 }
916
917 /**
918 * Unzipping file (action=7)
919 * This is permitted only if the user has fullAccess or if the file resides
920 *
921 * @param array $cmds $cmds['data'] is the zip-file. $cmds['target'] is the target directory. If not set we'll default to the same directory as the file is in.
922 * @return boolean Returns TRUE on success
923 */
924 function func_unzip($cmds) {
925 if (!$this->isInit || $this->dont_use_exec_commands) {
926 return FALSE;
927 }
928
929 $theFile = $cmds['data'];
930 if (!@is_file($theFile)) {
931 $this->writelog(7, 2, 105, 'The file "%s" did not exist!', array($theFile));
932 return FALSE;
933 }
934 $fI = t3lib_div::split_fileref($theFile);
935 if (!isset($cmds['target'])) {
936 $cmds['target'] = $fI['path'];
937 }
938 // Clean up destination directory
939 // !!! Method has been put in the local driver, can be saftely removed
940 $theDest = $this->is_directory($cmds['target']);
941 if (!$theDest) {
942 $this->writelog(7, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
943 return FALSE;
944 }
945 if (!$this->actionPerms['unzipFile']) {
946 $this->writelog(7, 1, 103, 'You are not allowed to unzip files', '');
947 return FALSE;
948 }
949 if ($fI['fileext'] != 'zip') {
950 $this->writelog(7, 1, 102, 'File extension is not "zip"', '');
951 return FALSE;
952 }
953 if (!$this->checkIfFullAccess($theDest)) {
954 $this->writelog(7, 1, 101, 'You don\'t have full access to the destination directory "%s"!', array($theDest));
955 return FALSE;
956 }
957 // !!! Method has been put in the sotrage driver, can be saftely removed
958 if ($this->checkPathAgainstMounts($theFile) && $this->checkPathAgainstMounts($theDest . '/')) {
959 // No way to do this under windows.
960 $cmd = $this->unzipPath . 'unzip -qq ' . escapeshellarg($theFile) . ' -d ' . escapeshellarg($theDest);
961 t3lib_utility_Command::exec($cmd);
962 $this->writelog(7, 0, 1, 'Unzipping file "%s" in "%s"', array($theFile, $theDest));
963 return TRUE;
964 } else {
965 $this->writelog(7, 1, 100, 'File "%s" or destination "%s" was not within your mountpoints!', array($theFile, $theDest));
966 return FALSE;
967 }
968 }
969 }
970
971 ?>