*** empty log message ***
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_extfilefunc.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skaarhoj (kasper@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 *
32 * @author Kasper Skaarhoj <kasper@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 122: class t3lib_extFileFunctions extends t3lib_basicFileFunctions
40 * 156: function start($data)
41 * 182: function init_actionPerms($setup)
42 * 215: function mapData($inputArray)
43 * 224: function processData()
44 * 276: function findRecycler($theFile)
45 *
46 * SECTION: File operation functions
47 * 318: function func_upload($cmds)
48 * 359: function func_copy($cmds)
49 * 449: function func_move($cmds)
50 * 540: function func_delete($cmds)
51 * 606: function func_rename($cmds)
52 * 654: function func_newfolder($cmds)
53 * 687: function func_unzip($cmds)
54 * 722: function func_newfile($cmds)
55 * 760: function func_edit($cmds)
56 * 809: function writeLog($action,$error,$details_nr,$details,$data)
57 *
58 * TOTAL FUNCTIONS: 15
59 * (This index is automatically created/updated by the extension "extdeveval")
60 *
61 */
62
63
64
65
66
67
68
69
70
71
72
73
74
75 /**
76 * COMMENT:
77 *
78 * see basicFileFunctions
79 * see tce_file.php for SYNTAX!
80 *
81 * This class contains functions primarily used by tce_file.php (Typo Core Engine for filemanipulation)
82 * Functions include copying, moving, deleting, uploading and so on...
83 *
84 * Important internal variables:
85 *
86 * $filemounts (see basicFileFunctions)
87 * $f_ext (see basicFileFunctions)
88 * ... All fileoperations must be within the filemount-paths. Further the fileextension MUST validate true with the f_ext array
89 *
90 * $actionPerms : 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
91 * $maxCopyFileSize = 10000; // max copy size for files
92 * $maxMoveFileSize = 10000; // max move size for files
93 * $maxUploadFileSize = 10000; // max upload size for files. Remember that PHP has an inner limit often set to 2 MB
94 *
95 * $recyclerFN='_recycler_' : This is regarded to be the recycler folder
96 *
97 * The unzip-function allows unzip only if the destination path has it's f_ext[]['allow'] set to '*'!!
98 * You are allowed to copy/move folders within the same 'space' (web/ftp).
99 * You are allowed to copy/move folders between spaces (web/ftp) IF the destination has it's f_ext[]['allow'] set to '*'!
100 *
101 *
102 * Advice:
103 * 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.
104 * 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
105 * Eg. THIS IS A BAD IDEA: you have an ftp-space that is '/www/' and a web-space that is '/www/htdocs/'
106 *
107 *
108 *
109 * Dependencies:
110 * t3lib_div
111 * t3lib_basicfilefunctions
112 */
113
114 /**
115 * Contains functions for performing file operations like copying, pasting, uploading, moving, deleting etc. through the TCE
116 * Extending class to class t3lib_basicFileFunctions.
117 *
118 * @author Kasper Skaarhoj <kasper@typo3.com>
119 * @package TYPO3
120 * @subpackage t3lib
121 */
122 class t3lib_extFileFunctions extends t3lib_basicFileFunctions {
123 var $maxCopyFileSize = 10000; // kb
124 var $maxMoveFileSize = 10000; // kb
125 var $maxUploadFileSize = 10000; // kb
126 var $unzipPath = ''; // Path to unzip-program (with trailing '/')
127 var $dontCheckForUnique=0; // If set, the uploaded files will overwrite existing files.
128
129 var $actionPerms = Array(
130 'deleteFile' => 0, // Deleting files physically
131 'deleteFolder' => 0, // Deleting foldes physically
132 '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!
133 'moveFile' => 0,
134 'moveFolder' => 0,
135 'copyFile' => 0,
136 'copyFolder' => 0,
137 'newFolder' => 0,
138 'newFile' => 0,
139 'editFile' => 0,
140 'unzipFile' => 0,
141 'uploadFile' => 0,
142 'renameFile' => 0,
143 'renameFolder' => 0
144 );
145
146 var $recyclerFN = '_recycler_';
147 var $useRecycler = 1; // 0 = no, 1 = if available, 2 = always
148 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
149 var $dont_use_exec_commands=0; // This is necessary under windows!
150
151
152 /**
153 * @param [type] $data: ...
154 * @return [type] ...
155 */
156 function start($data) {
157 if (TYPO3_OS=='WIN' || $GLOBALS['TYPO3_CONF_VARS']['BE']['disable_exec_function']) {
158 $this->PHPFileFunctions=1;
159 $this->dont_use_exec_commands=1;
160 } else {
161 $this->PHPFileFunctions = $GLOBALS['TYPO3_CONF_VARS']['BE']['usePHPFileFunctions'];
162 }
163 $this->data = $data;
164 $this->datamap = $this->mapData($this->data);
165 $this->unzipPath = $GLOBALS['TYPO3_CONF_VARS']['BE']['unzip_path'];
166
167 $maxFileSize=intval($GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize']);
168 if ($maxFileSize>0) {
169 $this->maxCopyFileSize = $maxFileSize;
170 $this->maxMoveFileSize = $maxFileSize;
171 $this->maxUploadFileSize = $maxFileSize;
172 }
173 }
174
175 /**
176 * Sets up permission to perform file/directory operations.
177 * See below or the be_user-table for the significanse of the various bits in $setup ($BE_USER->user['fileoper_perms'])
178 *
179 * @param [type] $setup: ...
180 * @return [type] ...
181 */
182 function init_actionPerms($setup) {
183 if (($setup&1)==1) { // Files: Upload,Copy,Move,Delete,Rename
184 $this->actionPerms['uploadFile']=1;
185 $this->actionPerms['copyFile']=1;
186 $this->actionPerms['moveFile']=1;
187 $this->actionPerms['deleteFile']=1;
188 $this->actionPerms['renameFile']=1;
189 $this->actionPerms['editFile']=1;
190 $this->actionPerms['newFile']=1;
191 }
192 if (($setup&2)==2) { // Files: Unzip
193 $this->actionPerms['unzipFile']=1;
194 }
195 if (($setup&4)==4) { // Directory: Move,Delete,Rename,New
196 $this->actionPerms['moveFolder']=1;
197 $this->actionPerms['deleteFolder']=1;
198 $this->actionPerms['renameFolder']=1;
199 $this->actionPerms['newFolder']=1;
200 }
201 if (($setup&8)==8) { // Directory: Copy
202 $this->actionPerms['copyFolder']=1;
203 }
204 if (($setup&16)==16) { // Directory: Delete recursively (rm -Rf)
205 $this->actionPerms['deleteFolderRecursively']=1;
206 }
207 }
208
209 /**
210 * If PHP4 then we just set the incoming data to the arrays as PHP4 submits multidimensional arrays
211 *
212 * @param [type] $inputArray: ...
213 * @return [type] ...
214 */
215 function mapData($inputArray) {
216 if (is_array($inputArray)) {
217 return $inputArray;
218 }
219 }
220
221 /**
222 * @return [type] ...
223 */
224 function processData() {
225 if (!$this->isInit) return false;
226 if (is_array($this->datamap)) {
227 reset($this->datamap);
228 while (list($action, $content) = each($this->datamap)) {
229 if (is_array($content)) {
230 while(list($id, $cmdArr) = each($content)) {
231 clearstatcache();
232 switch ($action) {
233 case 'delete':
234 $this->func_delete($cmdArr);
235 break;
236 case 'copy':
237 $this->func_copy($cmdArr);
238 break;
239 case 'move':
240 $this->func_move($cmdArr);
241 break;
242 case 'rename':
243 $this->func_rename($cmdArr);
244 break;
245 case 'newfolder':
246 $this->func_newfolder($cmdArr);
247 break;
248 case 'newfile':
249 $this->func_newfile($cmdArr);
250 break;
251 case 'editfile':
252 $this->func_edit($cmdArr);
253 break;
254 case 'upload':
255 $this->func_upload($cmdArr);
256 break;
257 case 'unzip':
258 $this->func_unzip($cmdArr);
259 break;
260 }
261 }
262 }
263 }
264 }
265 }
266
267 /**
268 * Takes a valid Path ($theFile)
269 * Goes back in the path and checks in each directory if a folder named $this->recyclerFN (usually '_recycler_') is present.
270 * Returns the path (without trailing slash) of the closest recycle-folder if found. Else false.
271 * 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
272 *
273 * @param [type] $theFile: ...
274 * @return [type] ...
275 */
276 function findRecycler($theFile) {
277 if ($this->isPathValid($theFile)) {
278 $theFile=$this->cleanDirectoryName($theFile);
279 $fI=t3lib_div::split_fileref($theFile);
280 $c=0;
281 while($this->checkPathAgainstMounts($fI['path']) && $c<20) {
282 $rDir = $fI['path'].$this->recyclerFN;
283 if (@is_dir($rDir) && $this->recyclerFN!=$fI['file']) {
284 return $rDir;
285 }
286 $theFile=$fI['path'];
287 $theFile=$this->cleanDirectoryName($theFile);
288 $fI=t3lib_div::split_fileref($theFile);
289 $c++;
290 }
291 }
292 }
293
294
295
296
297
298
299
300
301
302
303 /*************************************
304 *
305 * File operation functions
306 *
307 **************************************/
308
309 /**
310 * Upload of files (action=1)
311 * $cmds['data'] is the ID-number (points to the global var that holds the filename-ref ($GLOBALS['HTTP_POST_FILES']['upload_'.$id]['name'])
312 * $cmds['target'] is the target directory
313 * Returns the new filename upon success
314 *
315 * @param [type] $cmds: ...
316 * @return [type] ...
317 */
318 function func_upload($cmds) {
319 if (!$this->isInit) return false;
320 $id = $cmds['data'];
321 if ($GLOBALS['HTTP_POST_FILES']['upload_'.$id]['name']) {
322 $theFile = $GLOBALS['HTTP_POST_FILES']['upload_'.$id]['tmp_name']; // filename of the uploaded file
323 $theName = $this->cleanFileName(stripslashes($GLOBALS['HTTP_POST_FILES']['upload_'.$id]['name'])); // The original filename
324 if (@is_file($theFile) && $theName) { // Check the file
325 if ($this->actionPerms['uploadFile']) {
326 if (filesize($theFile)<($this->maxUploadFileSize*1024)) {
327 $fI = t3lib_div::split_fileref($theName);
328 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
329 if ($theTarget && $this->checkPathAgainstMounts($theTarget.'/')) {
330 if ($this->checkIfAllowed($fI['fileext'], $theTarget, $fI['file'])) {
331 $theNewFile = $this->getUniqueName($theName, $theTarget, $this->dontCheckForUnique);
332 if ($theNewFile) {
333 t3lib_div::upload_copy_move($theFile,$theNewFile);
334
335 clearstatcache();
336 if (@is_file($theNewFile)) {
337 $this->writelog(1,0,1,"Uploading file '%s' to '%s'",Array($theName,$theNewFile, $id));
338 return $theNewFile;
339 } else $this->writelog(1,1,100,"Uploaded file could not be moved! Write-permission problem in '%s'?",Array($theTarget.'/'));
340 } else $this->writelog(1,1,101,"No unique filename available in '%s'!",Array($theTarget.'/'));
341 } else $this->writelog(1,1,102,"Fileextension '%s' is not allowed in '%s'!",Array($fI['fileext'],$theTarget.'/'));
342 } else $this->writelog(1,1,103,"Destination path '%s' was not within your mountpoints!",Array($theTarget.'/'));
343 } else $this->writelog(1,1,104,"The uploaded file exceeds the size-limit of %s bytes",Array($this->maxUploadFileSize*1024));
344 } else $this->writelog(1,1,105,"You are not allowed to upload files!",'');
345 } else $this->writelog(1,2,106,'The uploaded file did not exist!','');
346 }
347 }
348
349 /**
350 * Copying files and folders (action=2)
351 * $cmds['data'] is the the file/folder to copy
352 * $cmds['target'] is the path where to copy to
353 * $cmds['altName'] (boolean): If set, another filename is found in case the target already exists
354 * Returns the new filename upon success
355 *
356 * @param [type] $cmds: ...
357 * @return [type] ...
358 */
359 function func_copy($cmds) {
360 if (!$this->isInit) return false;
361 $theFile = $cmds['data'];
362 $theDest = $this->is_directory($cmds['target']); // Clean up destination directory
363 $altName = $cmds['altName'];
364 if (!$theDest) {
365 $this->writelog(2,2,100,"Destination '%s' was not a directory",Array($cmds['target']));
366 return false;
367 }
368 if (!$this->isPathValid($theFile) || !$this->isPathValid($theDest)) {
369 $this->writelog(2,2,101,"Target or destination had invalid path ('..' and '//' is not allowed in path). T='%s', D='%s'",Array($theFile,$theDest));
370 return false;
371 }
372 if (@is_file($theFile)) { // If we are copying a file...
373 if ($this->actionPerms['copyFile']) {
374 if (filesize($theFile) < ($this->maxCopyFileSize*1024)) {
375 $fI=t3lib_div::split_fileref($theFile);
376 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
377 $theDestFile=$this->getUniqueName($fI['file'], $theDest);
378 $fI=t3lib_div::split_fileref($theDestFile);
379 } else {
380 $theDestFile=$theDest.'/'.$fI['file'];
381 }
382 if ($theDestFile && !@file_exists($theDestFile)) {
383 if ($this->checkIfAllowed($fI['fileext'], $theDest, $fI['file'])) {
384 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
385 if ($this->PHPFileFunctions) {
386 copy ($theFile,$theDestFile);
387 } else {
388 $cmd = 'cp "'.$theFile.'" "'.$theDestFile.'"';
389 exec($cmd);
390 }
391 clearstatcache();
392 if (@is_file($theDestFile)) {
393 $this->writelog(2,0,1,"File '%s' copied to '%s'",Array($theFile,$theDestFile));
394 return $theDestFile;
395 } else $this->writelog(2,2,109,"File '%s' WAS NOT copied to '%s'! Write-permission problem?",Array($theFile,$theDestFile));
396 } else $this->writelog(2,1,110,"Target or destination was not within your mountpoints! T='%s', D='%s'",Array($theFile,$theDestFile));
397 } else $this->writelog(2,1,111,"Fileextension '%s' is not allowed in '%s'!",Array($fI['fileext'],$theDest.'/'));
398 } else $this->writelog(2,1,112,"File '%s' already exists!",Array($theDestFile));
399 } else $this->writelog(2,1,113,"File '%s' exceeds the size-limit of %s bytes",Array($theFile,$this->maxCopyFileSize*1024));
400 } else $this->writelog(2,1,114,"You are not allowed to copy files",'');
401 // FINISHED copying file
402
403 } elseif (@is_dir($theFile) && !$this->dont_use_exec_commands) { // if we're copying a folder
404 if ($this->actionPerms['copyFolder']) {
405 $theFile = $this->is_directory($theFile);
406 if ($theFile) {
407 $fI=t3lib_div::split_fileref($theFile);
408 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
409 $theDestFile=$this->getUniqueName($fI['file'], $theDest);
410 $fI=t3lib_div::split_fileref($theDestFile);
411 } else {
412 $theDestFile=$theDest.'/'.$fI['file'];
413 }
414 if ($theDestFile && !@file_exists($theDestFile)) {
415 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?
416 if ($this->checkIfFullAccess($theDest) || $this->is_webPath($theDestFile)==$this->is_webPath($theFile)) { // no copy of folders between spaces
417 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
418 // No way to do this under windows!
419 $cmd = 'cp -R "'.$theFile.'" "'.$theDestFile.'"';
420 exec($cmd);
421 clearstatcache();
422 if (@is_dir($theDestFile)) {
423 $this->writelog(2,0,2,"Directory '%s' copied to '%s'",Array($theFile,$theDestFile));
424 return $theDestFile;
425 } else $this->writelog(2,2,119,"Directory '%s' WAS NOT copied to '%s'! Write-permission problem?",Array($theFile,$theDestFile));
426 } else $this->writelog(2,1,120,"Target or destination was not within your mountpoints! T='%s', D='%s'",Array($theFile,$theDestFile));
427 } else $this->writelog(2,1,121,"You don't have full access to the destination directory '%s'!",Array($theDest.'/'));
428 } else $this->writelog(2,1,122,"Destination cannot be inside the target! D='%s', T='%s'",Array($theDestFile.'/',$theFile.'/'));
429 } else $this->writelog(2,1,123,"Target '%s' already exists!",Array($theDestFile));
430 } else $this->writelog(2,2,124,"Target seemed not to be a directory! (Shouldn't happen here!)",'');
431 } else $this->writelog(2,1,125,"You are not allowed to copy directories",'');
432 // FINISHED copying directory
433
434 } else {
435 $this->writelog(2,2,130,"The item '%s' was not a file or directory!",Array($theFile));
436 }
437 }
438
439 /**
440 * Moving files and folders (action=3)
441 * $cmds['data'] is the the file/folder to move
442 * $cmds['target'] is the path where to move to
443 * $cmds['altName'] (boolean): If set, another filename is found in case the target already exists
444 * Returns the new filename upon success
445 *
446 * @param [type] $cmds: ...
447 * @return [type] ...
448 */
449 function func_move($cmds) {
450 if (!$this->isInit) return false;
451 $theFile = $cmds['data'];
452 $theDest = $this->is_directory($cmds['target']); // Clean up destination directory
453 $altName = $cmds['altName'];
454 if (!$theDest) {
455 $this->writelog(3,2,100,"Destination '%s' was not a directory",Array($cmds['target']));
456 return false;
457 }
458 if (!$this->isPathValid($theFile) || !$this->isPathValid($theDest)) {
459 $this->writelog(3,2,101,"Target or destination had invalid path ('..' and '//' is not allowed in path). T='%s', D='%s'",Array($theFile,$theDest));
460 return false;
461 }
462 if (@is_file($theFile)) { // If we are moving a file...
463 if ($this->actionPerms['moveFile']) {
464 if (filesize($theFile) < ($this->maxMoveFileSize*1024)) {
465 $fI=t3lib_div::split_fileref($theFile);
466 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
467 $theDestFile=$this->getUniqueName($fI['file'], $theDest);
468 $fI=t3lib_div::split_fileref($theDestFile);
469 } else {
470 $theDestFile=$theDest.'/'.$fI['file'];
471 }
472 if ($theDestFile && !@file_exists($theDestFile)) {
473 if ($this->checkIfAllowed($fI['fileext'], $theDest, $fI['file'])) {
474 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
475 if ($this->PHPFileFunctions) {
476 rename($theFile, $theDestFile);
477 } else {
478 $cmd = 'mv "'.$theFile.'" "'.$theDestFile.'"';
479 exec($cmd);
480 }
481 clearstatcache();
482 if (@is_file($theDestFile)) {
483 $this->writelog(3,0,1,"File '%s' moved to '%s'",Array($theFile,$theDestFile));
484 return $theDestFile;
485 } else $this->writelog(3,2,109,"File '%s' WAS NOT moved to '%s'! Write-permission problem?",Array($theFile,$theDestFile));
486 } else $this->writelog(3,1,110,"Target or destination was not within your mountpoints! T='%s', D='%s'",Array($theFile,$theDestFile));
487 } else $this->writelog(3,1,111,"Fileextension '%s' is not allowed in '%s'!",Array($fI['fileext'],$theDest.'/'));
488 } else $this->writelog(3,1,112,"File '%s' already exists!",Array($theDestFile));
489 } else $this->writelog(3,1,113,"File '%s' exceeds the size-limit of %s bytes",Array($theFile,$this->maxMoveFileSize*1024));
490 } else $this->writelog(3,1,114,"You are not allowed to move files",'');
491 // FINISHED moving file
492
493 } elseif (@is_dir($theFile)) { // if we're moving a folder
494 if ($this->actionPerms['moveFolder']) {
495 $theFile = $this->is_directory($theFile);
496 if ($theFile) {
497 $fI=t3lib_div::split_fileref($theFile);
498 if ($altName) { // If altName is set, we're allowed to create a new filename if the file already existed
499 $theDestFile=$this->getUniqueName($fI['file'], $theDest);
500 $fI=t3lib_div::split_fileref($theDestFile);
501 } else {
502 $theDestFile=$theDest.'/'.$fI['file'];
503 }
504 if ($theDestFile && !@file_exists($theDestFile)) {
505 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?
506 if ($this->checkIfFullAccess($theDest) || $this->is_webPath($theDestFile)==$this->is_webPath($theFile)) { // // no moving of folders between spaces
507 if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
508 if ($this->PHPFileFunctions) {
509 rename($theFile, $theDestFile);
510 } else {
511 $cmd = 'mv "'.$theFile.'" "'.$theDestFile.'"';
512 exec($cmd,$errArr,$retVar);
513 }
514 clearstatcache();
515 if (@is_dir($theDestFile)) {
516 $this->writelog(3,0,2,"Directory '%s' moved to '%s'",Array($theFile,$theDestFile));
517 return $theDestFile;
518 } else $this->writelog(3,2,119,"Directory '%s' WAS NOT moved to '%s'! Write-permission problem?",Array($theFile,$theDestFile));
519 } else $this->writelog(3,1,120,"Target or destination was not within your mountpoints! T='%s', D='%s'",Array($theFile,$theDestFile));
520 } else $this->writelog(3,1,121,"You don't have full access to the destination directory '%s'!",Array($theDest.'/'));
521 } else $this->writelog(3,1,122,"Destination cannot be inside the target! D='%s', T='%s'",Array($theDestFile.'/',$theFile.'/'));
522 } else $this->writelog(3,1,123,"Target '%s' already exists!",Array($theDestFile));
523 } else $this->writelog(3,2,124,"Target seemed not to be a directory! (Shouldn't happen here!)",'');
524 } else $this->writelog(3,1,125,"You are not allowed to move directories",'');
525 // FINISHED moving directory
526
527 } else {
528 $this->writelog(3,2,130,"The item '%s' was not a file or directory!",Array($theFile));
529 }
530 }
531
532 /**
533 * Deleting files and folders (action=4)
534 * $cmds['data'] is the the file/folder to delete
535 * Returns true upon success
536 *
537 * @param [type] $cmds: ...
538 * @return [type] ...
539 */
540 function func_delete($cmds) {
541 if (!$this->isInit) return false;
542 $theFile = $cmds['data'];
543 if (!$this->isPathValid($theFile)) {
544 $this->writelog(4,2,101,"Target '%s' had invalid path ('..' and '//' is not allowed in path).",Array($theFile));
545 return false;
546 }
547 if ($this->useRecycler && $recyclerPath=$this->findRecycler($theFile)) {
548 // If a recycler is found, the deleted items is moved to the recycler and not just deleted.
549 $newCmds=Array();
550 $newCmds['data']=$theFile;
551 $newCmds['target']=$recyclerPath;
552 $newCmds['altName']=1;
553 $this->func_move($newCmds);
554 $this->writelog(4,0,4,"Item '%s' moved to recycler at '%s'",Array($theFile,$recyclerPath));
555 return true;
556 } elseif ($this->useRecycler != 2) { // if $this->useRecycler==2 then we cannot delete for real!!
557 if (@is_file($theFile)) { // If we are deleting a file...
558 if ($this->actionPerms['deleteFile']) {
559 if ($this->checkPathAgainstMounts($theFile)) {
560 if (@unlink($theFile)) {
561 $this->writelog(4,0,1,"File '%s' deleted",Array($theFile));
562 return true;
563 } else $this->writelog(4,1,110,"Could not delete file '%s'. Write-permission problem?", Array($theFile));
564 } else $this->writelog(4,1,111,"Target was not within your mountpoints! T='%s'",Array($theFile));
565 } else $this->writelog(4,1,112,"You are not allowed to delete files",'');
566 // FINISHED deleting file
567
568 } elseif (@is_dir($theFile) && !$this->dont_use_exec_commands) { // if we're deleting a folder
569 if ($this->actionPerms['deleteFolder']) {
570 $theFile = $this->is_directory($theFile);
571 if ($theFile) {
572 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...)
573 if ($this->actionPerms['deleteFolderRecursively']) {
574 // No way to do this under windows
575 $cmd = 'rm -Rf "'.$theFile.'"';
576 exec($cmd); // This is a quite critical command...
577 clearstatcache();
578 if (!@file_exists($theFile)) {
579 $this->writelog(4,0,2,"Directory '%s' deleted recursively!",Array($theFile));
580 return true;
581 } else $this->writelog(4,2,119,"Directory '%s' WAS NOT deleted recursively! Write-permission problem?",Array($theFile));
582 } else {
583 if (@rmdir($theFile)) {
584 $this->writelog(4,0,3,"Directory '%s' deleted",Array($theFile));
585 return true;
586 } else $this->writelog(4,1,120,"Could not delete directory! Write-permission problem? Is directory '%s' empty?",Array($theFile));
587 }
588 } else $this->writelog(4,1,121,"Target was not within your mountpoints! T='%s'",Array($theFile));
589 } else $this->writelog(4,2,122,"Target seemed not to be a directory! (Shouldn't happen here!)",'');
590 } else $this->writelog(4,1,123,"You are not allowed to delete directories",'');
591 // FINISHED copying directory
592
593 } else $this->writelog(4,2,130,"The item was not a file or directory! '%s'",Array($theFile));
594 } else $this->writelog(4,1,131,"No recycler found!",'');
595 }
596
597 /**
598 * Renaming files or foldes (action=5)
599 * $cmds['data'] is the new name
600 * $cmds['target'] is the target (file or dir)
601 * Returns the new filename upon success
602 *
603 * @param [type] $cmds: ...
604 * @return [type] ...
605 */
606 function func_rename($cmds) {
607 if (!$this->isInit) return false;
608 $theNewName = $this->cleanFileName($cmds['data']);
609 if ($theNewName) {
610 if ($this->checkFileNameLen($theNewName)) {
611 $theTarget = $cmds['target'];
612 $type = filetype($theTarget);
613 if ($type=='file' || $type=='dir') { // $type MUST BE file or dir
614 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
615 if ($fileInfo['file']!=$theNewName) { // The name should be different from the current. And the filetype must be allowed
616 $theRenameName = $fileInfo['path'].$theNewName;
617 if ($this->checkPathAgainstMounts($fileInfo['path'])) {
618 if (!@file_exists($theRenameName)) {
619 if ($type=='file') {
620 if ($this->actionPerms['renameFile']) {
621 $fI = t3lib_div::split_fileref($theRenameName);
622 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
623 if (@rename($theTarget, $theRenameName)) {
624 $this->writelog(5,0,1,"File renamed from '%s' to '%s'",Array($fileInfo['file'],$theNewName));
625 return $theRenameName;
626 } else $this->writelog(5,1,100,"File '%s' was not renamed! Write-permission problem in '%s'?",Array($theTarget,$fileInfo['path']));
627 } else $this->writelog(5,1,101,"Fileextension '%s' was not allowed!",Array($fI['fileext']));
628 } else $this->writelog(5,1,102,"You are not allowed to rename files!",'');
629 } elseif ($type=='dir') {
630 if ($this->actionPerms['renameFolder']) {
631 if (@rename($theTarget, $theRenameName)) {
632 $this->writelog(5,0,2,"Directory renamed from '%s' to '%s'",Array($fileInfo['file'],$theNewName));
633 return $theRenameName;
634 } else $this->writelog(5,1,110,"Directory '%s' was not renamed! Write-permission problem in '%s'?",Array($theTarget,$fileInfo['path']));
635 } else $this->writelog(5,1,111,"You are not allowed to rename directories!",'');
636 }
637 } else $this->writelog(5,1,120,"Destination '%s' existed already!",Array($theRenameName));
638 } else $this->writelog(5,1,121,"Destination path '%s' was not within your mountpoints!",Array($fileInfo['path']));
639 } else $this->writelog(5,1,122,"Old and new name is the same (%s)",Array($theNewName));
640 } else $this->writelog(5,2,123,"Target '%s' was neither a directory nor a file!",Array($theTarget));
641 } else $this->writelog(5,1,124,"New name '%s' was too long (max %s characters)",Array($theNewName,$this->maxInputNameLen));
642 }
643 }
644
645 /**
646 * This creates a new folder. (action=6)
647 * $cmds['data'] is the foldername
648 * $cmds['target'] is the path where to create it
649 * Returns the new foldername upon success
650 *
651 * @param [type] $cmds: ...
652 * @return [type] ...
653 */
654 function func_newfolder($cmds) {
655 if (!$this->isInit) return false;
656 $theFolder = $this->cleanFileName($cmds['data']);
657 if ($theFolder) {
658 if ($this->checkFileNameLen($theFolder)) {
659 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
660 if ($theTarget) {
661 if ($this->actionPerms['newFolder']) {
662 $theNewFolder = $theTarget.'/'.$theFolder;
663 if ($this->checkPathAgainstMounts($theNewFolder)) {
664 if (!@file_exists($theNewFolder)) {
665 if (@mkdir($theNewFolder, 0755)) {
666 $this->writelog(6,0,1,"Directory '%s' created in '%s'",Array($theFolder,$theTarget.'/'));
667 return $theNewFolder;
668 } else $this->writelog(6,1,100,"Directory '%s' not created. Write-permission problem in '%s'?",Array($theFolder,$theTarget.'/'));
669 } else $this->writelog(6,1,101,"File or directory '%s' existed already!",Array($theNewFolder));
670 } else $this->writelog(6,1,102,"Destination path '%s' was not within your mountpoints!",Array($theTarget.'/'));
671 } else $this->writelog(6,1,103,"You are not allowed to create directories!",'');
672 } else $this->writelog(6,2,104,"Destination '%s' was not a directory",Array($cmds['target']));
673 } else $this->writelog(6,1,105,"New name '%s' was too long (max %s characters)",Array($theFolder,$this->maxInputNameLen));
674 }
675 }
676
677 /**
678 * Unzipping file (action=7)
679 * This is permitted only if the user has fullAccess or if the file resides
680 * $cmds['data'] is the zip-file
681 * $cmds['target'] is the target directory. If not set we'll default to the same directory as the file is in
682 * If target is not supplied the target will be the current directory
683 *
684 * @param [type] $cmds: ...
685 * @return [type] ...
686 */
687 function func_unzip($cmds) {
688 if (!$this->isInit || $this->dont_use_exec_commands) return false;
689 $theFile = $cmds['data'];
690 if (@is_file($theFile)) {
691 $fI=t3lib_div::split_fileref($theFile);
692 if (!isset($cmds['target'])) {
693 $cmds['target'] = $fI['path'];
694 }
695 $theDest = $this->is_directory($cmds['target']); // Clean up destination directory
696 if ($theDest) {
697 if ($this->actionPerms['unzipFile']) {
698 if ($fI['fileext']=='zip') {
699 if ($this->checkIfFullAccess($theDest)) {
700 if ($this->checkPathAgainstMounts($theFile) && $this->checkPathAgainstMounts($theDest.'/')) {
701 // No way to do this under windows.
702 $cmd = $this->unzipPath.'unzip -qq "'.$theFile.'" -d "'.$theDest.'"';
703 exec($cmd);
704 $this->writelog(7,0,1,"Unzipping file '%s' in '%s'",Array($theFile,$theDest));
705 } else $this->writelog(7,1,100,"File '%s' or destination '%s' was not within your mountpoints!",Array($theFile,$theDest));
706 } else $this->writelog(7,1,101,"You don't have full access to the destination directory '%s'!",Array($theDest));
707 } else $this->writelog(7,1,102,"Fileextension is not 'zip'",'');
708 } else $this->writelog(7,1,103,"You are not allowed to unzip files",'');
709 } else $this->writelog(7,2,104,"Destination '%s' was not a directory",Array($cmds['target']));
710 } else $this->writelog(7,2,105,"The file '%s' did not exist!",Array($theFile));
711 }
712
713 /**
714 * This creates a new file. (action=8)
715 * $cmds['data'] is the new filename
716 * $cmds['target'] is the path where to create it
717 * Returns the new filename upon success
718 *
719 * @param [type] $cmds: ...
720 * @return [type] ...
721 */
722 function func_newfile($cmds) {
723 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
724 if (!$this->isInit) return false;
725 $newName = $this->cleanFileName($cmds['data']);
726 if ($newName) {
727 if ($this->checkFileNameLen($newName)) {
728 $theTarget = $this->is_directory($cmds['target']); // Check the target dir
729 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
730 if ($theTarget) {
731 if ($this->actionPerms['newFile']) {
732 $theNewFile = $theTarget.'/'.$newName;
733 if ($this->checkPathAgainstMounts($theNewFile)) {
734 if (!@file_exists($theNewFile)) {
735 $fI = t3lib_div::split_fileref($theNewFile);
736 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
737 if (t3lib_div::inList($extList, $fI['fileext'])) {
738 if (t3lib_div::writeFile($theNewFile,'')) {
739 clearstatcache();
740 $this->writelog(8,0,1,"File created: '%s'",Array($fI['file']));
741 } else $this->writelog(8,1,100,"File '%s' was not created! Write-permission problem in '%s'?",Array($fI['file'], $theTarget));
742 } else $this->writelog(8,1,107,"Fileextension '%s' is not a textfile format! (%s)",Array($fI['fileext'], $extList));
743 } else $this->writelog(8,1,106,"Fileextension '%s' was not allowed!",Array($fI['fileext']));
744 } else $this->writelog(8,1,101,"File '%s' existed already!",Array($theNewFile));
745 } else $this->writelog(8,1,102,"Destination path '%s' was not within your mountpoints!",Array($theTarget.'/'));
746 } else $this->writelog(8,1,103,"You are not allowed to create files!",'');
747 } else $this->writelog(8,2,104,"Destination '%s' was not a directory",Array($cmds['target']));
748 } else $this->writelog(8,1,105,"New name '%s' was too long (max %s characters)",Array($newName,$this->maxInputNameLen));
749 }
750 }
751
752 /**
753 * Editing textfiles or foldes (action=9)
754 * $cmds['data'] is the new content
755 * $cmds['target'] is the target (file or dir)
756 *
757 * @param [type] $cmds: ...
758 * @return [type] ...
759 */
760 function func_edit($cmds) {
761 if (!$this->isInit) return false;
762 $theTarget = $cmds['target'];
763 $content = stripslashes($cmds['data']);
764 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
765 $type = filetype($theTarget);
766 if ($type=='file') { // $type MUST BE file
767 $fileInfo = t3lib_div::split_fileref($theTarget); // Fetches info about path, name, extention of $theTarget
768 $fI =$fileInfo;
769 if ($this->checkPathAgainstMounts($fileInfo['path'])) {
770 if ($this->actionPerms['editFile']) {
771 $fI = t3lib_div::split_fileref($theTarget);
772 if ($this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
773 if (t3lib_div::inList($extList, $fileInfo['fileext'])) {
774 if (t3lib_div::writeFile($theTarget,$content)) {
775 clearstatcache();
776 $this->writelog(9,0,1,"File saved to '%s', bytes: %s, MD5: %s ",Array($fileInfo['file'],@filesize($theTarget),md5($content)));
777 } else $this->writelog(9,1,100,"File '%s' was not saved! Write-permission problem in '%s'?",Array($theTarget,$fileInfo['path']));
778 } else $this->writelog(9,1,102,"Fileextension '%s' is not a textfile format! (%s)",Array($fI['fileext'], $extList));
779 } else $this->writelog(9,1,103,"Fileextension '%s' was not allowed!",Array($fI['fileext']));
780 } else $this->writelog(9,1,104,'You are not allowed to edit files!','');
781 } else $this->writelog(9,1,121,"Destination path '%s' was not within your mountpoints!",Array($fileInfo['path']));
782 } else $this->writelog(9,2,123,"Target '%s' was not a file!",Array($theTarget));
783 }
784
785 /**
786 * Logging actions
787 *
788 * Log messages:
789 * [action]-[details_nr.]
790 *
791 * REMEMBER to UPDATE the real messages set in tools/log/localconf_log.php
792 *
793 * 9-1: File saved to '%s', bytes: %s, MD5: %s
794 *
795 * $action: The action number. See the functions in the class for a hint. Eg. edit is '9', upload is '1' ...
796 * $error: The severity: 0 = message, 1 = error, 2 = System Error, 3 = security notice (admin)
797 * $details_nr: 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.
798 * $details: This is the default, raw error message in english
799 * $data: Array with special information that may go into $details by '%s' marks / sprintf() when the log is shown
800 *
801 * @param [type] $action: ...
802 * @param [type] $error: ...
803 * @param [type] $details_nr: ...
804 * @param [type] $details: ...
805 * @param [type] $data: ...
806 * @return [type] ...
807 * @see class.t3lib_userauthgroup.php
808 */
809 function writeLog($action,$error,$details_nr,$details,$data) {
810 $type=2; // Type value for tce_file.php
811 $GLOBALS['BE_USER']->writelog($type,$action,$error,$details_nr,$details,$data);
812 }
813 }
814
815 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extfilefunc.php']) {
816 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extfilefunc.php']);
817 }
818 ?>