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