00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00070 require_once(PATH_t3lib.'class.t3lib_cs.php');
00071
00072
00081 class t3lib_basicFileFunctions {
00082 var $getUniqueNamePrefix = '';
00083 var $maxNumber = 99;
00084 var $uniquePrecision = 6;
00085 var $maxInputNameLen = 60;
00086 var $tempFN = '_temp_';
00087
00088
00089 var $f_ext = Array();
00090 var $mounts = Array();
00091 var $webPath ='';
00092 var $isInit = 0;
00093
00094
00095
00096
00097
00098
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 $this->maxInputNameLen = $GLOBALS['TYPO3_CONF_VARS']['SYS']['maxFileNameLength'] ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['maxFileNameLength'] : $this->maxInputNameLen;
00144 }
00145
00152 function getTotalFileInfo($wholePath) {
00153 $theuser = getmyuid();
00154 $info = t3lib_div::split_fileref($wholePath);
00155 $info['tstamp'] = @filemtime($wholePath);
00156 $info['size'] = @filesize($wholePath);
00157 $info['type'] = @filetype($wholePath);
00158 $info['owner'] = @fileowner($wholePath);
00159 $info['perms'] = @fileperms($wholePath);
00160 $info['writable'] = !@is_writable($wholePath);
00161 $info['readable'] = !@is_readable($wholePath);
00162 return $info;
00163 }
00164
00172 function is_allowed($iconkey,$type) {
00173 if (isset($this->f_ext[$type])) {
00174 $ik = strtolower($iconkey);
00175 if ($ik) {
00176
00177 if ($this->f_ext[$type]['allow']=='*' || t3lib_div::inList($this->f_ext[$type]['allow'],$ik)) return true;
00178
00179 if ($this->f_ext[$type]['deny']=='*' || t3lib_div::inList($this->f_ext[$type]['deny'],$ik)) return false;
00180
00181 return true;
00182 } else {
00183 if ($this->f_ext[$type]['allow']=='*') return true;
00184 if ($this->f_ext[$type]['deny']=='*') return false;
00185 return true;
00186 }
00187 }
00188 return false;
00189 }
00190
00197 function checkIfFullAccess($theDest) {
00198 $type = $this->is_webpath($theDest)?'webspace':'ftpspace';
00199 if (isset($this->f_ext[$type])) {
00200 if ((string)$this->f_ext[$type]['deny']=='' || $this->f_ext[$type]['allow']=='*') return true;
00201 }
00202 }
00203
00211 function is_webpath($path) {
00212 if ($this->isInit) {
00213 $testPath = $this->slashPath($path);
00214 $testPathWeb = $this->slashPath($this->webPath);
00215 if ($testPathWeb && $testPath) {
00216 return t3lib_div::isFirstPartOfStr($testPath,$testPathWeb);
00217 }
00218 }
00219 return true;
00220 }
00221
00231 function checkIfAllowed($ext, $theDest, $filename='') {
00232 return t3lib_div::verifyFilenameAgainstDenyPattern($filename) && $this->is_allowed($ext,($this->is_webpath($theDest)?'webspace':'ftpspace'));
00233 }
00234
00241 function checkFileNameLen($fileName) {
00242 return strlen($fileName) <= $this->maxInputNameLen;
00243 }
00244
00251 function is_directory($theDir) {
00252 if ($this->isPathValid($theDir)) {
00253 $theDir=$this->cleanDirectoryName($theDir);
00254 if (@is_dir($theDir)) {
00255 return $theDir;
00256 }
00257 }
00258 return false;
00259 }
00260
00268 function isPathValid($theFile) {
00269 return t3lib_div::validPathStr($theFile);
00270 }
00271
00283 function getUniqueName($theFile, $theDest, $dontCheckForUnique=0) {
00284 $theDest = $this->is_directory($theDest);
00285 $origFileInfo = t3lib_div::split_fileref($theFile);
00286 if ($theDest) {
00287 if ($this->getUniqueNamePrefix) {
00288 $origFileInfo['file'] = $this->getUniqueNamePrefix.$origFileInfo['file'];
00289 $origFileInfo['filebody'] = $this->getUniqueNamePrefix.$origFileInfo['filebody'];
00290 }
00291
00292
00293 $fileInfo = $origFileInfo;
00294 $theDestFile = $theDest.'/'.$fileInfo['file'];
00295 if (!@file_exists($theDestFile) || $dontCheckForUnique) {
00296 return $theDestFile;
00297 }
00298
00299
00300 $theTempFileBody = preg_replace('/_[0-9][0-9]$/','',$origFileInfo['filebody']);
00301 $theOrigExt = $origFileInfo['realFileext'] ? '.'.$origFileInfo['realFileext'] : '';
00302
00303 for ($a=1; $a<=($this->maxNumber+1); $a++) {
00304 if ($a<=$this->maxNumber) {
00305 $insert = '_'.sprintf('%02d', $a);
00306 } else {
00307 $insert = '_'.substr(md5(uniqId('')),0,$this->uniquePrecision);
00308 }
00309 $theTestFile = $theTempFileBody.$insert.$theOrigExt;
00310 $theDestFile = $theDest.'/'.$theTestFile;
00311 if (!@file_exists($theDestFile)) {
00312 return $theDestFile;
00313 }
00314 }
00315 }
00316 }
00317
00326 function checkPathAgainstMounts($thePath) {
00327 if ($thePath && $this->isPathValid($thePath) && is_array($this->mounts)) {
00328 reset ($this->mounts);
00329 while(list($k,$val)=each($this->mounts)) {
00330 if (t3lib_div::isFirstPartOfStr($thePath,$val['path'])) {
00331 return $k;
00332 }
00333 }
00334 }
00335 }
00336
00342 function findFirstWebFolder() {
00343 global $TYPO3_CONF_VARS;
00344
00345 if (is_array($this->mounts)) {
00346 reset ($this->mounts);
00347 while(list($k,$val)=each($this->mounts)) {
00348 if (t3lib_div::isFirstPartOfStr($val['path'], PATH_site.$TYPO3_CONF_VARS['BE']['fileadminDir'])) {
00349 return $k;
00350 }
00351 }
00352 }
00353 }
00354
00362 function blindPath($thePath) {
00363 $k=$this->checkPathAgainstMounts($thePath);
00364 if ($k) {
00365 $name='';
00366 $name.='['.$this->mounts[$k]['name'].']: ';
00367 $name.=substr($thePath,strlen($this->mounts[$k]['path']));
00368 return $name;
00369 }
00370 }
00371
00378 function findTempFolder() {
00379 if ($this->tempFN && is_array($this->mounts)) {
00380 reset ($this->mounts);
00381 while(list($k,$val)=each($this->mounts)) {
00382 $tDir = $val['path'].$this->tempFN;
00383 if (@is_dir($tDir)) {
00384 return $tDir;
00385 }
00386 }
00387 }
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00412 function cleanDirectoryName($theDir) {
00413 return preg_replace('/[\/\. ]*$/','',$this->rmDoubleSlash($theDir));
00414 }
00415
00422 function rmDoubleSlash($string) {
00423 return str_replace('
00424 }
00425
00432 function slashPath($path) {
00433 if (substr($path,-1)!='/') {
00434 return $path.'/';
00435 }
00436 return $path;
00437 }
00438
00446 function cleanFileName($fileName,$charset='') {
00447
00448 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] == 'utf-8' && $GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
00449
00450 return preg_replace('/[\x00-\x2C\/\x3A-\x3F\x5B-\x60\x7B-\xBF]/u','_',trim($fileName));
00451 }
00452
00453 if (!is_object($this->csConvObj)) {
00454 if (TYPO3_MODE=='FE') {
00455 $this->csConvObj = &$GLOBALS['TSFE']->csConvObj;
00456 } elseif (is_object($GLOBALS['LANG'])) {
00457 $this->csConvObj = &$GLOBALS['LANG']->csConvObj;
00458 } else {
00459 $this->csConvObj = &t3lib_div::makeInstance('t3lib_cs');
00460 }
00461 }
00462
00463 if (!$charset) {
00464 if (TYPO3_MODE=='FE') {
00465 $charset = $GLOBALS['TSFE']->renderCharset;
00466 } elseif (is_object($GLOBALS['LANG'])) {
00467 $charset = $GLOBALS['LANG']->charSet;
00468 } else {
00469 $charset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
00470 }
00471 }
00472
00473 if ($charset) {
00474 $fileName = $this->csConvObj->specCharsToASCII($charset,$fileName);
00475 }
00476
00477 return preg_replace('/[^.[:alnum:]_-]/','_',trim($fileName));
00478 }
00479
00486 function formatSize($sizeInBytes) {
00487 if ($sizeInBytes>900) {
00488 if ($sizeInBytes>900000) {
00489 $val = $sizeInBytes/(1024*1024);
00490 return number_format($val, (($val<20)?1:0), '.', '').' M';
00491 } else {
00492 $val = $sizeInBytes/(1024);
00493 return number_format($val, (($val<20)?1:0), '.', '').' K';
00494 }
00495 } else {
00496 return $sizeInBytes.' ';
00497 }
00498 }
00499 }
00500
00501
00502
00503 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_basicfilefunc.php']) {
00504 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_basicfilefunc.php']);
00505 }
00506 ?>