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 
00398 
00399         stopProfile('fround');
00400 
00401         return round($sVal + $dprez, $iCurPrecision);
00402     }
00403 
00413     public function toStaticCache( $sName, $sContent, $sKey = null )
00414     {
00415         // if it's an array then we add
00416         if ( $sKey ) {
00417             $this->_aStaticCache[$sName][$sKey] = $sContent;
00418         } else {
00419             $this->_aStaticCache[$sName] = $sContent;
00420         }
00421     }
00422 
00430     public function fromStaticCache( $sName)
00431     {
00432         if ( isset( $this->_aStaticCache[$sName])) {
00433             return $this->_aStaticCache[$sName];
00434         }
00435         return null;
00436     }
00437 
00445     public function cleanStaticCache($sCacheName = null)
00446     {
00447         if ($sCacheName) {
00448             unset($this->_aStaticCache[$sCacheName]);
00449         } else {
00450             $this->_aStaticCache = null;
00451         }
00452     }
00453 
00463     public function toPhpFileCache( $sKey, $mContents )
00464     {
00465         //only simple arrays are supported
00466         if ( is_array( $mContents ) && ( $sCachePath = $this->getCacheFilePath( $sKey, false, 'php' ) ) ) {
00467 
00468             // setting meta
00469             $this->setCacheMeta( $sKey, array( "serialize" => false, "cachepath" => $sCachePath ) );
00470 
00471             // caching..
00472             $this->toFileCache( $sKey, $mContents );
00473         }
00474     }
00475 
00483     public function fromPhpFileCache( $sKey )
00484     {
00485         // setting meta
00486         $this->setCacheMeta( $sKey, array( "include" => true, "cachepath" => $this->getCacheFilePath( $sKey, false, 'php' ) ) );
00487         return $this->fromFileCache( $sKey );
00488     }
00489 
00497     public function getCacheMeta( $sKey )
00498     {
00499         return isset( $this->_aFileCacheMeta[$sKey] ) ? $this->_aFileCacheMeta[$sKey] : false;
00500     }
00501 
00510     public function setCacheMeta( $sKey, $aMeta )
00511     {
00512         // cache meta data
00513         $this->_aFileCacheMeta[$sKey] = $aMeta;
00514     }
00515 
00525     public function toFileCache( $sKey, $mContents )
00526     {
00527         $this->_aFileCacheContents[$sKey] = $mContents;
00528         $aMeta = $this->getCacheMeta( $sKey );
00529 
00530         // looking for cache meta
00531         $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00532         return ( bool ) $this->_lockFile( $sCachePath, $sKey );
00533     }
00534 
00542     public function fromFileCache( $sKey )
00543     {
00544         if ( !array_key_exists( $sKey, $this->_aFileCacheContents ) ) {
00545             $sRes = null;
00546 
00547             $aMeta = $this->getCacheMeta( $sKey );
00548             $blInclude  = isset( $aMeta["include"] ) ? $aMeta["include"] : false;
00549             $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00550 
00551             // trying to lock
00552             $this->_lockFile( $sCachePath, $sKey, LOCK_SH );
00553 
00554             clearstatcache();
00555             if ( is_readable( $sCachePath ) ) {
00556                 $sRes = $blInclude ? $this->_includeFile( $sCachePath ) : $this->_readFile( $sCachePath );
00557             }
00558 
00559             // release lock
00560             $this->_releaseFile( $sKey, LOCK_SH );
00561 
00562             // caching
00563             $this->_aFileCacheContents[$sKey] = $sRes;
00564         }
00565 
00566         return $this->_aFileCacheContents[$sKey];
00567     }
00568 
00576     protected function _readFile( $sFilePath )
00577     {
00578         $sRes = file_get_contents( $sFilePath );
00579         return $sRes ? unserialize( $sRes ) : null;
00580     }
00581 
00589     protected function _includeFile( $sFilePath )
00590     {
00591         $_aCacheContents = null;
00592         include $sFilePath;
00593         return $_aCacheContents;
00594     }
00595 
00604     protected function _processCache( $sKey, $mContents )
00605     {
00606         // looking for cache meta
00607         $aCacheMeta  = $this->getCacheMeta( $sKey );
00608         $blSerialize = isset( $aCacheMeta["serialize"] ) ? $aCacheMeta["serialize"] : true;
00609 
00610         if ( $blSerialize ) {
00611             $mContents = serialize( $mContents );
00612         } else {
00613             $mContents = "<?php\n//automatically generated file\n//" . date( "Y-m-d H:i:s" ) . "\n\n\$_aCacheContents = " . var_export( $mContents, true ) . "\n?>";
00614         }
00615 
00616         return $mContents;
00617     }
00618 
00625     public function commitFileCache()
00626     {
00627         if ( count( $this->_aLockedFileHandles[LOCK_EX] ) ) {
00628             startProfile("!__SAVING CACHE__! (warning)");
00629             foreach ( $this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle ) {
00630                 if ( $rHandle !== false && isset( $this->_aFileCacheContents[$sKey] ) ) {
00631 
00632                     // writing cache
00633                     fwrite( $rHandle, $this->_processCache( $sKey, $this->_aFileCacheContents[$sKey] ) );
00634 
00635                     // releasing locks
00636                     $this->_releaseFile( $sKey );
00637                 }
00638             }
00639 
00640             stopProfile("!__SAVING CACHE__! (warning)");
00641 
00642             //empty buffer
00643             $this->_aFileCacheContents = array();
00644         }
00645     }
00646 
00656     protected function _lockFile( $sFilePath, $sIdent, $iLockMode = LOCK_EX )
00657     {
00658         $rHandle = isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
00659         if ( $rHandle === null ) {
00660 
00661             $blLocked = false;
00662             $rHandle = @fopen( $sFilePath, "a+" );
00663 
00664             if ( $rHandle !== false ) {
00665 
00666                 if ( flock( $rHandle, $iLockMode | LOCK_NB ) ) {
00667                     if ( $iLockMode === LOCK_EX ) {
00668                         // truncate file
00669                         $blLocked = ftruncate( $rHandle, 0 );
00670                     } else {
00671                         // move to a start position
00672                         $blLocked = fseek( $rHandle, 0 ) === 0;
00673                     }
00674                 }
00675 
00676                 // on failure - closing and setting false..
00677                 if ( !$blLocked ) {
00678                     fclose( $rHandle );
00679                     $rHandle = false;
00680                 }
00681             }
00682 
00683             // in case system does not support file lockings
00684             if ( !$blLocked && $iLockMode === LOCK_EX ) {
00685 
00686                 // clearing on first call
00687                 if ( count( $this->_aLockedFileHandles ) == 0 ) {
00688                     clearstatcache();
00689                 }
00690 
00691                 // start a blank file to inform other processes we are dealing with it.
00692                 if (!( file_exists( $sFilePath ) && !filesize( $sFilePath ) && abs( time() - filectime( $sFilePath ) < 40 ) ) ) {
00693                     $rHandle = @fopen( $sFilePath, "w" );
00694                 }
00695             }
00696 
00697             $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
00698         }
00699 
00700         return $rHandle;
00701     }
00702 
00711     protected function _releaseFile( $sIdent, $iLockMode = LOCK_EX )
00712     {
00713         $blSuccess = true;
00714         if ( isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) &&
00715              $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false ) {
00716 
00717              // release the lock and close file
00718             $blSuccess = flock( $this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN ) &&
00719                          fclose( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00720             unset( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00721         }
00722 
00723         return $blSuccess;
00724     }
00725 
00733     public function oxResetFileCache()
00734     {
00735         $aPathes = glob( $this->getCacheFilePath( null, true ) . '*' );
00736         if ( is_array( $aPathes ) ) {
00737             // delete all the files, except cached tables fieldnames
00738             $aPathes = preg_grep( $this->_sPermanentCachePattern, $aPathes, PREG_GREP_INVERT );
00739             foreach ( $aPathes as $sFilename ) {
00740                 @unlink( $sFilename );
00741             }
00742         }
00743     }
00744 
00754     public function getRemoteCachePath($sRemote, $sLocal)
00755     {
00756         clearstatcache();
00757         if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
00758             return $sLocal;
00759         }
00760         $hRemote = @fopen( $sRemote, "rb");
00761         $blSuccess = false;
00762         if ( isset( $hRemote) && $hRemote ) {
00763             $hLocal = fopen( $sLocal, "wb");
00764             stream_copy_to_stream($hRemote, $hLocal);
00765             fclose($hRemote);
00766             fclose($hLocal);
00767             $blSuccess = true;
00768         } else {
00769             // try via fsockopen
00770             $aUrl = @parse_url( $sRemote);
00771             if ( !empty( $aUrl["host"])) {
00772                 $sPath = $aUrl["path"];
00773                 if ( empty( $sPath ) ) {
00774                     $sPath = "/";
00775                 }
00776                 $sHost = $aUrl["host"];
00777 
00778                 $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
00779                 if ( $hSocket) {
00780                     fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00781                     $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
00782                     if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
00783                         rewind($hLocal);
00784                         // does not copy all the data
00785                         // stream_copy_to_stream($hSocket, $hLocal);
00786                         fwrite ( $hLocal, stream_get_contents( $hSocket ) );
00787                         fclose( $hLocal );
00788                         fclose( $hSocket );
00789                         $blSuccess = true;
00790                     }
00791                 }
00792             }
00793         }
00794         if ( $blSuccess || file_exists( $sLocal ) ) {
00795             return $sLocal;
00796         }
00797         return false;
00798     }
00799 
00805     public function canPreview()
00806     {
00807         $blCan = null;
00808         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00809              ( $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' ) ) ) {
00810 
00811             $sTable = getViewName( 'oxuser' );
00812             $sQ = "select 1 from $sTable where MD5( CONCAT( ?, {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = ?";
00813             $blCan = (bool) oxDb::getDb()->getOne( $sQ, array( $sAdminSid, $sPrevId ) );
00814         }
00815 
00816         return $blCan;
00817     }
00818 
00824     public function getPreviewId()
00825     {
00826         $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' );
00827         if ( ( $oUser = $this->getUser() ) ) {
00828             return md5( $sAdminSid . $oUser->getId() . $oUser->oxuser__oxpassword->value . $oUser->oxuser__oxrights->value );
00829         }
00830     }
00831 
00837     public function checkAccessRights()
00838     {
00839         $myConfig  = $this->getConfig();
00840 
00841         $blIsAuth = false;
00842 
00843         $sUserID = oxSession::getVar( "auth");
00844 
00845         // deleting admin marker
00846         oxSession::setVar( "malladmin", 0);
00847         oxSession::setVar( "blIsAdmin", 0);
00848         oxSession::deleteVar( "blIsAdmin" );
00849         $myConfig->setConfigParam( 'blMallAdmin', false );
00850         //#1552T
00851         $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
00852 
00853         if ( $sUserID) {
00854             // escaping
00855             $oDb = oxDb::getDb();
00856             $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
00857 
00858             if ( $sRights != "user") {
00859                 // malladmin ?
00860                 if ( $sRights == "malladmin") {
00861                     oxSession::setVar( "malladmin", 1);
00862                     $myConfig->setConfigParam( 'blMallAdmin', true );
00863 
00864                     //#1552T
00865                     //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
00866                     $myConfig->setConfigParam( 'blAllowSharedEdit', true );
00867 
00868                     $sShop = oxSession::getVar( "actshop");
00869                     if ( !isset($sShop)) {
00870                         oxSession::setVar( "actshop", $myConfig->getBaseShopId());
00871                     }
00872                     $blIsAuth = true;
00873                 } else {
00874                     // Shopadmin... check if this shop is valid and exists
00875                     $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
00876                     if ( isset( $sShopID) && $sShopID) {
00877                         // success, this shop exists
00878 
00879                         oxSession::setVar( "actshop", $sRights);
00880                         oxSession::setVar( "currentadminshop", $sRights);
00881                         oxSession::setVar( "shp", $sRights);
00882 
00883                         // check if this subshop admin is evil.
00884                         if ('chshp' == oxConfig::getParameter( 'fnc' )) {
00885                             // dont allow this call
00886                             $blIsAuth = false;
00887                         } else {
00888                             $blIsAuth = true;
00889 
00890                             $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
00891                             foreach ($aShopIdVars as $sShopIdVar) {
00892                                 if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
00893                                     if ($sGotShop != $sRights) {
00894                                         $blIsAuth = false;
00895                                         break;
00896                                     }
00897                                 }
00898                             }
00899                         }
00900                     }
00901                 }
00902                 // marking user as admin
00903                 oxSession::setVar( "blIsAdmin", 1);
00904             }
00905         }
00906         return $blIsAuth;
00907     }
00908 
00918     public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
00919     {
00920         if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
00921             return $this->_blSeoIsActive;
00922         }
00923 
00924         $myConfig = $this->getConfig();
00925 
00926         if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
00927             $this->_blSeoIsActive = true;
00928 
00929             $aSeoModes  = $myConfig->getconfigParam( 'aSeoModes' );
00930             $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
00931             $iActLang   = $iActLang ? $iActLang : (int) oxLang::getInstance()->getBaseLanguage();
00932 
00933             // checking special config param for active shop and language
00934             if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
00935                 $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
00936             }
00937         }
00938 
00939         return $this->_blSeoIsActive;
00940     }
00941 
00951     public function getShopBit( $iShopId )
00952     {
00953         $iShopId = (int) $iShopId;
00954         //this works for large numbers when $sShopNr is up to (inclusive) 64
00955         $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00956 
00957         //as php ints supports only 32 bits, we return string.
00958         return $iRes;
00959     }
00960 
00970     public function bitwiseAnd( $iVal1, $iVal2 )
00971     {
00972         //this works for large numbers when $sShopNr is up to (inclusive) 64
00973         $iRes = oxDb::getDb()->getOne( "select ($iVal1 & $iVal2) as bitwiseAnd" );
00974 
00975         //as php ints supports only 32 bits, we return string.
00976         return $iRes;
00977     }
00978 
00988     public function bitwiseOr( $iVal1, $iVal2 )
00989     {
00990         //this works for large numbers when $sShopNr is up to (inclusive) 64
00991         $iRes = oxDb::getDb()->getOne( "select ($iVal1 | $iVal2) as bitwiseOr" );
00992 
00993         //as php ints supports only 32 bits, we return string.
00994         return $iRes;
00995     }
00996 
01004     public function isValidAlpha( $sField )
01005     {
01006         return (boolean) getStr()->preg_match( '/^[a-zA-Z0-9_]*$/', $sField );
01007     }
01008 
01018     protected function _simpleRedirect( $sUrl, $sHeaderCode )
01019     {
01020         header( $sHeaderCode );
01021         header( "Location: $sUrl" );
01022         header( "Connection: close" );
01023     }
01024 
01038     public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 301 )
01039     {
01040         //preventing possible cyclic redirection
01041         //#M341 and check only if redirect paramater must be added
01042         if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
01043             return;
01044         }
01045 
01046         if ( $blAddRedirectParam ) {
01047             $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
01048         }
01049 
01050         $sUrl = str_ireplace( "&amp;", "&", $sUrl );
01051 
01052         $sHeaderCode = '';
01053         switch ($iHeaderCode) {
01054             case 301:
01055                 $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
01056                 break;
01057             case 302:
01058             default:
01059                 $sHeaderCode = "HTTP/1.1 302 Found";
01060         }
01061 
01062         $this->_simpleRedirect( $sUrl, $sHeaderCode );
01063 
01064         try {//may occur in case db is lost
01065             $this->getSession()->freeze();
01066         } catch( oxException $oEx ) {
01067             $oEx->debugOut();
01068             //do nothing else to make sure the redirect takes place
01069         }
01070 
01071         if ( defined( 'OXID_PHP_UNIT' ) ) {
01072             return;
01073         }
01074 
01075         $this->showMessageAndExit( '' );
01076     }
01077 
01085     public function showMessageAndExit( $sMsg )
01086     {
01087         $this->getSession()->freeze();
01088         $this->commitFileCache();
01089 
01090         if ( defined( 'OXID_PHP_UNIT' ) ) {
01091             return;
01092         }
01093 
01094         exit( $sMsg );
01095     }
01096 
01104     public function setHeader($sHeader)
01105     {
01106         header($sHeader);
01107     }
01108 
01117     protected function _addUrlParameters( $sUrl, $aParams )
01118     {
01119         $sDelim = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
01120         foreach ( $aParams as $sName => $sVal ) {
01121             $sUrl = $sUrl . $sDelim . $sName . '=' . $sVal;
01122             $sDelim = '&';
01123         }
01124 
01125         return $sUrl;
01126     }
01127 
01139     protected function _fillExplodeArray( $aName, $dVat = null)
01140     {
01141         $myConfig = $this->getConfig();
01142         $oObject = new OxstdClass();
01143         $aPrice = explode( '!P!', $aName[0]);
01144 
01145         if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
01146 
01147             // yes, price is there
01148             $oObject->price = $aPrice[1];
01149             $aName[0] = $aPrice[0];
01150 
01151             $iPercPos = getStr()->strpos( $oObject->price, '%' );
01152             if ( $iPercPos !== false ) {
01153                 $oObject->priceUnit = '%';
01154                 $oObject->fprice = $oObject->price;
01155                 $oObject->price  = substr( $oObject->price, 0, $iPercPos );
01156             } else {
01157                 $oCur = $myConfig->getActShopCurrencyObject();
01158                 $oObject->price = str_replace(',', '.', $oObject->price);
01159                 $oObject->fprice = oxLang::getInstance()->formatCurrency( $oObject->price  * $oCur->rate, $oCur);
01160                 $oObject->priceUnit = 'abs';
01161             }
01162 
01163             // add price info into list
01164             if ( !$this->isAdmin() && $oObject->price != 0 ) {
01165                 $aName[0] .= " ";
01166                 if ( $oObject->price > 0 ) {
01167                     $aName[0] .= "+";
01168                 }
01169                 //V FS#2616
01170                 if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
01171                     $oPrice = oxNew('oxPrice');
01172                     $oPrice->setPrice($oObject->price, $dVat);
01173                     $aName[0] .= oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() * $oCur->rate, $oCur);
01174                 } else {
01175                     $aName[0] .= $oObject->fprice;
01176                 }
01177                 if ( $oObject->priceUnit == 'abs' ) {
01178                     $aName[0] .= " ".$oCur->sign;
01179                 }
01180             }
01181         } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
01182             // A. removing unused part of information
01183             $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
01184         }
01185 
01186         $oObject->name  = $aName[0];
01187         $oObject->value = $aName[1];
01188         return $oObject;
01189     }
01190 
01198     public function oxMimeContentType( $sFileName )
01199     {
01200         $sFileName = strtolower( $sFileName );
01201         $iLastDot  = strrpos( $sFileName, '.' );
01202 
01203         if ( $iLastDot !== false ) {
01204             $sType = substr( $sFileName, $iLastDot + 1 );
01205             switch ( $sType ) {
01206                 case 'gif':
01207                     $sType = 'image/gif';
01208                     break;
01209                 case 'jpeg':
01210                 case 'jpg':
01211                     $sType = 'image/jpeg';
01212                     break;
01213                 case 'png':
01214                     $sType = 'image/png';
01215                     break;
01216                 default:
01217                     $sType = false;
01218                     break;
01219             }
01220         }
01221         return $sType;
01222     }
01223 
01232     public function logger( $sText, $blNewline = false )
01233     {   $myConfig = $this->getConfig();
01234 
01235         if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
01236             if ( gettype( $sText ) != 'string' ) {
01237                 $sText = var_export( $sText, true);
01238             }
01239             $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
01240             $this->writeToLog( $sLogMsg, "log.txt" );
01241         }
01242 
01243     }
01244 
01252     protected function _stripQuotes($mInput)
01253     {
01254         return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
01255     }
01256 
01264     public function strRot13( $sStr )
01265     {
01266         $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
01267         $sTo   = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
01268 
01269         return strtr( $sStr, $sFrom, $sTo );
01270     }
01271 
01281     public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
01282     {
01283         $sVersionPrefix = "";
01284 
01285 
01286             $sVersionPrefix = 'pe';
01287 
01288         $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
01289 
01290         if (!$sPath) {
01291             return false;
01292         }
01293 
01294         return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
01295     }
01296 
01304     public function getLangCache( $sCacheName )
01305     {
01306         $aLangCache = null;
01307         $sFilePath = $this->getCacheFilePath( $sCacheName );
01308         if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
01309             include $sFilePath;
01310         }
01311         return $aLangCache;
01312     }
01313 
01322     public function setLangCache( $sCacheName, $aLangCache )
01323     {
01324         $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";";
01325         $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache);
01326         return $blRes;
01327     }
01328 
01336     public function checkUrlEndingSlash( $sUrl )
01337     {
01338         if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
01339             $sUrl .= '/';
01340         }
01341 
01342         return $sUrl;
01343     }
01344 
01353     public function writeToLog( $sLogMessage, $sLogFileName )
01354     {
01355         $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
01356         $blOk = false;
01357 
01358         if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
01359             fwrite( $oHandle, $sLogMessage );
01360             $blOk = fclose( $oHandle );
01361         }
01362 
01363         return $blOk;
01364     }
01365 
01373     public function handlePageNotFoundError($sUrl = '')
01374     {
01375         $this->setHeader("HTTP/1.0 404 Not Found");
01376         if ( oxConfig::getInstance()->isUtf() ) {
01377             $this->setHeader("Content-Type: text/html; charset=UTF-8");
01378         }
01379 
01380         $sReturn = "Page not found.";
01381         try {
01382             $oView = oxNew('oxubase');
01383             $oView->init();
01384             $oView->render();
01385             $oView->addTplParam('sUrl', $sUrl);
01386             if ($sRet = oxUtilsView::getInstance()->getTemplateOutput('message/err_404.tpl', $oView)) {
01387                 $sReturn = $sRet;
01388             }
01389         } catch (Exception $e) {
01390         }
01391         $this->showMessageAndExit( $sReturn );
01392     }
01393 
01401     public function extractDomain( $sHost )
01402     {
01403         $oStr = getStr();
01404         if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
01405              ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
01406             $iLen = $oStr->strlen( $sHost );
01407             if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
01408                 $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
01409             }
01410         }
01411 
01412         return $sHost;
01413     }
01414 }