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
00081 class t3lib_basicFileFunctions {
00082 var $getUniqueNamePrefix = '';
00083 var $maxNumber = 20;
00084 var $uniquePrecision = 6;
00085 var $maxInputNameLen = 30;
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
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
00175 if ($this->f_ext[$type]['allow']=='*' || t3lib_div::inList($this->f_ext[$type]['allow'],$ik)) return true;
00176
00177 if ($this->f_ext[$type]['deny']=='*' || t3lib_div::inList($this->f_ext[$type]['deny'],$ik)) return false;
00178
00179 return true;
00180 } else {
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;
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);
00283 $origFileInfo = t3lib_div::split_fileref($theFile);
00284 if ($theDest) {
00285 if ($this->getUniqueNamePrefix) {
00286 $origFileInfo['file'] = $this->getUniqueNamePrefix.$origFileInfo['file'];
00287 $origFileInfo['filebody'] = $this->getUniqueNamePrefix.$origFileInfo['filebody'];
00288 }
00289
00290
00291 $fileInfo = $origFileInfo;
00292 $theDestFile = $theDest.'/'.$fileInfo['file'];
00293 if (!@file_exists($theDestFile) || $dontCheckForUnique) {
00294 return $theDestFile;
00295 }
00296
00297
00298 $theTempFileBody = ereg_replace('_[0-9][0-9]$','',$origFileInfo['filebody']);
00299 $theOrigExt = $origFileInfo['realFileext'] ? '.'.$origFileInfo['realFileext'] : '';
00300
00301 for ($a=1; $a<=($this->maxNumber+1); $a++) {
00302 if ($a<=$this->maxNumber) {
00303 $insert = '_'.sprintf('%02d', $a);
00304 } else {
00305 $insert = '_'.substr(md5(uniqId('')),0,$this->uniquePrecision);
00306 }
00307 $theTestFile = $theTempFileBody.$insert.$theOrigExt;
00308 $theDestFile = $theDest.'/'.$theTestFile;
00309 if (!@file_exists($theDestFile)) {
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
00401
00402
00403
00410 function cleanDirectoryName($theDir) {
00411 return ereg_replace('[\/\. ]*$','',$this->rmDoubleSlash($theDir));
00412 }
00413
00420 function rmDoubleSlash($string) {
00421 return str_replace('
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) {
00457 $val = $sizeInBytes/(1024*1024);
00458 return number_format($val, (($val<20)?1:0), '.', '').' M';
00459 } else {
00460 $val = $sizeInBytes/(1024);
00461 return number_format($val, (($val<20)?1:0), '.', '').' K';
00462 }
00463 } else {
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 ?>