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
00080 class t3lib_basicFileFunctions {
00081 var $getUniqueNamePrefix = '';
00082 var $maxNumber = 20;
00083 var $uniquePrecision = 6;
00084 var $maxInputNameLen = 30;
00085 var $tempFN = '_temp_';
00086
00087
00088 var $f_ext = Array();
00089 var $mounts = Array();
00090 var $webPath ='';
00091 var $isInit = 0;
00092
00093
00094
00095
00096
00097
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['writeable'] = ($info['perms']&2 || ($theuser==$info['owner'] && $info['perms']&128));
00158 $info['readable'] = ($info['perms']&4 || ($theuser==$info['owner'] && $info['perms']&256));
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
00174 if ($this->f_ext[$type]['allow']=='*' || t3lib_div::inList($this->f_ext[$type]['allow'],$ik)) return true;
00175
00176 if ($this->f_ext[$type]['deny']=='*' || t3lib_div::inList($this->f_ext[$type]['deny'],$ik)) return false;
00177
00178 return true;
00179 } else {
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;
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);
00282 $origFileInfo = t3lib_div::split_fileref($theFile);
00283 if ($theDest) {
00284 if ($this->getUniqueNamePrefix) {
00285 $origFileInfo['file'] = $this->getUniqueNamePrefix.$origFileInfo['file'];
00286 $origFileInfo['filebody'] = $this->getUniqueNamePrefix.$origFileInfo['filebody'];
00287 }
00288
00289
00290 $fileInfo = $origFileInfo;
00291 $theDestFile = $theDest.'/'.$fileInfo['file'];
00292 if (!@file_exists($theDestFile) || $dontCheckForUnique) {
00293 return $theDestFile;
00294 }
00295
00296
00297 $theTempFileBody = ereg_replace('_[0-9][0-9]$','',$origFileInfo['filebody']);
00298 $theOrigExt = $origFileInfo['realFileext'] ? '.'.$origFileInfo['realFileext'] : '';
00299
00300 for ($a=1; $a<=($this->maxNumber+1); $a++) {
00301 if ($a<=$this->maxNumber) {
00302 $insert = '_'.sprintf('%02d', $a);
00303 } else {
00304 $insert = '_'.substr(md5(uniqId('')),0,$this->uniquePrecision);
00305 }
00306 $theTestFile = $theTempFileBody.$insert.$theOrigExt;
00307 $theDestFile = $theDest.'/'.$theTestFile;
00308 if (!@file_exists($theDestFile)) {
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
00377
00378
00379
00386 function cleanDirectoryName($theDir) {
00387 return ereg_replace('[\/\. ]*$','',$this->rmDoubleSlash($theDir));
00388 }
00389
00396 function rmDoubleSlash($string) {
00397 return str_replace('
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) {
00433 $val = $sizeInBytes/(1024*1024);
00434 return number_format($val, (($val<20)?1:0), '.', '').' M';
00435 } else {
00436 $val = $sizeInBytes/(1024);
00437 return number_format($val, (($val<20)?1:0), '.', '').' K';
00438 }
00439 } else {
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 ?>