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