Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2004 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 ***************************************************************/ 00080 class t3lib_basicFileFunctions { 00081 var $getUniqueNamePrefix = ''; // Prefix which will be prepended the file when using the getUniqueName-function 00082 var $maxNumber = 20; // This number decides the highest allowed appended number used on a filename before we use naming with unique strings 00083 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. 00084 var $maxInputNameLen = 30; // This is the maximum length of names treated by cleanFileName() 00085 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) 00086 00087 // internal 00088 var $f_ext = Array(); // See comment in header 00089 var $mounts = Array(); // See comment in header 00090 var $webPath =''; // Set to DOCUMENT_ROOT. 00091 var $isInit = 0; // Set to true after init()/start(); 00092 00093 00094 00095 /********************************** 00096 * 00097 * Checking functions 00098 * 00099 **********************************/ 00100 00132 function init($mounts, $f_ext) { 00133 $this->f_ext['webspace']['allow'] = t3lib_div::uniqueList(strtolower($f_ext['webspace']['allow'])); 00134 $this->f_ext['webspace']['deny'] = t3lib_div::uniqueList(strtolower($f_ext['webspace']['deny'])); 00135 $this->f_ext['ftpspace']['allow'] = t3lib_div::uniqueList(strtolower($f_ext['ftpspace']['allow'])); 00136 $this->f_ext['ftpspace']['deny'] = t3lib_div::uniqueList(strtolower($f_ext['ftpspace']['deny'])); 00137 00138 $this->mounts = $mounts; 00139 $this->webPath = t3lib_div::getIndpEnv('TYPO3_DOCUMENT_ROOT'); 00140 $this->isInit = 1; 00141 } 00142 00149 function getTotalFileInfo($wholePath) { 00150 $theuser = getmyuid(); 00151 $info = t3lib_div::split_fileref($wholePath); 00152 $info['tstamp'] = @filectime($wholePath); 00153 $info['size'] = @filesize($wholePath); 00154 $info['type'] = @filetype($wholePath); 00155 $info['owner'] = @fileowner($wholePath); 00156 $info['perms'] = @fileperms($wholePath); 00157 $info['writable'] = !@is_writable($wholePath); 00158 $info['readable'] = !@is_readable($wholePath); 00159 return $info; 00160 } 00161 00169 function is_allowed($iconkey,$type) { 00170 if (isset($this->f_ext[$type])) { 00171 $ik = strtolower($iconkey); 00172 if ($ik) { 00173 // If the extension is found amongst the allowed types, we return true immediately 00174 if ($this->f_ext[$type]['allow']=='*' || t3lib_div::inList($this->f_ext[$type]['allow'],$ik)) return true; 00175 // If the extension is found amongst the denied types, we return false immediately 00176 if ($this->f_ext[$type]['deny']=='*' || t3lib_div::inList($this->f_ext[$type]['deny'],$ik)) return false; 00177 // If no match we return true 00178 return true; 00179 } else { // If no extension: 00180 if ($this->f_ext[$type]['allow']=='*') return true; 00181 if ($this->f_ext[$type]['deny']=='*') return false; 00182 return true; 00183 } 00184 } 00185 return false; 00186 } 00187 00194 function checkIfFullAccess($theDest) { 00195 $type = $this->is_webpath($theDest)?'webspace':'ftpspace'; 00196 if (isset($this->f_ext[$type])) { 00197 if ((string)$this->f_ext[$type]['deny']=='' || $this->f_ext[$type]['allow']=='*') return true; 00198 } 00199 } 00200 00208 function is_webpath($path) { 00209 if ($this->isInit) { 00210 $testPath = $this->slashPath($path); 00211 $testPathWeb = $this->slashPath($this->webPath); 00212 if ($testPathWeb && $testPath) { 00213 return t3lib_div::isFirstPartOfStr($testPath,$testPathWeb); 00214 } 00215 } 00216 return true; // Its more safe to return true (as the webpath is more restricted) if something went wrong... 00217 } 00218 00228 function checkIfAllowed($ext, $theDest, $filename='') { 00229 return t3lib_div::verifyFilenameAgainstDenyPattern($filename) && $this->is_allowed($ext,($this->is_webpath($theDest)?'webspace':'ftpspace')); 00230 } 00231 00238 function checkFileNameLen($fileName) { 00239 return strlen($fileName) <= $this->maxInputNameLen; 00240 } 00241 00248 function is_directory($theDir) { 00249 if ($this->isPathValid($theDir)) { 00250 $theDir=$this->cleanDirectoryName($theDir); 00251 if (@is_dir($theDir)) { 00252 return $theDir; 00253 } 00254 } 00255 return false; 00256 } 00257 00265 function isPathValid($theFile) { 00266 return t3lib_div::validPathStr($theFile); 00267 } 00268 00280 function getUniqueName($theFile, $theDest, $dontCheckForUnique=0) { 00281 $theDest = $this->is_directory($theDest); // $theDest is cleaned up 00282 $origFileInfo = t3lib_div::split_fileref($theFile); // Fetches info about path, name, extention of $theFile 00283 if ($theDest) { 00284 if ($this->getUniqueNamePrefix) { // Adds prefix 00285 $origFileInfo['file'] = $this->getUniqueNamePrefix.$origFileInfo['file']; 00286 $origFileInfo['filebody'] = $this->getUniqueNamePrefix.$origFileInfo['filebody']; 00287 } 00288 00289 // Check if the file exists and if not - return the filename... 00290 $fileInfo = $origFileInfo; 00291 $theDestFile = $theDest.'/'.$fileInfo['file']; // The destinations file 00292 if (!@file_exists($theDestFile) || $dontCheckForUnique) { // If the file does NOT exist we return this filename 00293 return $theDestFile; 00294 } 00295 00296 // 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... 00297 $theTempFileBody = ereg_replace('_[0-9][0-9]$','',$origFileInfo['filebody']); // This removes _xx if appended to the file 00298 $theOrigExt = $origFileInfo['realFileext'] ? '.'.$origFileInfo['realFileext'] : ''; 00299 00300 for ($a=1; $a<=($this->maxNumber+1); $a++) { 00301 if ($a<=$this->maxNumber) { // First we try to append numbers 00302 $insert = '_'.sprintf('%02d', $a); 00303 } else { // .. then we try unique-strings... 00304 $insert = '_'.substr(md5(uniqId('')),0,$this->uniquePrecision); 00305 } 00306 $theTestFile = $theTempFileBody.$insert.$theOrigExt; 00307 $theDestFile = $theDest.'/'.$theTestFile; // The destinations file 00308 if (!@file_exists($theDestFile)) { // If the file does NOT exist we return this filename 00309 return $theDestFile; 00310 } 00311 } 00312 } 00313 } 00314 00323 function checkPathAgainstMounts($thePath) { 00324 if ($thePath && $this->isPathValid($thePath) && is_array($this->mounts)) { 00325 reset ($this->mounts); 00326 while(list($k,$val)=each($this->mounts)) { 00327 if (t3lib_div::isFirstPartOfStr($thePath,$val['path'])) { 00328 return $k; 00329 } 00330 } 00331 } 00332 } 00333 00341 function blindPath($thePath) { 00342 $k=$this->checkPathAgainstMounts($thePath); 00343 if ($k) { 00344 $name=''; 00345 $name.='['.$this->mounts[$k]['name'].']: '; 00346 $name.=substr($thePath,strlen($this->mounts[$k]['path'])); 00347 return $name; 00348 } 00349 } 00350 00357 function findTempFolder() { 00358 if ($this->tempFN && is_array($this->mounts)) { 00359 reset ($this->mounts); 00360 while(list($k,$val)=each($this->mounts)) { 00361 $tDir = $val['path'].$this->tempFN; 00362 if (@is_dir($tDir)) { 00363 return $tDir; 00364 } 00365 } 00366 } 00367 } 00368 00369 00370 00371 00372 00373 00374 /********************* 00375 * 00376 * Cleaning functions 00377 * 00378 *********************/ 00379 00386 function cleanDirectoryName($theDir) { 00387 return ereg_replace('[\/\. ]*$','',$this->rmDoubleSlash($theDir)); 00388 } 00389 00396 function rmDoubleSlash($string) { 00397 return str_replace('//','/',$string); 00398 } 00399 00406 function slashPath($path) { 00407 if (substr($path,-1)!='/') { 00408 return $path.'/'; 00409 } 00410 return $path; 00411 } 00412 00419 function cleanFileName($fileName) { 00420 $theNewName = ereg_replace('[^.[:alnum:]_-]','_',trim($fileName)); 00421 return $theNewName; 00422 } 00423 00430 function formatSize($sizeInBytes) { 00431 if ($sizeInBytes>900) { 00432 if ($sizeInBytes>900000) { // MB 00433 $val = $sizeInBytes/(1024*1024); 00434 return number_format($val, (($val<20)?1:0), '.', '').' M'; 00435 } else { // KB 00436 $val = $sizeInBytes/(1024); 00437 return number_format($val, (($val<20)?1:0), '.', '').' K'; 00438 } 00439 } else { // Bytes 00440 return $sizeInBytes.' '; 00441 } 00442 } 00443 } 00444 00445 00446 00447 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_basicfilefunc.php']) { 00448 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_basicfilefunc.php']); 00449 } 00450 ?>