Fixed bug #17184: Disable the CSRF protection in ExtDirect calls coming from the...
[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 * $Id$
31 * Revised for TYPO3 3.6 May/2004 by Kasper Skårhøj
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 105: class t3lib_extFileFunctions extends t3lib_basicFileFunctions
41 * 151: function start($fileCmds)
42 * 181: function init_actionPerms($setup)
43 * 213: function processData()
44 * 270: function printLogErrorMessages($redirect='')
45 * 328: function findRecycler($theFile)
46 * 357: function writeLog($action,$error,$details_nr,$details,$data)
47 *
48 * SECTION: File operation functions
49 * 384: function func_delete($cmds)
50 * 451: function func_copy($cmds)
51 * 542: function func_move($cmds)
52 * 637: function func_rename($cmds)
53 * 683: function func_newfolder($cmds)
54 * 713: function func_newfile($cmds)
55 * 750: function func_edit($cmds)
56 * 782: function func_upload($cmds)
57 * 821: function func_unzip($cmds)
58 *
59 * TOTAL FUNCTIONS: 15
60 * (This index is automatically created/updated by the extension "extdeveval")
61 *
62 */
63
64
65 /**
66 * Contains functions for performing file operations like copying, pasting, uploading, moving, deleting etc. through the TCE
67 * Extending class to class t3lib_basicFileFunctions.
68 *
69 * see basicFileFunctions
70 * see document "TYPO3 Core API" for syntax
71 *
72 * This class contains functions primarily used by tce_file.php (TYPO3 Core Engine for file manipulation)
73 * Functions include copying, moving, deleting, uploading and so on...
74 *
75 * Important internal variables:
76 *
77 * $filemounts (see basicFileFunctions)
78 * $f_ext (see basicFileFunctions)
79 * ... All fileoperations must be within the filemount-paths. Further the fileextension MUST validate true with the f_ext array
80 *
81 * The unzip-function allows unzip only if the destination path has it's f_ext[]['allow'] set to '*'!!
82 * You are allowed to copy/move folders within the same 'space' (web/ftp).
83 * You are allowed to copy/move folders between spaces (web/ftp) IF the destination has it's f_ext[]['allow'] set to '*'!
84 *
85 * Advice:
86 * 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.
87 * 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
88 * For example this is a bad idea: you have an ftp-space that is '/www/' and a web-space that is '/www/htdocs/'
89 *
90 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
91 * @package TYPO3
92 * @subpackage t3lib
93 */
94 class t3lib_extFileFunctions extends t3lib_basicFileFunctions {
95
96 // External static variables:
97 // Notice; some of these are overridden in the start() method with values from $GLOBALS['TYPO3_CONF_VARS']['BE']
98 var $maxCopyFileSize = 10000; // max copy size (kb) for files
99 var $maxMoveFileSize = 10000; // max move size (kb) for files
100 var $maxUploadFileSize = 10000; // max upload size (kb) for files. Remember that PHP has an inner limit often set to 2 MB
101 var $unzipPath = ''; // Path to unzip-program (with trailing '/')
102 var $dontCheckForUnique = 0; // If set, the uploaded files will overwrite existing files.
103
104 var $actionPerms = Array( // This array is self-explaning (look in the class below). It grants access to the functions. This could be set from outside in order to enabled functions to users. See also the function init_actionPerms() which takes input directly from the user-record
105 'deleteFile' => 0, // Deleting files physically
106 'deleteFolder' => 0, // Deleting foldes physically
107 '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!
108 'moveFile' => 0,
109 'moveFolder' => 0,
110 'copyFile' => 0,
111 'copyFolder' => 0,
112 'newFolder' => 0,
113 'newFile' => 0,
114 'editFile' => 0,
115 'unzipFile' => 0,
116 'uploadFile' => 0,
117 'renameFile' => 0,
118 'renameFolder' => 0
119 );
120
121 var $recyclerFN = '_recycler_'; // This is regarded to be the recycler folder
122 var $useRecycler = 1; // 0 = no, 1 = if available, 2 = always
123
124 // Internal, static:
125 var $PHPFileFunctions = 0; // If set, all fileoperations are done by the default PHP-functions. This is necessary under windows! On UNIX the system commands by exec() can be used unless safe_mode is enabled
126 var $dont_use_exec_commands = 0; // This is necessary under windows!
127
128 // Internal, dynamic:
129 var $internalUploadMap = array(); // Will contain map between upload ID and the final filename
130
131 var $lastError = '';
132
133
134 /**
135 * Initialization of the class
136 *
137 * @param array The $file array with the commands to execute. See "TYPO3 Core API" document
138 * @return void
139 */
140 function start($fileCmds) {
141
142 // Configure settings from TYPO3_CONF_VARS:
143 if (TYPO3_OS == 'WIN' || $GLOBALS['TYPO3_CONF_VARS']['BE']['disable_exec_function']) {
144 $this->PHPFileFunctions = 1;
145 $this->dont_use_exec_commands = 1;
146 } else {
147 $this->PHPFileFunctions = $GLOBALS['TYPO3_CONF_VARS']['BE']['usePHPFileFunctions'];
148 }
149
150 $this->unzipPath = $GLOBALS['TYPO3_CONF_VARS']['BE']['unzip_path'];
151
152 $maxFileSize = intval($GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize']);
153 if ($maxFileSize > 0) {
154 $this->maxCopyFileSize = $maxFileSize;
155 $this->maxMoveFileSize = $maxFileSize;
156 }
157 $this->maxUploadFileSize = t3lib_div::getMaxUploadFileSize();
158
159 // Initializing file processing commands:
160 $this->fileCmdMap = $fileCmds;
161 }
162
163 /**
164 * Sets up permission to perform file/directory operations.
165 * See below or the be_user-table for the significance of the various bits in $setup.
166 *
167 * @param integer File permission integer from BE_USER OR'ed with permissions of back-end groups this user is a member of
168 * @return void
169 */
170 function init_actionPerms($setup) {
171 if (($setup & 1) == 1) { // Files: Upload,Copy,Move,Delete,Rename
172 $this->actionPerms['uploadFile'] = 1;
173 $this->actionPerms['copyFile'] = 1;
174 $this->actionPerms['moveFile'] = 1;
175 $this->actionPerms['deleteFile'] = 1;
176 $this->actionPerms['renameFile'] = 1;
177 $this->actionPerms['editFile'] = 1;
178 $this->actionPerms['newFile'] = 1;
179 }
180 if (($setup & 2) == 2) { // Files: Unzip
181 $this->actionPerms['unzipFile'] = 1;
182 }
183 if (($setup & 4) == 4) { // Directory: Move,Delete,Rename,New
184 $this->actionPerms['moveFolder'] = 1;
185 $this->actionPerms['deleteFolder'] = 1;
186 $this->actionPerms['renameFolder'] = 1;
187 $this->actionPerms['newFolder'] = 1;
188 }
189 if (($setup & 8) == 8) { // Directory: Copy
190 $this->actionPerms['copyFolder'] = 1;
191 }
192 if (($setup & 16) == 16) { // Directory: Delete recursively (rm -Rf)
193 $this->actionPerms['deleteFolderRecursively'] = 1;
194 }
195 }
196
197 /**
198 * Processing the command array in $this->fileCmdMap
199 *
200 * @return mixed false, if the file functions were not initialized
201 * otherwise returns an array of all the results that are returned
202 * from each command, separated in each action.
203 */
204 function processData() {
205 $result = array();
206 if (!$this->isInit) {
207 return FALSE;
208 }
209
210 if (is_array($this->fileCmdMap)) {
211
212 // Check if there were uploads expected, but no one made
213 if ($this->fileCmdMap['upload']) {
214 $uploads = $this->fileCmdMap['upload'];
215 foreach ($uploads as $upload) {
216 if (!$_FILES['upload_' . $upload['data']]['name']) {
217 unset($this->fileCmdMap['upload'][$upload['data']]);
218 }
219 }
220 if (count($this->fileCmdMap['upload']) == 0) {
221 $this->writelog(1, 1, 108, 'No file was uploaded!', '');
222 }
223 }
224
225 // Traverse each set of actions
226 foreach ($this->fileCmdMap as $action => $actionData) {
227
228 // Traverse all action data. More than one file might be affected at the same time.
229 if (is_array($actionData)) {
230 $result[$action] = array();
231 foreach ($actionData as $cmdArr) {
232
233 // Clear file stats
234 clearstatcache();
235
236 // Branch out based on command:
237 switch ($action) {
238 case 'delete':
239 $result[$action][] = $this->func_delete($cmdArr);
240 break;
241 case 'copy':
242 $result[$action][] = $this->func_copy($cmdArr);
243 break;
244 case 'move':
245 $result[$action][] = $this->func_move($cmdArr);
246 break;
247 case 'rename':
248 $result[$action][] = $this->func_rename($cmdArr);
249 break;
250 case 'newfolder':
251 $result[$action][] = $this->func_newfolder($cmdArr);
252 break;
253 case 'newfile':
254 $result[$action][] = $this->func_newfile($cmdArr);
255 break;
256 case 'editfile':
257 $result[$action][] = $this->func_edit($cmdArr);
258 break;
259 case 'upload':
260 $result[$action][] = $this->func_upload($cmdArr);
261 break;
262 case 'unzip':
263 $result[$action][] = $this->func_unzip($cmdArr);
264 break;
265 }
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 = t3lib_div::getUserObj($classRef);
271
272 if (!($hookObject instanceof t3lib_extFileFunctions_processDataHook)) {
273 throw new UnexpectedValueException('$hookObject must implement interface t3lib_extFileFunctions_processDataHook', 1279719168);
274 }
275
276 $hookObject->processData_postProcessAction($action, $cmdArr, $result[$action], $this);
277 }
278 }
279 }
280 }
281 }
282 }
283 return $result;
284 }
285
286 /**
287 * Adds log error messages from the operations of this script instance to the FlashMessageQueue
288 *
289 * @param string Redirect URL (for creating link in message)
290 * @return void
291 */
292 function printLogErrorMessages($redirect = '') {
293 $this->getErrorMessages();
294 }
295
296
297 /**
298 * Adds log error messages from the previous file operations of this script instance
299 * to the FlashMessageQueue
300 *
301 * @return void
302 */
303 function getErrorMessages() {
304 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
305 '*',
306 'sys_log',
307 'type = 2 AND userid = ' . intval($GLOBALS['BE_USER']->user['uid'])
308 . ' AND tstamp=' . intval($GLOBALS['EXEC_TIME'])
309 . ' AND error != 0'
310 );
311 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
312 $logData = unserialize($row['log_data']);
313 $msg = $row['error'] . ': ' . sprintf($row['details'], $logData[0], $logData[1], $logData[2], $logData[3], $logData[4]);
314 $flashMessage = t3lib_div::makeInstance(
315 't3lib_FlashMessage',
316 $msg,
317 '',
318 t3lib_FlashMessage::ERROR,
319 TRUE
320 );
321 t3lib_FlashMessageQueue::addMessage($flashMessage);
322 }
323 $GLOBALS['TYPO3_DB']->sql_free_result($res);
324 }
325
326
327 /**
328 * Goes back in the path and checks in each directory if a folder named $this->recyclerFN (usually '_recycler_') is present.
329 * 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
330 *
331 * @param string Takes a valid Path ($theFile)
332 * @return string Returns the path (without trailing slash) of the closest recycle-folder if found. Else false.
333 */
334 function findRecycler($theFile) {
335 if ($this->isPathValid($theFile)) {
336 $theFile = $this->cleanDirectoryName($theFile);
337 $fI = t3lib_div::split_fileref($theFile);
338 $c = 0;
339 while ($this->checkPathAgainstMounts($fI['path']) && $c < 20) {
340 $rDir = $fI['path'] . $this->recyclerFN;
341 if (@is_dir($rDir) && $this->recyclerFN != $fI['file']) {
342 return $rDir;
343 }
344 $theFile = $fI['path'];
345 $theFile = $this->cleanDirectoryName($theFile);
346 $fI = t3lib_div::split_fileref($theFile);
347 $c++;
348 }
349 }
350 }
351
352 /**
353 * Logging file operations
354 *
355 * @param integer The action number. See the functions in the class for a hint. Eg. edit is '9', upload is '1' ...
356 * @param integer The severity: 0 = message, 1 = error, 2 = System Error, 3 = security notice (admin)
357 * @param integer 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.
358 * @param string This is the default, raw error message in english
359 * @param array Array with special information that may go into $details by "%s" marks / sprintf() when the log is shown
360 * @return void
361 * @see class.t3lib_userauthgroup.php
362 */
363 function writeLog($action, $error, $details_nr, $details, $data) {
364 $type = 2; // Type value for tce_file.php
365 if (is_object($GLOBALS['BE_USER'])) {
366 $GLOBALS['BE_USER']->writelog($type, $action, $error, $details_nr, $details, $data);
367 }
368 $this->lastError = vsprintf($details, $data);
369 }
370
371
372 /*************************************
373 *
374 * File operation functions
375 *
376 **************************************/
377
378 /**
379 * Deleting files and folders (action=4)
380 *
381 * @param array $cmds['data'] is the file/folder to delete
382 * @return boolean Returns true upon success
383 */
384 function func_delete($cmds) {
385 if (!$this->isInit) {
386 return FALSE;
387 }
388
389 // Checking path:
390 $theFile = $cmds['data'];
391 if (!$this->isPathValid($theFile)) {
392 $this->writelog(4, 2, 101, 'Target "%s" had invalid path (".." and "//" is not allowed in path).', Array($theFile));
393 return FALSE;
394 }
395
396 // Recycler moving or not?
397 if ($this->useRecycler && $recyclerPath = $this->findRecycler($theFile)) {
398 // If a recycler is found, the deleted items is moved to the recycler and not just deleted.
399 $newCmds = Array();
400 $newCmds['data'] = $theFile;
401 $newCmds['target'] = $recyclerPath;
402 $newCmds['altName'] = 1;
403 $this->func_move($newCmds);
404 $this->writelog(4, 0, 4, 'Item "%s" moved to recycler at "%s"', Array($theFile, $recyclerPath));
405 return TRUE;
406 } elseif ($this->useRecycler != 2) { // if $this->useRecycler==2 then we cannot delete for real!!
407 if (@is_file($theFile)) { // If we are deleting a file...
408 if ($this->actionPerms['deleteFile']) {
409 if ($this->checkPathAgainstMounts($theFile)) {
410 if (@unlink($theFile)) {
411 $this->writelog(4, 0, 1, 'File "%s" deleted', Array($theFile));
412 return TRUE;
413 } else {
414 $this->writelog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', Array($theFile));
415 }
416 } else {
417 $this->writelog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', Array($theFile));
418 }
419 } else {
420 $this->writelog(4, 1, 112, 'You are not allowed to delete files', '');
421 }
422 // FINISHED deleting file
423
424 } elseif (@is_dir($theFile)) { // if we're deleting a folder
425 if ($this->actionPerms['deleteFolder']) {
426 $theFile = $this->is_directory($theFile);
427 if ($theFile) {
428 if ($this->checkPathAgainstMounts($theFile)) { // I choose not to append '/' to $theFile here as this will prevent us from deleting mounts!! (which makes sense to me...)
429 if ($this->actionPerms['deleteFolderRecursively']) {
430 if (t3lib_div::rmdir($theFile, TRUE)) {
431 $this->writelog(4, 0, 2, 'Directory "%s" deleted recursively!', Array($theFile));
432 return TRUE;
433 } else {
434 $this->writelog(4, 2, 119, 'Directory "%s" WAS NOT deleted recursively! Write-permission problem?', Array($theFile));
435 }
436 } else {
437 if (@rmdir($theFile)) {
438 $this->writelog(4, 0, 3, 'Directory "%s" deleted', Array($theFile));
439 return TRUE;
440 } else {
441 $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($theFile));
442 }
443 }
444 } else {
445 $this->writelog(4, 1, 121, 'Target was not within your mountpoints! T="%s"', Array($theFile));
446 }
447 } else {
448 $this->writelog(4, 2, 122, 'Target seemed not to be a directory! (Shouldn\'t happen here!)', '');
449 }
450 } else {
451 $this->writelog(4, 1, 123, 'You are not allowed to delete directories', '');
452 }
453 // FINISHED copying directory
454
455 } else {
456 $this->writelog(4, 2, 130, 'The item was not a file or directory! "%s"', Array($theFile));
457 }
458 } else {
459 $this->writelog(4, 1, 131, 'No recycler found!', '');
460 }
461 }
462
463 /**
464 * Copying files and folders (action=2)
465 *
466 * @param array $cmds['data'] is the file/folder to copy. $cmds['target'] is the path where to copy to. $cmds['altName'] (boolean): If set, another filename is found in case the target already exists
467 * @return string Returns the new filename upon success
468 */
469 function func_copy($cmds) {
470 if (!$this->isInit) {
471 return FALSE;
472 }
473
474 // Initialize and check basic conditions:
475 $theFile = $cmds['data'];
476 $theDest = $this->is_directory($cmds['target']); // Clean up destination directory
477 $altName = $cmds['altName'];
478 if (!$theDest) {
479 $this->writelog(2, 2, 100, 'Destination "%s" was not a directory', Array($cmds['target']));
480 return FALSE;
481 }
482 if (!$this->isPathValid($theFile) || !$this->isPathValid($theDest)) {
483 $this->writelog(2, 2, 101, 'Target or destination had invalid path (".." and "//" is not allowed in path). T="%s", D="%s"', Array($theFile, $theDest));
484 return FALSE;
485 }
486
487 // Processing of file or directory.
488 if (@is_file($theFile)) { // If we are copying a file...
489 if ($this->actionPerms['copyFile']) {
490 if (filesize($theFile) < ($this->maxCopyFileSize * 1024)) {
491 $fI = t3lib_div::split_fileref($theFile);
492 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
493 $theDestFile = $this->getUniqueName($fI['file'], $theDest);
494 $fI = t3lib_div::split_fileref($theDestFile);
495 } else {
496 $theDestFile = $theDest . '/' . $fI['file'];
497 }
498 if ($theDestFile && !file_exists($theDestFile)) {
499 if ($this->checkIfAllowed($fI['fileext'], $theDest, $fI['file'])) {
500 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
501 if ($this->PHPFileFunctions) {
502 copy($theFile, $theDestFile);
503 } else {
504 $cmd = 'cp "' . $theFile . '" "' . $theDestFile . '"';
505 t3lib_utility_Command::exec($cmd);
506 }
507 t3lib_div::fixPermissions($theDestFile);
508 clearstatcache();
509 if (@is_file($theDestFile)) {
510 $this->writelog(2, 0, 1, 'File "%s" copied to "%s"', Array($theFile, $theDestFile));
511 return $theDestFile;
512 } else {
513 $this->writelog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', Array($theFile, $theDestFile));
514 }
515 } else {
516 $this->writelog(2, 1, 110, 'Target or destination was not within your mountpoints! T="%s", D="%s"', Array($theFile, $theDestFile));
517 }
518 } else {
519 $this->writelog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', Array($fI['file'], $theDest . '/'));
520 }
521 } else {
522 $this->writelog(2, 1, 112, 'File "%s" already exists!', Array($theDestFile));
523 }
524 } else {
525 $this->writelog(2, 1, 113, 'File "%s" exceeds the size-limit of %s bytes', Array($theFile, $this->maxCopyFileSize * 1024));
526 }
527 } else {
528 $this->writelog(2, 1, 114, 'You are not allowed to copy files', '');
529 }
530 // FINISHED copying file
531
532 } elseif (@is_dir($theFile) && !$this->dont_use_exec_commands) { // if we're copying a folder
533 if ($this->actionPerms['copyFolder']) {
534 $theFile = $this->is_directory($theFile);
535 if ($theFile) {
536 $fI = t3lib_div::split_fileref($theFile);
537 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
538 $theDestFile = $this->getUniqueName($fI['file'], $theDest);
539 $fI = t3lib_div::split_fileref($theDestFile);
540 } else {
541 $theDestFile = $theDest . '/' . $fI['file'];
542 }
543 if ($theDestFile && !file_exists($theDestFile)) {
544 if (!t3lib_div::isFirstPartOfStr($theDestFile . '/', $theFile . '/')) { // Check if the one folder is inside the other or on the same level... to target/dest is the same?
545 if ($this->checkIfFullAccess($theDest) || $this->is_webPath($theDestFile) == $this->is_webPath($theFile)) { // no copy of folders between spaces
546 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
547 // No way to do this under windows!
548 $cmd = 'cp -R "' . $theFile . '" "' . $theDestFile . '"';
549 t3lib_utility_Command::exec($cmd);
550 clearstatcache();
551 if (@is_dir($theDestFile)) {
552 $this->writelog(2, 0, 2, 'Directory "%s" copied to "%s"', Array($theFile, $theDestFile));
553 return $theDestFile;
554 } else {
555 $this->writelog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', Array($theFile, $theDestFile));
556 }
557 } else {
558 $this->writelog(2, 1, 120, 'Target or destination was not within your mountpoints! T="%s", D="%s"', Array($theFile, $theDestFile));
559 }
560 } else {
561 $this->writelog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', Array($theDest . '/'));
562 }
563 } else {
564 $this->writelog(2, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', Array($theDestFile . '/', $theFile . '/'));
565 }
566 } else {
567 $this->writelog(2, 1, 123, 'Target "%s" already exists!', Array($theDestFile));
568 }
569 } else {
570 $this->writelog(2, 2, 124, 'Target seemed not to be a directory! (Shouldn\'t happen here!)', '');
571 }
572 } else {
573 $this->writelog(2, 1, 125, 'You are not allowed to copy directories', '');
574 }
575 // FINISHED copying directory
576
577 } else {
578 $this->writelog(2, 2, 130, 'The item "%s" was not a file or directory!', Array($theFile));
579 }
580 }
581
582 /**
583 * Moving files and folders (action=3)
584 *
585 * @param array $cmds['data'] is the file/folder to move. $cmds['target'] is the path where to move to. $cmds['altName'] (boolean): If set, another filename is found in case the target already exists
586 * @return string Returns the new filename upon success
587 */
588 function func_move($cmds) {
589 if (!$this->isInit) {
590 return FALSE;
591 }
592
593 // Initialize and check basic conditions:
594 $theFile = $cmds['data'];
595 $theDest = $this->is_directory($cmds['target']); // Clean up destination directory
596 $altName = $cmds['altName'];
597 if (!$theDest) {
598 $this->writelog(3, 2, 100, 'Destination "%s" was not a directory', Array($cmds['target']));
599 return FALSE;
600 }
601 if (!$this->isPathValid($theFile) || !$this->isPathValid($theDest)) {
602 $this->writelog(3, 2, 101, 'Target or destination had invalid path (".." and "//" is not allowed in path). T="%s", D="%s"', Array($theFile, $theDest));
603 return FALSE;
604 }
605
606 // Processing of file or directory:
607 if (@is_file($theFile)) { // If we are moving a file...
608 if ($this->actionPerms['moveFile']) {
609 if (filesize($theFile) < ($this->maxMoveFileSize * 1024)) {
610 $fI = t3lib_div::split_fileref($theFile);
611 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
612 $theDestFile = $this->getUniqueName($fI['file'], $theDest);
613 $fI = t3lib_div::split_fileref($theDestFile);
614 } else {
615 $theDestFile = $theDest . '/' . $fI['file'];
616 }
617 if ($theDestFile && !file_exists($theDestFile)) {
618 if ($this->checkIfAllowed($fI['fileext'], $theDest, $fI['file'])) {
619 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
620 if ($this->PHPFileFunctions) {
621 @rename($theFile, $theDestFile);
622 } else {
623 $cmd = 'mv "' . $theFile . '" "' . $theDestFile . '"';
624 t3lib_utility_Command::exec($cmd);
625 }
626 clearstatcache();
627 if (@is_file($theDestFile)) {
628 $this->writelog(3, 0, 1, 'File "%s" moved to "%s"', Array($theFile, $theDestFile));
629 return $theDestFile;
630 } else {
631 $this->writelog(3, 2, 109, 'File "%s" WAS NOT moved to "%s"! Write-permission problem?', Array($theFile, $theDestFile));
632 }
633 } else {
634 $this->writelog(3, 1, 110, 'Target or destination was not within your mountpoints! T="%s", D="%s"', Array($theFile, $theDestFile));
635 }
636 } else {
637 $this->writelog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', Array($fI['file'], $theDest . '/'));
638 }
639 } else {
640 $this->writelog(3, 1, 112, 'File "%s" already exists!', Array($theDestFile));
641 }
642 } else {
643 $this->writelog(3, 1, 113, 'File "%s" exceeds the size-limit of %s bytes', Array($theFile, $this->maxMoveFileSize * 1024));
644 }
645 } else {
646 $this->writelog(3, 1, 114, 'You are not allowed to move files', '');
647 }
648 // FINISHED moving file
649
650 } elseif (@is_dir($theFile)) { // if we're moving a folder
651 if ($this->actionPerms['moveFolder']) {
652 $theFile = $this->is_directory($theFile);
653 if ($theFile) {
654 $fI = t3lib_div::split_fileref($theFile);
655 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
656 $theDestFile = $this->getUniqueName($fI['file'], $theDest);
657 $fI = t3lib_div::split_fileref($theDestFile);
658 } else {
659 $theDestFile = $theDest . '/' . $fI['file'];
660 }
661 if ($theDestFile && !file_exists($theDestFile)) {
662 if (!t3lib_div::isFirstPartOfStr($theDestFile . '/', $theFile . '/')) { // Check if the one folder is inside the other or on the same level... to target/dest is the same?
663 if ($this->checkIfFullAccess($theDest) || $this->is_webPath($theDestFile) == $this->is_webPath($theFile)) { // // no moving of folders between spaces
664 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
665 if ($this->PHPFileFunctions) {
666 @rename($theFile, $theDestFile);
667 } else {
668 $cmd = 'mv "' . $theFile . '" "' . $theDestFile . '"';
669 $errArr = array();
670 t3lib_utility_Command::exec($cmd, $errArr);
671 }
672 clearstatcache();
673 if (@is_dir($theDestFile)) {
674 $this->writelog(3, 0, 2, 'Directory "%s" moved to "%s"', Array($theFile, $theDestFile));
675 return $theDestFile;
676 } else {
677 $this->writelog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', Array($theFile, $theDestFile));
678 }
679 } else {
680 $this->writelog(3, 1, 120, 'Target or destination was not within your mountpoints! T="%s", D="%s"', Array($theFile, $theDestFile));
681 }
682 } else {
683 $this->writelog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', Array($theDest . '/'));
684 }
685 } else {
686 $this->writelog(3, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', Array($theDestFile . '/', $theFile . '/'));
687 }
688 } else {
689 $this->writelog(3, 1, 123, 'Target "%s" already exists!', Array($theDestFile));
690 }
691 } else {
692 $this->writelog(3, 2, 124, 'Target seemed not to be a directory! (Shouldn\'t happen here!)', '');
693 }
694 } else {
695 $this->writelog(3, 1, 125, 'You are not allowed to move directories', '');
696 }
697 // FINISHED moving directory
698
699 } else {
700 $this->writelog(3, 2, 130, 'The item "%s" was not a file or directory!', Array($theFile));
701 }
702 }
703
704 /**
705 * Renaming files or foldes (action=5)
706 *
707 * @param array $cmds['data'] is the new name. $cmds['target'] is the target (file or dir).
708 * @return string Returns the new filename upon success
709 */
710 function func_rename($cmds) {
711 if (!$this->isInit) {
712 return FALSE;
713 }
714
715 $theNewName = $this->cleanFileName($cmds['data']);
716 if ($theNewName) {
717 if ($this->checkFileNameLen($theNewName)) {
718 $theTarget = $cmds['target'];
719 $type = filetype($theTarget);
720 if ($type == 'file' || $type == 'dir') { // $type MUST BE file or dir
721 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
722 if ($fileInfo['file'] != $theNewName) { // The name should be different from the current. And the filetype must be allowed
723 $theRenameName = $fileInfo['path'] . $theNewName;
724 if ($this->checkPathAgainstMounts($fileInfo['path'])) {
725 if (!file_exists($theRenameName)) {
726 if ($type == 'file') {
727 if ($this->actionPerms['renameFile']) {
728 $fI = t3lib_div::split_fileref($theRenameName);
729 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
730 if (@rename($theTarget, $theRenameName)) {
731 $this->writelog(5, 0, 1, 'File renamed from "%s" to "%s"', Array($fileInfo['file'], $theNewName));
732 return $theRenameName;
733 } else {
734 $this->writelog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', Array($theTarget, $fileInfo['path']));
735 }
736 } else {
737 $this->writelog(5, 1, 101, 'Extension of file name "%s" was not allowed!', Array($fI['file']));
738 }
739 } else {
740 $this->writelog(5, 1, 102, 'You are not allowed to rename files!', '');
741 }
742 } elseif ($type == 'dir') {
743 if ($this->actionPerms['renameFolder']) {
744 if (@rename($theTarget, $theRenameName)) {
745 $this->writelog(5, 0, 2, 'Directory renamed from "%s" to "%s"', Array($fileInfo['file'], $theNewName));
746 return $theRenameName;
747 } else {
748 $this->writelog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', Array($theTarget, $fileInfo['path']));
749 }
750 } else {
751 $this->writelog(5, 1, 111, 'You are not allowed to rename directories!', '');
752 }
753 }
754 } else {
755 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', Array($theRenameName));
756 }
757 } else {
758 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', Array($fileInfo['path']));
759 }
760 } else {
761 $this->writelog(5, 1, 122, 'Old and new name is the same (%s)', Array($theNewName));
762 }
763 } else {
764 $this->writelog(5, 2, 123, 'Target "%s" was neither a directory nor a file!', Array($theTarget));
765 }
766 } else {
767 $this->writelog(5, 1, 124, 'New name "%s" was too long (max %s characters)', Array($theNewName, $this->maxInputNameLen));
768 }
769 }
770 }
771
772 /**
773 * This creates a new folder. (action=6)
774 *
775 * @param array $cmds['data'] is the foldername. $cmds['target'] is the path where to create it.
776 * @return string Returns the new foldername upon success
777 */
778 function func_newfolder($cmds) {
779 if (!$this->isInit) {
780 return FALSE;
781 }
782
783 $theFolder = $this->cleanFileName($cmds['data']);
784 if (isset($theFolder) && trim($theFolder) != '') {
785 if ($this->checkFileNameLen($theFolder)) {
786 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
787 if ($theTarget) {
788 if ($this->actionPerms['newFolder']) {
789 $theNewFolder = $theTarget . '/' . $theFolder;
790 if ($this->checkPathAgainstMounts($theNewFolder)) {
791 if (!file_exists($theNewFolder)) {
792 if (t3lib_div::mkdir($theNewFolder)) {
793 $this->writelog(6, 0, 1, 'Directory "%s" created in "%s"', Array($theFolder, $theTarget . '/'));
794 return $theNewFolder;
795 } else {
796 $this->writelog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', Array($theFolder, $theTarget . '/'));
797 }
798 } else {
799 $this->writelog(6, 1, 101, 'File or directory "%s" existed already!', Array($theNewFolder));
800 }
801 } else {
802 $this->writelog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', Array($theTarget . '/'));
803 }
804 } else {
805 $this->writelog(6, 1, 103, 'You are not allowed to create directories!', '');
806 }
807 } else {
808 $this->writelog(6, 2, 104, 'Destination "%s" was not a directory', Array($cmds['target']));
809 }
810 } else {
811 $this->writelog(6, 1, 105, 'New name "%s" was too long (max %s characters)', Array($theFolder, $this->maxInputNameLen));
812 }
813 }
814 }
815
816 /**
817 * This creates a new file. (action=8)
818 *
819 * @param array $cmds['data'] is the new filename. $cmds['target'] is the path where to create it
820 * @return string Returns the new filename upon success
821 */
822 function func_newfile($cmds) {
823 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
824 if (!$this->isInit) {
825 return FALSE;
826 }
827 $newName = $this->cleanFileName($cmds['data']);
828 if ($newName) {
829 if ($this->checkFileNameLen($newName)) {
830 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
831 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
832 if ($theTarget) {
833 if ($this->actionPerms['newFile']) {
834 $theNewFile = $theTarget . '/' . $newName;
835 if ($this->checkPathAgainstMounts($theNewFile)) {
836 if (!file_exists($theNewFile)) {
837 $fI = t3lib_div::split_fileref($theNewFile);
838 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
839 if (t3lib_div::inList($extList, $fI['fileext'])) {
840 if (t3lib_div::writeFile($theNewFile, '')) {
841 clearstatcache();
842 $this->writelog(8, 0, 1, 'File created: "%s"', Array($fI['file']));
843 return $theNewFile;
844 } else {
845 $this->writelog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', Array($fI['file'], $theTarget));
846 }
847 } else {
848 $this->writelog(8, 1, 107, 'File extension "%s" is not a textfile format! (%s)', Array($fI['fileext'], $extList));
849 }
850 } else {
851 $this->writelog(8, 1, 106, 'Extension of file name "%s" was not allowed!', Array($fI['file']));
852 }
853 } else {
854 $this->writelog(8, 1, 101, 'File "%s" existed already!', Array($theNewFile));
855 }
856 } else {
857 $this->writelog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', Array($theTarget . '/'));
858 }
859 } else {
860 $this->writelog(8, 1, 103, 'You are not allowed to create files!', '');
861 }
862 } else {
863 $this->writelog(8, 2, 104, 'Destination "%s" was not a directory', Array($cmds['target']));
864 }
865 } else {
866 $this->writelog(8, 1, 105, 'New name "%s" was too long (max %s characters)', Array($newName, $this->maxInputNameLen));
867 }
868 }
869 }
870
871 /**
872 * Editing textfiles or folders (action=9)
873 *
874 * @param array $cmds['data'] is the new content. $cmds['target'] is the target (file or dir)
875 * @return boolean Returns true on success
876 */
877 function func_edit($cmds) {
878 if (!$this->isInit) {
879 return FALSE;
880 }
881 $theTarget = $cmds['target'];
882 $content = $cmds['data'];
883 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
884 $type = filetype($theTarget);
885 if ($type == 'file') { // $type MUST BE file
886 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
887 $fI = $fileInfo;
888 if ($this->checkPathAgainstMounts($fileInfo['path'])) {
889 if ($this->actionPerms['editFile']) {
890 $fI = t3lib_div::split_fileref($theTarget);
891 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
892 if (t3lib_div::inList($extList, $fileInfo['fileext'])) {
893 if (t3lib_div::writeFile($theTarget, $content)) {
894 clearstatcache();
895 $this->writelog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', Array($fileInfo['file'], @filesize($theTarget), md5($content)));
896 return TRUE;
897 } else {
898 $this->writelog(9, 1, 100, 'File "%s" was not saved! Write-permission problem in "%s"?', Array($theTarget, $fileInfo['path']));
899 }
900 } else {
901 $this->writelog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', Array($fI['fileext'], $extList));
902 }
903 } else {
904 $this->writelog(9, 1, 103, 'Extension of file name "%s" was not allowed!', Array($fI['file']));
905 }
906 } else {
907 $this->writelog(9, 1, 104, 'You are not allowed to edit files!', '');
908 }
909 } else {
910 $this->writelog(9, 1, 121, 'Destination path "%s" was not within your mountpoints!', Array($fileInfo['path']));
911 }
912 } else {
913 $this->writelog(9, 2, 123, 'Target "%s" was not a file!', Array($theTarget));
914 }
915 }
916
917 /**
918 * Upload of files (action=1)
919 *
920 * @param array $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)
921 * @return string Returns the new filename upon success
922 */
923 function func_upload($cmds) {
924 if (!$this->isInit) {
925 return FALSE;
926 }
927 $id = $cmds['data'];
928 if ($_FILES['upload_' . $id]['name']) {
929 $theFile = $_FILES['upload_' . $id]['tmp_name']; // filename of the uploaded file
930 $theFileSize = $_FILES['upload_' . $id]['size']; // filesize of the uploaded file
931 $theName = $this->cleanFileName(stripslashes($_FILES['upload_' . $id]['name']), (isset($cmds['charset']) ? $cmds['charset'] : '')); // The original filename
932 if (is_uploaded_file($theFile) && $theName) { // Check the file
933 if ($this->actionPerms['uploadFile']) {
934 if ($theFileSize < ($this->maxUploadFileSize * 1024)) {
935 $fI = t3lib_div::split_fileref($theName);
936 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
937 if ($theTarget && $this->checkPathAgainstMounts($theTarget . '/')) {
938 if ($this->checkIfAllowed($fI['fileext'], $theTarget, $fI['file'])) {
939 $theNewFile = $this->getUniqueName($theName, $theTarget, $this->dontCheckForUnique);
940 if ($theNewFile) {
941 t3lib_div::upload_copy_move($theFile, $theNewFile);
942 clearstatcache();
943 if (@is_file($theNewFile)) {
944 $this->internalUploadMap[$id] = $theNewFile;
945 $this->writelog(1, 0, 1, 'Uploading file "%s" to "%s"', Array($theName, $theNewFile, $id));
946 return $theNewFile;
947 } else {
948 $this->writelog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', Array($theTarget . '/'));
949 }
950 } else {
951 $this->writelog(1, 1, 101, 'No unique filename available in "%s"!', Array($theTarget . '/'));
952 }
953 } else {
954 $this->writelog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', Array($fI['file'], $theTarget . '/'));
955 }
956 } else {
957 $this->writelog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', Array($theTarget . '/'));
958 }
959 } else {
960 $this->writelog(1, 1, 104, 'The uploaded file exceeds the size-limit of %s bytes', Array($this->maxUploadFileSize * 1024));
961 }
962 } else {
963 $this->writelog(1, 1, 105, 'You are not allowed to upload files!', '');
964 }
965 } else {
966 $this->writelog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
967 }
968 } else {
969 $this->writelog(1, 2, 108, 'No file was uploaded!', '');
970 }
971 }
972
973 /**
974 * Unzipping file (action=7)
975 * This is permitted only if the user has fullAccess or if the file resides
976 *
977 * @param array $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.
978 * @return boolean Returns true on success
979 */
980 function func_unzip($cmds) {
981 if (!$this->isInit || $this->dont_use_exec_commands) {
982 return FALSE;
983 }
984
985 $theFile = $cmds['data'];
986 if (@is_file($theFile)) {
987 $fI = t3lib_div::split_fileref($theFile);
988 if (!isset($cmds['target'])) {
989 $cmds['target'] = $fI['path'];
990 }
991 $theDest = $this->is_directory($cmds['target']); // Clean up destination directory
992 if ($theDest) {
993 if ($this->actionPerms['unzipFile']) {
994 if ($fI['fileext'] == 'zip') {
995 if ($this->checkIfFullAccess($theDest)) {
996 if ($this->checkPathAgainstMounts($theFile) && $this->checkPathAgainstMounts($theDest . '/')) {
997 // No way to do this under windows.
998 $cmd = $this->unzipPath . 'unzip -qq "' . $theFile . '" -d "' . $theDest . '"';
999 t3lib_utility_Command::exec($cmd);
1000 $this->writelog(7, 0, 1, 'Unzipping file "%s" in "%s"', Array($theFile, $theDest));
1001 return TRUE;
1002 } else {
1003 $this->writelog(7, 1, 100, 'File "%s" or destination "%s" was not within your mountpoints!', Array($theFile, $theDest));
1004 }
1005 } else {
1006 $this->writelog(7, 1, 101, 'You don\'t have full access to the destination directory "%s"!', Array($theDest));
1007 }
1008 } else {
1009 $this->writelog(7, 1, 102, 'File extension is not "zip"', '');
1010 }
1011 } else {
1012 $this->writelog(7, 1, 103, 'You are not allowed to unzip files', '');
1013 }
1014 } else {
1015 $this->writelog(7, 2, 104, 'Destination "%s" was not a directory', Array($cmds['target']));
1016 }
1017 } else {
1018 $this->writelog(7, 2, 105, 'The file "%s" did not exist!', Array($theFile));
1019 }
1020 }
1021 }
1022
1023 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extfilefunc.php'])) {
1024 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extfilefunc.php']);
1025 }
1026
1027 ?>