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

Generated on Tue Aug 18 09:21:06 2009 for OXID eShop CE by  doxygen 1.5.5