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 
00224     public function currency2Float( $sValue)
00225     {
00226         $fRet = $sValue;
00227         $iPos = strrpos( $sValue, ".");
00228         if ($iPos && ((strlen($sValue)-1-$iPos) < 2+1)) {
00229             // replace decimal with ","
00230             $fRet = substr_replace( $fRet, ",", $iPos, 1);
00231         }
00232         // remove thousands
00233         $fRet = str_replace( array(" ","."), "", $fRet);
00234 
00235         $fRet = str_replace( ",", ".", $fRet);
00236         return (float) $fRet;
00237     }
00238 
00246     public function isSearchEngine( $sClient = null )
00247     {
00248 
00249         if (!is_null($this->_blIsSe)) {
00250             return $this->_blIsSe;
00251         }
00252 
00253         startProfile("isSearchEngine");
00254 
00255         $myConfig = $this->getConfig();
00256         $blIsSe   = false;
00257 
00258         if ( !( $myConfig->getConfigParam( 'iDebug' ) && $this->isAdmin() ) ) {
00259 
00260             // caching
00261             $blIsSe = $myConfig->getGlobalParameter( 'blIsSearchEngine' );
00262             if ( !isset( $blIsSe ) ) {
00263 
00264                 $aRobots = $myConfig->getConfigParam( 'aRobots' );
00265                 $aRobots = is_array( $aRobots )?$aRobots:array();
00266 
00267                 $aRobotsExcept = $myConfig->getConfigParam( 'aRobotsExcept' );
00268                 $aRobotsExcept = is_array( $aRobotsExcept )?$aRobotsExcept:array();
00269 
00270                 $sClient = $sClient?$sClient:strtolower( getenv( 'HTTP_USER_AGENT' ) );
00271                 $blIsSe  = false;
00272                 $aRobots = array_merge( $aRobots, $aRobotsExcept );
00273                 foreach ( $aRobots as $sRobot ) {
00274                     if ( strpos( $sClient, $sRobot ) !== false ) {
00275                         $blIsSe = true;
00276                         break;
00277                     }
00278                 }
00279                 $myConfig->setGlobalParameter( 'blIsSearchEngine', $blIsSe );
00280             }
00281         }
00282 
00283         stopProfile("isSearchEngine");
00284 
00285         $this->_blIsSe = $blIsSe;
00286 
00287         return $blIsSe;
00288     }
00289 
00298     public function isValidEmail( $sEmail )
00299     {
00300         $blValid = true;
00301         if ( $sEmail != 'admin' ) {
00302             $blValid = ( getStr()->preg_match( $this->_sEmailTpl, $sEmail ) != 0 );
00303         }
00304 
00305         return $blValid;
00306     }
00307 
00313     public function rebuildCache()
00314     {
00315         // not needed from 3.0 on and unused <- MK: not correct, its used for example in shop_config.php, oxbase.php
00316 
00317         //$smarty  = & oxUtils::getInstance()->getSmarty();
00318         //$smarty->clear_all_cache();
00319 
00320         if ( function_exists( "UserdefinedRebuildCache")) {
00321             UserdefinedRebuildCache();
00322         }
00323     }
00324 
00332     public function loadAdminProfile($aInterfaceProfiles)
00333     {
00334         // improved #533
00335         // checking for available profiles list
00336         $aInterfaceProfiles = $aInterfaceProfiles;
00337         if ( is_array( $aInterfaceProfiles ) ) {
00338             //checking for previous profiles
00339             $sPrevProfile = oxUtilsServer::getInstance()->getOxCookie('oxidadminprofile');
00340             if (isset($sPrevProfile)) {
00341                 $aPrevProfile = @explode("@", trim($sPrevProfile));
00342             }
00343 
00344             //array to store profiles
00345             $aProfiles = array();
00346             foreach ( $aInterfaceProfiles as $iPos => $sProfile) {
00347                 $aProfileSettings = array($iPos, $sProfile);
00348                 $aProfiles[] = $aProfileSettings;
00349             }
00350             // setting previous used profile as active
00351             if (isset($aPrevProfile[0]) && isset($aProfiles[$aPrevProfile[0]])) {
00352                 $aProfiles[$aPrevProfile[0]][2] = 1;
00353             }
00354 
00355             oxSession::setVar("aAdminProfiles", $aProfiles);
00356             return $aProfiles;
00357         }
00358         return null;
00359     }
00360 
00369     public function fRound($sVal, $oCur = null)
00370     {
00371         startProfile('fround');
00372 
00373         //cached currency precision, this saves about 1% of execution time
00374         $iCurPrecision = null;
00375         if (! defined('OXID_PHP_UNIT')) {
00376             $iCurPrecision = $this->_iCurPrecision;
00377         }
00378 
00379         if (is_null($iCurPrecision)) {
00380             if ( !$oCur ) {
00381                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00382             }
00383 
00384             $iCurPrecision = $oCur->decimal;
00385             $this->_iCurPrecision = $iCurPrecision;
00386         }
00387 
00388         // this is a workaround for #36008 bug in php - incorrect round() & number_format() result (R)
00389         static $dprez = null;
00390         if (!$dprez) {
00391             $prez = @ini_get("precision");
00392             if (!$prez) {
00393                 $prez = 9;
00394             }
00395             $dprez = pow(10, -$prez);
00396         }
00397         stopProfile('fround');
00398 
00399         return round($sVal + $dprez * ( $sVal >= 0 ? 1 : -1 ), $iCurPrecision);
00400     }
00401 
00411     public function toStaticCache( $sName, $sContent, $sKey = null )
00412     {
00413         // if it's an array then we add
00414         if ( $sKey ) {
00415             $this->_aStaticCache[$sName][$sKey] = $sContent;
00416         } else {
00417             $this->_aStaticCache[$sName] = $sContent;
00418         }
00419     }
00420 
00428     public function fromStaticCache( $sName)
00429     {
00430         if ( isset( $this->_aStaticCache[$sName])) {
00431             return $this->_aStaticCache[$sName];
00432         }
00433         return null;
00434     }
00435 
00443     public function cleanStaticCache($sCacheName = null)
00444     {
00445         if ($sCacheName) {
00446             unset($this->_aStaticCache[$sCacheName]);
00447         } else {
00448             $this->_aStaticCache = null;
00449         }
00450     }
00451 
00461     public function toPhpFileCache( $sKey, $mContents )
00462     {
00463         //only simple arrays are supported
00464         if ( is_array( $mContents ) && ( $sCachePath = $this->getCacheFilePath( $sKey, false, 'php' ) ) ) {
00465 
00466             // setting meta
00467             $this->setCacheMeta( $sKey, array( "serialize" => false, "cachepath" => $sCachePath ) );
00468 
00469             // caching..
00470             $this->toFileCache( $sKey, $mContents );
00471         }
00472     }
00473 
00481     public function fromPhpFileCache( $sKey )
00482     {
00483         // setting meta
00484         $this->setCacheMeta( $sKey, array( "include" => true, "cachepath" => $this->getCacheFilePath( $sKey, false, 'php' ) ) );
00485         return $this->fromFileCache( $sKey );
00486     }
00487 
00495     public function getCacheMeta( $sKey )
00496     {
00497         return isset( $this->_aFileCacheMeta[$sKey] ) ? $this->_aFileCacheMeta[$sKey] : false;
00498     }
00499 
00508     public function setCacheMeta( $sKey, $aMeta )
00509     {
00510         // cache meta data
00511         $this->_aFileCacheMeta[$sKey] = $aMeta;
00512     }
00513 
00523     public function toFileCache( $sKey, $mContents )
00524     {
00525         $this->_aFileCacheContents[$sKey] = $mContents;
00526         $aMeta = $this->getCacheMeta( $sKey );
00527 
00528         // looking for cache meta
00529         $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00530         return ( bool ) $this->_lockFile( $sCachePath, $sKey );
00531     }
00532 
00540     public function fromFileCache( $sKey )
00541     {
00542         if ( !array_key_exists( $sKey, $this->_aFileCacheContents ) ) {
00543             $sRes = null;
00544 
00545             $aMeta = $this->getCacheMeta( $sKey );
00546             $blInclude  = isset( $aMeta["include"] ) ? $aMeta["include"] : false;
00547             $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00548 
00549             // trying to lock
00550             $this->_lockFile( $sCachePath, $sKey, LOCK_SH );
00551 
00552             clearstatcache();
00553             if ( is_readable( $sCachePath ) ) {
00554                 $sRes = $blInclude ? $this->_includeFile( $sCachePath ) : $this->_readFile( $sCachePath );
00555             }
00556 
00557             // release lock
00558             $this->_releaseFile( $sKey, LOCK_SH );
00559 
00560             // caching
00561             $this->_aFileCacheContents[$sKey] = $sRes;
00562         }
00563 
00564         return $this->_aFileCacheContents[$sKey];
00565     }
00566 
00574     protected function _readFile( $sFilePath )
00575     {
00576         $sRes = file_get_contents( $sFilePath );
00577         return $sRes ? unserialize( $sRes ) : null;
00578     }
00579 
00587     protected function _includeFile( $sFilePath )
00588     {
00589         $_aCacheContents = null;
00590         include $sFilePath;
00591         return $_aCacheContents;
00592     }
00593 
00602     protected function _processCache( $sKey, $mContents )
00603     {
00604         // looking for cache meta
00605         $aCacheMeta  = $this->getCacheMeta( $sKey );
00606         $blSerialize = isset( $aCacheMeta["serialize"] ) ? $aCacheMeta["serialize"] : true;
00607 
00608         if ( $blSerialize ) {
00609             $mContents = serialize( $mContents );
00610         } else {
00611             $mContents = "<?php\n//automatically generated file\n//" . date( "Y-m-d H:i:s" ) . "\n\n\$_aCacheContents = " . var_export( $mContents, true ) . "\n?>";
00612         }
00613 
00614         return $mContents;
00615     }
00616 
00623     public function commitFileCache()
00624     {
00625         if ( count( $this->_aLockedFileHandles[LOCK_EX] ) ) {
00626             startProfile("!__SAVING CACHE__! (warning)");
00627             foreach ( $this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle ) {
00628                 if ( $rHandle !== false && isset( $this->_aFileCacheContents[$sKey] ) ) {
00629 
00630                     // writing cache
00631                     fwrite( $rHandle, $this->_processCache( $sKey, $this->_aFileCacheContents[$sKey] ) );
00632 
00633                     // releasing locks
00634                     $this->_releaseFile( $sKey );
00635                 }
00636             }
00637 
00638             stopProfile("!__SAVING CACHE__! (warning)");
00639 
00640             //empty buffer
00641             $this->_aFileCacheContents = array();
00642         }
00643     }
00644 
00654     protected function _lockFile( $sFilePath, $sIdent, $iLockMode = LOCK_EX )
00655     {
00656         $rHandle = isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
00657         if ( $rHandle === null ) {
00658 
00659             $blLocked = false;
00660             $rHandle = @fopen( $sFilePath, "a+" );
00661 
00662             if ( $rHandle !== false ) {
00663 
00664                 if ( flock( $rHandle, $iLockMode | LOCK_NB ) ) {
00665                     if ( $iLockMode === LOCK_EX ) {
00666                         // truncate file
00667                         $blLocked = ftruncate( $rHandle, 0 );
00668                     } else {
00669                         // move to a start position
00670                         $blLocked = fseek( $rHandle, 0 ) === 0;
00671                     }
00672                 }
00673 
00674                 // on failure - closing and setting false..
00675                 if ( !$blLocked ) {
00676                     fclose( $rHandle );
00677                     $rHandle = false;
00678                 }
00679             }
00680 
00681             // in case system does not support file lockings
00682             if ( !$blLocked && $iLockMode === LOCK_EX ) {
00683 
00684                 // clearing on first call
00685                 if ( count( $this->_aLockedFileHandles ) == 0 ) {
00686                     clearstatcache();
00687                 }
00688 
00689                 // start a blank file to inform other processes we are dealing with it.
00690                 if (!( file_exists( $sFilePath ) && !filesize( $sFilePath ) && abs( time() - filectime( $sFilePath ) < 40 ) ) ) {
00691                     $rHandle = @fopen( $sFilePath, "w" );
00692                 }
00693             }
00694 
00695             $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
00696         }
00697 
00698         return $rHandle;
00699     }
00700 
00709     protected function _releaseFile( $sIdent, $iLockMode = LOCK_EX )
00710     {
00711         $blSuccess = true;
00712         if ( isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) &&
00713              $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false ) {
00714 
00715              // release the lock and close file
00716             $blSuccess = flock( $this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN ) &&
00717                          fclose( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00718             unset( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00719         }
00720 
00721         return $blSuccess;
00722     }
00723 
00731     public function oxResetFileCache()
00732     {
00733         $aPathes = glob( $this->getCacheFilePath( null, true ) . '*' );
00734         if ( is_array( $aPathes ) ) {
00735             // delete all the files, except cached tables fieldnames
00736             $aPathes = preg_grep( $this->_sPermanentCachePattern, $aPathes, PREG_GREP_INVERT );
00737             foreach ( $aPathes as $sFilename ) {
00738                 @unlink( $sFilename );
00739             }
00740         }
00741     }
00742 
00752     public function getRemoteCachePath($sRemote, $sLocal)
00753     {
00754         clearstatcache();
00755         if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
00756             return $sLocal;
00757         }
00758         $hRemote = @fopen( $sRemote, "rb");
00759         $blSuccess = false;
00760         if ( isset( $hRemote) && $hRemote ) {
00761             $hLocal = fopen( $sLocal, "wb");
00762             stream_copy_to_stream($hRemote, $hLocal);
00763             fclose($hRemote);
00764             fclose($hLocal);
00765             $blSuccess = true;
00766         } else {
00767             // try via fsockopen
00768             $aUrl = @parse_url( $sRemote);
00769             if ( !empty( $aUrl["host"])) {
00770                 $sPath = $aUrl["path"];
00771                 if ( empty( $sPath ) ) {
00772                     $sPath = "/";
00773                 }
00774                 $sHost = $aUrl["host"];
00775 
00776                 $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
00777                 if ( $hSocket) {
00778                     fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00779                     $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
00780                     if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
00781                         rewind($hLocal);
00782                         // does not copy all the data
00783                         // stream_copy_to_stream($hSocket, $hLocal);
00784                         fwrite ( $hLocal, stream_get_contents( $hSocket ) );
00785                         fclose( $hLocal );
00786                         fclose( $hSocket );
00787                         $blSuccess = true;
00788                     }
00789                 }
00790             }
00791         }
00792         if ( $blSuccess || file_exists( $sLocal ) ) {
00793             return $sLocal;
00794         }
00795         return false;
00796     }
00797 
00803     public function canPreview()
00804     {
00805         $blCan = null;
00806         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00807              ( $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' ) ) ) {
00808 
00809             $sTable = getViewName( 'oxuser' );
00810             $sQ = "select 1 from $sTable where MD5( CONCAT( ?, {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = ?";
00811             $blCan = (bool) oxDb::getDb()->getOne( $sQ, array( $sAdminSid, $sPrevId ) );
00812         }
00813 
00814         return $blCan;
00815     }
00816 
00822     public function getPreviewId()
00823     {
00824         $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' );
00825         if ( ( $oUser = $this->getUser() ) ) {
00826             return md5( $sAdminSid . $oUser->getId() . $oUser->oxuser__oxpassword->value . $oUser->oxuser__oxrights->value );
00827         }
00828     }
00829 
00835     public function checkAccessRights()
00836     {
00837         $myConfig  = $this->getConfig();
00838 
00839         $blIsAuth = false;
00840 
00841         $sUserID = oxSession::getVar( "auth");
00842 
00843         // deleting admin marker
00844         oxSession::setVar( "malladmin", 0);
00845         oxSession::setVar( "blIsAdmin", 0);
00846         oxSession::deleteVar( "blIsAdmin" );
00847         $myConfig->setConfigParam( 'blMallAdmin', false );
00848         //#1552T
00849         $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
00850 
00851         if ( $sUserID) {
00852             // escaping
00853             $oDb = oxDb::getDb();
00854             $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
00855 
00856             if ( $sRights != "user") {
00857                 // malladmin ?
00858                 if ( $sRights == "malladmin") {
00859                     oxSession::setVar( "malladmin", 1);
00860                     $myConfig->setConfigParam( 'blMallAdmin', true );
00861 
00862                     //#1552T
00863                     //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
00864                     $myConfig->setConfigParam( 'blAllowSharedEdit', true );
00865 
00866                     $sShop = oxSession::getVar( "actshop");
00867                     if ( !isset($sShop)) {
00868                         oxSession::setVar( "actshop", $myConfig->getBaseShopId());
00869                     }
00870                     $blIsAuth = true;
00871                 } else {
00872                     // Shopadmin... check if this shop is valid and exists
00873                     $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
00874                     if ( isset( $sShopID) && $sShopID) {
00875                         // success, this shop exists
00876 
00877                         oxSession::setVar( "actshop", $sRights);
00878                         oxSession::setVar( "currentadminshop", $sRights);
00879                         oxSession::setVar( "shp", $sRights);
00880 
00881                         // check if this subshop admin is evil.
00882                         if ('chshp' == oxConfig::getParameter( 'fnc' )) {
00883                             // dont allow this call
00884                             $blIsAuth = false;
00885                         } else {
00886                             $blIsAuth = true;
00887 
00888                             $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
00889                             foreach ($aShopIdVars as $sShopIdVar) {
00890                                 if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
00891                                     if ($sGotShop != $sRights) {
00892                                         $blIsAuth = false;
00893                                         break;
00894                                     }
00895                                 }
00896                             }
00897                         }
00898                     }
00899                 }
00900                 // marking user as admin
00901                 oxSession::setVar( "blIsAdmin", 1);
00902             }
00903         }
00904         return $blIsAuth;
00905     }
00906 
00916     public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
00917     {
00918         if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
00919             return $this->_blSeoIsActive;
00920         }
00921 
00922         $myConfig = $this->getConfig();
00923 
00924         if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
00925             $this->_blSeoIsActive = true;
00926 
00927             $aSeoModes  = $myConfig->getconfigParam( 'aSeoModes' );
00928             $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
00929             $iActLang   = $iActLang ? $iActLang : (int) oxLang::getInstance()->getBaseLanguage();
00930 
00931             // checking special config param for active shop and language
00932             if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
00933                 $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
00934             }
00935         }
00936 
00937         return $this->_blSeoIsActive;
00938     }
00939 
00949     public function getShopBit( $iShopId )
00950     {
00951         $iShopId = (int) $iShopId;
00952         //this works for large numbers when $sShopNr is up to (inclusive) 64
00953         $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00954 
00955         //as php ints supports only 32 bits, we return string.
00956         return $iRes;
00957     }
00958 
00968     public function bitwiseAnd( $iVal1, $iVal2 )
00969     {
00970         //this works for large numbers when $sShopNr is up to (inclusive) 64
00971         $iRes = oxDb::getDb()->getOne( "select ($iVal1 & $iVal2) as bitwiseAnd" );
00972 
00973         //as php ints supports only 32 bits, we return string.
00974         return $iRes;
00975     }
00976 
00986     public function bitwiseOr( $iVal1, $iVal2 )
00987     {
00988         //this works for large numbers when $sShopNr is up to (inclusive) 64
00989         $iRes = oxDb::getDb()->getOne( "select ($iVal1 | $iVal2) as bitwiseOr" );
00990 
00991         //as php ints supports only 32 bits, we return string.
00992         return $iRes;
00993     }
00994 
01002     public function isValidAlpha( $sField )
01003     {
01004         return (boolean) getStr()->preg_match( '/^[a-zA-Z0-9_]*$/', $sField );
01005     }
01006 
01016     protected function _simpleRedirect( $sUrl, $sHeaderCode )
01017     {
01018         header( $sHeaderCode );
01019         header( "Location: $sUrl" );
01020         header( "Connection: close" );
01021     }
01022 
01036     public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 301 )
01037     {
01038         //preventing possible cyclic redirection
01039         //#M341 and check only if redirect paramater must be added
01040         if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
01041             return;
01042         }
01043 
01044         if ( $blAddRedirectParam ) {
01045             $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
01046         }
01047 
01048         $sUrl = str_ireplace( "&amp;", "&", $sUrl );
01049 
01050         $sHeaderCode = '';
01051         switch ($iHeaderCode) {
01052             case 301:
01053                 $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
01054                 break;
01055             case 302:
01056             default:
01057                 $sHeaderCode = "HTTP/1.1 302 Found";
01058         }
01059 
01060         $this->_simpleRedirect( $sUrl, $sHeaderCode );
01061 
01062         try {//may occur in case db is lost
01063             $this->getSession()->freeze();
01064         } catch( oxException $oEx ) {
01065             $oEx->debugOut();
01066             //do nothing else to make sure the redirect takes place
01067         }
01068 
01069         if ( defined( 'OXID_PHP_UNIT' ) ) {
01070             return;
01071         }
01072 
01073         $this->showMessageAndExit( '' );
01074     }
01075 
01083     public function showMessageAndExit( $sMsg )
01084     {
01085         $this->getSession()->freeze();
01086         $this->commitFileCache();
01087 
01088         if ( defined( 'OXID_PHP_UNIT' ) ) {
01089             return;
01090         }
01091 
01092         exit( $sMsg );
01093     }
01094 
01102     public function setHeader($sHeader)
01103     {
01104         header($sHeader);
01105     }
01106 
01115     protected function _addUrlParameters( $sUrl, $aParams )
01116     {
01117         $sDelim = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
01118         foreach ( $aParams as $sName => $sVal ) {
01119             $sUrl = $sUrl . $sDelim . $sName . '=' . $sVal;
01120             $sDelim = '&';
01121         }
01122 
01123         return $sUrl;
01124     }
01125 
01137     protected function _fillExplodeArray( $aName, $dVat = null)
01138     {
01139         $myConfig = $this->getConfig();
01140         $oObject = new oxStdClass();
01141         $aPrice = explode( '!P!', $aName[0]);
01142 
01143         if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
01144 
01145             // yes, price is there
01146             $oObject->price = isset( $aPrice[1] ) ? $aPrice[1] : 0;
01147             $aName[0] = isset( $aPrice[0] ) ? $aPrice[0] : '';
01148 
01149             $iPercPos = getStr()->strpos( $oObject->price, '%' );
01150             if ( $iPercPos !== false ) {
01151                 $oObject->priceUnit = '%';
01152                 $oObject->fprice = $oObject->price;
01153                 $oObject->price  = substr( $oObject->price, 0, $iPercPos );
01154             } else {
01155                 $oCur = $myConfig->getActShopCurrencyObject();
01156                 $oObject->price = str_replace(',', '.', $oObject->price);
01157                 $oObject->fprice = oxLang::getInstance()->formatCurrency( $oObject->price  * $oCur->rate, $oCur);
01158                 $oObject->priceUnit = 'abs';
01159             }
01160 
01161             // add price info into list
01162             if ( !$this->isAdmin() && $oObject->price != 0 ) {
01163                 $aName[0] .= " ";
01164                 if ( $oObject->price > 0 ) {
01165                     $aName[0] .= "+";
01166                 }
01167                 //V FS#2616
01168                 if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
01169                     $oPrice = oxNew('oxPrice');
01170                     $oPrice->setPrice($oObject->price, $dVat);
01171                     $aName[0] .= oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() * $oCur->rate, $oCur);
01172                 } else {
01173                     $aName[0] .= $oObject->fprice;
01174                 }
01175                 if ( $oObject->priceUnit == 'abs' ) {
01176                     $aName[0] .= " ".$oCur->sign;
01177                 }
01178             }
01179         } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
01180             // A. removing unused part of information
01181             $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
01182         }
01183 
01184         $oObject->name  = $aName[0];
01185         $oObject->value = $aName[1];
01186         return $oObject;
01187     }
01188 
01196     public function oxMimeContentType( $sFileName )
01197     {
01198         $sFileName = strtolower( $sFileName );
01199         $iLastDot  = strrpos( $sFileName, '.' );
01200 
01201         if ( $iLastDot !== false ) {
01202             $sType = substr( $sFileName, $iLastDot + 1 );
01203             switch ( $sType ) {
01204                 case 'gif':
01205                     $sType = 'image/gif';
01206                     break;
01207                 case 'jpeg':
01208                 case 'jpg':
01209                     $sType = 'image/jpeg';
01210                     break;
01211                 case 'png':
01212                     $sType = 'image/png';
01213                     break;
01214                 default:
01215                     $sType = false;
01216                     break;
01217             }
01218         }
01219         return $sType;
01220     }
01221 
01230     public function logger( $sText, $blNewline = false )
01231     {   $myConfig = $this->getConfig();
01232 
01233         if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
01234             if ( gettype( $sText ) != 'string' ) {
01235                 $sText = var_export( $sText, true);
01236             }
01237             $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
01238             $this->writeToLog( $sLogMsg, "log.txt" );
01239         }
01240 
01241     }
01242 
01250     protected function _stripQuotes($mInput)
01251     {
01252         return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
01253     }
01254 
01262     public function strRot13( $sStr )
01263     {
01264         $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
01265         $sTo   = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
01266 
01267         return strtr( $sStr, $sFrom, $sTo );
01268     }
01269 
01279     public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
01280     {
01281         $sVersionPrefix = "";
01282 
01283 
01284             $sVersionPrefix = 'pe';
01285 
01286         $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
01287 
01288         if (!$sPath) {
01289             return false;
01290         }
01291 
01292         return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
01293     }
01294 
01302     public function getLangCache( $sCacheName )
01303     {
01304         $aLangCache = null;
01305         $sFilePath = $this->getCacheFilePath( $sCacheName );
01306         if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
01307             include $sFilePath;
01308         }
01309         return $aLangCache;
01310     }
01311 
01320     public function setLangCache( $sCacheName, $aLangCache )
01321     {
01322         $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";";
01323         $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache);
01324         return $blRes;
01325     }
01326 
01334     public function checkUrlEndingSlash( $sUrl )
01335     {
01336         if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
01337             $sUrl .= '/';
01338         }
01339 
01340         return $sUrl;
01341     }
01342 
01351     public function writeToLog( $sLogMessage, $sLogFileName )
01352     {
01353         $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
01354         $blOk = false;
01355 
01356         if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
01357             fwrite( $oHandle, $sLogMessage );
01358             $blOk = fclose( $oHandle );
01359         }
01360 
01361         return $blOk;
01362     }
01363 
01371     public function handlePageNotFoundError($sUrl = '')
01372     {
01373         $this->setHeader("HTTP/1.0 404 Not Found");
01374         if ( oxConfig::getInstance()->isUtf() ) {
01375             $this->setHeader("Content-Type: text/html; charset=UTF-8");
01376         }
01377 
01378         $sReturn = "Page not found.";
01379         try {
01380             $oView = oxNew('oxubase');
01381             $oView->init();
01382             $oView->render();
01383             $oView->addTplParam('sUrl', $sUrl);
01384             if ($sRet = oxUtilsView::getInstance()->getTemplateOutput('message/err_404.tpl', $oView)) {
01385                 $sReturn = $sRet;
01386             }
01387         } catch (Exception $e) {
01388         }
01389         $this->showMessageAndExit( $sReturn );
01390     }
01391 
01399     public function extractDomain( $sHost )
01400     {
01401         $oStr = getStr();
01402         if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
01403              ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
01404             $iLen = $oStr->strlen( $sHost );
01405             if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
01406                 $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
01407             }
01408         }
01409 
01410         return $sHost;
01411     }
01412 }