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_|c_tbdsc_|_allfields_/";
00036 
00042     protected $_sLanguageCachePattern = "/c_langcache_/i";
00043 
00049     protected $_sMenuCachePattern = "/c_menu_/i";
00050 
00056     protected $_aLockedFileHandles = array();
00057 
00063     protected $_aFileCacheContents = array();
00064 
00070     protected $_blIsSe = null;
00071 
00079     public static function getInstance()
00080     {
00081         return oxRegistry::getUtils();
00082     }
00083 
00089     protected $_aStaticCache;
00090 
00096     protected $_blSeoIsActive = null;
00097 
00103     public function stripGpcMagicQuotes()
00104     {
00105         if (!get_magic_quotes_gpc()) {
00106             return;
00107         }
00108         $_REQUEST = self::_stripQuotes($_REQUEST);
00109         $_POST = self::_stripQuotes($_POST);
00110         $_GET = self::_stripQuotes($_GET);
00111         $_COOKIE = self::_stripQuotes($_COOKIE);
00112     }
00113 
00122     public function strMan( $sVal, $sKey = null )
00123     {
00124         $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
00125         $sVal = "ox{$sVal}id";
00126 
00127         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00128         $sVal = $this->strRot13( $sVal );
00129         $sVal = $sVal ^ $sKey;
00130         $sVal = base64_encode ( $sVal );
00131         $sVal = str_replace( "=", "!", $sVal );
00132 
00133         return "ox_$sVal";
00134     }
00135 
00144     public function strRem( $sVal, $sKey = null )
00145     {
00146         $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
00147         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00148 
00149         $sVal = substr( $sVal, 3 );
00150         $sVal = str_replace( '!', '=', $sVal );
00151         $sVal = base64_decode( $sVal );
00152         $sVal = $sVal ^ $sKey;
00153         $sVal = $this->strRot13( $sVal );
00154 
00155         return substr( $sVal, 2, -2 );
00156     }
00157 
00165     public function getArrFldName( $sName)
00166     {
00167         return str_replace( ".", "__", $sName);
00168     }
00169 
00178     public function assignValuesFromText( $sIn, $dVat = null)
00179     {
00180         $aRet = array();
00181         $aPieces = explode( '@@', $sIn );
00182         while ( list( $sKey, $sVal ) = each( $aPieces ) ) {
00183             if ( $sVal ) {
00184                 $aName = explode( '__', $sVal );
00185                 if ( isset( $aName[0] ) && isset( $aName[1] ) ) {
00186                     $aRet[] = $this->_fillExplodeArray( $aName, $dVat );
00187                 }
00188             }
00189         }
00190         return $aRet;
00191     }
00192 
00200     public function assignValuesToText( $aIn)
00201     {
00202         $sRet = "";
00203         reset( $aIn );
00204         while (list($sKey, $sVal) = each($aIn)) {
00205             $sRet .= $sKey;
00206             $sRet .= "__";
00207             $sRet .= $sVal;
00208             $sRet .= "@@";
00209         }
00210         return $sRet;
00211     }
00212 
00220     public function currency2Float( $sValue)
00221     {
00222         $fRet = $sValue;
00223         $iPos = strrpos( $sValue, ".");
00224         if ($iPos && ((strlen($sValue)-1-$iPos) < 2+1)) {
00225             // replace decimal with ","
00226             $fRet = substr_replace( $fRet, ",", $iPos, 1);
00227         }
00228         // remove thousands
00229         $fRet = str_replace( array(" ","."), "", $fRet);
00230 
00231         $fRet = str_replace( ",", ".", $fRet);
00232         return (float) $fRet;
00233     }
00234 
00242     public function isSearchEngine( $sClient = null )
00243     {
00244         if (is_null($this->_blIsSe)) {
00245             $this->setSearchEngine( null, $sClient );
00246         }
00247         return $this->_blIsSe;
00248     }
00249 
00258     public function setSearchEngine( $blIsSe = null, $sClient = null )
00259     {
00260         if (isset($blIsSe)) {
00261             $this->_blIsSe = $blIsSe;
00262             return;
00263         }
00264         startProfile("isSearchEngine");
00265 
00266         $myConfig = $this->getConfig();
00267         $blIsSe   = false;
00268 
00269         if ( !( $myConfig->getConfigParam( 'iDebug' ) && $this->isAdmin() ) ) {
00270             $aRobots = $myConfig->getConfigParam( 'aRobots' );
00271             $aRobots = is_array( $aRobots )?$aRobots:array();
00272 
00273             $aRobotsExcept = $myConfig->getConfigParam( 'aRobotsExcept' );
00274             $aRobotsExcept = is_array( $aRobotsExcept )?$aRobotsExcept:array();
00275 
00276             $sClient = $sClient?$sClient:strtolower( getenv( 'HTTP_USER_AGENT' ) );
00277             $blIsSe  = false;
00278             $aRobots = array_merge( $aRobots, $aRobotsExcept );
00279             foreach ( $aRobots as $sRobot ) {
00280                 if ( strpos( $sClient, $sRobot ) !== false ) {
00281                     $blIsSe = true;
00282                     break;
00283                 }
00284             }
00285         }
00286 
00287         $this->_blIsSe = $blIsSe;
00288 
00289         stopProfile("isSearchEngine");
00290     }
00291 
00300     public function isValidEmail( $sEmail )
00301     {
00302         $blValid = true;
00303         if ( $sEmail != 'admin' ) {
00304             $sEmailTpl = "/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i";
00305             $blValid = ( getStr()->preg_match( $sEmailTpl, $sEmail ) != 0 );
00306         }
00307 
00308         return $blValid;
00309     }
00310 
00318     public function loadAdminProfile($aInterfaceProfiles)
00319     {
00320         // improved #533
00321         // checking for available profiles list
00322         $aInterfaceProfiles = $aInterfaceProfiles;
00323         if ( is_array( $aInterfaceProfiles ) ) {
00324             //checking for previous profiles
00325             $sPrevProfile = oxRegistry::get("oxUtilsServer")->getOxCookie('oxidadminprofile');
00326             if (isset($sPrevProfile)) {
00327                 $aPrevProfile = @explode("@", trim($sPrevProfile));
00328             }
00329 
00330             //array to store profiles
00331             $aProfiles = array();
00332             foreach ( $aInterfaceProfiles as $iPos => $sProfile) {
00333                 $aProfileSettings = array($iPos, $sProfile);
00334                 $aProfiles[] = $aProfileSettings;
00335             }
00336             // setting previous used profile as active
00337             if (isset($aPrevProfile[0]) && isset($aProfiles[$aPrevProfile[0]])) {
00338                 $aProfiles[$aPrevProfile[0]][2] = 1;
00339             }
00340 
00341             oxSession::setVar("aAdminProfiles", $aProfiles);
00342             return $aProfiles;
00343         }
00344         return null;
00345     }
00346 
00355     public function fRound($sVal, $oCur = null)
00356     {
00357         startProfile('fround');
00358 
00359         //cached currency precision, this saves about 1% of execution time
00360         $iCurPrecision = null;
00361         if (! defined('OXID_PHP_UNIT')) {
00362             $iCurPrecision = $this->_iCurPrecision;
00363         }
00364 
00365         if (is_null($iCurPrecision)) {
00366             if ( !$oCur ) {
00367                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00368             }
00369 
00370             $iCurPrecision = $oCur->decimal;
00371             $this->_iCurPrecision = $iCurPrecision;
00372         }
00373 
00374         // if < 5.3.x this is a workaround for #36008 bug in php - incorrect round() & number_format() result (R)
00375         static $dprez = null;
00376         if (!$dprez) {
00377             $prez = @ini_get("precision");
00378             if (!$prez || $prez > 12 ) {
00379                $prez = 12;
00380             }
00381             $dprez = pow(10, -$prez);
00382         }
00383         stopProfile('fround');
00384         return round($sVal + $dprez * ( $sVal >= 0 ? 1 : -1 ), $iCurPrecision);
00385     }
00386 
00396     public function toStaticCache( $sName, $sContent, $sKey = null )
00397     {
00398         // if it's an array then we add
00399         if ( $sKey ) {
00400             $this->_aStaticCache[$sName][$sKey] = $sContent;
00401         } else {
00402             $this->_aStaticCache[$sName] = $sContent;
00403         }
00404     }
00405 
00413     public function fromStaticCache( $sName)
00414     {
00415         if ( isset( $this->_aStaticCache[$sName])) {
00416             return $this->_aStaticCache[$sName];
00417         }
00418         return null;
00419     }
00420 
00428     public function cleanStaticCache($sCacheName = null)
00429     {
00430         if ($sCacheName) {
00431             unset($this->_aStaticCache[$sCacheName]);
00432         } else {
00433             $this->_aStaticCache = null;
00434         }
00435     }
00436 
00446     public function toPhpFileCache( $sKey, $mContents )
00447     {
00448         //only simple arrays are supported
00449         if ( is_array( $mContents ) && ( $sCachePath = $this->getCacheFilePath( $sKey, false, 'php' ) ) ) {
00450 
00451             // setting meta
00452             $this->setCacheMeta( $sKey, array( "serialize" => false, "cachepath" => $sCachePath ) );
00453 
00454             // caching..
00455             $this->toFileCache( $sKey, $mContents );
00456         }
00457     }
00458 
00466     public function fromPhpFileCache( $sKey )
00467     {
00468         // setting meta
00469         $this->setCacheMeta( $sKey, array( "include" => true, "cachepath" => $this->getCacheFilePath( $sKey, false, 'php' ) ) );
00470         return $this->fromFileCache( $sKey );
00471     }
00472 
00480     public function getCacheMeta( $sKey )
00481     {
00482         return isset( $this->_aFileCacheMeta[$sKey] ) ? $this->_aFileCacheMeta[$sKey] : false;
00483     }
00484 
00493     public function setCacheMeta( $sKey, $aMeta )
00494     {
00495         // cache meta data
00496         $this->_aFileCacheMeta[$sKey] = $aMeta;
00497     }
00498 
00508     public function toFileCache( $sKey, $mContents )
00509     {
00510         $this->_aFileCacheContents[$sKey] = $mContents;
00511         $aMeta = $this->getCacheMeta( $sKey );
00512 
00513         // looking for cache meta
00514         $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00515         return ( bool ) $this->_lockFile( $sCachePath, $sKey );
00516     }
00517 
00525     public function fromFileCache( $sKey )
00526     {
00527         if ( !array_key_exists( $sKey, $this->_aFileCacheContents ) ) {
00528             $sRes = null;
00529 
00530             $aMeta = $this->getCacheMeta( $sKey );
00531             $blInclude  = isset( $aMeta["include"] ) ? $aMeta["include"] : false;
00532             $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00533 
00534             // trying to lock
00535             $this->_lockFile( $sCachePath, $sKey, LOCK_SH );
00536 
00537             clearstatcache();
00538             if ( is_readable( $sCachePath ) ) {
00539                 $sRes = $blInclude ? $this->_includeFile( $sCachePath ) : $this->_readFile( $sCachePath );
00540             }
00541 
00542             // release lock
00543             $this->_releaseFile( $sKey, LOCK_SH );
00544 
00545             // caching
00546             $this->_aFileCacheContents[$sKey] = $sRes;
00547         }
00548 
00549         return $this->_aFileCacheContents[$sKey];
00550     }
00551 
00559     protected function _readFile( $sFilePath )
00560     {
00561         $sRes = file_get_contents( $sFilePath );
00562         return $sRes ? unserialize( $sRes ) : null;
00563     }
00564 
00572     protected function _includeFile( $sFilePath )
00573     {
00574         $_aCacheContents = null;
00575         include $sFilePath;
00576         return $_aCacheContents;
00577     }
00578 
00587     protected function _processCache( $sKey, $mContents )
00588     {
00589         // looking for cache meta
00590         $aCacheMeta  = $this->getCacheMeta( $sKey );
00591         $blSerialize = isset( $aCacheMeta["serialize"] ) ? $aCacheMeta["serialize"] : true;
00592 
00593         if ( $blSerialize ) {
00594             $mContents = serialize( $mContents );
00595         } else {
00596             $mContents = "<?php\n//automatically generated file\n//" . date( "Y-m-d H:i:s" ) . "\n\n\$_aCacheContents = " . var_export( $mContents, true ) . "\n?>";
00597         }
00598 
00599         return $mContents;
00600     }
00601 
00608     public function commitFileCache()
00609     {
00610         if ( count( $this->_aLockedFileHandles[LOCK_EX] ) ) {
00611             startProfile("!__SAVING CACHE__! (warning)");
00612             foreach ( $this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle ) {
00613                 if ( $rHandle !== false && isset( $this->_aFileCacheContents[$sKey] ) ) {
00614 
00615                     // #0002931A truncate file once more before writing
00616                     ftruncate( $rHandle, 0 );
00617 
00618                     // writing cache
00619                     fwrite( $rHandle, $this->_processCache( $sKey, $this->_aFileCacheContents[$sKey] ) );
00620 
00621                     // releasing locks
00622                     $this->_releaseFile( $sKey );
00623                 }
00624             }
00625 
00626             stopProfile("!__SAVING CACHE__! (warning)");
00627 
00628             //empty buffer
00629             $this->_aFileCacheContents = array();
00630         }
00631     }
00632 
00642     protected function _lockFile( $sFilePath, $sIdent, $iLockMode = LOCK_EX )
00643     {
00644         $rHandle = isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
00645         if ( $rHandle === null ) {
00646 
00647             $blLocked = false;
00648             $rHandle = @fopen( $sFilePath, "a+" );
00649 
00650             if ( $rHandle !== false ) {
00651 
00652                 if ( flock( $rHandle, $iLockMode | LOCK_NB ) ) {
00653                     if ( $iLockMode === LOCK_EX ) {
00654                         // truncate file
00655                         $blLocked = ftruncate( $rHandle, 0 );
00656                     } else {
00657                         // move to a start position
00658                         $blLocked = fseek( $rHandle, 0 ) === 0;
00659                     }
00660                 }
00661 
00662                 // on failure - closing and setting false..
00663                 if ( !$blLocked ) {
00664                     fclose( $rHandle );
00665                     $rHandle = false;
00666                 }
00667             }
00668 
00669             // in case system does not support file lockings
00670             if ( !$blLocked && $iLockMode === LOCK_EX ) {
00671 
00672                 // clearing on first call
00673                 if ( count( $this->_aLockedFileHandles ) == 0 ) {
00674                     clearstatcache();
00675                 }
00676 
00677                 // start a blank file to inform other processes we are dealing with it.
00678                 if (!( file_exists( $sFilePath ) && !filesize( $sFilePath ) && abs( time() - filectime( $sFilePath ) < 40 ) ) ) {
00679                     $rHandle = @fopen( $sFilePath, "w" );
00680                 }
00681             }
00682 
00683             $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
00684         }
00685 
00686         return $rHandle;
00687     }
00688 
00697     protected function _releaseFile( $sIdent, $iLockMode = LOCK_EX )
00698     {
00699         $blSuccess = true;
00700         if ( isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) &&
00701              $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false ) {
00702 
00703              // release the lock and close file
00704             $blSuccess = flock( $this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN ) &&
00705                          fclose( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00706             unset( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00707         }
00708 
00709         return $blSuccess;
00710     }
00711 
00719     public function oxResetFileCache()
00720     {
00721         $aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
00722         if ( is_array( $aFiles ) ) {
00723             // delete all the files, except cached tables fieldnames
00724             $aFiles = preg_grep( $this->_sPermanentCachePattern, $aFiles, PREG_GREP_INVERT );
00725             foreach ( $aFiles as $sFile ) {
00726                 @unlink( $sFile );
00727             }
00728         }
00729     }
00730 
00738     public function resetTemplateCache($aTemplates)
00739     {
00740         $sSmartyDir = oxRegistry::get("oxUtilsView")->getSmartyDir();
00741         //$aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
00742         $aFiles = glob( $sSmartyDir . '*' );
00743 
00744         if ( is_array( $aFiles ) && is_array( $aTemplates ) && count($aTemplates) ) {
00745             // delete all template cache files
00746             foreach ($aTemplates as &$sTemplate) {
00747                 $sTemplate = preg_quote(basename(strtolower($sTemplate), '.tpl'));
00748             }
00749 
00750             $sPattern = sprintf("/%%(%s)\.tpl\.php$/i", implode('|', $aTemplates));
00751             $aFiles = preg_grep( $sPattern, $aFiles );
00752 
00753             if (is_array( $aFiles ) ) {
00754                 foreach ( $aFiles as $sFile ) {
00755                     @unlink( $sFile );
00756                 }
00757             }
00758         }
00759 
00760     }
00761 
00767     public function resetLanguageCache()
00768     {
00769         $aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
00770         if ( is_array( $aFiles ) ) {
00771             // delete all language cache files
00772             $sPattern = $this->_sLanguageCachePattern;
00773             $aFiles = preg_grep( $sPattern, $aFiles );
00774             foreach ( $aFiles as $sFile ) {
00775                 @unlink( $sFile );
00776             }
00777         }
00778     }
00779 
00785     public function resetMenuCache()
00786     {
00787         $aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
00788         if ( is_array( $aFiles ) ) {
00789             // delete all menu cache files
00790             $sPattern = $this->_sMenuCachePattern;
00791             $aFiles = preg_grep( $sPattern, $aFiles );
00792             foreach ( $aFiles as $sFile ) {
00793                 @unlink( $sFile );
00794             }
00795         }
00796     }
00797 
00807     public function getRemoteCachePath($sRemote, $sLocal)
00808     {
00809         clearstatcache();
00810         if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
00811             return $sLocal;
00812         }
00813         $hRemote = @fopen( $sRemote, "rb");
00814         $blSuccess = false;
00815         if ( isset( $hRemote) && $hRemote ) {
00816             $hLocal = fopen( $sLocal, "wb");
00817             stream_copy_to_stream($hRemote, $hLocal);
00818             fclose($hRemote);
00819             fclose($hLocal);
00820             $blSuccess = true;
00821         } else {
00822             // try via fsockopen
00823             $aUrl = @parse_url( $sRemote);
00824             if ( !empty( $aUrl["host"])) {
00825                 $sPath = $aUrl["path"];
00826                 if ( empty( $sPath ) ) {
00827                     $sPath = "/";
00828                 }
00829                 $sHost = $aUrl["host"];
00830 
00831                 $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
00832                 if ( $hSocket) {
00833                     fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00834                     $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
00835                     if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
00836                         rewind($hLocal);
00837                         // does not copy all the data
00838                         // stream_copy_to_stream($hSocket, $hLocal);
00839                         fwrite ( $hLocal, stream_get_contents( $hSocket ) );
00840                         fclose( $hLocal );
00841                         fclose( $hSocket );
00842                         $blSuccess = true;
00843                     }
00844                 }
00845             }
00846         }
00847         if ( $blSuccess || file_exists( $sLocal ) ) {
00848             return $sLocal;
00849         }
00850         return false;
00851     }
00852 
00858     public function canPreview()
00859     {
00860         $blCan = null;
00861         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00862              ( $sAdminSid = oxRegistry::get("oxUtilsServer")->getOxCookie( 'admin_sid' ) ) ) {
00863 
00864             $sTable = getViewName( 'oxuser' );
00865             $oDb = oxDb::getDb();
00866             $sQ = "select 1 from $sTable where MD5( CONCAT( ".$oDb->quote($sAdminSid).", {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = ".oxDb::getDb()->quote($sPrevId);
00867             $blCan = (bool) $oDb->getOne( $sQ );
00868         }
00869 
00870         return $blCan;
00871     }
00872 
00878     public function getPreviewId()
00879     {
00880         $sAdminSid = oxRegistry::get("oxUtilsServer")->getOxCookie( 'admin_sid' );
00881         if ( ( $oUser = $this->getUser() ) ) {
00882             return md5( $sAdminSid . $oUser->getId() . $oUser->oxuser__oxpassword->value . $oUser->oxuser__oxrights->value );
00883         }
00884     }
00885 
00891     public function checkAccessRights()
00892     {
00893         $myConfig  = $this->getConfig();
00894 
00895         $blIsAuth = false;
00896 
00897         $sUserID = oxSession::getVar( "auth");
00898 
00899         // deleting admin marker
00900         oxSession::setVar( "malladmin", 0);
00901         oxSession::setVar( "blIsAdmin", 0);
00902         oxSession::deleteVar( "blIsAdmin" );
00903         $myConfig->setConfigParam( 'blMallAdmin', false );
00904         //#1552T
00905         $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
00906 
00907         if ( $sUserID) {
00908             // escaping
00909             $oDb = oxDb::getDb();
00910             $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
00911 
00912             if ( $sRights != "user") {
00913                 // malladmin ?
00914                 if ( $sRights == "malladmin") {
00915                     oxSession::setVar( "malladmin", 1);
00916                     $myConfig->setConfigParam( 'blMallAdmin', true );
00917 
00918                     //#1552T
00919                     //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
00920                     $myConfig->setConfigParam( 'blAllowSharedEdit', true );
00921 
00922                     $sShop = oxSession::getVar( "actshop");
00923                     if ( !isset($sShop)) {
00924                         oxSession::setVar( "actshop", $myConfig->getBaseShopId());
00925                     }
00926                     $blIsAuth = true;
00927                 } else {
00928                     // Shopadmin... check if this shop is valid and exists
00929                     $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
00930                     if ( isset( $sShopID) && $sShopID) {
00931                         // success, this shop exists
00932 
00933                         oxSession::setVar( "actshop", $sRights);
00934                         oxSession::setVar( "currentadminshop", $sRights);
00935                         oxSession::setVar( "shp", $sRights);
00936 
00937                         // check if this subshop admin is evil.
00938                         if ('chshp' == oxConfig::getParameter( 'fnc' )) {
00939                             // dont allow this call
00940                             $blIsAuth = false;
00941                         } else {
00942                             $blIsAuth = true;
00943 
00944                             $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
00945                             foreach ($aShopIdVars as $sShopIdVar) {
00946                                 if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
00947                                     if ($sGotShop != $sRights) {
00948                                         $blIsAuth = false;
00949                                         break;
00950                                     }
00951                                 }
00952                             }
00953                         }
00954                     }
00955                 }
00956                 // marking user as admin
00957                 oxSession::setVar( "blIsAdmin", 1);
00958             }
00959         }
00960         return $blIsAuth;
00961     }
00962 
00972     public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
00973     {
00974         if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
00975             return $this->_blSeoIsActive;
00976         }
00977 
00978         $myConfig = $this->getConfig();
00979 
00980         if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
00981             $this->_blSeoIsActive = true;
00982 
00983             $aSeoModes  = $myConfig->getconfigParam( 'aSeoModes' );
00984             $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
00985             $iActLang   = $iActLang ? $iActLang : (int) oxRegistry::getLang()->getBaseLanguage();
00986 
00987             // checking special config param for active shop and language
00988             if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
00989                 $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
00990             }
00991         }
00992 
00993         return $this->_blSeoIsActive;
00994     }
00995 
01003     public function isValidAlpha( $sField )
01004     {
01005         return (boolean) getStr()->preg_match( '/^[a-zA-Z0-9_]*$/', $sField );
01006     }
01007 
01017     protected function _simpleRedirect( $sUrl, $sHeaderCode )
01018     {
01019         header( $sHeaderCode );
01020         header( "Location: $sUrl" );
01021         header( "Connection: close" );
01022     }
01023 
01037     public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 301 )
01038     {
01039         //preventing possible cyclic redirection
01040         //#M341 and check only if redirect paramater must be added
01041         if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
01042             return;
01043         }
01044 
01045         if ( $blAddRedirectParam ) {
01046             $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
01047         }
01048 
01049         $sUrl = str_ireplace( "&amp;", "&", $sUrl );
01050 
01051         $sHeaderCode = '';
01052         switch ($iHeaderCode) {
01053             case 301:
01054                 $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
01055                 break;
01056             case 302:
01057             default:
01058                 $sHeaderCode = "HTTP/1.1 302 Found";
01059         }
01060 
01061         $this->_simpleRedirect( $sUrl, $sHeaderCode );
01062 
01063         try {//may occur in case db is lost
01064             $this->getSession()->freeze();
01065         } catch( oxException $oEx ) {
01066             $oEx->debugOut();
01067             //do nothing else to make sure the redirect takes place
01068         }
01069 
01070         if ( defined( 'OXID_PHP_UNIT' ) ) {
01071             return;
01072         }
01073 
01074         $this->showMessageAndExit( '' );
01075     }
01076 
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 
01095         exit( $sMsg );
01096     }
01097 
01105     public function setHeader($sHeader)
01106     {
01107         header($sHeader);
01108     }
01109 
01118     protected function _addUrlParameters( $sUrl, $aParams )
01119     {
01120         $sDelim = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
01121         foreach ( $aParams as $sName => $sVal ) {
01122             $sUrl = $sUrl . $sDelim . $sName . '=' . $sVal;
01123             $sDelim = '&';
01124         }
01125 
01126         return $sUrl;
01127     }
01128 
01140     protected function _fillExplodeArray( $aName, $dVat = null)
01141     {
01142         $myConfig = $this->getConfig();
01143         $oObject = new stdClass();
01144         $aPrice = explode( '!P!', $aName[0]);
01145 
01146         if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
01147 
01148             // yes, price is there
01149             $oObject->price = isset( $aPrice[1] ) ? $aPrice[1] : 0;
01150             $aName[0] = isset( $aPrice[0] ) ? $aPrice[0] : '';
01151 
01152             $iPercPos = getStr()->strpos( $oObject->price, '%' );
01153             if ( $iPercPos !== false ) {
01154                 $oObject->priceUnit = '%';
01155                 $oObject->fprice = $oObject->price;
01156                 $oObject->price  = substr( $oObject->price, 0, $iPercPos );
01157             } else {
01158                 $oCur = $myConfig->getActShopCurrencyObject();
01159                 $oObject->price = str_replace(',', '.', $oObject->price);
01160                 $oObject->fprice = oxRegistry::getLang()->formatCurrency( $oObject->price  * $oCur->rate, $oCur);
01161                 $oObject->priceUnit = 'abs';
01162             }
01163 
01164             // add price info into list
01165             if ( !$this->isAdmin() && $oObject->price != 0 ) {
01166                 $aName[0] .= " ";
01167                 if ( $oObject->price > 0 ) {
01168                     $aName[0] .= "+";
01169                 }
01170                 //V FS#2616
01171                 if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
01172                     $oPrice = oxNew('oxPrice');
01173                     $oPrice->setPrice($oObject->price, $dVat);
01174                     $aName[0] .= oxRegistry::getLang()->formatCurrency( $oPrice->getBruttoPrice() * $oCur->rate, $oCur);
01175                 } else {
01176                     $aName[0] .= $oObject->fprice;
01177                 }
01178                 if ( $oObject->priceUnit == 'abs' ) {
01179                     $aName[0] .= " ".$oCur->sign;
01180                 }
01181             }
01182         } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
01183             // A. removing unused part of information
01184             $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
01185         }
01186 
01187         $oObject->name  = $aName[0];
01188         $oObject->value = $aName[1];
01189         return $oObject;
01190     }
01191 
01199     public function oxMimeContentType( $sFileName )
01200     {
01201         $sFileName = strtolower( $sFileName );
01202         $iLastDot  = strrpos( $sFileName, '.' );
01203 
01204         if ( $iLastDot !== false ) {
01205             $sType = substr( $sFileName, $iLastDot + 1 );
01206             switch ( $sType ) {
01207                 case 'gif':
01208                     $sType = 'image/gif';
01209                     break;
01210                 case 'jpeg':
01211                 case 'jpg':
01212                     $sType = 'image/jpeg';
01213                     break;
01214                 case 'png':
01215                     $sType = 'image/png';
01216                     break;
01217                 default:
01218                     $sType = false;
01219                     break;
01220             }
01221         }
01222         return $sType;
01223     }
01224 
01233     public function logger( $sText, $blNewline = false )
01234     {   $myConfig = $this->getConfig();
01235 
01236         if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
01237             if ( gettype( $sText ) != 'string' ) {
01238                 $sText = var_export( $sText, true);
01239             }
01240             $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
01241             $this->writeToLog( $sLogMsg, "log.txt" );
01242         }
01243 
01244     }
01245 
01253     protected function _stripQuotes($mInput)
01254     {
01255         return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
01256     }
01257 
01265     public function strRot13( $sStr )
01266     {
01267         $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
01268         $sTo   = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
01269 
01270         return strtr( $sStr, $sFrom, $sTo );
01271     }
01272 
01282     public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
01283     {
01284         $sVersionPrefix = "";
01285 
01286 
01287             $sVersionPrefix = 'pe';
01288 
01289         $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
01290 
01291         if (!$sPath) {
01292             return false;
01293         }
01294 
01295         return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
01296     }
01297 
01305     public function getLangCache( $sCacheName )
01306     {
01307         $aLangCache = null;
01308         $sFilePath = $this->getCacheFilePath( $sCacheName );
01309         if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
01310             include $sFilePath;
01311         }
01312         return $aLangCache;
01313     }
01314 
01323     public function setLangCache( $sCacheName, $aLangCache )
01324     {
01325         $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";\n?>";
01326         $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache);
01327         return $blRes;
01328     }
01329 
01337     public function checkUrlEndingSlash( $sUrl )
01338     {
01339         if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
01340             $sUrl .= '/';
01341         }
01342 
01343         return $sUrl;
01344     }
01345 
01354     public function writeToLog( $sLogMessage, $sLogFileName )
01355     {
01356         $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
01357         $blOk = false;
01358 
01359         if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
01360             fwrite( $oHandle, $sLogMessage );
01361             $blOk = fclose( $oHandle );
01362         }
01363 
01364         return $blOk;
01365     }
01366 
01374     public function handlePageNotFoundError($sUrl = '')
01375     {
01376         $this->setHeader("HTTP/1.0 404 Not Found");
01377         if ( oxRegistry::getConfig()->isUtf() ) {
01378             $this->setHeader("Content-Type: text/html; charset=UTF-8");
01379         }
01380 
01381         $sReturn = "Page not found.";
01382         try {
01383             $oView = oxNew('oxubase');
01384             $oView->init();
01385             $oView->render();
01386             $oView->setClassName( 'oxubase' );
01387             $oView->addTplParam('sUrl', $sUrl);
01388             if ($sRet = oxRegistry::get("oxUtilsView")->getTemplateOutput('message/err_404.tpl', $oView)) {
01389                 $sReturn = $sRet;
01390             }
01391         } catch (Exception $e) {
01392         }
01393         $this->showMessageAndExit( $sReturn );
01394     }
01395 
01403     public function extractDomain( $sHost )
01404     {
01405         $oStr = getStr();
01406         if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
01407              ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
01408             $iLen = $oStr->strlen( $sHost );
01409             if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
01410                 $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
01411             }
01412         }
01413 
01414         return $sHost;
01415     }
01416 }