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 
00361     protected function _moveImage( $sSource, $sTarget )
00362     {
00363         $blDone = false;
00364         if ( ( $blDone = move_uploaded_file( $sSource, $sTarget ) ) ) {
00365             $blDone = chmod( $sTarget, 0644 );
00366         }
00367 
00368         return $blDone;
00369     }
00370 
00380     public function processFiles( $oObject = null, $aFiles = array() )
00381     {
00382         $aFiles = $aFiles ? $aFiles : $_FILES;
00383         if ( isset( $aFiles['myfile']['name'] ) ) {
00384 
00385             // A. protection for demoshops - strictly defining allowed file extensions
00386             $blDemo = (bool) $this->getConfig()->isDemoShop();
00387 
00388             // process all files
00389             while ( list( $sKey, $sValue ) = each( $aFiles['myfile']['name'] ) ) {
00390 
00391                 $aSource = $aFiles['myfile']['tmp_name'];
00392                 $sSource = $aSource[$sKey];
00393                 $aFiletype = explode( "@", $sKey );
00394                 $sKey    = $aFiletype[1];
00395                 $sType   = $aFiletype[0];
00396                 $sValue  = strtolower( $sValue );
00397 
00398                 // no file ? - skip
00399                 if ( $sValue ) {
00400 
00401                     // building file name
00402                     $sValue = $this->_prepareImageName( $sValue, $sType, $blDemo );
00403 
00404                     // finding directory
00405                     $sTarget = $this->_getImagePath( $sType ) . $sValue;
00406 
00407                     // processing images
00408                     $blCopy = $this->_prepareImage( $sType, $sSource, $sTarget );
00409 
00410                     // moving ..
00411                     if ( !$blCopy && $sSource ) {
00412                         $this->_moveImage( $sSource, $sTarget );
00413                     }
00414 
00415                     // assign the name
00416                     if ( isset( $sValue ) && $sValue ) {
00417                         $oObject->{$sKey}->setValue( $sValue );
00418                     }
00419                 }
00420             }
00421         }
00422 
00423         return $oObject;
00424     }
00425 
00434     function checkFile( $sFile )
00435     {
00436         $aCheckCache = oxSession::getVar("checkcache");
00437 
00438         if ( isset( $aCheckCache[$sFile] ) ) {
00439             return $aCheckCache[$sFile];
00440         }
00441 
00442         $blRet = false;
00443 
00444         if (is_readable( $sFile)) {
00445             $blRet = true;
00446         } else {
00447             // try again via socket
00448             $blRet = $this->urlValidate( $sFile );
00449         }
00450 
00451         $aCheckCache[$sFile] = $blRet;
00452         oxSession::setVar( "checkcache", $aCheckCache );
00453 
00454         return $blRet;
00455     }
00456 
00464     function urlValidate( $sLink )
00465     {
00466         $aUrlParts = @parse_url( $sLink );
00467         $sHost = ( isset( $aUrlParts["host"] ) && $aUrlParts["host"] ) ? $aUrlParts["host"] : null;
00468 
00469         $blValid = false;
00470         if ( $sHost ) {
00471             $sDocumentPath  = ( isset( $aUrlParts["path"] ) && $aUrlParts["path"] ) ? $aUrlParts["path"] : '/';
00472             $sDocumentPath .= ( isset( $aUrlParts["query"] ) && $aUrlParts["query"] ) ? '?' . $aUrlParts["query"] : '';
00473 
00474             $sPort = ( isset( $aUrlParts["port"] ) && $aUrlParts["port"] ) ? $aUrlParts["port"] : '80';
00475 
00476             // Now (HTTP-)GET $documentpath at $sHost";
00477             if ( ( $oConn = @fsockopen( $sHost, $sPort, $iErrNo, $sErrStr, 30 ) ) ) {
00478                 fwrite ( $oConn, "HEAD {$sDocumentPath} HTTP/1.0\r\nHost: {$sHost}\r\n\r\n" );
00479                 $sResponse = fgets( $oConn, 22 );
00480                 fclose( $oConn );
00481 
00482                 if ( ereg( "200 OK", $sResponse ) ) {
00483                     $blValid = true;
00484                 }
00485             }
00486         }
00487 
00488         return $blValid;
00489     }
00490 
00501     public function handleUploadedFile($aFileInfo, $sUploadPath)
00502     {
00503         $sBasePath = $this->getConfig()->getConfigParam('sShopDir');
00504 
00505         //checking params
00506         if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00507             throw new oxException( 'EXCEPTION_NOFILE' );
00508         }
00509 
00510         //wrong chars in file name?
00511         if ( !eregi('^[_a-z0-3\.]+$', $aFileInfo['name'] ) ) {
00512             throw new oxException( 'EXCEPTION_FILENAMEINVALIDCHARS' );
00513         }
00514 
00515         // error uploading file ?
00516         if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
00517             throw new oxException( 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
00518         }
00519 
00520         $aPathInfo = pathinfo($aFileInfo['name']);
00521 
00522         $sExt = $aPathInfo['extension'];
00523         $sFileName = $aPathInfo['filename'];
00524 
00525         if ( !in_array( $sExt, $this->getConfig()->getConfigParam( 'aAllowedUploadTypes' ) ) ) {
00526             throw new oxException( 'EXCEPTION_NOTALLOWEDTYPE' );
00527         }
00528 
00529         //file exists ?
00530         while (file_exists($sBasePath . "/" .$sUploadPath . "/" . $sFileName . "." . $sExt)) {
00531             $sFileName .= "(1)";
00532         }
00533 
00534         move_uploaded_file($aFileInfo['tmp_name'], $sBasePath . "/" .$sUploadPath . "/" . $sFileName . "." . $sExt);
00535 
00536         $sUrl = $this->getConfig()->getShopUrl() . "/" . $sUploadPath . "/" . $sFileName . "." . $sExt;
00537 
00538         //removing dublicate slashes
00539         $sUrl = str_replace('//', '/', $sUrl);
00540         $sUrl = str_replace('http:/', 'http://', $sUrl);
00541 
00542         return $sUrl;
00543     }
00544 }

Generated on Wed Apr 22 12:26:31 2009 for OXID eShop CE by  doxygen 1.5.5