Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00081 class t3lib_basicFileFunctions { 00082 var $getUniqueNamePrefix = ''; // Prefix which will be prepended the file when using the getUniqueName-function 00083 var $maxNumber = 20; // This number decides the highest allowed appended number used on a filename before we use naming with unique strings 00084 var $uniquePrecision = 6; // This number decides how many characters out of a unique MD5-hash that is appended to a filename if getUniqueName is asked to find an available filename. 00085 var $maxInputNameLen = 30; // This is the maximum length of names treated by cleanFileName() 00086 var $tempFN = '_temp_'; // Temp-foldername. A folder in the root of one of the mounts with this name is regarded a TEMP-folder (used for upload from clipboard) 00087 00088 // internal 00089 var $f_ext = Array(); // See comment in header 00090 var $mounts = Array(); // See comment in header 00091 var $webPath =''; // Set to DOCUMENT_ROOT. 00092 var $isInit = 0; // Set to true after init()/start(); 00093 00094 00095 00096 /********************************** 00097 * 00098 * Checking functions 00099 * 00100 **********************************/ 00101 00133 function init($mounts, $f_ext) { 00134 $this->f_ext['webspace']['allow'] = t3lib_div::uniqueList(strtolower($f_ext['webspace']['allow'])); 00135 $this->f_ext['webspace']['deny'] = t3lib_div::uniqueList(strtolower($f_ext['webspace']['deny'])); 00136 $this->f_ext['ftpspace']['allow'] = t3lib_div::uniqueList(strtolower($f_ext['ftpspace']['allow'])); 00137 $this->f_ext['ftpspace']['deny'] = t3lib_div::uniqueList(strtolower($f_ext['ftpspace']['deny'])); 00138 00139 $this->mounts = $mounts; 00140 $this->webPath = t3lib_div::getIndpEnv('TYPO3_DOCUMENT_ROOT'); 00141 $this->isInit = 1; 00142 } 00143 00150 function getTotalFileInfo($wholePath) { 00151 $theuser = getmyuid(); 00152 $info = t3lib_div::split_fileref($wholePath); 00153 $info['tstamp'] = @filectime($wholePath); 00154 $info['size'] = @filesize($wholePath); 00155 $info['type'] = @filetype($wholePath); 00156 $info['owner'] = @fileowner($wholePath); 00157 $info['perms'] = @fileperms($wholePath); 00158 $info['writable'] = !@is_writable($wholePath); 00159 $info['readable'] = !@is_readable($wholePath); 00160 return $info; 00161 } 00162 00170 function is_allowed($iconkey,$type) { 00171 if (isset($this->f_ext[$type])) { 00172 $ik = strtolower($iconkey); 00173 if ($ik) { 00174 // If the extension is found amongst the allowed types, we return true immediately 00175 if ($this->f_ext[$type]['allow']=='*' || t3lib_div::inList($this->f_ext[$type]['allow'],$ik)) return true; 00176 // If the extension is found amongst the denied types, we return false immediately 00177 if ($this->f_ext[$type]['deny']=='*' || t3lib_div::inList($this->f_ext[$type]['deny'],$ik)) return false; 00178 // If no match we return true 00179 return true; 00180 } else { // If no extension: 00181 if ($this->f_ext[$type]['allow']=='*') return true; 00182 if ($this->f_ext[$type]['deny']=='*') return false; 00183 return true; 00184 } 00185 } 00186 return false; 00187 } 00188 00195 function checkIfFullAccess($theDest) { 00196 $type = $this->is_webpath($theDest)?'webspace':'ftpspace'; 00197 if (isset($this->f_ext[$type])) { 00198 if ((string)$this->f_ext[$type]['deny']=='' || $this->f_ext[$type]['allow']=='*') return true; 00199 } 00200 } 00201 00209 function is_webpath($path) { 00210 if ($this->isInit) { 00211 $testPath = $this->slashPath($path); 00212 $testPathWeb = $this->slashPath($this->webPath); 00213 if ($testPathWeb && $testPath) { 00214 return t3lib_div::isFirstPartOfStr($testPath,$testPathWeb); 00215 } 00216 } 00217 return true; // Its more safe to return true (as the webpath is more restricted) if something went wrong... 00218 } 00219 00229 function checkIfAllowed($ext, $theDest, $filename='') { 00230 return t3lib_div::verifyFilenameAgainstDenyPattern($filename) && $this->is_allowed($ext,($this->is_webpath($theDest)?'webspace':'ftpspace')); 00231 } 00232 00239 function checkFileNameLen($fileName) { 00240 return strlen($fileName) <= $this->maxInputNameLen; 00241 } 00242 00249 function is_directory($theDir) { 00250 if ($this->isPathValid($theDir)) { 00251 $theDir=$this->cleanDirectoryName($theDir); 00252 if (@is_dir($theDir)) { 00253 return $theDir; 00254 } 00255 } 00256 return false; 00257 } 00258 00266 function isPathValid($theFile) { 00267 return t3lib_div::validPathStr($theFile); 00268 } 00269 00281 function getUniqueName($theFile, $theDest, $dontCheckForUnique=0) { 00282 $theDest = $this->is_directory($theDest); // $theDest is cleaned up 00283 $origFileInfo = t3lib_div::split_fileref($theFile); // Fetches info about path, name, extention of $theFile 00284 if ($theDest) { 00285 if ($this->getUniqueNamePrefix) { // Adds prefix 00286 $origFileInfo['file'] = $this->getUniqueNamePrefix.$origFileInfo['file']; 00287 $origFileInfo['filebody'] = $this->getUniqueNamePrefix.$origFileInfo['filebody']; 00288 } 00289 00290 // Check if the file exists and if not - return the filename... 00291 $fileInfo = $origFileInfo; 00292 $theDestFile = $theDest.'/'.$fileInfo['file']; // The destinations file 00293 if (!@file_exists($theDestFile) || $dontCheckForUnique) { // If the file does NOT exist we return this filename 00294 return $theDestFile; 00295 } 00296 00297 // Well the filename in its pure form existed. Now we try to append numbers / unique-strings and see if we can find an available filename... 00298 $theTempFileBody = ereg_replace('_[0-9][0-9]$','',$origFileInfo['filebody']); // This removes _xx if appended to the file 00299 $theOrigExt = $origFileInfo['realFileext'] ? '.'.$origFileInfo['realFileext'] : ''; 00300 00301 for ($a=1; $a<=($this->maxNumber+1); $a++) { 00302 if ($a<=$this->maxNumber) { // First we try to append numbers 00303 $insert = '_'.sprintf('%02d', $a); 00304 } else { // .. then we try unique-strings... 00305 $insert = '_'.substr(md5(uniqId('')),0,$this->uniquePrecision); 00306 } 00307 $theTestFile = $theTempFileBody.$insert.$theOrigExt; 00308 $theDestFile = $theDest.'/'.$theTestFile; // The destinations file 00309 if (!@file_exists($theDestFile)) { // If the file does NOT exist we return this filename 00310 return $theDestFile; 00311 } 00312 } 00313 } 00314 } 00315 00324 function checkPathAgainstMounts($thePath) { 00325 if ($thePath && $this->isPathValid($thePath) && is_array($this->mounts)) { 00326 reset ($this->mounts); 00327 while(list($k,$val)=each($this->mounts)) { 00328 if (t3lib_div::isFirstPartOfStr($thePath,$val['path'])) { 00329 return $k; 00330 } 00331 } 00332 } 00333 } 00334 00340 function findFirstWebFolder() { 00341 global $TYPO3_CONF_VARS; 00342 00343 if (is_array($this->mounts)) { 00344 reset ($this->mounts); 00345 while(list($k,$val)=each($this->mounts)) { 00346 if (t3lib_div::isFirstPartOfStr($val['path'], PATH_site.$TYPO3_CONF_VARS['BE']['fileadminDir'])) { 00347 return $k; 00348 } 00349 } 00350 } 00351 } 00352 00360 function blindPath($thePath) { 00361 $k=$this->checkPathAgainstMounts($thePath); 00362 if ($k) { 00363 $name=''; 00364 $name.='['.$this->mounts[$k]['name'].']: '; 00365 $name.=substr($thePath,strlen($this->mounts[$k]['path'])); 00366 return $name; 00367 } 00368 } 00369 00376 function findTempFolder() { 00377 if ($this->tempFN && is_array($this->mounts)) { 00378 reset ($this->mounts); 00379 while(list($k,$val)=each($this->mounts)) { 00380 $tDir = $val['path'].$this->tempFN; 00381 if (@is_dir($tDir)) { 00382 return $tDir; 00383 } 00384 } 00385 } 00386 } 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 /********************* 00399 * 00400 * Cleaning functions 00401 * 00402 *********************/ 00403 00410 function cleanDirectoryName($theDir) { 00411 return ereg_replace('[\/\. ]*$','',$this->rmDoubleSlash($theDir)); 00412 } 00413 00420 function rmDoubleSlash($string) { 00421 return str_replace('//','/',$string); 00422 } 00423 00430 function slashPath($path) { 00431 if (substr($path,-1)!='/') { 00432 return $path.'/'; 00433 } 00434 return $path; 00435 } 00436 00443 function cleanFileName($fileName) { 00444 $theNewName = ereg_replace('[^.[:alnum:]_-]','_',trim($fileName)); 00445 return $theNewName; 00446 } 00447 00454 function formatSize($sizeInBytes) { 00455 if ($sizeInBytes>900) { 00456 if ($sizeInBytes>900000) { // MB 00457 $val = $sizeInBytes/(1024*1024); 00458 return number_format($val, (($val<20)?1:0), '.', '').' M'; 00459 } else { // KB 00460 $val = $sizeInBytes/(1024); 00461 return number_format($val, (($val<20)?1:0), '.', '').' K'; 00462 } 00463 } else { // Bytes 00464 return $sizeInBytes.' '; 00465 } 00466 } 00467 } 00468 00469 00470 00471 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_basicfilefunc.php']) { 00472 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_basicfilefunc.php']); 00473 } 00474 ?>