oxutils.php

Go to the documentation of this file.
00001 <?php
00002 
00006 require_once getShopBasePath()."core/smarty/Smarty.class.php";
00007 
00012 class oxUtils extends oxSuperCfg
00013 {
00019     private static $_instance = null;
00020 
00026     protected $_iCurPrecision = null;
00027 
00033     protected $_sEmailTpl = "/^([-!#\$%&'*+.\/0-9=?A-Z^_`a-z{|}~\177])+@([-!#\$%&'*+\/0-9=?A-Z^_`a-z{|}~\177]+\\.)+[a-zA-Z]{2,6}\$/i";
00034 
00042     protected $_sPermanentCachePattern = "/c_fieldnames_/";
00043 
00049     protected $_aLockedFileHandles = array();
00050 
00056     protected $_aFileCacheContents = array();
00057 
00063     protected $_blIsSe = null;
00064 
00070     public static function getInstance()
00071     {
00072         // disable caching for test modules
00073         if ( defined( 'OXID_PHP_UNIT' ) ) {
00074             self::$_instance = modInstances::getMod( __CLASS__ );
00075         }
00076 
00077         if ( !(self::$_instance instanceof oxUtils) ) {
00078 
00079             self::$_instance = oxNew( 'oxUtils' );
00080 
00081             if ( defined( 'OXID_PHP_UNIT' ) ) {
00082                 modInstances::addMod( __CLASS__, self::$_instance);
00083             }
00084         }
00085         return self::$_instance;
00086     }
00087 
00093     protected $_aStaticCache;
00094 
00100     protected $_blSeoIsActive = null;
00101 
00107     public function stripGpcMagicQuotes()
00108     {
00109         if (!get_magic_quotes_gpc()) {
00110             return;
00111         }
00112         $_REQUEST = self::_stripQuotes($_REQUEST);
00113         $_POST = self::_stripQuotes($_POST);
00114         $_GET = self::_stripQuotes($_GET);
00115         $_COOKIE = self::_stripQuotes($_COOKIE);
00116     }
00117 
00126     public function strMan( $sVal, $sKey = null )
00127     {
00128         $sKey = $sKey?$sKey:'oxid123456789';
00129         $sVal = "ox{$sVal}id";
00130 
00131         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00132         $sVal = $this->strRot13( $sVal );
00133         $sVal = $sVal ^ $sKey;
00134         $sVal = base64_encode( $sVal );
00135         $sVal = str_replace( "=", "!", $sVal );
00136 
00137         return "ox_$sVal";
00138     }
00139 
00148     public function strRem( $sVal, $sKey = null )
00149     {
00150         $sKey = $sKey?$sKey:'oxid123456789';
00151         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00152 
00153         $sVal = substr( $sVal, 3 );
00154         $sVal = str_replace( '!', '=', $sVal );
00155         $sVal = base64_decode( $sVal );
00156         $sVal = $sVal ^ $sKey;
00157         $sVal = $this->strRot13( $sVal );
00158 
00159         return substr( $sVal, 2, -2 );
00160     }
00161 
00169     public function getArrFldName( $sName)
00170     {
00171         return str_replace( ".", "__", $sName);
00172     }
00173 
00182     public function assignValuesFromText( $sIn, $dVat = null)
00183     {
00184         $aRet = array();
00185         $aPieces = explode( '@@', $sIn );
00186         while ( list( $sKey, $sVal ) = each( $aPieces ) ) {
00187             if ( $sVal ) {
00188                 $aName = explode( '__', $sVal );
00189                 if ( isset( $aName[0] ) && isset( $aName[1] ) ) {
00190                     $aRet[] = $this->_fillExplodeArray( $aName, $dVat );
00191                 }
00192             }
00193         }
00194         return $aRet;
00195     }
00196 
00204     public function assignValuesToText( $aIn)
00205     {
00206         $sRet = "";
00207         reset( $aIn );
00208         while (list($sKey, $sVal) = each($aIn)) {
00209             $sRet .= $sKey;
00210             $sRet .= "__";
00211             $sRet .= $sVal;
00212             $sRet .= "@@";
00213         }
00214         return $sRet;
00215     }
00216 
00227     public function formatCurrency( $dValue, $oActCur = null )
00228     {
00229         if (!$oActCur) {
00230             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00231         }
00232         $sFormated = number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand);
00233 
00234         return $sFormated;
00235     }
00236 
00244     public function currency2Float( $sValue)
00245     {
00246         $fRet = $sValue;
00247         $iPos = strrpos( $sValue, ".");
00248         if ($iPos && ((strlen($sValue)-1-$iPos) < 2+1)) {
00249             // replace decimal with ","
00250             $fRet = substr_replace( $fRet, ",", $iPos, 1);
00251         }
00252         // remove thousands
00253         $fRet = str_replace( array(" ","."), "", $fRet);
00254 
00255         $fRet = str_replace( ",", ".", $fRet);
00256         return (float) $fRet;
00257     }
00258 
00266     public function isSearchEngine( $sClient = null )
00267     {
00268 
00269         if (!is_null($this->_blIsSe)) {
00270             return $this->_blIsSe;
00271         }
00272 
00273         startProfile("isSearchEngine");
00274 
00275         $myConfig = $this->getConfig();
00276         $blIsSe   = false;
00277 
00278         if ( !( $myConfig->getConfigParam( 'iDebug' ) && $this->isAdmin() ) ) {
00279 
00280             // caching
00281             $blIsSe = $myConfig->getGlobalParameter( 'blIsSearchEngine' );
00282             if ( !isset( $blIsSe ) ) {
00283 
00284                 $aRobots = $myConfig->getConfigParam( 'aRobots' );
00285                 $aRobots = is_array( $aRobots )?$aRobots:array();
00286 
00287                 $aRobotsExcept = $myConfig->getConfigParam( 'aRobotsExcept' );
00288                 $aRobotsExcept = is_array( $aRobotsExcept )?$aRobotsExcept:array();
00289 
00290                 $sClient = $sClient?$sClient:strtolower( getenv( 'HTTP_USER_AGENT' ) );
00291                 $blIsSe  = false;
00292                 $aRobots = array_merge( $aRobots, $aRobotsExcept );
00293                 foreach ( $aRobots as $sRobot ) {
00294                     if ( strpos( $sClient, $sRobot ) !== false ) {
00295                         $blIsSe = true;
00296                         break;
00297                     }
00298                 }
00299                 $myConfig->setGlobalParameter( 'blIsSearchEngine', $blIsSe );
00300             }
00301         }
00302 
00303         stopProfile("isSearchEngine");
00304 
00305         $this->_blIsSe = $blIsSe;
00306 
00307         return $blIsSe;
00308     }
00309 
00318     public function isValidEmail( $sEmail )
00319     {
00320         $blValid = true;
00321         if ( $sEmail != 'admin' ) {
00322             $blValid = ( getStr()->preg_match( $this->_sEmailTpl, $sEmail ) != 0 );
00323         }
00324 
00325         return $blValid;
00326     }
00327 
00333     public function rebuildCache()
00334     {
00335         // not needed from 3.0 on and unused <- MK: not correct, its used for example in shop_config.php, oxbase.php
00336 
00337         //$smarty  = & oxUtils::getInstance()->getSmarty();
00338         //$smarty->clear_all_cache();
00339 
00340         if ( function_exists( "UserdefinedRebuildCache")) {
00341             UserdefinedRebuildCache();
00342         }
00343     }
00344 
00352     public function loadAdminProfile($aInterfaceProfiles)
00353     {
00354         // improved #533
00355         // checking for available profiles list
00356         $aInterfaceProfiles = $aInterfaceProfiles;
00357         if ( is_array( $aInterfaceProfiles ) ) {
00358             //checking for previous profiles
00359             $sPrevProfile = oxUtilsServer::getInstance()->getOxCookie('oxidadminprofile');
00360             if (isset($sPrevProfile)) {
00361                 $aPrevProfile = @explode("@", trim($sPrevProfile));
00362             }
00363 
00364             //array to store profiles
00365             $aProfiles = array();
00366             foreach ( $aInterfaceProfiles as $iPos => $sProfile) {
00367                 $aProfileSettings = array($iPos, $sProfile);
00368                 $aProfiles[] = $aProfileSettings;
00369             }
00370             // setting previous used profile as active
00371             if (isset($aPrevProfile[0]) && isset($aProfiles[$aPrevProfile[0]])) {
00372                 $aProfiles[$aPrevProfile[0]][2] = 1;
00373             }
00374 
00375             oxSession::setVar("aAdminProfiles", $aProfiles);
00376             return $aProfiles;
00377         }
00378         return null;
00379     }
00380 
00389     public function fRound($sVal, $oCur = null)
00390     {
00391         startProfile('fround');
00392 
00393         //cached currency precision, this saves about 1% of execution time
00394         $iCurPrecision = null;
00395         if (! defined('OXID_PHP_UNIT')) {
00396             $iCurPrecision = $this->_iCurPrecision;
00397         }
00398 
00399         if (is_null($iCurPrecision)) {
00400             if ( !$oCur ) {
00401                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00402             }
00403 
00404             $iCurPrecision = $oCur->decimal;
00405             $this->_iCurPrecision = $iCurPrecision;
00406         }
00407 
00408         // this is a workaround for #36008 bug in php - incorrect round() & number_format() result (R)
00409         static $dprez = null;
00410         if (!$dprez) {
00411             $prez = @ini_get("precision");
00412             if (!$prez) {
00413                 $prez = 9;
00414             }
00415             $dprez = pow(10, -$prez);
00416         }
00417 
00418 
00419         stopProfile('fround');
00420 
00421         return round($sVal + $dprez, $iCurPrecision);
00422     }
00423 
00433     public function toStaticCache( $sName, $sContent, $sKey = null )
00434     {
00435         // if it's an array then we add
00436         if ( $sKey ) {
00437             $this->_aStaticCache[$sName][$sKey] = $sContent;
00438         } else {
00439             $this->_aStaticCache[$sName] = $sContent;
00440         }
00441     }
00442 
00450     public function fromStaticCache( $sName)
00451     {
00452         if ( isset( $this->_aStaticCache[$sName])) {
00453             return $this->_aStaticCache[$sName];
00454         }
00455         return null;
00456     }
00457 
00465     public function cleanStaticCache($sCacheName = null)
00466     {
00467         if ($sCacheName) {
00468             unset($this->_aStaticCache[$sCacheName]);
00469         } else {
00470             $this->_aStaticCache = null;
00471         }
00472     }
00473 
00483     public function toPhpFileCache( $sKey, $mContents )
00484     {
00485         //only simple arrays are supported
00486         if ( is_array( $mContents ) && ( $sCachePath = $this->getCacheFilePath( $sKey, false, 'php' ) ) ) {
00487 
00488             // setting meta
00489             $this->setCacheMeta( $sKey, array( "serialize" => false, "cachepath" => $sCachePath ) );
00490 
00491             // caching..
00492             $this->toFileCache( $sKey, $mContents );
00493         }
00494     }
00495 
00503     public function fromPhpFileCache( $sKey )
00504     {
00505         // setting meta
00506         $this->setCacheMeta( $sKey, array( "include" => true, "cachepath" => $this->getCacheFilePath( $sKey, false, 'php' ) ) );
00507         return $this->fromFileCache( $sKey );
00508     }
00509 
00517     public function getCacheMeta( $sKey )
00518     {
00519         return isset( $this->_aFileCacheMeta[$sKey] ) ? $this->_aFileCacheMeta[$sKey] : false;
00520     }
00521 
00530     public function setCacheMeta( $sKey, $aMeta )
00531     {
00532         // cache meta data
00533         $this->_aFileCacheMeta[$sKey] = $aMeta;
00534     }
00535 
00545     public function toFileCache( $sKey, $mContents )
00546     {
00547         $this->_aFileCacheContents[$sKey] = $mContents;
00548         $aMeta = $this->getCacheMeta( $sKey );
00549 
00550         // looking for cache meta
00551         $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00552         return ( bool ) $this->_lockFile( $sCachePath, $sKey );
00553     }
00554 
00562     public function fromFileCache( $sKey )
00563     {
00564         if ( !array_key_exists( $sKey, $this->_aFileCacheContents ) ) {
00565             $sRes = null;
00566 
00567             $aMeta = $this->getCacheMeta( $sKey );
00568             $blInclude  = isset( $aMeta["include"] ) ? $aMeta["include"] : false;
00569             $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00570 
00571             // trying to lock
00572             $this->_lockFile( $sCachePath, $sKey, LOCK_SH );
00573 
00574             clearstatcache();
00575             if ( is_readable( $sCachePath ) ) {
00576                 $sRes = $blInclude ? $this->_includeFile( $sCachePath ) : $this->_readFile( $sCachePath );
00577             }
00578 
00579             // release lock
00580             $this->_releaseFile( $sKey, LOCK_SH );
00581 
00582             // caching
00583             $this->_aFileCacheContents[$sKey] = $sRes;
00584         }
00585 
00586         return $this->_aFileCacheContents[$sKey];
00587     }
00588 
00596     protected function _readFile( $sFilePath )
00597     {
00598         $sRes = file_get_contents( $sFilePath );
00599         return $sRes ? unserialize( $sRes ) : null;
00600     }
00601 
00609     protected function _includeFile( $sFilePath )
00610     {
00611         $_aCacheContents = null;
00612         include $sFilePath;
00613         return $_aCacheContents;
00614     }
00615 
00624     protected function _processCache( $sKey, $mContents )
00625     {
00626         // looking for cache meta
00627         $aCacheMeta  = $this->getCacheMeta( $sKey );
00628         $blSerialize = isset( $aCacheMeta["serialize"] ) ? $aCacheMeta["serialize"] : true;
00629 
00630         if ( $blSerialize ) {
00631             $mContents = serialize( $mContents );
00632         } else {
00633             $mContents = "<?php\n//automatically generated file\n//" . date( "Y-m-d H:i:s" ) . "\n\n\$_aCacheContents = " . var_export( $mContents, true ) . "\n?>";
00634         }
00635 
00636         return $mContents;
00637     }
00638 
00645     public function commitFileCache()
00646     {
00647         if ( count( $this->_aLockedFileHandles[LOCK_EX] ) ) {
00648             startProfile("!__SAVING CACHE__! (warning)");
00649             foreach ( $this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle ) {
00650                 if ( $rHandle !== false && isset( $this->_aFileCacheContents[$sKey] ) ) {
00651 
00652                     // writing cache
00653                     fwrite( $rHandle, $this->_processCache( $sKey, $this->_aFileCacheContents[$sKey] ) );
00654 
00655                     // releasing locks
00656                     $this->_releaseFile( $sKey );
00657                 }
00658             }
00659 
00660             stopProfile("!__SAVING CACHE__! (warning)");
00661 
00662             //empty buffer
00663             $this->_aFileCacheContents = array();
00664         }
00665     }
00666 
00676     protected function _lockFile( $sFilePath, $sIdent, $iLockMode = LOCK_EX )
00677     {
00678         $rHandle = isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
00679         if ( $rHandle === null ) {
00680 
00681             $blLocked = false;
00682             $rHandle = @fopen( $sFilePath, "a+" );
00683 
00684             if ( $rHandle !== false ) {
00685 
00686                 if ( flock( $rHandle, $iLockMode | LOCK_NB ) ) {
00687                     if ( $iLockMode === LOCK_EX ) {
00688                         // truncate file
00689                         $blLocked = ftruncate( $rHandle, 0 );
00690                     } else {
00691                         // move to a start position
00692                         $blLocked = fseek( $rHandle, 0 ) === 0;
00693                     }
00694                 }
00695 
00696                 // on failure - closing and setting false..
00697                 if ( !$blLocked ) {
00698                     fclose( $rHandle );
00699                     $rHandle = false;
00700                 }
00701             }
00702 
00703             // in case system does not support file lockings
00704             if ( !$blLocked && $iLockMode === LOCK_EX ) {
00705 
00706                 // clearing on first call
00707                 if ( count( $this->_aLockedFileHandles ) == 0 ) {
00708                     clearstatcache();
00709                 }
00710 
00711                 // start a blank file to inform other processes we are dealing with it.
00712                 if (!( file_exists( $sFilePath ) && !filesize( $sFilePath ) && abs( time() - filectime( $sFilePath ) < 40 ) ) ) {
00713                     $rHandle = @fopen( $sFilePath, "w" );
00714                 }
00715             }
00716 
00717             $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
00718         }
00719 
00720         return $rHandle;
00721     }
00722 
00731     protected function _releaseFile( $sIdent, $iLockMode = LOCK_EX )
00732     {
00733         $blSuccess = true;
00734         if ( isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) &&
00735              $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false ) {
00736 
00737              // release the lock and close file
00738             $blSuccess = flock( $this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN ) &&
00739                          fclose( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00740             unset( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00741         }
00742 
00743         return $blSuccess;
00744     }
00745 
00753     public function oxResetFileCache()
00754     {
00755         $aPathes = glob( $this->getCacheFilePath( null, true ) . '*' );
00756         if ( is_array( $aPathes ) ) {
00757             // delete all the files, except cached tables fieldnames
00758             $aPathes = preg_grep( $this->_sPermanentCachePattern, $aPathes, PREG_GREP_INVERT );
00759             foreach ( $aPathes as $sFilename ) {
00760                 @unlink( $sFilename );
00761             }
00762         }
00763     }
00764 
00774     public function getRemoteCachePath($sRemote, $sLocal)
00775     {
00776         clearstatcache();
00777         if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
00778             return $sLocal;
00779         }
00780         $hRemote = @fopen( $sRemote, "rb");
00781         $blSuccess = false;
00782         if ( isset( $hRemote) && $hRemote ) {
00783             $hLocal = fopen( $sLocal, "wb");
00784             stream_copy_to_stream($hRemote, $hLocal);
00785             fclose($hRemote);
00786             fclose($hLocal);
00787             $blSuccess = true;
00788         } else {
00789             // try via fsockopen
00790             $aUrl = @parse_url( $sRemote);
00791             if ( !empty( $aUrl["host"])) {
00792                 $sPath = $aUrl["path"];
00793                 if ( empty( $sPath ) ) {
00794                     $sPath = "/";
00795                 }
00796                 $sHost = $aUrl["host"];
00797 
00798                 $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
00799                 if ( $hSocket) {
00800                     fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00801                     $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
00802                     if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
00803                         rewind($hLocal);
00804                         // does not copy all the data
00805                         // stream_copy_to_stream($hSocket, $hLocal);
00806                         fwrite ( $hLocal, stream_get_contents( $hSocket ) );
00807                         fclose( $hLocal );
00808                         fclose( $hSocket );
00809                         $blSuccess = true;
00810                     }
00811                 }
00812             }
00813         }
00814         if ( $blSuccess || file_exists( $sLocal ) ) {
00815             return $sLocal;
00816         }
00817         return false;
00818     }
00819 
00825     public function canPreview()
00826     {
00827         $blCan = null;
00828         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00829              ( $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' ) ) ) {
00830 
00831             $sTable = getViewName( 'oxuser' );
00832             $sQ = "select 1 from $sTable where MD5( CONCAT( ?, {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = ?";
00833             $blCan = (bool) oxDb::getDb()->getOne( $sQ, array( $sAdminSid, $sPrevId ) );
00834         }
00835 
00836         return $blCan;
00837     }
00838 
00844     public function getPreviewId()
00845     {
00846         $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' );
00847         if ( ( $oUser = $this->getUser() ) ) {
00848             return md5( $sAdminSid . $oUser->getId() . $oUser->oxuser__oxpassword->value . $oUser->oxuser__oxrights->value );
00849         }
00850     }
00851 
00857     public function checkAccessRights()
00858     {
00859         $myConfig  = $this->getConfig();
00860 
00861         $blIsAuth = false;
00862 
00863         $sUserID = oxSession::getVar( "auth");
00864 
00865         // deleting admin marker
00866         oxSession::setVar( "malladmin", 0);
00867         oxSession::setVar( "blIsAdmin", 0);
00868         oxSession::deleteVar( "blIsAdmin" );
00869         $myConfig->setConfigParam( 'blMallAdmin', false );
00870         //#1552T
00871         $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
00872 
00873         if ( $sUserID) {
00874             // escaping
00875             $oDb = oxDb::getDb();
00876             $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
00877 
00878             if ( $sRights != "user") {
00879                 // malladmin ?
00880                 if ( $sRights == "malladmin") {
00881                     oxSession::setVar( "malladmin", 1);
00882                     $myConfig->setConfigParam( 'blMallAdmin', true );
00883 
00884                     //#1552T
00885                     //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
00886                     $myConfig->setConfigParam( 'blAllowSharedEdit', true );
00887 
00888                     $sShop = oxSession::getVar( "actshop");
00889                     if ( !isset($sShop)) {
00890                         oxSession::setVar( "actshop", $myConfig->getBaseShopId());
00891                     }
00892                     $blIsAuth = true;
00893                 } else {
00894                     // Shopadmin... check if this shop is valid and exists
00895                     $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
00896                     if ( isset( $sShopID) && $sShopID) {
00897                         // success, this shop exists
00898 
00899                         oxSession::setVar( "actshop", $sRights);
00900                         oxSession::setVar( "currentadminshop", $sRights);
00901                         oxSession::setVar( "shp", $sRights);
00902 
00903                         // check if this subshop admin is evil.
00904                         if ('chshp' == oxConfig::getParameter( 'fnc' )) {
00905                             // dont allow this call
00906                             $blIsAuth = false;
00907                         } else {
00908                             $blIsAuth = true;
00909 
00910                             $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
00911                             foreach ($aShopIdVars as $sShopIdVar) {
00912                                 if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
00913                                     if ($sGotShop != $sRights) {
00914                                         $blIsAuth = false;
00915                                         break;
00916                                     }
00917                                 }
00918                             }
00919                         }
00920                     }
00921                 }
00922                 // marking user as admin
00923                 oxSession::setVar( "blIsAdmin", 1);
00924             }
00925         }
00926         return $blIsAuth;
00927     }
00928 
00938     public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
00939     {
00940         if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
00941             return $this->_blSeoIsActive;
00942         }
00943 
00944         $myConfig = $this->getConfig();
00945 
00946         if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
00947             $this->_blSeoIsActive = true;
00948 
00949             $aSeoModes  = $myConfig->getconfigParam( 'aSeoModes' );
00950             $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
00951             $iActLang   = $iActLang ? $iActLang : (int) oxLang::getInstance()->getBaseLanguage();
00952 
00953             // checking special config param for active shop and language
00954             if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
00955                 $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
00956             }
00957         }
00958 
00959         return $this->_blSeoIsActive;
00960     }
00961 
00971     public function getShopBit( $iShopId )
00972     {
00973         $iShopId = (int) $iShopId;
00974         //this works for large numbers when $sShopNr is up to (inclusive) 64
00975         $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00976 
00977         //as php ints supports only 32 bits, we return string.
00978         return $iRes;
00979     }
00980 
00990     public function bitwiseAnd( $iVal1, $iVal2 )
00991     {
00992         //this works for large numbers when $sShopNr is up to (inclusive) 64
00993         $iRes = oxDb::getDb()->getOne( "select ($iVal1 & $iVal2) as bitwiseAnd" );
00994 
00995         //as php ints supports only 32 bits, we return string.
00996         return $iRes;
00997     }
00998 
01008     public function bitwiseOr( $iVal1, $iVal2 )
01009     {
01010         //this works for large numbers when $sShopNr is up to (inclusive) 64
01011         $iRes = oxDb::getDb()->getOne( "select ($iVal1 | $iVal2) as bitwiseOr" );
01012 
01013         //as php ints supports only 32 bits, we return string.
01014         return $iRes;
01015     }
01016 
01024     public function isValidAlpha( $sField )
01025     {
01026         return (boolean) getStr()->preg_match( "#^[\w]*$#", $sField );
01027     }
01028 
01038     protected function _simpleRedirect( $sUrl, $sHeaderCode )
01039     {
01040         header( $sHeaderCode );
01041         header( "Location: $sUrl" );
01042         header( "Connection: close" );
01043     }
01044 
01054     public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 301 )
01055     {
01056         //preventing possible cyclic redirection
01057         //#M341 and check only if redirect paramater must be added
01058         if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
01059             return;
01060         }
01061 
01062         if ( $blAddRedirectParam ) {
01063             $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
01064         }
01065 
01066         $sUrl = str_ireplace( "&amp;", "&", $sUrl );
01067 
01068         $sHeaderCode = '';
01069         switch ($iHeaderCode) {
01070             case 301:
01071                 $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
01072                 break;
01073             case 302:
01074             default:
01075                 $sHeaderCode = "HTTP/1.1 302 Found";
01076         }
01077 
01078         $this->_simpleRedirect( $sUrl, $sHeaderCode );
01079 
01080         try {//may occur in case db is lost
01081             $this->getSession()->freeze();
01082         } catch( oxException $oEx ) {
01083             $oEx->debugOut();
01084             //do nothing else to make sure the redirect takes place
01085         }
01086 
01087         if ( defined( 'OXID_PHP_UNIT' ) ) {
01088             return;
01089         }
01090 
01091         $this->showMessageAndExit( '' );
01092     }
01093 
01101     public function showMessageAndExit( $sMsg )
01102     {
01103         $this->getSession()->freeze();
01104         $this->commitFileCache();
01105 
01106         if ( defined( 'OXID_PHP_UNIT' ) ) {
01107             return;
01108         }
01109 
01110         exit( $sMsg );
01111     }
01112 
01120     public function setHeader($sHeader)
01121     {
01122         header($sHeader);
01123     }
01124 
01133     protected function _addUrlParameters( $sUrl, $aParams )
01134     {
01135         $sDelim = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
01136         foreach ( $aParams as $sName => $sVal ) {
01137             $sUrl = $sUrl . $sDelim . $sName . '=' . $sVal;
01138             $sDelim = '&';
01139         }
01140 
01141         return $sUrl;
01142     }
01143 
01155     protected function _fillExplodeArray( $aName, $dVat = null)
01156     {
01157         $myConfig = $this->getConfig();
01158         $oObject = new OxstdClass();
01159         $aPrice = explode( '!P!', $aName[0]);
01160 
01161         if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
01162 
01163             // yes, price is there
01164             $oObject->price = $aPrice[1];
01165             $aName[0] = $aPrice[0];
01166 
01167             $iPercPos = getStr()->strpos( $oObject->price, '%' );
01168             if ( $iPercPos !== false ) {
01169                 $oObject->priceUnit = '%';
01170                 $oObject->fprice = $oObject->price;
01171                 $oObject->price  = substr( $oObject->price, 0, $iPercPos );
01172             } else {
01173                 $oCur = $myConfig->getActShopCurrencyObject();
01174                 $oObject->price = str_replace(',', '.', $oObject->price);
01175                 $oObject->fprice = oxLang::getInstance()->formatCurrency( $oObject->price  * $oCur->rate, $oCur);
01176                 $oObject->priceUnit = 'abs';
01177             }
01178 
01179             // add price info into list
01180             if ( !$this->isAdmin() && $oObject->price != 0 ) {
01181                 $aName[0] .= " ";
01182                 if ( $oObject->price > 0 ) {
01183                     $aName[0] .= "+";
01184                 }
01185                 //V FS#2616
01186                 if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
01187                     $oPrice = oxNew('oxPrice');
01188                     $oPrice->setPrice($oObject->price, $dVat);
01189                     $aName[0] .= oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() * $oCur->rate, $oCur);
01190                 } else {
01191                     $aName[0] .= $oObject->fprice;
01192                 }
01193                 if ( $oObject->priceUnit == 'abs' ) {
01194                     $aName[0] .= " ".$oCur->sign;
01195                 }
01196             }
01197         } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
01198             // A. removing unused part of information
01199             $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
01200         }
01201 
01202         $oObject->name  = $aName[0];
01203         $oObject->value = $aName[1];
01204         return $oObject;
01205     }
01206 
01214     public function oxMimeContentType( $sFileName )
01215     {
01216         $sFileName = strtolower( $sFileName );
01217         $iLastDot  = strrpos( $sFileName, '.' );
01218 
01219         if ( $iLastDot !== false ) {
01220             $sType = substr( $sFileName, $iLastDot + 1 );
01221             switch ( $sType ) {
01222                 case 'gif':
01223                     $sType = 'image/gif';
01224                     break;
01225                 case 'jpeg':
01226                 case 'jpg':
01227                     $sType = 'image/jpeg';
01228                     break;
01229                 case 'png':
01230                     $sType = 'image/png';
01231                     break;
01232                 default:
01233                     $sType = false;
01234                     break;
01235             }
01236         }
01237         return $sType;
01238     }
01239 
01248     public function logger( $sText, $blNewline = false )
01249     {   $myConfig = $this->getConfig();
01250 
01251         if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
01252             if ( gettype( $sText ) != 'string' ) {
01253                 $sText = var_export( $sText, true);
01254             }
01255             $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
01256             $this->writeToLog( $sLogMsg, "log.txt" );
01257         }
01258 
01259     }
01260 
01268     protected function _stripQuotes($mInput)
01269     {
01270         return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
01271     }
01272 
01280     public function strRot13( $sStr )
01281     {
01282         $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
01283         $sTo   = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
01284 
01285         return strtr( $sStr, $sFrom, $sTo );
01286     }
01287 
01288 
01299     public function prepareUrlForNoSession( $sUrl )
01300     {
01301         if ( $this->seoIsActive() ) {
01302             return $sUrl;
01303         }
01304 
01305         return oxUtilsUrl::getInstance()->prepareUrlForNoSession( $sUrl );
01306     }
01307 
01318     protected function _getCacheFilePath( $sCacheName, $blPathOnly = false )
01319     {
01320         return $this->getCacheFilePath( $sCacheName, $blPathOnly );
01321     }
01322 
01332     public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
01333     {
01334         $sVersionPrefix = "";
01335 
01336 
01337             $sVersionPrefix = 'pe';
01338 
01339         $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
01340 
01341         if (!$sPath) {
01342             return false;
01343         }
01344 
01345         return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
01346     }
01347 
01355     public function getLangCache( $sCacheName )
01356     {
01357         $aLangCache = null;
01358         $sFilePath = $this->getCacheFilePath( $sCacheName );
01359         if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
01360             include $sFilePath;
01361         }
01362         return $aLangCache;
01363     }
01364 
01373     public function setLangCache( $sCacheName, $aLangCache )
01374     {
01375         $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";";
01376         $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache);
01377         return $blRes;
01378     }
01379 
01387     public function checkUrlEndingSlash( $sUrl )
01388     {
01389         if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
01390             $sUrl .= '/';
01391         }
01392 
01393         return $sUrl;
01394     }
01395 
01404     public function writeToLog( $sLogMessage, $sLogFileName )
01405     {
01406         $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
01407         $blOk = false;
01408 
01409         if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
01410             fwrite( $oHandle, $sLogMessage );
01411             $blOk = fclose( $oHandle );
01412         }
01413 
01414         return $blOk;
01415     }
01416 
01424     public function handlePageNotFoundError($sUrl = '')
01425     {
01426         $this->setHeader("HTTP/1.0 404 Not Found");
01427         $sReturn = "Page not found.";
01428         try {
01429             $oView = oxNew('oxubase');
01430             $oView->init();
01431             $oView->render();
01432             $oView->addTplParam('sUrl', $sUrl);
01433             if ($sRet = oxUtilsView::getInstance()->getTemplateOutput('err_404.tpl', $oView)) {
01434                 $sReturn = $sRet;
01435             }
01436         } catch (Exception $e) {
01437         }
01438         $this->showMessageAndExit( $sReturn );
01439     }
01440 
01448     public function extractDomain( $sHost )
01449     {
01450         $oStr = getStr();
01451         if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
01452              ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
01453             $iLen = $oStr->strlen( $sHost );
01454             if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
01455                 $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
01456             }
01457         }
01458 
01459         return $sHost;
01460     }
01461 }