00001 <?php
00002
00006 class oxUtilsFile extends oxSuperCfg
00007 {
00013 const PROMO_PICTURE_DIR = 'promo';
00014
00020 private static $_instance = null;
00021
00027 protected $_iMaxPicImgCount = 12;
00028
00034 protected $_iMaxZoomImgCount = 12;
00035
00041 protected $_aTypeToPath = array( 'TC' => 'master/category/thumb',
00042 'CICO' => 'master/category/icon',
00043 'PICO' => 'master/category/promo_icon',
00044 'MICO' => 'master/manufacturer/icon',
00045 'VICO' => 'master/vendor/icon',
00046 'PROMO'=> self::PROMO_PICTURE_DIR,
00047 'ICO' => 'master/product/icon',
00048 'TH' => 'master/product/thumb',
00049 'M1' => 'master/product/1',
00050 'M2' => 'master/product/2',
00051 'M3' => 'master/product/3',
00052 'M4' => 'master/product/4',
00053 'M5' => 'master/product/5',
00054 'M6' => 'master/product/6',
00055 'M7' => 'master/product/7',
00056 'M8' => 'master/product/8',
00057 'M9' => 'master/product/9',
00058 'M10' => 'master/product/10',
00059 'M11' => 'master/product/11',
00060 'M12' => 'master/product/12',
00061
00062 'P1' => '1',
00063 'P2' => '2',
00064 'P3' => '3',
00065 'P4' => '4',
00066 'P5' => '5',
00067 'P6' => '6',
00068 'P7' => '7',
00069 'P8' => '8',
00070 'P9' => '9',
00071 'P10' => '10',
00072 'P11' => '11',
00073 'P12' => '12',
00074 'Z1' => 'z1',
00075 'Z2' => 'z2',
00076 'Z3' => 'z3',
00077 'Z4' => 'z4',
00078 'Z5' => 'z5',
00079 'Z6' => 'z6',
00080 'Z7' => 'z7',
00081 'Z8' => 'z8',
00082 'Z9' => 'z9',
00083 'Z10' => 'z10',
00084 'Z11' => 'z11',
00085 'Z12' => 'z12',
00086
00087 'WP' => 'master/wrapping',
00088 'FL' => 'media',
00089 );
00090
00096 protected $_aBadFiles = array( 'php', 'php3', 'php4', 'php5', 'phps', 'php6', 'jsp', 'cgi', 'cmf', 'exe' );
00097
00103 protected $_aAllowedFiles = array( 'gif', 'jpg', 'jpeg', 'png', 'pdf' );
00109 public static function getInstance()
00110 {
00111
00112 if ( defined( 'OXID_PHP_UNIT' ) ) {
00113 self::$_instance = modInstances::getMod( __CLASS__ );
00114 }
00115
00116 if ( !self::$_instance instanceof oxUtilsFile ) {
00117
00118 self::$_instance = oxNew( 'oxUtilsFile' );
00119 if ( defined( 'OXID_PHP_UNIT' ) ) {
00120 modInstances::addMod( __CLASS__, self::$_instance);
00121 }
00122 }
00123 return self::$_instance;
00124 }
00125
00131 public function __construct()
00132 {
00133 $myConfig = $this->getConfig();
00134
00135 if ( $iPicCount = $myConfig->getConfigParam( 'iPicCount' ) ) {
00136 $this->_iMaxPicImgCount = $iPicCount;
00137 }
00138
00139 $this->_iMaxZoomImgCount = $this->_iMaxPicImgCount;
00140 }
00141
00149 public function normalizeDir( $sDir )
00150 {
00151 if ( isset($sDir) && $sDir != "" && substr($sDir, -1) !== '/' ) {
00152 $sDir .= "/";
00153 }
00154
00155 return $sDir;
00156 }
00157
00166 public function copyDir( $sSourceDir, $sTargetDir )
00167 {
00168 $oStr = getStr();
00169 $handle = opendir( $sSourceDir );
00170 while ( false !== ( $file = readdir( $handle ) ) ) {
00171 if ( $file != '.' && $file != '..' ) {
00172 if ( is_dir( $sSourceDir.'/'.$file ) ) {
00173
00174
00175 $sNewSourceDir = $sSourceDir.'/'.$file;
00176 $sNewTargetDir = $sTargetDir.'/'.$file;
00177 if ( strcasecmp( $file, 'CVS' ) && strcasecmp( $file, '.svn' )) {
00178 @mkdir( $sNewTargetDir, 0777 );
00179 $this->copyDir( $sNewSourceDir, $sNewTargetDir );
00180 }
00181 } else {
00182 $sSourceFile = $sSourceDir.'/'.$file;
00183 $sTargetFile = $sTargetDir.'/'.$file;
00184
00185
00186 if ( !$oStr->strstr( $sSourceDir, 'dyn_images' ) || $file == 'nopic.jpg' || $file == 'nopic_ico.jpg' ) {
00187 @copy( $sSourceFile, $sTargetFile );
00188 }
00189 }
00190 }
00191 }
00192 closedir($handle);
00193 }
00194
00202 public function deleteDir( $sSourceDir )
00203 {
00204 if ( is_dir( $sSourceDir ) ) {
00205 if ( $oDir = dir( $sSourceDir ) ) {
00206
00207 while ( false !== $sFile = $oDir->read() ) {
00208 if ( $sFile == '.' || $sFile == '..' ) {
00209 continue;
00210 }
00211
00212 if ( !$this->deleteDir( $oDir->path . DIRECTORY_SEPARATOR . $sFile ) ) {
00213 $oDir->close();
00214 return false;
00215 }
00216 }
00217
00218 $oDir->close();
00219 return rmdir( $sSourceDir );
00220 }
00221 } elseif ( file_exists( $sSourceDir ) ) {
00222 return unlink ( $sSourceDir );
00223 }
00224 }
00225
00233 public function readRemoteFileAsString( $sPath )
00234 {
00235 $sRet = '';
00236 $hFile = @fopen( $sPath, 'r' );
00237 if ( $hFile ) {
00238 socket_set_timeout( $hFile, 2 );
00239 while ( !feof( $hFile ) ) {
00240 $sLine = fgets( $hFile, 4096 );
00241 $sRet .= $sLine;
00242 }
00243 fclose( $hFile );
00244 }
00245
00246 return $sRet;
00247 }
00248
00260 protected function _prepareImageName( $sValue, $sType, $blDemo, $sImagePath, $blUnique = true )
00261 {
00262 if ( $sValue ) {
00263
00264 $aFilename = explode( ".", $sValue );
00265
00266 $sFileType = trim( $aFilename[count( $aFilename )-1] );
00267
00268 if ( isset( $sFileType ) ) {
00269
00270 $oStr = getStr();
00271
00272
00273 if ( in_array( $sFileType, $this->_aBadFiles ) || ( $blDemo && !in_array( $sFileType, $this->_aAllowedFiles ) ) ) {
00274 oxUtils::getInstance()->showMessageAndExit( "We don't play this game, go away" );
00275 }
00276
00277
00278 if ( count( $aFilename ) > 0 ) {
00279 unset( $aFilename[count( $aFilename )-1] );
00280 }
00281
00282 $sFName = '';
00283 if ( isset( $aFilename[0] ) ) {
00284 $sFName = $oStr->preg_replace( '/[^a-zA-Z0-9()_\.-]/', '', implode( '.', $aFilename ) );
00285 }
00286
00287 $sValue = $this->_getUniqueFileName( $sImagePath, "{$sFName}", $sFileType, "", $blUnique );
00288 }
00289 }
00290 return $sValue;
00291 }
00292
00300 protected function _getImagePath( $sType )
00301 {
00302 $sFolder = array_key_exists( $sType, $this->_aTypeToPath ) ? $this->_aTypeToPath[ $sType ] : '0';
00303 $sPath = $this->normalizeDir( $this->getConfig()->getPictureDir(false) ) . "{$sFolder}/";
00304 return $sPath;
00305 }
00306
00317 protected function _getImageSize( $sImgType, $iImgNum, $sImgConf )
00318 {
00319 $myConfig = $this->getConfig();
00320 $sSize = false;
00321
00322 switch ( $sImgConf ) {
00323 case 'aDetailImageSizes':
00324 $aDetailImageSizes = $myConfig->getConfigParam( $sImgConf );
00325 $sSize = $myConfig->getConfigParam( 'sDetailImageSize' );
00326 if ( isset( $aDetailImageSizes['oxpic'.$iImgNum] ) ) {
00327 $sSize = $aDetailImageSizes['oxpic'.$iImgNum];
00328 }
00329 break;
00330 default:
00331 $sSize = $myConfig->getConfigParam( $sImgConf );
00332 break;
00333 }
00334 if ( $sSize ) {
00335 return explode( '*', $sSize );
00336 }
00337 }
00338
00347 protected function _copyFile( $sSource, $sTarget )
00348 {
00349 $blDone = false;
00350
00351 if ( $sSource === $sTarget ) {
00352 $blDone = true;
00353 } else {
00354 $blDone = copy( $sSource, $sTarget );
00355 }
00356
00357 if ( $blDone ) {
00358 $blDone = @chmod( $sTarget, 0644 );
00359 }
00360
00361 return $blDone;
00362 }
00363
00372 protected function _moveImage( $sSource, $sTarget )
00373 {
00374 $blDone = false;
00375
00376 if ( $sSource === $sTarget ) {
00377 $blDone = true;
00378 } else {
00379 $blDone = move_uploaded_file( $sSource, $sTarget );
00380 }
00381
00382 if ( $blDone ) {
00383 $blDone = @chmod( $sTarget, 0644 );
00384 }
00385
00386 return $blDone;
00387 }
00388
00400 public function processFiles( $oObject = null, $aFiles = array(), $blUseMasterImage = false, $blUnique = true )
00401 {
00402 $aFiles = $aFiles ? $aFiles : $_FILES;
00403 if ( isset( $aFiles['myfile']['name'] ) ) {
00404
00405 $oConfig = $this->getConfig();
00406 $oStr = getStr();
00407
00408
00409 $blDemo = (bool) $oConfig->isDemoShop();
00410
00411
00412 $sTmpFolder = $oConfig->getConfigParam( "sCompileDir" );
00413
00414 $iNewImagesCounter = 0;
00415 $aSource = $aFiles['myfile']['tmp_name'];
00416 $aError = $aFiles['myfile']['error'];
00417 $sErrorsDescription = '';
00418
00419 $oEx = oxNew( "oxExceptionToDisplay" );
00420
00421 while ( list( $sKey, $sValue ) = each( $aFiles['myfile']['name'] ) ) {
00422
00423 $sSource = $aSource[$sKey];
00424 $iError = $aError[$sKey];
00425 $aFiletype = explode( "@", $sKey );
00426 $sKey = $aFiletype[1];
00427 $sType = $aFiletype[0];
00428
00429 $sValue = strtolower( $sValue );
00430 $sImagePath = $this->_getImagePath( $sType );
00431
00432
00433 if ( UPLOAD_ERR_OK !== $iError && UPLOAD_ERR_NO_FILE !== $iError ) {
00434 $sErrorsDescription = $this->translateError( $iError );
00435 $oEx->setMessage( $sErrorsDescription );
00436 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false );
00437 }
00438
00439
00440 if ( $sSource && ( $sValue = $this->_prepareImageName( $sValue, $sType, $blDemo, $sImagePath, $blUnique ) ) ) {
00441
00442
00443 $sProcessPath = $sTmpFolder . basename( $sSource );
00444
00445 if ( $sProcessPath ) {
00446
00447 if ( $blUseMasterImage ) {
00448
00449 $blMoved = $this->_copyFile( $sSource, $sImagePath . $sValue );
00450 } else {
00451 $blMoved = $this->_moveImage( $sSource, $sImagePath . $sValue );
00452 }
00453
00454 if ( $blMoved ) {
00455
00456 $iNewImagesCounter++;
00457
00458 if ( $oObject && isset( $oObject->$sKey ) ) {
00459 $oObject->{$sKey}->setValue( $sValue );
00460
00461
00462 $oObject->iNewImagesCounter = $iNewImagesCounter;
00463 }
00464 }
00465 }
00466 }
00467 }
00468 }
00469
00470 return $oObject;
00471 }
00472
00481 function checkFile( $sFile )
00482 {
00483 $aCheckCache = oxSession::getVar("checkcache");
00484
00485 if ( isset( $aCheckCache[$sFile] ) ) {
00486 return $aCheckCache[$sFile];
00487 }
00488
00489 $blRet = false;
00490
00491 if (is_readable( $sFile)) {
00492 $blRet = true;
00493 } else {
00494
00495 $blRet = $this->urlValidate( $sFile );
00496 }
00497
00498 $aCheckCache[$sFile] = $blRet;
00499 oxSession::setVar( "checkcache", $aCheckCache );
00500
00501 return $blRet;
00502 }
00503
00511 function urlValidate( $sLink )
00512 {
00513 $aUrlParts = @parse_url( $sLink );
00514 $sHost = ( isset( $aUrlParts["host"] ) && $aUrlParts["host"] ) ? $aUrlParts["host"] : null;
00515
00516 $blValid = false;
00517 if ( $sHost ) {
00518 $sDocumentPath = ( isset( $aUrlParts["path"] ) && $aUrlParts["path"] ) ? $aUrlParts["path"] : '/';
00519 $sDocumentPath .= ( isset( $aUrlParts["query"] ) && $aUrlParts["query"] ) ? '?' . $aUrlParts["query"] : '';
00520
00521 $sPort = ( isset( $aUrlParts["port"] ) && $aUrlParts["port"] ) ? $aUrlParts["port"] : '80';
00522
00523
00524 if ( ( $oConn = @fsockopen( $sHost, $sPort, $iErrNo, $sErrStr, 30 ) ) ) {
00525 fwrite ( $oConn, "HEAD {$sDocumentPath} HTTP/1.0\r\nHost: {$sHost}\r\n\r\n" );
00526 $sResponse = fgets( $oConn, 22 );
00527 fclose( $oConn );
00528
00529 if ( preg_match( "/200 OK/", $sResponse ) ) {
00530 $blValid = true;
00531 }
00532 }
00533 }
00534
00535 return $blValid;
00536 }
00537
00550 public function handleUploadedFile($aFileInfo, $sUploadPath)
00551 {
00552 $sBasePath = $this->getConfig()->getConfigParam('sShopDir');
00553
00554
00555 if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00556 throw oxNew( "oxException", 'EXCEPTION_NOFILE' );
00557 }
00558
00559
00560 if ( !getStr()->preg_match('/^[\-_a-z0-9\.]+$/i', $aFileInfo['name'] ) ) {
00561 throw oxNew( "oxException", 'EXCEPTION_FILENAMEINVALIDCHARS' );
00562 }
00563
00564
00565 if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
00566 throw oxNew( "oxException", 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
00567 }
00568
00569 $aPathInfo = pathinfo($aFileInfo['name']);
00570
00571 $sExt = $aPathInfo['extension'];
00572 $sFileName = $aPathInfo['filename'];
00573
00574 $aAllowedUploadTypes = (array) $this->getConfig()->getConfigParam( 'aAllowedUploadTypes' );
00575 $aAllowedUploadTypes = array_map( "strtolower", $aAllowedUploadTypes );
00576 if ( !in_array( strtolower( $sExt ), $aAllowedUploadTypes ) ) {
00577 throw oxNew( "oxException", 'EXCEPTION_NOTALLOWEDTYPE' );
00578 }
00579
00580 $sFileName = $this->_getUniqueFileName( $sBasePath . $sUploadPath, $sFileName, $sExt );
00581 $this->_moveImage( $aFileInfo['tmp_name'], $sBasePath . $sUploadPath . "/" . $sFileName );
00582
00583 $sUrl = $this->getConfig()->getShopUrl() . $sUploadPath . "/" . $sFileName;
00584
00585
00586 $sUrl = str_replace('//', '/', $sUrl);
00587 $sUrl = str_replace(array('http:/', 'https:/'), array('http://', 'https://'), $sUrl);
00588
00589 return $sUrl;
00590 }
00591
00602 public function processFile( $sFileName, $sUploadPath )
00603 {
00604 $aFileInfo = $_FILES[$sFileName];
00605
00606 $sBasePath = $this->getConfig()->getConfigParam('sShopDir');
00607
00608
00609 if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00610 throw oxNew( "oxException", 'EXCEPTION_NOFILE' );
00611 }
00612
00613
00614 if ( !getStr()->preg_match('/^[\-_a-z0-9\.]+$/i', $aFileInfo['name'] ) ) {
00615 throw oxNew( "oxException", 'EXCEPTION_FILENAMEINVALIDCHARS' );
00616 }
00617
00618
00619 if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
00620 throw oxNew( "oxException", 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
00621 }
00622
00623 $aPathInfo = pathinfo($aFileInfo['name']);
00624
00625 $sExt = $aPathInfo['extension'];
00626 $sFileName = $aPathInfo['filename'];
00627
00628 $aAllowedUploadTypes = (array) $this->getConfig()->getConfigParam( 'aAllowedUploadTypes' );
00629 $aAllowedUploadTypes = array_map( "strtolower", $aAllowedUploadTypes );
00630
00631 if ( !in_array( strtolower( $sExt ), $aAllowedUploadTypes ) ) {
00632 throw oxNew( "oxException", 'EXCEPTION_NOTALLOWEDTYPE' );
00633 }
00634
00635 $sFileName = $this->_getUniqueFileName( $sBasePath . $sUploadPath, $sFileName, $sExt );
00636
00637 if ( $this->_moveImage( $aFileInfo['tmp_name'], $sBasePath . $sUploadPath . "/" . $sFileName ) ) {
00638 return $sFileName ;
00639 } else {
00640 return false;
00641 }
00642 }
00643
00656 protected function _getUniqueFileName( $sFilePath, $sFileName, $sFileExt, $sSufix = "", $blUnique = true )
00657 {
00658 $sFilePath = $this->normalizeDir( $sFilePath );
00659 $iFileCounter = 0;
00660 $sTempFileName = $sFileName;
00661 $oStr = getStr();
00662
00663
00664 while ( $blUnique && file_exists( $sFilePath . "/" . $sFileName . $sSufix . "." . $sFileExt ) ) {
00665 $iFileCounter++;
00666
00667
00668 $sTempFileName = $oStr->preg_replace("/\(".$iFileCounter."\)/", "", $sTempFileName );
00669
00670 $sFileName = $sTempFileName . "($iFileCounter)";
00671 }
00672
00673 return $sFileName . $sSufix . "." . $sFileExt;
00674 }
00675
00683 public function getImageDirByType( $sType )
00684 {
00685 $sFolder = array_key_exists( $sType, $this->_aTypeToPath ) ? $this->_aTypeToPath[ $sType ] : '0';
00686 return $this->normalizeDir( $sFolder );
00687 }
00688
00696 function translateError( $iError )
00697 {
00698 $message = '';
00699
00700 if ( $iError > 0 && $iError < 9 && 5 !== $iError ) {
00701 $message = 'EXCEPTION_FILEUPLOADERROR_'.( (int) $iError );
00702 }
00703
00704 return $message;
00705 }
00706 }