oxutilsfile.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsFile extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00020     public static function getInstance()
00021     {
00022         // disable caching for test modules
00023         if ( defined( 'OXID_PHP_UNIT' ) ) {
00024             static $inst = array();
00025             self::$_instance = $inst[oxClassCacheKey()];
00026         }
00027 
00028         if ( !self::$_instance instanceof oxUtilsFile ) {
00029 
00030             self::$_instance = oxNew( 'oxUtilsFile' );
00031             if ( defined( 'OXID_PHP_UNIT' ) ) {
00032                 $inst[oxClassCacheKey()] = self::$_instance;
00033             }
00034         }
00035         return self::$_instance;
00036     }
00037 
00045     public function normalizeDir( $sDir )
00046     {
00047         if ( isset($sDir) && substr($sDir, -1) !== '/' ) {
00048             $sDir .= "/";
00049         }
00050 
00051         return $sDir;
00052     }
00053 
00062     public function copyDir( $sSourceDir, $sTargetDir )
00063     {
00064         $handle = opendir( $sSourceDir );
00065         while ( false !== ( $file = readdir( $handle ) ) ) {
00066             if ( $file != '.' && $file != '..' ) {
00067                 if ( is_dir( $sSourceDir.'/'.$file ) ) {
00068 
00069                     // recursive
00070                     $sNewSourceDir = $sSourceDir.'/'.$file;
00071                     $sNewTargetDir = $sTargetDir.'/'.$file;
00072                     if ( strcasecmp( $file, 'CVS' ) &&  strcasecmp( $file, '.svn' )) {
00073                         @mkdir( $sNewTargetDir, 0777 );
00074                         $this->copyDir( $sNewSourceDir, $sNewTargetDir );
00075                     }
00076                 } else {
00077                     $sSourceFile = $sSourceDir.'/'.$file;
00078                     $sTargetFile = $sTargetDir.'/'.$file;
00079 
00080                     //do not copy files within dyn_images
00081                     if ( !strstr( $sSourceDir, 'dyn_images' ) ||  $file == 'nopic.jpg' || $file == 'nopic_ico.jpg' ) {
00082                         @copy( $sSourceFile, $sTargetFile );
00083                     }
00084                 }
00085             }
00086         }
00087         closedir($handle);
00088     }
00089 
00097     public function deleteDir( $sSourceDir )
00098     {
00099         if ( is_dir( $sSourceDir ) ) {
00100             if ( $oDir = dir( $sSourceDir ) ) {
00101 
00102                 while ( false !== $sFile = $oDir->read() ) {
00103                     if ( $sFile == '.' || $sFile == '..' ) {
00104                         continue;
00105                     }
00106 
00107                     if ( !$this->deleteDir( $oDir->path . DIRECTORY_SEPARATOR . $sFile ) ) {
00108                         $oDir->close();
00109                         return false;
00110                     }
00111                 }
00112 
00113                 $oDir->close();
00114                 return rmdir( $sSourceDir );
00115             }
00116         } elseif ( file_exists( $sSourceDir ) ) {
00117             return unlink ( $sSourceDir );
00118         }
00119     }
00120 
00128     public function readRemoteFileAsString( $sPath )
00129     {
00130         $sRet  = '';
00131         $hFile = @fopen( $sPath, 'r' );
00132         if ( $hFile ) {
00133             socket_set_timeout( $hFile, 2 );
00134             while ( !feof( $hFile ) ) {
00135                 $sLine = fgets( $hFile, 4096 );
00136                 $sRet .= $sLine;
00137             }
00138             fclose( $hFile );
00139         }
00140 
00141         return $sRet;
00142     }
00143 
00152     public function processFiles( $oObject = null )
00153     {
00154         global $_FILES;
00155         $myConfig = $this->getConfig();
00156 
00157         if ( isset( $_FILES['myfile']['name'])) {
00158 
00159             // A. protection for demoshops - strictly defining allowed file extensions
00160             $blDemo = false;
00161             if ( $myConfig->isDemoShop() ) {
00162                 $blDemo = true;
00163                 $aAllowedFiles = array( 'gif', 'jpg', 'png', 'pdf' );
00164             }
00165 
00166             // process all files
00167             while (list($key, $value) = each($_FILES['myfile']['name'])) {
00168                 $aSource = $_FILES['myfile']['tmp_name'];
00169                 $sSource = $aSource[$key];
00170                 $aFiletype = explode( "@", $key);
00171                 $key    = $aFiletype[1];
00172                 $sType  = $aFiletype[0];
00173                 $value = strtolower( $value);
00174 
00175                 // no file ? - skip
00176                 if (!$value)
00177                     continue;
00178 
00179                 // add type to name
00180                 $aFilename = explode( ".", $value);
00181 
00182                 $sFileType = trim($aFilename[count($aFilename)-1]);
00183 
00184                 //hack?
00185                 $aBadFiles = array("php", "jsp", "cgi", "cmf", "exe");
00186 
00187                 if ( in_array($sFileType, $aBadFiles) || ( $blDemo && !in_array( $sFileType, $aAllowedFiles ) ) ) {
00188                     oxUtils::getInstance()->showMessageAndExit( "We don't play this game, go away" );
00189                 }
00190 
00191                 if ( isset($sFileType)) {
00192                     // removing file type
00193                     if ( count($aFilename) > 0) {
00194                         unset($aFilename[count($aFilename)-1]);
00195                     }
00196                     $sFName = "";
00197                     if ( isset($aFilename[0])) {
00198                         $sFName = preg_replace('/[^a-zA-Z0-9_\.-]/', '', implode('.', $aFilename));
00199                     }
00200                     $value = $sFName . "_" .strtolower($sType).".".$sFileType;
00201                 }
00202                 // Directory
00203                 $iPos = 0;
00204                 switch( $sType) {
00205                     case 'ICO':
00206                     case 'CICO':
00207                         $iPos = "icon";
00208                         break;
00209                     case 'TH':
00210                     case 'TC':
00211                     default:
00212                         $iPos = 0;
00213                         break;
00214                     case 'P1':
00215                         $iPos = 1;
00216                         break;
00217                     case 'P2':
00218                         $iPos = 2;
00219                         break;
00220                     case 'P3':
00221                         $iPos = 3;
00222                         break;
00223                     case 'P4':
00224                         $iPos = 4;
00225                         break;
00226                     case 'P5':
00227                         $iPos = 5;
00228                         break;
00229                     case 'P6':
00230                         $iPos = 6;
00231                         break;
00232                     case 'P7':
00233                         $iPos = 7;
00234                         break;
00235                     case 'P8':
00236                         $iPos = 8;
00237                         break;
00238                     case 'P9':
00239                         $iPos = 9;
00240                         break;
00241                     case 'P10':
00242                         $iPos = 10;
00243                         break;
00244                     case 'P11':
00245                         $iPos = 11;
00246                         break;
00247                     case 'P12':
00248                         $iPos = 12;
00249                         break;
00250                     case 'Z1':
00251                         $iPos = 'z1';
00252                         break;
00253                     case 'Z2':
00254                         $iPos = 'z2';
00255                         break;
00256                     case 'Z3':
00257                         $iPos = 'z3';
00258                         break;
00259                     case 'Z4':
00260                         $iPos = 'z4';
00261                         break;
00262                     case 'Z5':
00263                         $iPos = 'z5';
00264                         break;
00265                     case 'Z6':
00266                         $iPos = 'z6';
00267                         break;
00268                     case 'Z7':
00269                         $iPos = 'z7';
00270                         break;
00271                     case 'Z8':
00272                         $iPos = 'z8';
00273                         break;
00274                     case 'Z9':
00275                         $iPos = 'z9';
00276                         break;
00277                     case 'Z10':
00278                         $iPos = 'z10';
00279                         break;
00280                     case 'Z11':
00281                         $iPos = 'z11';
00282                         break;
00283                     case 'Z12':
00284                         $iPos = 'z12';
00285                         break;
00286 
00287                 }
00288 
00289                 $sTarget = $myConfig->getAbsDynImageDir() . "/$iPos/$value";
00290 
00291                 // add file process here
00292                 $blCopy = false;
00293                 switch ( $sType) {
00294                     case 'TH':
00295                         if ( $myConfig->getConfigParam( 'sThumbnailsize' )) {
00296                             // convert this file
00297                             $aSize = explode( "*", $myConfig->getConfigParam( 'sThumbnailsize' ));
00298                             $iX = $aSize[0];
00299                             $iY = $aSize[1];
00300                             $blCopy = oxUtilspic::getInstance()->resizeImage( $sSource, $sTarget, $iX, $iY );
00301                         }
00302                         break;
00303                     case 'TC':
00304                         if ( $myConfig->getConfigParam( 'sCatThumbnailsize' )) {
00305                             // convert this file
00306                             $aSize = explode( "*", $myConfig->getConfigParam( 'sCatThumbnailsize' ));
00307                             $iX = $aSize[0];
00308                             $iY = $aSize[1];
00309                             $blCopy = oxUtilspic::getInstance()->resizeImage( $sSource, $sTarget, $iX, $iY );
00310                         }
00311                         break;
00312                     case 'CICO':
00313                     case 'ICO':
00314                         if ( $myConfig->getConfigParam( 'sIconsize' ) ) {
00315                             // convert this file
00316                             $aSize = explode( "*", $myConfig->getConfigParam( 'sIconsize' ) );
00317                             $iX = $aSize[0];
00318                             $iY = $aSize[1];
00319                             $blCopy = oxUtilspic::getInstance()->resizeImage( $sSource, $sTarget, $iX, $iY );
00320 
00321                         }
00322                         break;
00323                     case 'P1':
00324                     case 'P2':
00325                     case 'P3':
00326                     case 'P4':
00327                     case 'P5':
00328                     case 'P6':
00329                     case 'P7':
00330                     case 'P8':
00331                     case 'P9':
00332                     case 'P10':
00333                     case 'P11':
00334                     case 'P12':
00335                         //
00336                         $aPType = explode("P", $sType);
00337                         $iPic = intval($aPType[1]) - 1;
00338 
00339                         // #840A + compatibility with prev. versions
00340                         $aDetailImageSizes = $myConfig->getConfigParam( 'aDetailImageSizes' );
00341                         $sDetailImageSize = $myConfig->getConfigParam( 'sDetailImageSize' );
00342                         if ( isset($aDetailImageSizes["oxpic".intval($aPType[1])])) {
00343                             $sDetailImageSize = $aDetailImageSizes["oxpic".intval($aPType[1])];
00344                         }
00345 
00346                         if ( $sDetailImageSize ) {
00347                             // convert this file
00348                             $aSize = explode( "*", $sDetailImageSize);
00349                             $iX = $aSize[0];
00350                             $iY = $aSize[1];
00351                             $blCopy = oxUtilspic::getInstance()->resizeImage( $sSource, $sTarget, $iX, $iY );
00352 
00353                             //make an icon
00354                             $sIconName = oxUtilsPic::getInstance()->iconName($sTarget);
00355                             $aSize = explode( "*", $myConfig->getConfigParam( 'sIconsize' ) );
00356                             $iX = $aSize[0];
00357                             $iY = $aSize[1];
00358                             $blCopy = oxUtilspic::getInstance()->resizeImage( $sSource, $sIconName, $iX, $iY );
00359 
00360                         }
00361                         break;
00362 
00363                     case 'Z1':
00364                     case 'Z2':
00365                     case 'Z3':
00366                     case 'Z4':
00367                     case 'Z5':
00368                     case 'Z6':
00369                     case 'Z7':
00370                     case 'Z8':
00371                     case 'Z9':
00372                     case 'Z10':
00373                     case 'Z11':
00374                     case 'Z12':
00375 
00376                         //
00377                         $aPType = explode("Z", $sType);
00378                         $iPic = intval($aPType[1]) - 1;
00379 
00380                         // #840A + compatibility with prev. versions
00381                         $aZoomImageSizes = $myConfig->getConfigParam( 'aZoomImageSizes' );
00382                         $sZoomImageSize  = $myConfig->getConfigParam( 'sZoomImageSize' );
00383                         if ( isset($aZoomImageSizes["oxzoom".intval($aPType[1])])) {
00384                             $sZoomImageSize = $aZoomImageSizes["oxzoom".intval($aPType[1])];
00385                         }
00386 
00387                         //
00388                         if ( $sZoomImageSize) {
00389                             // convert this file
00390                             $aSize = explode( "*", $sZoomImageSize);
00391                             $iX = $aSize[0];
00392                             $iY = $aSize[1];
00393                             $blCopy = oxUtilspic::getInstance()->resizeImage( $sSource, $sTarget, $iX, $iY );
00394                         }
00395                         break;
00396 
00397                     default:
00398                         break;
00399                     }
00400 
00401                 if ( !$blCopy && $sSource) {
00402                     move_uploaded_file( $sSource, $sTarget);
00403                     chmod( $sTarget, 0644);
00404                 }
00405                 // assign the name
00406                 if ( isset( $value) && $value) {
00407                     $oObject->$key->setValue($value);
00408                 }
00409             }
00410         }
00411 
00412         return $oObject;
00413     }
00414 
00423     function checkFile( $sFile )
00424     {
00425         $mySession = $this->getSession();
00426 
00427         $aCheckCache = oxSession::getVar("checkcache");
00428 
00429         if ( isset( $aCheckCache[$sFile])) {
00430             return $aCheckCache[$sFile];
00431         }
00432 
00433         $blRet = false;
00434 
00435         //if (@fclose(@fopen( $sFile, "r")))
00436         if (is_readable( $sFile)) {
00437             $blRet = true;
00438         } else {
00439             // try again via socket
00440             $blRet = $this->urlValidate( $sFile );
00441         }
00442 
00443         $aCheckCache[$sFile] = $blRet;
00444         oxSession::setVar("checkcache", $aCheckCache);
00445 
00446         return $blRet;
00447     }
00448 
00456     function urlValidate( $sLink )
00457     {
00458         $aUrlParts = @parse_url( $sLink );
00459 
00460         if ( empty( $aUrlParts["host"] ) ) {
00461             return( false );
00462         }
00463 
00464         if ( !empty( $aUrlParts["path"] ) ) {
00465             $sDocumentPath = $aUrlParts["path"];
00466         } else {
00467             $sDocumentPath = "/";
00468         }
00469 
00470         if ( !empty( $aUrlParts["query"] ) ) {
00471             $sDocumentPath .= "?" . $aUrlParts["query"];
00472         }
00473 
00474         $sHost = $aUrlParts["host"];
00475         $sPort = $aUrlParts["port"];
00476 
00477         // Now (HTTP-)GET $documentpath at $host";
00478         if (empty( $sPort ) ) {
00479             $sPort = "80";
00480         }
00481         $socket = @fsockopen( $sHost, $sPort, $errno, $errstr, 30 );
00482         if (!$socket) {
00483             return(false);
00484         } else {
00485             fwrite ($socket, "HEAD ".$sDocumentPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00486             $http_response = fgets( $socket, 22 );
00487 
00488             if ( ereg("200 OK", $http_response, $regs ) ) {
00489                 return(true);
00490                 fclose( $socket );
00491             } else {
00492                 return(false);
00493             }
00494         }
00495     }
00496 
00507     public function handleUploadedFile($aFileInfo, $sUploadPath)
00508     {
00509         $sBasePath = $this->getConfig()->getConfigParam('sShopDir');
00510 
00511         //checking params
00512         if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00513             throw new oxException( 'EXCEPTION_NOFILE' );
00514         }
00515 
00516         //wrong chars in file name?
00517         if ( !eregi('^[_a-z0-3\.]+$', $aFileInfo['name'] ) ) {
00518             throw new oxException( 'EXCEPTION_FILENAMEINVALIDCHARS' );
00519         }
00520 
00521         // error uploading file ?
00522         if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
00523             throw new oxException( 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
00524         }
00525 
00526         $aPathInfo = pathinfo($aFileInfo['name']);
00527 
00528         $sExt = $aPathInfo['extension'];
00529         $sFileName = $aPathInfo['filename'];
00530 
00531         if ( !in_array( $sExt, $this->getConfig()->getConfigParam( 'aAllowedUploadTypes' ) ) ) {
00532             throw new oxException( 'EXCEPTION_NOTALLOWEDTYPE' );
00533         }
00534 
00535         //file exists ?
00536         while (file_exists($sBasePath . "/" .$sUploadPath . "/" . $sFileName . "." . $sExt)) {
00537             $sFileName .= "(1)";
00538         }
00539 
00540         move_uploaded_file($aFileInfo['tmp_name'], $sBasePath . "/" .$sUploadPath . "/" . $sFileName . "." . $sExt);
00541 
00542         $sUrl = $this->getConfig()->getShopUrl() . "/" . $sUploadPath . "/" . $sFileName . "." . $sExt;
00543 
00544         //removing dublicate slashes
00545         $sUrl = str_replace('//', '/', $sUrl);
00546         $sUrl = str_replace('http:/', 'http://', $sUrl);
00547 
00548         return $sUrl;
00549     }
00550 }

Generated on Thu Dec 4 12:04:57 2008 for OXID eShop CE by  doxygen 1.5.5