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     protected $_aTypeToPath = array( 'ICO'  => 'icon',
00021                                      'CICO' => 'icon',
00022                                      'TH'   => '0',
00023                                      'TC'   => '0',
00024                                      'P1'   => '1',
00025                                      'P2'   => '2',
00026                                      'P3'   => '3',
00027                                      'P4'   => '4',
00028                                      'P5'   => '5',
00029                                      'P6'   => '6',
00030                                      'P7'   => '7',
00031                                      'P8'   => '8',
00032                                      'P9'   => '9',
00033                                      'P10'  => '10',
00034                                      'P11'  => '11',
00035                                      'P12'  => '12',
00036                                      'Z1'   => 'z1',
00037                                      'Z2'   => 'z2',
00038                                      'Z3'   => 'z3',
00039                                      'Z4'   => 'z4',
00040                                      'Z5'   => 'z5',
00041                                      'Z6'   => 'z6',
00042                                      'Z7'   => 'z7',
00043                                      'Z8'   => 'z8',
00044                                      'Z9'   => 'z9',
00045                                      'Z10'  => 'z10',
00046                                      'Z11'  => 'z11',
00047                                      'Z12'  => 'z12'
00048                                    );
00049 
00055     protected $_aBadFiles = array( 'php', 'jsp', 'cgi', 'cmf', 'exe' );
00056 
00057 
00063     protected $_aAllowedFiles = array( 'gif', 'jpg', 'png', 'pdf' );
00069     public static function getInstance()
00070     {
00071         // disable caching for test modules
00072         if ( defined( 'OXID_PHP_UNIT' ) ) {
00073             static $inst = array();
00074             self::$_instance = $inst[oxClassCacheKey()];
00075         }
00076 
00077         if ( !self::$_instance instanceof oxUtilsFile ) {
00078 
00079             self::$_instance = oxNew( 'oxUtilsFile' );
00080             if ( defined( 'OXID_PHP_UNIT' ) ) {
00081                 $inst[oxClassCacheKey()] = self::$_instance;
00082             }
00083         }
00084         return self::$_instance;
00085     }
00086 
00094     public function normalizeDir( $sDir )
00095     {
00096         if ( isset($sDir) && substr($sDir, -1) !== '/' ) {
00097             $sDir .= "/";
00098         }
00099 
00100         return $sDir;
00101     }
00102 
00111     public function copyDir( $sSourceDir, $sTargetDir )
00112     {
00113         $handle = opendir( $sSourceDir );
00114         while ( false !== ( $file = readdir( $handle ) ) ) {
00115             if ( $file != '.' && $file != '..' ) {
00116                 if ( is_dir( $sSourceDir.'/'.$file ) ) {
00117 
00118                     // recursive
00119                     $sNewSourceDir = $sSourceDir.'/'.$file;
00120                     $sNewTargetDir = $sTargetDir.'/'.$file;
00121                     if ( strcasecmp( $file, 'CVS' ) &&  strcasecmp( $file, '.svn' )) {
00122                         @mkdir( $sNewTargetDir, 0777 );
00123                         $this->copyDir( $sNewSourceDir, $sNewTargetDir );
00124                     }
00125                 } else {
00126                     $sSourceFile = $sSourceDir.'/'.$file;
00127                     $sTargetFile = $sTargetDir.'/'.$file;
00128 
00129                     //do not copy files within dyn_images
00130                     if ( !strstr( $sSourceDir, 'dyn_images' ) ||  $file == 'nopic.jpg' || $file == 'nopic_ico.jpg' ) {
00131                         @copy( $sSourceFile, $sTargetFile );
00132                     }
00133                 }
00134             }
00135         }
00136         closedir($handle);
00137     }
00138 
00146     public function deleteDir( $sSourceDir )
00147     {
00148         if ( is_dir( $sSourceDir ) ) {
00149             if ( $oDir = dir( $sSourceDir ) ) {
00150 
00151                 while ( false !== $sFile = $oDir->read() ) {
00152                     if ( $sFile == '.' || $sFile == '..' ) {
00153                         continue;
00154                     }
00155 
00156                     if ( !$this->deleteDir( $oDir->path . DIRECTORY_SEPARATOR . $sFile ) ) {
00157                         $oDir->close();
00158                         return false;
00159                     }
00160                 }
00161 
00162                 $oDir->close();
00163                 return rmdir( $sSourceDir );
00164             }
00165         } elseif ( file_exists( $sSourceDir ) ) {
00166             return unlink ( $sSourceDir );
00167         }
00168     }
00169 
00177     public function readRemoteFileAsString( $sPath )
00178     {
00179         $sRet  = '';
00180         $hFile = @fopen( $sPath, 'r' );
00181         if ( $hFile ) {
00182             socket_set_timeout( $hFile, 2 );
00183             while ( !feof( $hFile ) ) {
00184                 $sLine = fgets( $hFile, 4096 );
00185                 $sRet .= $sLine;
00186             }
00187             fclose( $hFile );
00188         }
00189 
00190         return $sRet;
00191     }
00192 
00202     protected function _prepareImageName( $sValue, $sType, $blDemo = false )
00203     {
00204         // add type to name
00205         $aFilename = explode( ".", $sValue );
00206         $sFileType = trim( $aFilename[count( $aFilename )-1] );
00207 
00208         if ( isset( $sFileType ) ) {
00209 
00210             // unallowed files ?
00211             if ( in_array( $sFileType, $this->_aBadFiles ) || ( $blDemo && !in_array( $sFileType, $this->_aAllowedFiles ) ) ) {
00212                 oxUtils::getInstance()->showMessageAndExit( "We don't play this game, go away" );
00213             }
00214 
00215             // removing file type
00216             if ( count( $aFilename ) > 0 ) {
00217                 unset( $aFilename[count( $aFilename )-1] );
00218             }
00219 
00220             $sFName = '';
00221             if ( isset( $aFilename[0] ) ) {
00222                 $sFName = preg_replace( '/[^a-zA-Z0-9_\.-]/', '', implode( '.', $aFilename ) );
00223             }
00224 
00225             $sValue = "{$sFName}_" . strtolower( $sType ) . ".{$sFileType}";
00226         }
00227 
00228         return $sValue;
00229     }
00230 
00238     protected function _getImagePath( $sType )
00239     {
00240         $sFolder = array_key_exists( $sType, $this->_aTypeToPath ) ? $this->_aTypeToPath[ $sType ] : '0';
00241         return $this->getConfig()->getAbsDynImageDir() . "/{$sFolder}/";
00242     }
00243 
00254     protected function _prepareImage( $sType, $sSource, $sTarget )
00255     {
00256         $myConfig  = $this->getConfig();
00257         $oUtilsPic = oxUtilspic::getInstance();
00258 
00259         // add file process here
00260         $blCopy = false;
00261         switch ( $sType ) {
00262             case 'TH':
00263                 if ( ( $sSize = $myConfig->getConfigParam( 'sThumbnailsize' ) ) ) {
00264                     // convert this file
00265                     $aSize = explode( '*', $sSize );
00266                     $blCopy = $oUtilsPic->resizeImage( $sSource, $sTarget, $aSize[0], $aSize[1] );
00267                 }
00268                 break;
00269             case 'TC':
00270                 if ( ( $sSize = $myConfig->getConfigParam( 'sCatThumbnailsize' ) ) ) {
00271                     // convert this file
00272                     $aSize = explode( '*', $sSize );
00273                     $blCopy = $oUtilsPic->resizeImage( $sSource, $sTarget, $aSize[0], $aSize[1] );
00274                 }
00275                 break;
00276             case 'CICO':
00277             case 'ICO':
00278                 if ( ( $sSize = $myConfig->getConfigParam( 'sIconsize' ) ) ) {
00279                     // convert this file
00280                     $aSize = explode( '*', $sSize );
00281                     $blCopy = $oUtilsPic->resizeImage( $sSource, $sTarget, $aSize[0], $aSize[1] );
00282                 }
00283                 break;
00284             case 'P1':
00285             case 'P2':
00286             case 'P3':
00287             case 'P4':
00288             case 'P5':
00289             case 'P6':
00290             case 'P7':
00291             case 'P8':
00292             case 'P9':
00293             case 'P10':
00294             case 'P11':
00295             case 'P12':
00296                 //
00297                 $aPType = explode( 'P', $sType );
00298                 $iPic = intval( $aPType[1] ) - 1;
00299 
00300                 // #840A + compatibility with prev. versions
00301                 $aDetailImageSizes = $myConfig->getConfigParam( 'aDetailImageSizes' );
00302                 $sDetailImageSize  = $myConfig->getConfigParam( 'sDetailImageSize' );
00303                 if ( isset( $aDetailImageSizes['oxpic'.intval( $aPType[1] )] ) ) {
00304                     $sDetailImageSize = $aDetailImageSizes['oxpic'.intval( $aPType[1] )];
00305                 }
00306 
00307                 if ( $sDetailImageSize ) {
00308                     // convert this file
00309                     $aSize = explode( '*', $sDetailImageSize );
00310                     $blCopy = $oUtilsPic->resizeImage( $sSource, $sTarget, $aSize[0], $aSize[1] );
00311 
00312                     //make an icon
00313                     $sIconName = $oUtilsPic->iconName( $sTarget );
00314                     $aSize = explode( '*', $myConfig->getConfigParam( 'sIconsize' ) );
00315                     $blCopy = $oUtilsPic->resizeImage( $sSource, $sIconName, $aSize[0], $aSize[1] );
00316                 }
00317                 break;
00318             case 'Z1':
00319             case 'Z2':
00320             case 'Z3':
00321             case 'Z4':
00322             case 'Z5':
00323             case 'Z6':
00324             case 'Z7':
00325             case 'Z8':
00326             case 'Z9':
00327             case 'Z10':
00328             case 'Z11':
00329             case 'Z12':
00330                 //
00331                 $aPType = explode( 'Z', $sType );
00332                 $iPic = intval( $aPType[1] ) - 1;
00333 
00334                 // #840A + compatibility with prev. versions
00335                 $aZoomImageSizes = $myConfig->getConfigParam( 'aZoomImageSizes' );
00336                 $sZoomImageSize  = $myConfig->getConfigParam( 'sZoomImageSize' );
00337                 if ( isset( $aZoomImageSizes['oxzoom'.intval( $aPType[1] )] ) ) {
00338                     $sZoomImageSize = $aZoomImageSizes['oxzoom'.intval( $aPType[1] )];
00339                 }
00340 
00341                 //
00342                 if ( $sZoomImageSize ) {
00343                     // convert this file
00344                     $aSize = explode( '*', $sZoomImageSize );
00345                     $blCopy = $oUtilsPic->resizeImage( $sSource, $sTarget, $aSize[0], $aSize[1] );
00346                 }
00347                 break;
00348         }
00349 
00350         return $blCopy;
00351     }
00352 
00359     protected function _moveImage( $sSource, $sTarget )
00360     {
00361         $blDone = false;
00362         if ( ( $blDone = move_uploaded_file( $sSource, $sTarget ) ) ) {
00363             $blDone = chmod( $sTarget, 0644 );
00364         }
00365 
00366         return $blDone;
00367     }
00368 
00377     public function processFiles( $oObject = null, $aFiles = array() )
00378     {
00379         $aFiles = $aFiles ? $aFiles : $_FILES;
00380         if ( isset( $aFiles['myfile']['name'] ) ) {
00381 
00382             // A. protection for demoshops - strictly defining allowed file extensions
00383             $blDemo = (bool) $this->getConfig()->isDemoShop();
00384 
00385             // process all files
00386             while ( list( $sKey, $sValue ) = each( $aFiles['myfile']['name'] ) ) {
00387 
00388                 $aSource = $aFiles['myfile']['tmp_name'];
00389                 $sSource = $aSource[$sKey];
00390                 $aFiletype = explode( "@", $sKey );
00391                 $sKey    = $aFiletype[1];
00392                 $sType   = $aFiletype[0];
00393                 $sValue  = strtolower( $sValue );
00394 
00395                 // no file ? - skip
00396                 if ( $sValue ) {
00397 
00398                     // building file name
00399                     $sValue = $this->_prepareImageName( $sValue, $sType, $blDemo );
00400 
00401                     // finding directory
00402                     $sTarget = $this->_getImagePath( $sType ) . $sValue;
00403 
00404                     // processing images
00405                     $blCopy = $this->_prepareImage( $sType, $sSource, $sTarget );
00406 
00407                     // moving ..
00408                     if ( !$blCopy && $sSource ) {
00409                         $this->_moveImage( $sSource, $sTarget );
00410                     }
00411 
00412                     // assign the name
00413                     if ( isset( $sValue ) && $sValue ) {
00414                         $oObject->{$sKey}->setValue( $sValue );
00415                     }
00416                 }
00417             }
00418         }
00419 
00420         return $oObject;
00421     }
00422 
00431     function checkFile( $sFile )
00432     {
00433         $aCheckCache = oxSession::getVar("checkcache");
00434 
00435         if ( isset( $aCheckCache[$sFile] ) ) {
00436             return $aCheckCache[$sFile];
00437         }
00438 
00439         $blRet = false;
00440 
00441         if (is_readable( $sFile)) {
00442             $blRet = true;
00443         } else {
00444             // try again via socket
00445             $blRet = $this->urlValidate( $sFile );
00446         }
00447 
00448         $aCheckCache[$sFile] = $blRet;
00449         oxSession::setVar( "checkcache", $aCheckCache );
00450 
00451         return $blRet;
00452     }
00453 
00461     function urlValidate( $sLink )
00462     {
00463         $aUrlParts = @parse_url( $sLink );
00464         $sHost = ( isset( $aUrlParts["host"] ) && $aUrlParts["host"] ) ? $aUrlParts["host"] : null;
00465 
00466         $blValid = false;
00467         if ( $sHost ) {
00468             $sDocumentPath  = ( isset( $aUrlParts["path"] ) && $aUrlParts["path"] ) ? $aUrlParts["path"] : '/';
00469             $sDocumentPath .= ( isset( $aUrlParts["query"] ) && $aUrlParts["query"] ) ? '?' . $aUrlParts["query"] : '';
00470 
00471             $sPort = ( isset( $aUrlParts["port"] ) && $aUrlParts["port"] ) ? $aUrlParts["port"] : '80';
00472 
00473             // Now (HTTP-)GET $documentpath at $sHost";
00474             if ( ( $oConn = @fsockopen( $sHost, $sPort, $iErrNo, $sErrStr, 30 ) ) ) {
00475                 fwrite ( $oConn, "HEAD {$sDocumentPath} HTTP/1.0\r\nHost: {$sHost}\r\n\r\n" );
00476                 $sResponse = fgets( $oConn, 22 );
00477                 fclose( $oConn );
00478 
00479                 if ( ereg( "200 OK", $sResponse ) ) {
00480                     $blValid = true;
00481                 }
00482             }
00483         }
00484 
00485         return $blValid;
00486     }
00487 
00498     public function handleUploadedFile($aFileInfo, $sUploadPath)
00499     {
00500         $sBasePath = $this->getConfig()->getConfigParam('sShopDir');
00501 
00502         //checking params
00503         if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00504             throw new oxException( 'EXCEPTION_NOFILE' );
00505         }
00506 
00507         //wrong chars in file name?
00508         if ( !eregi('^[_a-z0-3\.]+$', $aFileInfo['name'] ) ) {
00509             throw new oxException( 'EXCEPTION_FILENAMEINVALIDCHARS' );
00510         }
00511 
00512         // error uploading file ?
00513         if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
00514             throw new oxException( 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
00515         }
00516 
00517         $aPathInfo = pathinfo($aFileInfo['name']);
00518 
00519         $sExt = $aPathInfo['extension'];
00520         $sFileName = $aPathInfo['filename'];
00521 
00522         if ( !in_array( $sExt, $this->getConfig()->getConfigParam( 'aAllowedUploadTypes' ) ) ) {
00523             throw new oxException( 'EXCEPTION_NOTALLOWEDTYPE' );
00524         }
00525 
00526         //file exists ?
00527         while (file_exists($sBasePath . "/" .$sUploadPath . "/" . $sFileName . "." . $sExt)) {
00528             $sFileName .= "(1)";
00529         }
00530 
00531         move_uploaded_file($aFileInfo['tmp_name'], $sBasePath . "/" .$sUploadPath . "/" . $sFileName . "." . $sExt);
00532 
00533         $sUrl = $this->getConfig()->getShopUrl() . "/" . $sUploadPath . "/" . $sFileName . "." . $sExt;
00534 
00535         //removing dublicate slashes
00536         $sUrl = str_replace('//', '/', $sUrl);
00537         $sUrl = str_replace('http:/', 'http://', $sUrl);
00538 
00539         return $sUrl;
00540     }
00541 }

Generated on Tue Apr 21 15:45:45 2009 for OXID eShop CE by  doxygen 1.5.5