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 $_aFileCacheContents = array();
00050 
00056     protected $_aFileCacheWritable = array();
00057 
00063     protected $_blIsSe = null;
00064 
00070     public static function getInstance()
00071     {
00072         // disable caching for test modules
00073         if ( defined( 'OXID_PHP_UNIT' ) ) {
00074             self::$_instance = modInstances::getMod( __CLASS__ );
00075         }
00076 
00077         if ( !(self::$_instance instanceof oxUtils) ) {
00078 
00079             self::$_instance = oxNew( 'oxUtils' );
00080 
00081             if ( defined( 'OXID_PHP_UNIT' ) ) {
00082                 modInstances::addMod( __CLASS__, self::$_instance);
00083             }
00084         }
00085         return self::$_instance;
00086     }
00087 
00093     protected $_aStaticCache;
00094 
00100     protected $_blSeoIsActive = null;
00101 
00107     public function stripGpcMagicQuotes()
00108     {
00109         if (!get_magic_quotes_gpc()) {
00110             return;
00111         }
00112         $_REQUEST = self::_stripQuotes($_REQUEST);
00113         $_POST = self::_stripQuotes($_POST);
00114         $_GET = self::_stripQuotes($_GET);
00115         $_COOKIE = self::_stripQuotes($_COOKIE);
00116     }
00117 
00126     public function strMan( $sVal, $sKey = null )
00127     {
00128         $sKey = $sKey?$sKey:'oxid123456789';
00129         $sVal = "ox{$sVal}id";
00130 
00131         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00132         $sVal = $this->strRot13( $sVal );
00133         $sVal = $sVal ^ $sKey;
00134         $sVal = base64_encode( $sVal );
00135         $sVal = str_replace( "=", "!", $sVal );
00136 
00137         return "ox_$sVal";
00138     }
00139 
00148     public function strRem( $sVal, $sKey = null )
00149     {
00150         $sKey = $sKey?$sKey:'oxid123456789';
00151         $sKey = str_repeat( $sKey, strlen( $sVal ) / strlen( $sKey ) + 5 );
00152 
00153         $sVal = substr( $sVal, 3 );
00154         $sVal = str_replace( '!', '=', $sVal );
00155         $sVal = base64_decode( $sVal );
00156         $sVal = $sVal ^ $sKey;
00157         $sVal = $this->strRot13( $sVal );
00158 
00159         return substr( $sVal, 2, -2 );
00160     }
00161 
00169     public function getArrFldName( $sName)
00170     {
00171         return str_replace( ".", "__", $sName);
00172     }
00173 
00182     public function assignValuesFromText( $sIn, $dVat = null)
00183     {
00184         $aRet = array();
00185         $aPieces = explode( '@@', $sIn );
00186         while ( list( $sKey, $sVal ) = each( $aPieces ) ) {
00187             if ( $sVal ) {
00188                 $aName = explode( '__', $sVal );
00189                 if ( isset( $aName[0] ) && isset( $aName[1] ) ) {
00190                     $aRet[] = $this->_fillExplodeArray( $aName, $dVat );
00191                 }
00192             }
00193         }
00194         return $aRet;
00195     }
00196 
00204     public function assignValuesToText( $aIn)
00205     {
00206         $sRet = "";
00207         reset( $aIn );
00208         while (list($sKey, $sVal) = each($aIn)) {
00209             $sRet .= $sKey;
00210             $sRet .= "__";
00211             $sRet .= $sVal;
00212             $sRet .= "@@";
00213         }
00214         return $sRet;
00215     }
00216 
00227     public function formatCurrency( $dValue, $oActCur = null )
00228     {
00229         if (!$oActCur) {
00230             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00231         }
00232         $sFormated = number_format( (double)$dValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand);
00233 
00234         return $sFormated;
00235     }
00236 
00244     public function currency2Float( $sValue)
00245     {
00246         $fRet = $sValue;
00247         $iPos = strrpos( $sValue, ".");
00248         if ($iPos && ((strlen($sValue)-1-$iPos) < 2+1)) {
00249             // replace decimal with ","
00250             $fRet = substr_replace( $fRet, ",", $iPos, 1);
00251         }
00252         // remove thousands
00253         $fRet = str_replace( array(" ","."), "", $fRet);
00254 
00255         $fRet = str_replace( ",", ".", $fRet);
00256         return (float) $fRet;
00257     }
00258 
00266     public function isSearchEngine( $sClient = null )
00267     {
00268 
00269         if (!is_null($this->_blIsSe)) {
00270             return $this->_blIsSe;
00271         }
00272 
00273         startProfile("isSearchEngine");
00274 
00275         $myConfig = $this->getConfig();
00276         $blIsSe   = false;
00277 
00278         if ( !( $myConfig->getConfigParam( 'iDebug' ) && $this->isAdmin() ) ) {
00279 
00280             // caching
00281             $blIsSe = $myConfig->getGlobalParameter( 'blIsSearchEngine' );
00282             if ( !isset( $blIsSe ) ) {
00283 
00284                 $aRobots = $myConfig->getConfigParam( 'aRobots' );
00285                 $aRobots = is_array( $aRobots )?$aRobots:array();
00286 
00287                 $aRobotsExcept = $myConfig->getConfigParam( 'aRobotsExcept' );
00288                 $aRobotsExcept = is_array( $aRobotsExcept )?$aRobotsExcept:array();
00289 
00290                 $sClient = $sClient?$sClient:strtolower( getenv( 'HTTP_USER_AGENT' ) );
00291                 $blIsSe  = false;
00292                 $aRobots = array_merge( $aRobots, $aRobotsExcept );
00293                 foreach ( $aRobots as $sRobot ) {
00294                     if ( strpos( $sClient, $sRobot ) !== false ) {
00295                         $blIsSe = true;
00296                         break;
00297                     }
00298                 }
00299                 $myConfig->setGlobalParameter( 'blIsSearchEngine', $blIsSe );
00300             }
00301         }
00302 
00303         stopProfile("isSearchEngine");
00304 
00305         $this->_blIsSe = $blIsSe;
00306 
00307         return $blIsSe;
00308     }
00309 
00318     public function isValidEmail( $sEmail )
00319     {
00320         $blValid = true;
00321         if ( $sEmail != 'admin' ) {
00322             $blValid = ( getStr()->preg_match( $this->_sEmailTpl, $sEmail ) != 0 );
00323         }
00324 
00325         return $blValid;
00326     }
00327 
00333     public function rebuildCache()
00334     {
00335         // not needed from 3.0 on and unused <- MK: not correct, its used for example in shop_config.php, oxbase.php
00336 
00337         //$smarty  = & oxUtils::getInstance()->getSmarty();
00338         //$smarty->clear_all_cache();
00339 
00340         if ( function_exists( "UserdefinedRebuildCache")) {
00341             UserdefinedRebuildCache();
00342         }
00343     }
00344 
00352     public function loadAdminProfile($aInterfaceProfiles)
00353     {
00354         // improved #533
00355         // checking for available profiles list
00356         $aInterfaceProfiles = $aInterfaceProfiles;
00357         if ( is_array( $aInterfaceProfiles ) ) {
00358             //checking for previous profiles
00359             $sPrevProfile = oxUtilsServer::getInstance()->getOxCookie('oxidadminprofile');
00360             if (isset($sPrevProfile)) {
00361                 $aPrevProfile = @explode("@", trim($sPrevProfile));
00362             }
00363 
00364             //array to store profiles
00365             $aProfiles = array();
00366             foreach ( $aInterfaceProfiles as $iPos => $sProfile) {
00367                 $aProfileSettings = array($iPos, $sProfile);
00368                 $aProfiles[] = $aProfileSettings;
00369             }
00370             // setting previous used profile as active
00371             if (isset($aPrevProfile[0]) && isset($aProfiles[$aPrevProfile[0]])) {
00372                 $aProfiles[$aPrevProfile[0]][2] = 1;
00373             }
00374 
00375             oxSession::setVar("aAdminProfiles", $aProfiles);
00376             return $aProfiles;
00377         }
00378         return null;
00379     }
00380 
00389     public function fRound($sVal, $oCur = null)
00390     {
00391         startProfile('fround');
00392 
00393         //cached currency precision, this saves about 1% of execution time
00394         $iCurPrecision = null;
00395         if (! defined('OXID_PHP_UNIT')) {
00396             $iCurPrecision = $this->_iCurPrecision;
00397         }
00398 
00399         if (is_null($iCurPrecision)) {
00400             if ( !$oCur ) {
00401                 $oCur = $this->getConfig()->getActShopCurrencyObject();
00402             }
00403 
00404             $iCurPrecision = $oCur->decimal;
00405             $this->_iCurPrecision = $iCurPrecision;
00406         }
00407 
00408         // this is a workaround for #36008 bug in php - incorrect round() & number_format() result (R)
00409         static $dprez = null;
00410         if (!$dprez) {
00411             $prez = @ini_get("precision");
00412             if (!$prez) {
00413                 $prez = 9;
00414             }
00415             $dprez = pow(10, -$prez);
00416         }
00417 
00418 
00419         stopProfile('fround');
00420 
00421         return round($sVal + $dprez, $iCurPrecision);
00422     }
00423 
00433     public function toStaticCache( $sName, $sContent, $sKey = null )
00434     {
00435         // if it's an array then we add
00436         if ( $sKey ) {
00437             $this->_aStaticCache[$sName][$sKey] = $sContent;
00438         } else {
00439             $this->_aStaticCache[$sName] = $sContent;
00440         }
00441     }
00442 
00450     public function fromStaticCache( $sName)
00451     {
00452         if ( isset( $this->_aStaticCache[$sName])) {
00453             return $this->_aStaticCache[$sName];
00454         }
00455         return null;
00456     }
00457 
00465     public function cleanStaticCache($sCacheName = null)
00466     {
00467         if ($sCacheName) {
00468             unset($this->_aStaticCache[$sCacheName]);
00469         } else {
00470             $this->_aStaticCache = null;
00471         }
00472     }
00473 
00483     public function toPhpFileCache($sKey, $mContents)
00484     {
00485         $sFilePath = $this->getCacheFilePath( $sKey, false, 'php' );
00486 
00487         if (!$sFilePath) {
00488             return;
00489         }
00490 
00491         $sDate = date("Y-m-d H:i:s");
00492         $sVarName = '$_aCacheContents';
00493 
00494         //only simple arrays are supported
00495         if (!is_array($mContents))
00496             return;
00497 
00498         $sContents = "<?php ?>";
00499         if (is_array($mContents)) {
00500             $sContents  = "<?php\n//automatically generated file\n//$sDate\n\n$sVarName = array (\n";
00501             foreach ($mContents as $sKey => $mVal) {
00502                 if (!is_numeric($mVal)) {
00503                     $mVal = "'$mVal'";
00504                 }
00505                 if (!is_numeric($sKey)) {
00506                     $sKey = "'$sKey'";
00507                 }
00508                 $sContents .= "  $sKey => $mVal,\n";
00509             }
00510             $sContents .= ");\n?>";
00511         }
00512 
00513         $hFile = fopen( $sFilePath, "w");
00514         if ( $hFile) {
00515             fwrite( $hFile, $sContents);
00516             fclose( $hFile);
00517         }
00518 
00519     }
00520 
00528     public function fromPhpFileCache($sKey)
00529     {
00530         $sFilePath = $this->getCacheFilePath( $sKey, false, 'php' );
00531 
00532         if (file_exists($sFilePath)) {
00533             include $sFilePath;
00534             return $_aCacheContents;
00535         }
00536 
00537         return null;
00538     }
00539 
00549     public function toFileCache($sKey, $mContents)
00550     {
00551         $sFilePath = $this->getCacheFilePath( $sKey );
00552         $iCurTime = oxUtilsDate::getInstance()->getTime();
00553 
00554         //T2009-05-26
00555         //due to possible race conditions
00556         //check if there are any other cache files already opened for writing by another process
00557         //additionally perform the check for older (aged 40 or more secs) locked files
00558         clearstatcache();
00559         if (!isset($this->_aFileCacheContents[$sKey]) && file_exists($sFilePath) && (!filesize($sFilePath)) && abs($iCurTime - filectime($sFilePath) < 40) ) {
00560             //then leave the cache to be dealt by another process and do nothing
00561             return false;
00562         }
00563         //the above code ensures that $mContents is writen only in case cache file has not been started
00564         //by another process
00565 
00566         //start a blank file to inform other processes we are dealing with it.
00567         $hFile = fopen($sFilePath, "w");
00568         if ($hFile) {
00569             fclose($hFile);
00570         }
00571         clearstatcache();
00572 
00573         $this->_aFileCacheWritable[$sKey] = $mContents;
00574         $this->_aFileCacheContents[$sKey] = $mContents;
00575 
00576         return true;
00577     }
00578 
00586     public function fromFileCache( $sKey )
00587     {
00588         if ( !isset( $this->_aFileCacheContents[$sKey] ) ) {
00589             $sRes = null;
00590 
00591             // read the file
00592             $sFilePath = $this->getCacheFilePath( $sKey );
00593 
00594             $blFileExists = file_exists( $sFilePath ) && is_readable( $sFilePath );
00595 
00596             if ( $blFileExists ) {
00597                 // read it
00598                 $sRes = file_get_contents( $sFilePath );
00599                 $sRes = $sRes ? unserialize( $sRes ) : null;
00600             }
00601 
00602             $this->_aFileCacheContents[$sKey] = $sRes;
00603         }
00604 
00605         return $this->_aFileCacheContents[$sKey];
00606     }
00607 
00613     public function commitFileCache()
00614     {
00615         foreach ($this->_aFileCacheWritable as $sKey => $mContents) {
00616             startProfile("!__SAVING CACHE__! (warning)");
00617             $mContents = serialize($mContents);
00618             $sFilePath = $this->getCacheFilePath( $sKey );
00619             $hFile = fopen( $sFilePath, "w");
00620             if ( $hFile) {
00621                 fwrite( $hFile, $mContents);
00622                 fclose( $hFile);
00623             }
00624             stopProfile("!__SAVING CACHE__! (warning)");
00625         }
00626 
00627         //empty buffer
00628         $this->_aFileCacheWritable = array();
00629         clearstatcache ();
00630     }
00631 
00639     public function oxResetFileCache()
00640     {
00641         $aPathes = glob( $this->getCacheFilePath( null, true ) . '*' );
00642         if ( is_array( $aPathes ) ) {
00643             // delete all the files, except cached tables fieldnames
00644             $aPathes = preg_grep( $this->_sPermanentCachePattern, $aPathes, PREG_GREP_INVERT );
00645             foreach ( $aPathes as $sFilename ) {
00646                 @unlink( $sFilename );
00647             }
00648         }
00649     }
00650 
00660     public function getRemoteCachePath($sRemote, $sLocal)
00661     {
00662         clearstatcache();
00663         if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
00664             return $sLocal;
00665         }
00666         $hRemote = @fopen( $sRemote, "rb");
00667         $blSuccess = false;
00668         if ( isset( $hRemote) && $hRemote ) {
00669             $hLocal = fopen( $sLocal, "wb");
00670             stream_copy_to_stream($hRemote, $hLocal);
00671             fclose($hRemote);
00672             fclose($hLocal);
00673             $blSuccess = true;
00674         } else {
00675             // try via fsockopen
00676             $aUrl = @parse_url( $sRemote);
00677             if ( !empty( $aUrl["host"])) {
00678                 $sPath = $aUrl["path"];
00679                 if ( empty( $sPath ) ) {
00680                     $sPath = "/";
00681                 }
00682                 $sHost = $aUrl["host"];
00683 
00684                 $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
00685                 if ( $hSocket) {
00686                     fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00687                     $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
00688                     if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
00689                         rewind($hLocal);
00690                         // does not copy all the data
00691                         // stream_copy_to_stream($hSocket, $hLocal);
00692                         fwrite ( $hLocal, stream_get_contents( $hSocket ) );
00693                         fclose( $hLocal );
00694                         fclose( $hSocket );
00695                         $blSuccess = true;
00696                     }
00697                 }
00698             }
00699         }
00700         if ( $blSuccess || file_exists( $sLocal ) ) {
00701             return $sLocal;
00702         }
00703         return false;
00704     }
00705 
00711     public function checkAccessRights()
00712     {
00713         $myConfig  = $this->getConfig();
00714 
00715         $blIsAuth = false;
00716 
00717         $sUserID = oxSession::getVar( "auth");
00718 
00719         // deleting admin marker
00720         oxSession::setVar( "malladmin", 0);
00721         oxSession::setVar( "blIsAdmin", 0);
00722         oxSession::deleteVar( "blIsAdmin" );
00723         $myConfig->setConfigParam( 'blMallAdmin', false );
00724         //#1552T
00725         $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
00726 
00727         if ( $sUserID) {
00728             // escaping
00729             $oDb = oxDb::getDb();
00730             $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
00731 
00732             if ( $sRights != "user") {
00733                 // malladmin ?
00734                 if ( $sRights == "malladmin") {
00735                     oxSession::setVar( "malladmin", 1);
00736                     $myConfig->setConfigParam( 'blMallAdmin', true );
00737 
00738                     //#1552T
00739                     //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
00740                     $myConfig->setConfigParam( 'blAllowSharedEdit', true );
00741 
00742                     $sShop = oxSession::getVar( "actshop");
00743                     if ( !isset($sShop)) {
00744                         oxSession::setVar( "actshop", $myConfig->getBaseShopId());
00745                     }
00746                     $blIsAuth = true;
00747                 } else {
00748                     // Shopadmin... check if this shop is valid and exists
00749                     $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
00750                     if ( isset( $sShopID) && $sShopID) {
00751                         // success, this shop exists
00752 
00753                         oxSession::setVar( "actshop", $sRights);
00754                         oxSession::setVar( "currentadminshop", $sRights);
00755                         oxSession::setVar( "shp", $sRights);
00756 
00757                         // check if this subshop admin is evil.
00758                         if ('chshp' == oxConfig::getParameter( 'fnc' )) {
00759                             // dont allow this call
00760                             $blIsAuth = false;
00761                         } else {
00762                             $blIsAuth = true;
00763 
00764                             $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
00765                             foreach ($aShopIdVars as $sShopIdVar) {
00766                                 if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
00767                                     if ($sGotShop != $sRights) {
00768                                         $blIsAuth = false;
00769                                         break;
00770                                     }
00771                                 }
00772                             }
00773                         }
00774                     }
00775                 }
00776                 // marking user as admin
00777                 oxSession::setVar( "blIsAdmin", 1);
00778             }
00779         }
00780         return $blIsAuth;
00781     }
00782 
00792     public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
00793     {
00794         if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
00795             return $this->_blSeoIsActive;
00796         }
00797 
00798         $myConfig = $this->getConfig();
00799 
00800         if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
00801             $this->_blSeoIsActive = true;
00802 
00803             $aSeoModes  = $myConfig->getconfigParam( 'aSeoModes' );
00804             $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
00805             $iActLang   = $iActLang ? $iActLang : (int) oxLang::getInstance()->getBaseLanguage();
00806 
00807             // checking special config param for active shop and language
00808             if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
00809                 $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
00810             }
00811         }
00812 
00813         return $this->_blSeoIsActive;
00814     }
00815 
00825     public function getShopBit( $iShopId )
00826     {
00827         $iShopId = (int) $iShopId;
00828         //this works for large numbers when $sShopNr is up to (inclusive) 64
00829         $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00830 
00831         //as php ints supports only 32 bits, we return string.
00832         return $iRes;
00833     }
00834 
00844     public function bitwiseAnd( $iVal1, $iVal2 )
00845     {
00846         //this works for large numbers when $sShopNr is up to (inclusive) 64
00847         $iRes = oxDb::getDb()->getOne( "select ($iVal1 & $iVal2) as bitwiseAnd" );
00848 
00849         //as php ints supports only 32 bits, we return string.
00850         return $iRes;
00851     }
00852 
00862     public function bitwiseOr( $iVal1, $iVal2 )
00863     {
00864         //this works for large numbers when $sShopNr is up to (inclusive) 64
00865         $iRes = oxDb::getDb()->getOne( "select ($iVal1 | $iVal2) as bitwiseOr" );
00866 
00867         //as php ints supports only 32 bits, we return string.
00868         return $iRes;
00869     }
00870 
00878     public function isValidAlpha( $sField )
00879     {
00880         return (boolean) getStr()->preg_match( "#^[\w]*$#", $sField );
00881     }
00882 
00892     protected function _simpleRedirect( $sUrl, $sHeaderCode )
00893     {
00894         header( $sHeaderCode );
00895         header( "Location: $sUrl" );
00896         header( "Connection: close" );
00897     }
00898 
00908     public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 301 )
00909     {
00910         //preventing possible cyclic redirection
00911         //#M341 and check only if redirect paramater must be added
00912         if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
00913             return;
00914         }
00915 
00916         if ( $blAddRedirectParam ) {
00917             $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
00918         }
00919 
00920         $sUrl = str_ireplace( "&amp;", "&", $sUrl );
00921 
00922         $sHeaderCode = '';
00923         switch ($iHeaderCode) {
00924             case 301:
00925                 $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
00926                 break;
00927             case 302:
00928             default:
00929                 $sHeaderCode = "HTTP/1.1 302 Found";
00930         }
00931 
00932         $this->_simpleRedirect( $sUrl, $sHeaderCode );
00933 
00934         try {//may occur in case db is lost
00935             $this->getSession()->freeze();
00936         } catch( oxException $oEx ) {
00937             $oEx->debugOut();
00938             //do nothing else to make sure the redirect takes place
00939         }
00940 
00941         if ( defined( 'OXID_PHP_UNIT' ) ) {
00942             return;
00943         }
00944 
00945         $this->showMessageAndExit( '' );
00946     }
00947 
00955     public function showMessageAndExit( $sMsg )
00956     {
00957         $this->getSession()->freeze();
00958         $this->commitFileCache();
00959 
00960         if ( defined( 'OXID_PHP_UNIT' ) ) {
00961             return;
00962         }
00963 
00964         exit( $sMsg );
00965     }
00966 
00974     public function setHeader($sHeader)
00975     {
00976         header($sHeader);
00977     }
00978 
00987     protected function _addUrlParameters( $sUrl, $aParams )
00988     {
00989         $sDelim = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
00990         foreach ( $aParams as $sName => $sVal ) {
00991             $sUrl = $sUrl . $sDelim . $sName . '=' . $sVal;
00992             $sDelim = '&';
00993         }
00994 
00995         return $sUrl;
00996     }
00997 
01009     protected function _fillExplodeArray( $aName, $dVat = null)
01010     {
01011         $myConfig = $this->getConfig();
01012         $oObject = new OxstdClass();
01013         $aPrice = explode( '!P!', $aName[0]);
01014 
01015         if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
01016 
01017             // yes, price is there
01018             $oObject->price = $aPrice[1];
01019             $aName[0] = $aPrice[0];
01020 
01021             $iPercPos = getStr()->strpos( $oObject->price, '%' );
01022             if ( $iPercPos !== false ) {
01023                 $oObject->priceUnit = '%';
01024                 $oObject->fprice = $oObject->price;
01025                 $oObject->price  = substr( $oObject->price, 0, $iPercPos );
01026             } else {
01027                 $oCur = $myConfig->getActShopCurrencyObject();
01028                 $oObject->price = str_replace(',', '.', $oObject->price);
01029                 $oObject->fprice = oxLang::getInstance()->formatCurrency( $oObject->price  * $oCur->rate, $oCur);
01030                 $oObject->priceUnit = 'abs';
01031             }
01032 
01033             // add price info into list
01034             if ( !$this->isAdmin() && $oObject->price != 0 ) {
01035                 $aName[0] .= " ";
01036                 if ( $oObject->price > 0 ) {
01037                     $aName[0] .= "+";
01038                 }
01039                 //V FS#2616
01040                 if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
01041                     $oPrice = oxNew('oxPrice');
01042                     $oPrice->setPrice($oObject->price, $dVat);
01043                     $aName[0] .= oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() * $oCur->rate, $oCur);
01044                 } else {
01045                     $aName[0] .= $oObject->fprice;
01046                 }
01047                 if ( $oObject->priceUnit == 'abs' ) {
01048                     $aName[0] .= " ".$oCur->sign;
01049                 }
01050             }
01051         } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
01052             // A. removing unused part of information
01053             $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
01054         }
01055 
01056         $oObject->name  = $aName[0];
01057         $oObject->value = $aName[1];
01058         return $oObject;
01059     }
01060 
01068     public function oxMimeContentType( $sFileName )
01069     {
01070         $sFileName = strtolower( $sFileName );
01071         $iLastDot  = strrpos( $sFileName, '.' );
01072 
01073         if ( $iLastDot !== false ) {
01074             $sType = substr( $sFileName, $iLastDot + 1 );
01075             switch ( $sType ) {
01076                 case 'gif':
01077                     $sType = 'image/gif';
01078                     break;
01079                 case 'jpeg':
01080                 case 'jpg':
01081                     $sType = 'image/jpeg';
01082                     break;
01083                 case 'png':
01084                     $sType = 'image/png';
01085                     break;
01086                 default:
01087                     $sType = false;
01088                     break;
01089             }
01090         }
01091         return $sType;
01092     }
01093 
01102     public function logger( $sText, $blNewline = false )
01103     {   $myConfig = $this->getConfig();
01104 
01105         if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
01106             if ( gettype( $sText ) != 'string' ) {
01107                 $sText = var_export( $sText, true);
01108             }
01109             $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
01110             $this->writeToLog( $sLogMsg, "log.txt" );
01111         }
01112 
01113     }
01114 
01122     protected function _stripQuotes($mInput)
01123     {
01124         return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
01125     }
01126 
01134     public function strRot13( $sStr )
01135     {
01136         $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
01137         $sTo   = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
01138 
01139         return strtr( $sStr, $sFrom, $sTo );
01140     }
01141 
01142 
01153     public function prepareUrlForNoSession( $sUrl )
01154     {
01155         if ( $this->seoIsActive() ) {
01156             return $sUrl;
01157         }
01158 
01159         return oxUtilsUrl::getInstance()->prepareUrlForNoSession( $sUrl );
01160     }
01161 
01172     protected function _getCacheFilePath( $sCacheName, $blPathOnly = false )
01173     {
01174         return $this->getCacheFilePath( $sCacheName, $blPathOnly );
01175     }
01176 
01186     public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
01187     {
01188         $sVersionPrefix = "";
01189 
01190 
01191             $sVersionPrefix = 'pe';
01192 
01193         $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
01194 
01195         if (!$sPath) {
01196             return false;
01197         }
01198 
01199         return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
01200     }
01201 
01209     public function getLangCache( $sCacheName )
01210     {
01211         $aLangCache = null;
01212         $sFilePath = $this->getCacheFilePath( $sCacheName );
01213         if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
01214             include $sFilePath;
01215         }
01216         return $aLangCache;
01217     }
01218 
01227     public function setLangCache( $sCacheName, $aLangCache )
01228     {
01229         $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";";
01230         $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache);
01231         return $blRes;
01232     }
01233 
01241     public function checkUrlEndingSlash( $sUrl )
01242     {
01243         if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
01244             $sUrl .= '/';
01245         }
01246 
01247         return $sUrl;
01248     }
01249 
01258     public function writeToLog( $sLogMessage, $sLogFileName )
01259     {
01260         $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
01261         $blOk = false;
01262 
01263         if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
01264             fwrite( $oHandle, $sLogMessage );
01265             $blOk = fclose( $oHandle );
01266         }
01267 
01268         return $blOk;
01269     }
01270 
01278     public function handlePageNotFoundError($sUrl = '')
01279     {
01280         $this->setHeader("HTTP/1.0 404 Not Found");
01281         $sReturn = "Page not found.";
01282         try {
01283             $oView = oxNew('oxubase');
01284             $oView->init();
01285             $oView->render();
01286             $oView->addTplParam('sUrl', $sUrl);
01287             if ($sRet = oxUtilsView::getInstance()->getTemplateOutput('err_404.tpl', $oView)) {
01288                 $sReturn = $sRet;
01289             }
01290         } catch (Exception $e) {
01291         }
01292         $this->showMessageAndExit( $sReturn );
01293     }
01294 
01302     public function extractDomain( $sHost )
01303     {
01304         $oStr = getStr();
01305         if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
01306              ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
01307             $iLen = $oStr->strlen( $sHost );
01308             if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
01309                 $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
01310             }
01311         }
01312 
01313         return $sHost;
01314     }
01315 }