00001 <?php
00002
00006 class oxUtilsFile extends oxSuperCfg
00007 {
00013 private static $_instance = null;
00014
00020 protected $_iMaxPicImgCount = 12;
00021
00027 protected $_iMaxZoomImgCount = 12;
00028
00034 protected $_aTypeToPath = array( 'ICO' => 'icon',
00035 'CICO' => 'icon',
00036 'TH' => '0',
00037 'TC' => '0',
00038 'P1' => '1',
00039 'P2' => '2',
00040 'P3' => '3',
00041 'P4' => '4',
00042 'P5' => '5',
00043 'P6' => '6',
00044 'P7' => '7',
00045 'P8' => '8',
00046 'P9' => '9',
00047 'P10' => '10',
00048 'P11' => '11',
00049 'P12' => '12',
00050 'Z1' => 'z1',
00051 'Z2' => 'z2',
00052 'Z3' => 'z3',
00053 'Z4' => 'z4',
00054 'Z5' => 'z5',
00055 'Z6' => 'z6',
00056 'Z7' => 'z7',
00057 'Z8' => 'z8',
00058 'Z9' => 'z9',
00059 'Z10' => 'z10',
00060 'Z11' => 'z11',
00061 'Z12' => 'z12'
00062 );
00063
00069 protected $_aBadFiles = array( 'php', 'jsp', 'cgi', 'cmf', 'exe' );
00070
00071
00077 protected $_aAllowedFiles = array( 'gif', 'jpg', 'png', 'pdf' );
00083 public static function getInstance()
00084 {
00085
00086 if ( defined( 'OXID_PHP_UNIT' ) ) {
00087 static $inst = array();
00088 self::$_instance = $inst[oxClassCacheKey()];
00089 }
00090
00091 if ( !self::$_instance instanceof oxUtilsFile ) {
00092
00093 self::$_instance = oxNew( 'oxUtilsFile' );
00094 if ( defined( 'OXID_PHP_UNIT' ) ) {
00095 $inst[oxClassCacheKey()] = self::$_instance;
00096 }
00097 }
00098 return self::$_instance;
00099 }
00100
00106 public function __construct()
00107 {
00108 $myConfig = $this->getConfig();
00109
00110 if ( $iPicCount = $myConfig->getConfigParam( 'iPicCount' ) ) {
00111 $this->_iMaxPicImgCount = $iPicCount;
00112 }
00113
00114 if ( $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' ) ) {
00115 $this->_iMaxZoomImgCount = $iZoomPicCount;
00116 }
00117 }
00118
00126 public function normalizeDir( $sDir )
00127 {
00128 if ( isset($sDir) && $sDir && substr($sDir, -1) !== '/' ) {
00129 $sDir .= "/";
00130 }
00131
00132 return $sDir;
00133 }
00134
00143 public function copyDir( $sSourceDir, $sTargetDir )
00144 {
00145 $oStr = getStr();
00146 $handle = opendir( $sSourceDir );
00147 while ( false !== ( $file = readdir( $handle ) ) ) {
00148 if ( $file != '.' && $file != '..' ) {
00149 if ( is_dir( $sSourceDir.'/'.$file ) ) {
00150
00151
00152 $sNewSourceDir = $sSourceDir.'/'.$file;
00153 $sNewTargetDir = $sTargetDir.'/'.$file;
00154 if ( strcasecmp( $file, 'CVS' ) && strcasecmp( $file, '.svn' )) {
00155 @mkdir( $sNewTargetDir, 0777 );
00156 $this->copyDir( $sNewSourceDir, $sNewTargetDir );
00157 }
00158 } else {
00159 $sSourceFile = $sSourceDir.'/'.$file;
00160 $sTargetFile = $sTargetDir.'/'.$file;
00161
00162
00163 if ( !$oStr->strstr( $sSourceDir, 'dyn_images' ) || $file == 'nopic.jpg' || $file == 'nopic_ico.jpg' ) {
00164 @copy( $sSourceFile, $sTargetFile );
00165 }
00166 }
00167 }
00168 }
00169 closedir($handle);
00170 }
00171
00179 public function deleteDir( $sSourceDir )
00180 {
00181 if ( is_dir( $sSourceDir ) ) {
00182 if ( $oDir = dir( $sSourceDir ) ) {
00183
00184 while ( false !== $sFile = $oDir->read() ) {
00185 if ( $sFile == '.' || $sFile == '..' ) {
00186 continue;
00187 }
00188
00189 if ( !$this->deleteDir( $oDir->path . DIRECTORY_SEPARATOR . $sFile ) ) {
00190 $oDir->close();
00191 return false;
00192 }
00193 }
00194
00195 $oDir->close();
00196 return rmdir( $sSourceDir );
00197 }
00198 } elseif ( file_exists( $sSourceDir ) ) {
00199 return unlink ( $sSourceDir );
00200 }
00201 }
00202
00210 public function readRemoteFileAsString( $sPath )
00211 {
00212 $sRet = '';
00213 $hFile = @fopen( $sPath, 'r' );
00214 if ( $hFile ) {
00215 socket_set_timeout( $hFile, 2 );
00216 while ( !feof( $hFile ) ) {
00217 $sLine = fgets( $hFile, 4096 );
00218 $sRet .= $sLine;
00219 }
00220 fclose( $hFile );
00221 }
00222
00223 return $sRet;
00224 }
00225
00236 protected function _prepareImageName( $sValue, $sType, $blDemo = false, $sImagePath )
00237 {
00238 if ( $sValue ) {
00239
00240 $aFilename = explode( ".", $sValue );
00241 $sFileType = trim( $aFilename[count( $aFilename )-1] );
00242
00243 if ( isset( $sFileType ) ) {
00244
00245
00246 if ( in_array( $sFileType, $this->_aBadFiles ) || ( $blDemo && !in_array( $sFileType, $this->_aAllowedFiles ) ) ) {
00247 oxUtils::getInstance()->showMessageAndExit( "We don't play this game, go away" );
00248 }
00249
00250
00251 if ( count( $aFilename ) > 0 ) {
00252 unset( $aFilename[count( $aFilename )-1] );
00253 }
00254
00255 $sFName = '';
00256 if ( isset( $aFilename[0] ) ) {
00257 $sFName = preg_replace( '/[^a-zA-Z0-9_\.-]/', '', implode( '.', $aFilename ) );
00258 }
00259
00260 $sValue = $this->_getUniqueFileName( $sImagePath, "{$sFName}_" . strtolower( $sType ), $sFileType );
00261 }
00262 }
00263 return $sValue;
00264 }
00265
00273 protected function _getImagePath( $sType )
00274 {
00275 $sFolder = array_key_exists( $sType, $this->_aTypeToPath ) ? $this->_aTypeToPath[ $sType ] : '0';
00276 return $this->getConfig()->getAbsDynImageDir() . "/{$sFolder}/";
00277 }
00278
00289 protected function _getImageSize( $sImgType, $iImgNum, $sImgConf )
00290 {
00291 $myConfig = $this->getConfig();
00292 $sSize = false;
00293
00294 switch ( $sImgConf ) {
00295 case 'aDetailImageSizes':
00296 $aDetailImageSizes = $myConfig->getConfigParam( $sImgConf );
00297 $sSize = $myConfig->getConfigParam( 'sDetailImageSize' );
00298 if ( isset( $aDetailImageSizes['oxpic'.$iImgNum] ) ) {
00299 $sSize = $aDetailImageSizes['oxpic'.$iImgNum];
00300 }
00301 break;
00302 case 'aZoomImageSizes':
00303 $aZoomImageSizes = $myConfig->getConfigParam( $sImgConf );
00304 $sSize = $myConfig->getConfigParam( 'sZoomImageSize' );
00305 if ( isset( $aZoomImageSizes['oxzoom'.intval( $iImgNum )] ) ) {
00306 $sSize = $aZoomImageSizes['oxzoom'.$iImgNum];
00307 }
00308 break;
00309 default:
00310 $sSize = $myConfig->getConfigParam( $sImgConf );
00311 }
00312 if ( $sSize ) {
00313 return explode( '*', $sSize );
00314 }
00315 }
00316
00327 protected function _prepareImage( $sType, $sSource, $sTarget )
00328 {
00329 $oUtilsPic = oxUtilspic::getInstance();
00330
00331
00332 $sPicType = preg_replace( "/\d*$/", "", $sType );
00333
00334
00335 $iPicNum = (int) preg_replace( "/^\D*/", "", $sType );
00336 $iPicNum = $iPicNum ? abs( $iPicNum ) : 1;
00337
00338 $aSize = false;
00339 $blResize = false;
00340
00341
00342 switch ( $sPicType ) {
00343 case 'TH':
00344 $aSize = $this->_getImageSize( $sType, $iPicNum, 'sThumbnailsize' );
00345 break;
00346 case 'TC':
00347 $aSize = $this->_getImageSize( $sType, $iPicNum, 'sCatThumbnailsize' );
00348 break;
00349 case 'CICO':
00350 case 'ICO':
00351 $aSize = $this->_getImageSize( $sType, $iPicNum, 'sIconsize' );
00352 break;
00353 case 'P':
00354
00355 $iPicNum = ( $iPicNum > $this->_iMaxPicImgCount ) ? $this->_iMaxPicImgCount : $iPicNum;
00356
00357
00358 if ( ( $aSize = $this->_getImageSize( $sType, $iPicNum, 'sIconsize' ) ) ) {
00359 $oUtilsPic->resizeImage( $sSource, $oUtilsPic->iconName( $sTarget ), $aSize[0], $aSize[1] );
00360 }
00361
00362 $aSize = $this->_getImageSize( $sType, $iPicNum, 'aDetailImageSizes' );
00363 break;
00364 case 'Z':
00365
00366 $iPicNum = ( $iPicNum > $this->_iMaxZoomImgCount ) ? $this->_iMaxZoomImgCount : $iPicNum;
00367
00368 $aSize = $this->_getImageSize( $sType, $iPicNum, 'aZoomImageSizes' );
00369 break;
00370 }
00371
00372 if ( $aSize ) {
00373 $blResize = $oUtilsPic->resizeImage( $sSource, $sTarget, $aSize[0], $aSize[1] );
00374 }
00375 return $blResize;
00376 }
00377
00386 protected function _moveImage( $sSource, $sTarget )
00387 {
00388 $blDone = false;
00389 if ( ( $blDone = move_uploaded_file( $sSource, $sTarget ) ) ) {
00390 $blDone = chmod( $sTarget, 0644 );
00391 }
00392
00393 return $blDone;
00394 }
00395
00403 protected function _removeTempImage( $sImagePath )
00404 {
00405 return unlink( $sImagePath );
00406 }
00407
00417 public function processFiles( $oObject = null, $aFiles = array() )
00418 {
00419 $aFiles = $aFiles ? $aFiles : $_FILES;
00420 if ( isset( $aFiles['myfile']['name'] ) ) {
00421
00422 $oConfig = $this->getConfig();
00423
00424
00425 $blDemo = (bool) $oConfig->isDemoShop();
00426
00427
00428 $sTmpFolder = $oConfig->getConfigParam( "sCompileDir" );
00429
00430
00431 while ( list( $sKey, $sValue ) = each( $aFiles['myfile']['name'] ) ) {
00432
00433 $aSource = $aFiles['myfile']['tmp_name'];
00434 $sSource = $aSource[$sKey];
00435 $aFiletype = explode( "@", $sKey );
00436 $sKey = $aFiletype[1];
00437 $sType = $aFiletype[0];
00438 $sValue = strtolower( $sValue );
00439 $sImagePath = $this->_getImagePath( $sType );
00440
00441
00442 if ( $sSource && ( $sValue = $this->_prepareImageName( $sValue, $sType, $blDemo, $sImagePath ) ) ) {
00443
00444
00445
00446 $sProcessPath = $sTmpFolder . basename( $sSource );
00447 if ( $sProcessPath && $this->_moveImage( $sSource, $sProcessPath ) ) {
00448
00449
00450 if ( ( $sTarget = $sImagePath . $sValue ) ) {
00451
00452
00453 $this->_prepareImage( $sType, $sProcessPath, $sTarget );
00454
00455
00456 if ( $oObject ) {
00457 $oObject->{$sKey}->setValue( $sValue );
00458 }
00459 }
00460
00461
00462 $this->_removeTempImage( $sProcessPath );
00463 }
00464 }
00465 }
00466 }
00467
00468 return $oObject;
00469 }
00470
00479 function checkFile( $sFile )
00480 {
00481 $aCheckCache = oxSession::getVar("checkcache");
00482
00483 if ( isset( $aCheckCache[$sFile] ) ) {
00484 return $aCheckCache[$sFile];
00485 }
00486
00487 $blRet = false;
00488
00489 if (is_readable( $sFile)) {
00490 $blRet = true;
00491 } else {
00492
00493 $blRet = $this->urlValidate( $sFile );
00494 }
00495
00496 $aCheckCache[$sFile] = $blRet;
00497 oxSession::setVar( "checkcache", $aCheckCache );
00498
00499 return $blRet;
00500 }
00501
00509 function urlValidate( $sLink )
00510 {
00511 $aUrlParts = @parse_url( $sLink );
00512 $sHost = ( isset( $aUrlParts["host"] ) && $aUrlParts["host"] ) ? $aUrlParts["host"] : null;
00513
00514 $blValid = false;
00515 if ( $sHost ) {
00516 $sDocumentPath = ( isset( $aUrlParts["path"] ) && $aUrlParts["path"] ) ? $aUrlParts["path"] : '/';
00517 $sDocumentPath .= ( isset( $aUrlParts["query"] ) && $aUrlParts["query"] ) ? '?' . $aUrlParts["query"] : '';
00518
00519 $sPort = ( isset( $aUrlParts["port"] ) && $aUrlParts["port"] ) ? $aUrlParts["port"] : '80';
00520
00521
00522 if ( ( $oConn = @fsockopen( $sHost, $sPort, $iErrNo, $sErrStr, 30 ) ) ) {
00523 fwrite ( $oConn, "HEAD {$sDocumentPath} HTTP/1.0\r\nHost: {$sHost}\r\n\r\n" );
00524 $sResponse = fgets( $oConn, 22 );
00525 fclose( $oConn );
00526
00527 if ( preg_match( "/200 OK/", $sResponse ) ) {
00528 $blValid = true;
00529 }
00530 }
00531 }
00532
00533 return $blValid;
00534 }
00535
00546 public function handleUploadedFile($aFileInfo, $sUploadPath)
00547 {
00548 $sBasePath = $this->getConfig()->getConfigParam('sShopDir');
00549
00550
00551 if ( !isset( $aFileInfo['name'] ) || !isset( $aFileInfo['tmp_name'] ) ) {
00552 throw new oxException( 'EXCEPTION_NOFILE' );
00553 }
00554
00555
00556 if ( !preg_match('/^[\-_a-z0-9\.]+$/i', $aFileInfo['name'] ) ) {
00557 throw new oxException( 'EXCEPTION_FILENAMEINVALIDCHARS' );
00558 }
00559
00560
00561 if ( isset( $aFileInfo['error'] ) && $aFileInfo['error'] ) {
00562 throw new oxException( 'EXCEPTION_FILEUPLOADERROR_'.( (int) $aFileInfo['error'] ) );
00563 }
00564
00565 $aPathInfo = pathinfo($aFileInfo['name']);
00566
00567 $sExt = $aPathInfo['extension'];
00568 $sFileName = $aPathInfo['filename'];
00569
00570 $aAllowedUploadTypes = (array) $this->getConfig()->getConfigParam( 'aAllowedUploadTypes' );
00571 $aAllowedUploadTypes = array_map( "strtolower", $aAllowedUploadTypes );
00572 if ( !in_array( strtolower( $sExt ), $aAllowedUploadTypes ) ) {
00573 throw new oxException( 'EXCEPTION_NOTALLOWEDTYPE' );
00574 }
00575
00576 $sFileName = $this->_getUniqueFileName( $sBasePath . "/" .$sUploadPath, $sFileName, $sExt );
00577 $this->_moveImage( $aFileInfo['tmp_name'], $sBasePath . "/" .$sUploadPath . "/" . $sFileName . "." . $sExt );
00578
00579 $sUrl = $this->getConfig()->getShopUrl() . "/" . $sUploadPath . "/" . $sFileName . "." . $sExt;
00580
00581
00582 $sUrl = str_replace('//', '/', $sUrl);
00583 $sUrl = str_replace('http:/', 'http://', $sUrl);
00584
00585 return $sUrl;
00586 }
00587
00598 protected function _getUniqueFileName( $sFilePath, $sFileName, $sFileExt )
00599 {
00600 $sFilePath = $this->normalizeDir( $sFilePath );
00601 $iFileCounter = 0;
00602 $sTempFileName = $sFileName;
00603
00604
00605 while ( file_exists( $sFilePath . "/" . $sFileName . "." . $sFileExt ) ) {
00606 $iFileCounter++;
00607 $sFileName = $sTempFileName . "($iFileCounter)";
00608 }
00609 return $sFileName . "." . $sFileExt;
00610 }
00611 }