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