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 
00035     protected $_sPermanentCachePattern = "/c_fieldnames_/";
00036 
00042     protected $_aLockedFileHandles = array();
00043 
00049     protected $_aFileCacheContents = array();
00050 
00056     protected $_blIsSe = null;
00057 
00063     public static function getInstance()
00064     {
00065         // disable caching for test modules
00066         if ( defined( 'OXID_PHP_UNIT' ) ) {
00067             self::$_instance = modInstances::getMod( __CLASS__ );
00068         }
00069 
00070         if ( !(self::$_instance instanceof oxUtils) ) {
00071 
00072             self::$_instance = oxNew( 'oxUtils' );
00073 
00074             if ( defined( 'OXID_PHP_UNIT' ) ) {
00075                 modInstances::addMod( __CLASS__, self::$_instance);
00076             }
00077         }
00078         return self::$_instance;
00079     }
00080 
00086     protected $_aStaticCache;
00087 
00093     protected $_blSeoIsActive = null;
00094 
00100     public function stripGpcMagicQuotes()
00101     {
00102         if (!get_magic_quotes_gpc()) {
00103             return;
00104         }
00105         $_REQUEST = self::_stripQuotes($_REQUEST);
00106         $_POST = self::_stripQuotes($_POST);
00107         $_GET = self::_stripQuotes($_GET);
00108         $_COOKIE = self::_stripQuotes($_COOKIE);
00109     }
00110 
00119     public function strMan( $sVal, $sKey = null )
00120     {
00121         $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
00122         $sVal = "ox{$sVal}id";
00123 
00124         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00125         $sVal = $this->strRot13( $sVal );
00126         $sVal = $sVal ^ $sKey;
00127         $sVal = base64_encode ( $sVal );
00128         $sVal = str_replace( "=", "!", $sVal );
00129 
00130         return "ox_$sVal";
00131     }
00132 
00141     public function strRem( $sVal, $sKey = null )
00142     {
00143         $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
00144         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00145 
00146         $sVal = substr( $sVal, 3 );
00147         $sVal = str_replace( '!', '=', $sVal );
00148         $sVal = base64_decode( $sVal );
00149         $sVal = $sVal ^ $sKey;
00150         $sVal = $this->strRot13( $sVal );
00151 
00152         return substr( $sVal, 2, -2 );
00153     }
00154 
00162     public function getArrFldName( $sName)
00163     {
00164         return str_replace( ".", "__", $sName);
00165     }
00166 
00175     public function assignValuesFromText( $sIn, $dVat = null)
00176     {
00177         $aRet = array();
00178         $aPieces = explode( '@@', $sIn );
00179         while ( list( $sKey, $sVal ) = each( $aPieces ) ) {
00180             if ( $sVal ) {
00181                 $aName = explode( '__', $sVal );
00182                 if ( isset( $aName[0] ) && isset( $aName[1] ) ) {
00183                     $aRet[] = $this->_fillExplodeArray( $aName, $dVat );
00184                 }
00185             }
00186         }
00187         return $aRet;
00188     }
00189 
00197     public function assignValuesToText( $aIn)
00198     {
00199         $sRet = "";
00200         reset( $aIn );
00201         while (list($sKey, $sVal) = each($aIn)) {
00202             $sRet .= $sKey;
00203             $sRet .= "__";
00204             $sRet .= $sVal;
00205             $sRet .= "@@";
00206         }
00207         return $sRet;
00208     }
00209 
00217     public function currency2Float( $sValue)
00218     {
00219         $fRet = $sValue;
00220         $iPos = strrpos( $sValue, ".");
00221         if ($iPos && ((strlen($sValue)-1-$iPos) < 2+1)) {
00222             // replace decimal with ","
00223             $fRet = substr_replace( $fRet, ",", $iPos, 1);
00224         }
00225         // remove thousands
00226         $fRet = str_replace( array(" ","."), "", $fRet);
00227 
00228         $fRet = str_replace( ",", ".", $fRet);
00229         return (float) $fRet;
00230     }
00231 
00239     public function isSearchEngine( $sClient = null )
00240     {
00241 
00242         if (!is_null($this->_blIsSe)) {
00243             return $this->_blIsSe;
00244         }
00245 
00246         startProfile("isSearchEngine");
00247 
00248         $myConfig = $this->getConfig();
00249         $blIsSe   = false;
00250 
00251         if ( !( $myConfig->getConfigParam( 'iDebug' ) && $this->isAdmin() ) ) {
00252 
00253             // caching
00254             $blIsSe = $myConfig->getGlobalParameter( 'blIsSearchEngine' );
00255             if ( !isset( $blIsSe ) ) {
00256 
00257                 $aRobots = $myConfig->getConfigParam( 'aRobots' );
00258                 $aRobots = is_array( $aRobots )?$aRobots:array();
00259 
00260                 $aRobotsExcept = $myConfig->getConfigParam( 'aRobotsExcept' );
00261                 $aRobotsExcept = is_array( $aRobotsExcept )?$aRobotsExcept:array();
00262 
00263                 $sClient = $sClient?$sClient:strtolower( getenv( 'HTTP_USER_AGENT' ) );
00264                 $blIsSe  = false;
00265                 $aRobots = array_merge( $aRobots, $aRobotsExcept );
00266                 foreach ( $aRobots as $sRobot ) {
00267                     if ( strpos( $sClient, $sRobot ) !== false ) {
00268                         $blIsSe = true;
00269                         break;
00270                     }
00271                 }
00272                 $myConfig->setGlobalParameter( 'blIsSearchEngine', $blIsSe );
00273             }
00274         }
00275 
00276         stopProfile("isSearchEngine");
00277 
00278         $this->_blIsSe = $blIsSe;
00279 
00280         return $blIsSe;
00281     }
00282 
00291     public function isValidEmail( $sEmail )
00292     {
00293         $blValid = true;
00294         if ( $sEmail != 'admin' ) {
00295             $sEmailTpl = "/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i";
00296             $blValid = ( getStr()->preg_match( $sEmailTpl, $sEmail ) != 0 );
00297         }
00298 
00299         return $blValid;
00300     }
00301 
00309     public function rebuildCache()
00310     {
00311         // not needed from 3.0 on and unused <- MK: not correct, its used for example in shop_config.php, oxbase.php
00312 
00313         //$smarty  = & oxUtils::getInstance()->getSmarty();
00314         //$smarty->clear_all_cache();
00315 
00316         if ( function_exists( "UserdefinedRebuildCache")) {
00317             UserdefinedRebuildCache();
00318         }
00319     }
00320 
00328     public function loadAdminProfile($aInterfaceProfiles)
00329     {
00330         // improved #533
00331         // checking for available profiles list
00332         $aInterfaceProfiles = $aInterfaceProfiles;
00333         if ( is_array( $aInterfaceProfiles ) ) {
00334             //checking for previous profiles
00335             $sPrevProfile = oxUtilsServer::getInstance()->getOxCookie('oxidadminprofile');
00336             if (isset($sPrevProfile)) {
00337                 $aPrevProfile = @explode("@", trim($sPrevProfile));
00338             }
00339 
00340             //array to store profiles
00341             $aProfiles = array();
00342             foreach ( $aInterfaceProfiles as $iPos => $sProfile) {
00343                 $aProfileSettings = array($iPos, $sProfile);
00344                 $aProfiles[] = $aProfileSettings;
00345             }
00346             // setting previous used profile as active
00347             if (isset($aPrevProfile[0]) && isset($aProfiles[$aPrevProfile[0]])) {
00348                 $aProfiles[$aPrevProfile[0]][2] = 1;
00349             }
00350 
00351             oxSession::setVar("aAdminProfiles", $aProfiles);
00352             return $aProfiles;
00353         }
00354         return null;
00355     }
00356 
00365     public function fRound($sVal, $oCur = null)
00366     {
00367         startProfile('fround');
00368 
00369         //cached currency precision, this saves about 1% of execution time
00370         $iCurPrecision = null;
00371         if (! defined('OXID_PHP_UNIT')) {
00372             $iCurPrecision = $this->_iCurPrecision;
00373         }
00374 
00375         if (is_null($iCurPrecision)) {
00376             if ( !$oCur ) {
00377                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00378             }
00379 
00380             $iCurPrecision = $oCur->decimal;
00381             $this->_iCurPrecision = $iCurPrecision;
00382         }
00383 
00384         // this is a workaround for #36008 bug in php - incorrect round() & number_format() result (R)
00385         static $dprez = null;
00386         if (!$dprez) {
00387             $prez = @ini_get("precision");
00388             if (!$prez) {
00389                 $prez = 9;
00390             }
00391             $dprez = pow(10, -$prez);
00392         }
00393         stopProfile('fround');
00394 
00395         return round($sVal + $dprez * ( $sVal >= 0 ? 1 : -1 ), $iCurPrecision);
00396     }
00397 
00407     public function toStaticCache( $sName, $sContent, $sKey = null )
00408     {
00409         // if it's an array then we add
00410         if ( $sKey ) {
00411             $this->_aStaticCache[$sName][$sKey] = $sContent;
00412         } else {
00413             $this->_aStaticCache[$sName] = $sContent;
00414         }
00415     }
00416 
00424     public function fromStaticCache( $sName)
00425     {
00426         if ( isset( $this->_aStaticCache[$sName])) {
00427             return $this->_aStaticCache[$sName];
00428         }
00429         return null;
00430     }
00431 
00439     public function cleanStaticCache($sCacheName = null)
00440     {
00441         if ($sCacheName) {
00442             unset($this->_aStaticCache[$sCacheName]);
00443         } else {
00444             $this->_aStaticCache = null;
00445         }
00446     }
00447 
00457     public function toPhpFileCache( $sKey, $mContents )
00458     {
00459         //only simple arrays are supported
00460         if ( is_array( $mContents ) && ( $sCachePath = $this->getCacheFilePath( $sKey, false, 'php' ) ) ) {
00461 
00462             // setting meta
00463             $this->setCacheMeta( $sKey, array( "serialize" => false, "cachepath" => $sCachePath ) );
00464 
00465             // caching..
00466             $this->toFileCache( $sKey, $mContents );
00467         }
00468     }
00469 
00477     public function fromPhpFileCache( $sKey )
00478     {
00479         // setting meta
00480         $this->setCacheMeta( $sKey, array( "include" => true, "cachepath" => $this->getCacheFilePath( $sKey, false, 'php' ) ) );
00481         return $this->fromFileCache( $sKey );
00482     }
00483 
00491     public function getCacheMeta( $sKey )
00492     {
00493         return isset( $this->_aFileCacheMeta[$sKey] ) ? $this->_aFileCacheMeta[$sKey] : false;
00494     }
00495 
00504     public function setCacheMeta( $sKey, $aMeta )
00505     {
00506         // cache meta data
00507         $this->_aFileCacheMeta[$sKey] = $aMeta;
00508     }
00509 
00519     public function toFileCache( $sKey, $mContents )
00520     {
00521         $this->_aFileCacheContents[$sKey] = $mContents;
00522         $aMeta = $this->getCacheMeta( $sKey );
00523 
00524         // looking for cache meta
00525         $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00526         return ( bool ) $this->_lockFile( $sCachePath, $sKey );
00527     }
00528 
00536     public function fromFileCache( $sKey )
00537     {
00538         if ( !array_key_exists( $sKey, $this->_aFileCacheContents ) ) {
00539             $sRes = null;
00540 
00541             $aMeta = $this->getCacheMeta( $sKey );
00542             $blInclude  = isset( $aMeta["include"] ) ? $aMeta["include"] : false;
00543             $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00544 
00545             // trying to lock
00546             $this->_lockFile( $sCachePath, $sKey, LOCK_SH );
00547 
00548             clearstatcache();
00549             if ( is_readable( $sCachePath ) ) {
00550                 $sRes = $blInclude ? $this->_includeFile( $sCachePath ) : $this->_readFile( $sCachePath );
00551             }
00552 
00553             // release lock
00554             $this->_releaseFile( $sKey, LOCK_SH );
00555 
00556             // caching
00557             $this->_aFileCacheContents[$sKey] = $sRes;
00558         }
00559 
00560         return $this->_aFileCacheContents[$sKey];
00561     }
00562 
00570     protected function _readFile( $sFilePath )
00571     {
00572         $sRes = file_get_contents( $sFilePath );
00573         return $sRes ? unserialize( $sRes ) : null;
00574     }
00575 
00583     protected function _includeFile( $sFilePath )
00584     {
00585         $_aCacheContents = null;
00586         include $sFilePath;
00587         return $_aCacheContents;
00588     }
00589 
00598     protected function _processCache( $sKey, $mContents )
00599     {
00600         // looking for cache meta
00601         $aCacheMeta  = $this->getCacheMeta( $sKey );
00602         $blSerialize = isset( $aCacheMeta["serialize"] ) ? $aCacheMeta["serialize"] : true;
00603 
00604         if ( $blSerialize ) {
00605             $mContents = serialize( $mContents );
00606         } else {
00607             $mContents = "<?php\n//automatically generated file\n//" . date( "Y-m-d H:i:s" ) . "\n\n\$_aCacheContents = " . var_export( $mContents, true ) . "\n?>";
00608         }
00609 
00610         return $mContents;
00611     }
00612 
00619     public function commitFileCache()
00620     {
00621         if ( count( $this->_aLockedFileHandles[LOCK_EX] ) ) {
00622             startProfile("!__SAVING CACHE__! (warning)");
00623             foreach ( $this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle ) {
00624                 if ( $rHandle !== false && isset( $this->_aFileCacheContents[$sKey] ) ) {
00625 
00626                     // #0002931A truncate file once more before writing
00627                     ftruncate( $rHandle, 0 );
00628 
00629                     // writing cache
00630                     fwrite( $rHandle, $this->_processCache( $sKey, $this->_aFileCacheContents[$sKey] ) );
00631 
00632                     // releasing locks
00633                     $this->_releaseFile( $sKey );
00634                 }
00635             }
00636 
00637             stopProfile("!__SAVING CACHE__! (warning)");
00638 
00639             //empty buffer
00640             $this->_aFileCacheContents = array();
00641         }
00642     }
00643 
00653     protected function _lockFile( $sFilePath, $sIdent, $iLockMode = LOCK_EX )
00654     {
00655         $rHandle = isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
00656         if ( $rHandle === null ) {
00657 
00658             $blLocked = false;
00659             $rHandle = @fopen( $sFilePath, "a+" );
00660 
00661             if ( $rHandle !== false ) {
00662 
00663                 if ( flock( $rHandle, $iLockMode | LOCK_NB ) ) {
00664                     if ( $iLockMode === LOCK_EX ) {
00665                         // truncate file
00666                         $blLocked = ftruncate( $rHandle, 0 );
00667                     } else {
00668                         // move to a start position
00669                         $blLocked = fseek( $rHandle, 0 ) === 0;
00670                     }
00671                 }
00672 
00673                 // on failure - closing and setting false..
00674                 if ( !$blLocked ) {
00675                     fclose( $rHandle );
00676                     $rHandle = false;
00677                 }
00678             }
00679 
00680             // in case system does not support file lockings
00681             if ( !$blLocked && $iLockMode === LOCK_EX ) {
00682 
00683                 // clearing on first call
00684                 if ( count( $this->_aLockedFileHandles ) == 0 ) {
00685                     clearstatcache();
00686                 }
00687 
00688                 // start a blank file to inform other processes we are dealing with it.
00689                 if (!( file_exists( $sFilePath ) && !filesize( $sFilePath ) && abs( time() - filectime( $sFilePath ) < 40 ) ) ) {
00690                     $rHandle = @fopen( $sFilePath, "w" );
00691                 }
00692             }
00693 
00694             $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
00695         }
00696 
00697         return $rHandle;
00698     }
00699 
00708     protected function _releaseFile( $sIdent, $iLockMode = LOCK_EX )
00709     {
00710         $blSuccess = true;
00711         if ( isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) &&
00712              $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false ) {
00713 
00714              // release the lock and close file
00715             $blSuccess = flock( $this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN ) &&
00716                          fclose( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00717             unset( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00718         }
00719 
00720         return $blSuccess;
00721     }
00722 
00730     public function oxResetFileCache()
00731     {
00732         $aPathes = glob( $this->getCacheFilePath( null, true ) . '*' );
00733         if ( is_array( $aPathes ) ) {
00734             // delete all the files, except cached tables fieldnames
00735             $aPathes = preg_grep( $this->_sPermanentCachePattern, $aPathes, PREG_GREP_INVERT );
00736             foreach ( $aPathes as $sFilename ) {
00737                 @unlink( $sFilename );
00738             }
00739         }
00740     }
00741 
00751     public function getRemoteCachePath($sRemote, $sLocal)
00752     {
00753         clearstatcache();
00754         if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
00755             return $sLocal;
00756         }
00757         $hRemote = @fopen( $sRemote, "rb");
00758         $blSuccess = false;
00759         if ( isset( $hRemote) && $hRemote ) {
00760             $hLocal = fopen( $sLocal, "wb");
00761             stream_copy_to_stream($hRemote, $hLocal);
00762             fclose($hRemote);
00763             fclose($hLocal);
00764             $blSuccess = true;
00765         } else {
00766             // try via fsockopen
00767             $aUrl = @parse_url( $sRemote);
00768             if ( !empty( $aUrl["host"])) {
00769                 $sPath = $aUrl["path"];
00770                 if ( empty( $sPath ) ) {
00771                     $sPath = "/";
00772                 }
00773                 $sHost = $aUrl["host"];
00774 
00775                 $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
00776                 if ( $hSocket) {
00777                     fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00778                     $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
00779                     if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
00780                         rewind($hLocal);
00781                         // does not copy all the data
00782                         // stream_copy_to_stream($hSocket, $hLocal);
00783                         fwrite ( $hLocal, stream_get_contents( $hSocket ) );
00784                         fclose( $hLocal );
00785                         fclose( $hSocket );
00786                         $blSuccess = true;
00787                     }
00788                 }
00789             }
00790         }
00791         if ( $blSuccess || file_exists( $sLocal ) ) {
00792             return $sLocal;
00793         }
00794         return false;
00795     }
00796 
00802     public function canPreview()
00803     {
00804         $blCan = null;
00805         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00806              ( $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' ) ) ) {
00807 
00808             $sTable = getViewName( 'oxuser' );
00809             $sQ = "select 1 from $sTable where MD5( CONCAT( ?, {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = ?";
00810             $blCan = (bool) oxDb::getDb()->getOne( $sQ, array( $sAdminSid, $sPrevId ) );
00811         }
00812 
00813         return $blCan;
00814     }
00815 
00821     public function getPreviewId()
00822     {
00823         $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' );
00824         if ( ( $oUser = $this->getUser() ) ) {
00825             return md5( $sAdminSid . $oUser->getId() . $oUser->oxuser__oxpassword->value . $oUser->oxuser__oxrights->value );
00826         }
00827     }
00828 
00834     public function checkAccessRights()
00835     {
00836         $myConfig  = $this->getConfig();
00837 
00838         $blIsAuth = false;
00839 
00840         $sUserID = oxSession::getVar( "auth");
00841 
00842         // deleting admin marker
00843         oxSession::setVar( "malladmin", 0);
00844         oxSession::setVar( "blIsAdmin", 0);
00845         oxSession::deleteVar( "blIsAdmin" );
00846         $myConfig->setConfigParam( 'blMallAdmin', false );
00847         //#1552T
00848         $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
00849 
00850         if ( $sUserID) {
00851             // escaping
00852             $oDb = oxDb::getDb();
00853             $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
00854 
00855             if ( $sRights != "user") {
00856                 // malladmin ?
00857                 if ( $sRights == "malladmin") {
00858                     oxSession::setVar( "malladmin", 1);
00859                     $myConfig->setConfigParam( 'blMallAdmin', true );
00860 
00861                     //#1552T
00862                     //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
00863                     $myConfig->setConfigParam( 'blAllowSharedEdit', true );
00864 
00865                     $sShop = oxSession::getVar( "actshop");
00866                     if ( !isset($sShop)) {
00867                         oxSession::setVar( "actshop", $myConfig->getBaseShopId());
00868                     }
00869                     $blIsAuth = true;
00870                 } else {
00871                     // Shopadmin... check if this shop is valid and exists
00872                     $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
00873                     if ( isset( $sShopID) && $sShopID) {
00874                         // success, this shop exists
00875 
00876                         oxSession::setVar( "actshop", $sRights);
00877                         oxSession::setVar( "currentadminshop", $sRights);
00878                         oxSession::setVar( "shp", $sRights);
00879 
00880                         // check if this subshop admin is evil.
00881                         if ('chshp' == oxConfig::getParameter( 'fnc' )) {
00882                             // dont allow this call
00883                             $blIsAuth = false;
00884                         } else {
00885                             $blIsAuth = true;
00886 
00887                             $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
00888                             foreach ($aShopIdVars as $sShopIdVar) {
00889                                 if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
00890                                     if ($sGotShop != $sRights) {
00891                                         $blIsAuth = false;
00892                                         break;
00893                                     }
00894                                 }
00895                             }
00896                         }
00897                     }
00898                 }
00899                 // marking user as admin
00900                 oxSession::setVar( "blIsAdmin", 1);
00901             }
00902         }
00903         return $blIsAuth;
00904     }
00905 
00915     public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
00916     {
00917         if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
00918             return $this->_blSeoIsActive;
00919         }
00920 
00921         $myConfig = $this->getConfig();
00922 
00923         if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
00924             $this->_blSeoIsActive = true;
00925 
00926             $aSeoModes  = $myConfig->getconfigParam( 'aSeoModes' );
00927             $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
00928             $iActLang   = $iActLang ? $iActLang : (int) oxLang::getInstance()->getBaseLanguage();
00929 
00930             // checking special config param for active shop and language
00931             if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
00932                 $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
00933             }
00934         }
00935 
00936         return $this->_blSeoIsActive;
00937     }
00938 
00948     public function getShopBit( $iShopId )
00949     {
00950         $iShopId = (int) $iShopId;
00951         //this works for large numbers when $sShopNr is up to (inclusive) 64
00952         $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00953 
00954         //as php ints supports only 32 bits, we return string.
00955         return $iRes;
00956     }
00957 
00967     public function bitwiseAnd( $iVal1, $iVal2 )
00968     {
00969         //this works for large numbers when $sShopNr is up to (inclusive) 64
00970         $iRes = oxDb::getDb()->getOne( "select ($iVal1 & $iVal2) as bitwiseAnd" );
00971 
00972         //as php ints supports only 32 bits, we return string.
00973         return $iRes;
00974     }
00975 
00985     public function bitwiseOr( $iVal1, $iVal2 )
00986     {
00987         //this works for large numbers when $sShopNr is up to (inclusive) 64
00988         $iRes = oxDb::getDb()->getOne( "select ($iVal1 | $iVal2) as bitwiseOr" );
00989 
00990         //as php ints supports only 32 bits, we return string.
00991         return $iRes;
00992     }
00993 
01001     public function isValidAlpha( $sField )
01002     {
01003         return (boolean) getStr()->preg_match( '/^[a-zA-Z0-9_]*$/', $sField );
01004     }
01005 
01015     protected function _simpleRedirect( $sUrl, $sHeaderCode )
01016     {
01017         header( $sHeaderCode );
01018         header( "Location: $sUrl" );
01019         header( "Connection: close" );
01020     }
01021 
01035     public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 301 )
01036     {
01037         //preventing possible cyclic redirection
01038         //#M341 and check only if redirect paramater must be added
01039         if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
01040             return;
01041         }
01042 
01043         if ( $blAddRedirectParam ) {
01044             $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
01045         }
01046 
01047         $sUrl = str_ireplace( "&amp;", "&", $sUrl );
01048 
01049         $sHeaderCode = '';
01050         switch ($iHeaderCode) {
01051             case 301:
01052                 $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
01053                 break;
01054             case 302:
01055             default:
01056                 $sHeaderCode = "HTTP/1.1 302 Found";
01057         }
01058 
01059         $this->_simpleRedirect( $sUrl, $sHeaderCode );
01060 
01061         try {//may occur in case db is lost
01062             $this->getSession()->freeze();
01063         } catch( oxException $oEx ) {
01064             $oEx->debugOut();
01065             //do nothing else to make sure the redirect takes place
01066         }
01067 
01068         if ( defined( 'OXID_PHP_UNIT' ) ) {
01069             return;
01070         }
01071 
01072         $this->showMessageAndExit( '' );
01073     }
01074 
01082     public function showMessageAndExit( $sMsg )
01083     {
01084         $this->getSession()->freeze();
01085         $this->commitFileCache();
01086 
01087         if ( defined( 'OXID_PHP_UNIT' ) ) {
01088             return;
01089         }
01090 
01091         exit( $sMsg );
01092     }
01093 
01101     public function setHeader($sHeader)
01102     {
01103         header($sHeader);
01104     }
01105 
01114     protected function _addUrlParameters( $sUrl, $aParams )
01115     {
01116         $sDelim = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
01117         foreach ( $aParams as $sName => $sVal ) {
01118             $sUrl = $sUrl . $sDelim . $sName . '=' . $sVal;
01119             $sDelim = '&';
01120         }
01121 
01122         return $sUrl;
01123     }
01124 
01136     protected function _fillExplodeArray( $aName, $dVat = null)
01137     {
01138         $myConfig = $this->getConfig();
01139         $oObject = new oxStdClass();
01140         $aPrice = explode( '!P!', $aName[0]);
01141 
01142         if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
01143 
01144             // yes, price is there
01145             $oObject->price = isset( $aPrice[1] ) ? $aPrice[1] : 0;
01146             $aName[0] = isset( $aPrice[0] ) ? $aPrice[0] : '';
01147 
01148             $iPercPos = getStr()->strpos( $oObject->price, '%' );
01149             if ( $iPercPos !== false ) {
01150                 $oObject->priceUnit = '%';
01151                 $oObject->fprice = $oObject->price;
01152                 $oObject->price  = substr( $oObject->price, 0, $iPercPos );
01153             } else {
01154                 $oCur = $myConfig->getActShopCurrencyObject();
01155                 $oObject->price = str_replace(',', '.', $oObject->price);
01156                 $oObject->fprice = oxLang::getInstance()->formatCurrency( $oObject->price  * $oCur->rate, $oCur);
01157                 $oObject->priceUnit = 'abs';
01158             }
01159 
01160             // add price info into list
01161             if ( !$this->isAdmin() && $oObject->price != 0 ) {
01162                 $aName[0] .= " ";
01163                 if ( $oObject->price > 0 ) {
01164                     $aName[0] .= "+";
01165                 }
01166                 //V FS#2616
01167                 if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
01168                     $oPrice = oxNew('oxPrice');
01169                     $oPrice->setPrice($oObject->price, $dVat);
01170                     $aName[0] .= oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() * $oCur->rate, $oCur);
01171                 } else {
01172                     $aName[0] .= $oObject->fprice;
01173                 }
01174                 if ( $oObject->priceUnit == 'abs' ) {
01175                     $aName[0] .= " ".$oCur->sign;
01176                 }
01177             }
01178         } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
01179             // A. removing unused part of information
01180             $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
01181         }
01182 
01183         $oObject->name  = $aName[0];
01184         $oObject->value = $aName[1];
01185         return $oObject;
01186     }
01187 
01195     public function oxMimeContentType( $sFileName )
01196     {
01197         $sFileName = strtolower( $sFileName );
01198         $iLastDot  = strrpos( $sFileName, '.' );
01199 
01200         if ( $iLastDot !== false ) {
01201             $sType = substr( $sFileName, $iLastDot + 1 );
01202             switch ( $sType ) {
01203                 case 'gif':
01204                     $sType = 'image/gif';
01205                     break;
01206                 case 'jpeg':
01207                 case 'jpg':
01208                     $sType = 'image/jpeg';
01209                     break;
01210                 case 'png':
01211                     $sType = 'image/png';
01212                     break;
01213                 default:
01214                     $sType = false;
01215                     break;
01216             }
01217         }
01218         return $sType;
01219     }
01220 
01229     public function logger( $sText, $blNewline = false )
01230     {   $myConfig = $this->getConfig();
01231 
01232         if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
01233             if ( gettype( $sText ) != 'string' ) {
01234                 $sText = var_export( $sText, true);
01235             }
01236             $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
01237             $this->writeToLog( $sLogMsg, "log.txt" );
01238         }
01239 
01240     }
01241 
01249     protected function _stripQuotes($mInput)
01250     {
01251         return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
01252     }
01253 
01261     public function strRot13( $sStr )
01262     {
01263         $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
01264         $sTo   = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
01265 
01266         return strtr( $sStr, $sFrom, $sTo );
01267     }
01268 
01278     public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
01279     {
01280         $sVersionPrefix = "";
01281 
01282 
01283             $sVersionPrefix = 'pe';
01284 
01285         $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
01286 
01287         if (!$sPath) {
01288             return false;
01289         }
01290 
01291         return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
01292     }
01293 
01301     public function getLangCache( $sCacheName )
01302     {
01303         $aLangCache = null;
01304         $sFilePath = $this->getCacheFilePath( $sCacheName );
01305         if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
01306             include $sFilePath;
01307         }
01308         return $aLangCache;
01309     }
01310 
01319     public function setLangCache( $sCacheName, $aLangCache )
01320     {
01321         $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";";
01322         $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache);
01323         return $blRes;
01324     }
01325 
01333     public function checkUrlEndingSlash( $sUrl )
01334     {
01335         if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
01336             $sUrl .= '/';
01337         }
01338 
01339         return $sUrl;
01340     }
01341 
01350     public function writeToLog( $sLogMessage, $sLogFileName )
01351     {
01352         $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
01353         $blOk = false;
01354 
01355         if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
01356             fwrite( $oHandle, $sLogMessage );
01357             $blOk = fclose( $oHandle );
01358         }
01359 
01360         return $blOk;
01361     }
01362 
01370     public function handlePageNotFoundError($sUrl = '')
01371     {
01372         $this->setHeader("HTTP/1.0 404 Not Found");
01373         if ( oxConfig::getInstance()->isUtf() ) {
01374             $this->setHeader("Content-Type: text/html; charset=UTF-8");
01375         }
01376 
01377         $sReturn = "Page not found.";
01378         try {
01379             $oView = oxNew('oxubase');
01380             $oView->init();
01381             $oView->render();
01382             $oView->addTplParam('sUrl', $sUrl);
01383             if ($sRet = oxUtilsView::getInstance()->getTemplateOutput('message/err_404.tpl', $oView)) {
01384                 $sReturn = $sRet;
01385             }
01386         } catch (Exception $e) {
01387         }
01388         $this->showMessageAndExit( $sReturn );
01389     }
01390 
01398     public function extractDomain( $sHost )
01399     {
01400         $oStr = getStr();
01401         if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
01402              ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
01403             $iLen = $oStr->strlen( $sHost );
01404             if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
01405                 $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
01406             }
01407         }
01408 
01409         return $sHost;
01410     }
01411 }