Fixed bug #16445: Performance Tuning: Replace exec_SELECTgetRows with exec_SELECTgetS...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_extfilefunc.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 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 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 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 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 $retVar = 0;
671 exec($cmd, $errArr, $retVar);
672 }
673 clearstatcache();
674 if (@is_dir($theDestFile)) {
675 $this->writelog(3, 0, 2, 'Directory "%s" moved to "%s"', Array($theFile, $theDestFile));
676 return $theDestFile;
677 } else {
678 $this->writelog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', Array($theFile, $theDestFile));
679 }
680 } else {
681 $this->writelog(3, 1, 120, 'Target or destination was not within your mountpoints! T="%s", D="%s"', Array($theFile, $theDestFile));
682 }
683 } else {
684 $this->writelog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', Array($theDest . '/'));
685 }
686 } else {
687 $this->writelog(3, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', Array($theDestFile . '/', $theFile . '/'));
688 }
689 } else {
690 $this->writelog(3, 1, 123, 'Target "%s" already exists!', Array($theDestFile));
691 }
692 } else {
693 $this->writelog(3, 2, 124, 'Target seemed not to be a directory! (Shouldn\'t happen here!)', '');
694 }
695 } else {
696 $this->writelog(3, 1, 125, 'You are not allowed to move directories', '');
697 }
698 // FINISHED moving directory
699
700 } else {
701 $this->writelog(3, 2, 130, 'The item "%s" was not a file or directory!', Array($theFile));
702 }
703 }
704
705 /**
706 * Renaming files or foldes (action=5)
707 *
708 * @param array $cmds['data'] is the new name. $cmds['target'] is the target (file or dir).
709 * @return string Returns the new filename upon success
710 */
711 function func_rename($cmds) {
712 if (!$this->isInit) {
713 return FALSE;
714 }
715
716 $theNewName = $this->cleanFileName($cmds['data']);
717 if ($theNewName) {
718 if ($this->checkFileNameLen($theNewName)) {
719 $theTarget = $cmds['target'];
720 $type = filetype($theTarget);
721 if ($type == 'file' || $type == 'dir') { // $type MUST BE file or dir
722 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
723 if ($fileInfo['file'] != $theNewName) { // The name should be different from the current. And the filetype must be allowed
724 $theRenameName = $fileInfo['path'] . $theNewName;
725 if ($this->checkPathAgainstMounts($fileInfo['path'])) {
726 if (!file_exists($theRenameName)) {
727 if ($type == 'file') {
728 if ($this->actionPerms['renameFile']) {
729 $fI = t3lib_div::split_fileref($theRenameName);
730 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
731 if (@rename($theTarget, $theRenameName)) {
732 $this->writelog(5, 0, 1, 'File renamed from "%s" to "%s"', Array($fileInfo['file'], $theNewName));
733 return $theRenameName;
734 } else {
735 $this->writelog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', Array($theTarget, $fileInfo['path']));
736 }
737 } else {
738 $this->writelog(5, 1, 101, 'Extension of file name "%s" was not allowed!', Array($fI['file']));
739 }
740 } else {
741 $this->writelog(5, 1, 102, 'You are not allowed to rename files!', '');
742 }
743 } elseif ($type == 'dir') {
744 if ($this->actionPerms['renameFolder']) {
745 if (@rename($theTarget, $theRenameName)) {
746 $this->writelog(5, 0, 2, 'Directory renamed from "%s" to "%s"', Array($fileInfo['file'], $theNewName));
747 return $theRenameName;
748 } else {
749 $this->writelog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', Array($theTarget, $fileInfo['path']));
750 }
751 } else {
752 $this->writelog(5, 1, 111, 'You are not allowed to rename directories!', '');
753 }
754 }
755 } else {
756 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', Array($theRenameName));
757 }
758 } else {
759 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', Array($fileInfo['path']));
760 }
761 } else {
762 $this->writelog(5, 1, 122, 'Old and new name is the same (%s)', Array($theNewName));
763 }
764 } else {
765 $this->writelog(5, 2, 123, 'Target "%s" was neither a directory nor a file!', Array($theTarget));
766 }
767 } else {
768 $this->writelog(5, 1, 124, 'New name "%s" was too long (max %s characters)', Array($theNewName, $this->maxInputNameLen));
769 }
770 }
771 }
772
773 /**
774 * This creates a new folder. (action=6)
775 *
776 * @param array $cmds['data'] is the foldername. $cmds['target'] is the path where to create it.
777 * @return string Returns the new foldername upon success
778 */
779 function func_newfolder($cmds) {
780 if (!$this->isInit) {
781 return FALSE;
782 }
783
784 $theFolder = $this->cleanFileName($cmds['data']);
785 if (isset($theFolder) && trim($theFolder) != '') {
786 if ($this->checkFileNameLen($theFolder)) {
787 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
788 if ($theTarget) {
789 if ($this->actionPerms['newFolder']) {
790 $theNewFolder = $theTarget . '/' . $theFolder;
791 if ($this->checkPathAgainstMounts($theNewFolder)) {
792 if (!file_exists($theNewFolder)) {
793 if (t3lib_div::mkdir($theNewFolder)) {
794 $this->writelog(6, 0, 1, 'Directory "%s" created in "%s"', Array($theFolder, $theTarget . '/'));
795 return $theNewFolder;
796 } else {
797 $this->writelog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', Array($theFolder, $theTarget . '/'));
798 }
799 } else {
800 $this->writelog(6, 1, 101, 'File or directory "%s" existed already!', Array($theNewFolder));
801 }
802 } else {
803 $this->writelog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', Array($theTarget . '/'));
804 }
805 } else {
806 $this->writelog(6, 1, 103, 'You are not allowed to create directories!', '');
807 }
808 } else {
809 $this->writelog(6, 2, 104, 'Destination "%s" was not a directory', Array($cmds['target']));
810 }
811 } else {
812 $this->writelog(6, 1, 105, 'New name "%s" was too long (max %s characters)', Array($theFolder, $this->maxInputNameLen));
813 }
814 }
815 }
816
817 /**
818 * This creates a new file. (action=8)
819 *
820 * @param array $cmds['data'] is the new filename. $cmds['target'] is the path where to create it
821 * @return string Returns the new filename upon success
822 */
823 function func_newfile($cmds) {
824 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
825 if (!$this->isInit) {
826 return FALSE;
827 }
828 $newName = $this->cleanFileName($cmds['data']);
829 if ($newName) {
830 if ($this->checkFileNameLen($newName)) {
831 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
832 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
833 if ($theTarget) {
834 if ($this->actionPerms['newFile']) {
835 $theNewFile = $theTarget . '/' . $newName;
836 if ($this->checkPathAgainstMounts($theNewFile)) {
837 if (!file_exists($theNewFile)) {
838 $fI = t3lib_div::split_fileref($theNewFile);
839 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
840 if (t3lib_div::inList($extList, $fI['fileext'])) {
841 if (t3lib_div::writeFile($theNewFile, '')) {
842 clearstatcache();
843 $this->writelog(8, 0, 1, 'File created: "%s"', Array($fI['file']));
844 return $theNewFile;
845 } else {
846 $this->writelog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', Array($fI['file'], $theTarget));
847 }
848 } else {
849 $this->writelog(8, 1, 107, 'File extension "%s" is not a textfile format! (%s)', Array($fI['fileext'], $extList));
850 }
851 } else {
852 $this->writelog(8, 1, 106, 'Extension of file name "%s" was not allowed!', Array($fI['file']));
853 }
854 } else {
855 $this->writelog(8, 1, 101, 'File "%s" existed already!', Array($theNewFile));
856 }
857 } else {
858 $this->writelog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', Array($theTarget . '/'));
859 }
860 } else {
861 $this->writelog(8, 1, 103, 'You are not allowed to create files!', '');
862 }
863 } else {
864 $this->writelog(8, 2, 104, 'Destination "%s" was not a directory', Array($cmds['target']));
865 }
866 } else {
867 $this->writelog(8, 1, 105, 'New name "%s" was too long (max %s characters)', Array($newName, $this->maxInputNameLen));
868 }
869 }
870 }
871
872 /**
873 * Editing textfiles or folders (action=9)
874 *
875 * @param array $cmds['data'] is the new content. $cmds['target'] is the target (file or dir)
876 * @return boolean Returns true on success
877 */
878 function func_edit($cmds) {
879 if (!$this->isInit) {
880 return FALSE;
881 }
882 $theTarget = $cmds['target'];
883 $content = $cmds['data'];
884 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
885 $type = filetype($theTarget);
886 if ($type == 'file') { // $type MUST BE file
887 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
888 $fI = $fileInfo;
889 if ($this->checkPathAgainstMounts($fileInfo['path'])) {
890 if ($this->actionPerms['editFile']) {
891 $fI = t3lib_div::split_fileref($theTarget);
892 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
893 if (t3lib_div::inList($extList, $fileInfo['fileext'])) {
894 if (t3lib_div::writeFile($theTarget, $content)) {
895 clearstatcache();
896 $this->writelog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', Array($fileInfo['file'], @filesize($theTarget), md5($content)));
897 return TRUE;
898 } else {
899 $this->writelog(9, 1, 100, 'File "%s" was not saved! Write-permission problem in "%s"?', Array($theTarget, $fileInfo['path']));
900 }
901 } else {
902 $this->writelog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', Array($fI['fileext'], $extList));
903 }
904 } else {
905 $this->writelog(9, 1, 103, 'Extension of file name "%s" was not allowed!', Array($fI['file']));
906 }
907 } else {
908 $this->writelog(9, 1, 104, 'You are not allowed to edit files!', '');
909 }
910 } else {
911 $this->writelog(9, 1, 121, 'Destination path "%s" was not within your mountpoints!', Array($fileInfo['path']));
912 }
913 } else {
914 $this->writelog(9, 2, 123, 'Target "%s" was not a file!', Array($theTarget));
915 }
916 }
917
918 /**
919 * Upload of files (action=1)
920 *
921 * @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)
922 * @return string Returns the new filename upon success
923 */
924 function func_upload($cmds) {
925 if (!$this->isInit) {
926 return FALSE;
927 }
928 $id = $cmds['data'];
929 if ($_FILES['upload_' . $id]['name']) {
930 $theFile = $_FILES['upload_' . $id]['tmp_name']; // filename of the uploaded file
931 $theFileSize = $_FILES['upload_' . $id]['size']; // filesize of the uploaded file
932 $theName = $this->cleanFileName(stripslashes($_FILES['upload_' . $id]['name']), (isset($cmds['charset']) ? $cmds['charset'] : '')); // The original filename
933 if (is_uploaded_file($theFile) && $theName) { // Check the file
934 if ($this->actionPerms['uploadFile']) {
935 if ($theFileSize < ($this->maxUploadFileSize * 1024)) {
936 $fI = t3lib_div::split_fileref($theName);
937 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
938 if ($theTarget && $this->checkPathAgainstMounts($theTarget . '/')) {
939 if ($this->checkIfAllowed($fI['fileext'], $theTarget, $fI['file'])) {
940 $theNewFile = $this->getUniqueName($theName, $theTarget, $this->dontCheckForUnique);
941 if ($theNewFile) {
942 t3lib_div::upload_copy_move($theFile, $theNewFile);
943 clearstatcache();
944 if (@is_file($theNewFile)) {
945 $this->internalUploadMap[$id] = $theNewFile;
946 $this->writelog(1, 0, 1, 'Uploading file "%s" to "%s"', Array($theName, $theNewFile, $id));
947 return $theNewFile;
948 } else {
949 $this->writelog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', Array($theTarget . '/'));
950 }
951 } else {
952 $this->writelog(1, 1, 101, 'No unique filename available in "%s"!', Array($theTarget . '/'));
953 }
954 } else {
955 $this->writelog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', Array($fI['file'], $theTarget . '/'));
956 }
957 } else {
958 $this->writelog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', Array($theTarget . '/'));
959 }
960 } else {
961 $this->writelog(1, 1, 104, 'The uploaded file exceeds the size-limit of %s bytes', Array($this->maxUploadFileSize * 1024));
962 }
963 } else {
964 $this->writelog(1, 1, 105, 'You are not allowed to upload files!', '');
965 }
966 } else {
967 $this->writelog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
968 }
969 } else {
970 $this->writelog(1, 2, 108, 'No file was uploaded!', '');
971 }
972 }
973
974 /**
975 * Unzipping file (action=7)
976 * This is permitted only if the user has fullAccess or if the file resides
977 *
978 * @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.
979 * @return boolean Returns true on success
980 */
981 function func_unzip($cmds) {
982 if (!$this->isInit || $this->dont_use_exec_commands) {
983 return FALSE;
984 }
985
986 $theFile = $cmds['data'];
987 if (@is_file($theFile)) {
988 $fI = t3lib_div::split_fileref($theFile);
989 if (!isset($cmds['target'])) {
990 $cmds['target'] = $fI['path'];
991 }
992 $theDest = $this->is_directory($cmds['target']); // Clean up destination directory
993 if ($theDest) {
994 if ($this->actionPerms['unzipFile']) {
995 if ($fI['fileext'] == 'zip') {
996 if ($this->checkIfFullAccess($theDest)) {
997 if ($this->checkPathAgainstMounts($theFile) && $this->checkPathAgainstMounts($theDest . '/')) {
998 // No way to do this under windows.
999 $cmd = $this->unzipPath . 'unzip -qq "' . $theFile . '" -d "' . $theDest . '"';
1000 exec($cmd);
1001 $this->writelog(7, 0, 1, 'Unzipping file "%s" in "%s"', Array($theFile, $theDest));
1002 return TRUE;
1003 } else {
1004 $this->writelog(7, 1, 100, 'File "%s" or destination "%s" was not within your mountpoints!', Array($theFile, $theDest));
1005 }
1006 } else {
1007 $this->writelog(7, 1, 101, 'You don\'t have full access to the destination directory "%s"!', Array($theDest));
1008 }
1009 } else {
1010 $this->writelog(7, 1, 102, 'File extension is not "zip"', '');
1011 }
1012 } else {
1013 $this->writelog(7, 1, 103, 'You are not allowed to unzip files', '');
1014 }
1015 } else {
1016 $this->writelog(7, 2, 104, 'Destination "%s" was not a directory', Array($cmds['target']));
1017 }
1018 } else {
1019 $this->writelog(7, 2, 105, 'The file "%s" did not exist!', Array($theFile));
1020 }
1021 }
1022 }
1023
1024 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extfilefunc.php']) {
1025 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extfilefunc.php']);
1026 }
1027
1028 ?>