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
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
00250 $fRet = substr_replace( $fRet, ",", $iPos, 1);
00251 }
00252
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
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
00336
00337
00338
00339
00340 if ( function_exists( "UserdefinedRebuildCache")) {
00341 UserdefinedRebuildCache();
00342 }
00343 }
00344
00352 public function loadAdminProfile($aInterfaceProfiles)
00353 {
00354
00355
00356 $aInterfaceProfiles = $aInterfaceProfiles;
00357 if ( is_array( $aInterfaceProfiles ) ) {
00358
00359 $sPrevProfile = oxUtilsServer::getInstance()->getOxCookie('oxidadminprofile');
00360 if (isset($sPrevProfile)) {
00361 $aPrevProfile = @explode("@", trim($sPrevProfile));
00362 }
00363
00364
00365 $aProfiles = array();
00366 foreach ( $aInterfaceProfiles as $iPos => $sProfile) {
00367 $aProfileSettings = array($iPos, $sProfile);
00368 $aProfiles[] = $aProfileSettings;
00369 }
00370
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
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
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
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
00486 if ( is_array( $mContents ) && ( $sCachePath = $this->getCacheFilePath( $sKey, false, 'php' ) ) ) {
00487
00488
00489 $this->setCacheMeta( $sKey, array( "serialize" => false, "cachepath" => $sCachePath ) );
00490
00491
00492 $this->toFileCache( $sKey, $mContents );
00493 }
00494 }
00495
00503 public function fromPhpFileCache( $sKey )
00504 {
00505
00506 $this->setCacheMeta( $sKey, array( "include" => true, "cachepath" => $this->getCacheFilePath( $sKey, false, 'php' ) ) );
00507 return $this->fromFileCache( $sKey );
00508 }
00509
00517 public function getCacheMeta( $sKey )
00518 {
00519 return isset( $this->_aFileCacheMeta[$sKey] ) ? $this->_aFileCacheMeta[$sKey] : false;
00520 }
00521
00530 public function setCacheMeta( $sKey, $aMeta )
00531 {
00532
00533 $this->_aFileCacheMeta[$sKey] = $aMeta;
00534 }
00535
00545 public function toFileCache( $sKey, $mContents )
00546 {
00547 $this->_aFileCacheContents[$sKey] = $mContents;
00548 $aMeta = $this->getCacheMeta( $sKey );
00549
00550
00551 $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00552 return ( bool ) $this->_lockFile( $sCachePath, $sKey );
00553 }
00554
00562 public function fromFileCache( $sKey )
00563 {
00564 if ( !array_key_exists( $sKey, $this->_aFileCacheContents ) ) {
00565 $sRes = null;
00566
00567 $aMeta = $this->getCacheMeta( $sKey );
00568 $blInclude = isset( $aMeta["include"] ) ? $aMeta["include"] : false;
00569 $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
00570
00571
00572 $this->_lockFile( $sCachePath, $sKey, LOCK_SH );
00573
00574 clearstatcache();
00575 if ( is_readable( $sCachePath ) ) {
00576 $sRes = $blInclude ? $this->_includeFile( $sCachePath ) : $this->_readFile( $sCachePath );
00577 }
00578
00579
00580 $this->_releaseFile( $sKey, LOCK_SH );
00581
00582
00583 $this->_aFileCacheContents[$sKey] = $sRes;
00584 }
00585
00586 return $this->_aFileCacheContents[$sKey];
00587 }
00588
00596 protected function _readFile( $sFilePath )
00597 {
00598 $sRes = file_get_contents( $sFilePath );
00599 return $sRes ? unserialize( $sRes ) : null;
00600 }
00601
00609 protected function _includeFile( $sFilePath )
00610 {
00611 $_aCacheContents = null;
00612 include $sFilePath;
00613 return $_aCacheContents;
00614 }
00615
00624 protected function _processCache( $sKey, $mContents )
00625 {
00626
00627 $aCacheMeta = $this->getCacheMeta( $sKey );
00628 $blSerialize = isset( $aCacheMeta["serialize"] ) ? $aCacheMeta["serialize"] : true;
00629
00630 if ( $blSerialize ) {
00631 $mContents = serialize( $mContents );
00632 } else {
00633 $mContents = "<?php\n//automatically generated file\n//" . date( "Y-m-d H:i:s" ) . "\n\n\$_aCacheContents = " . var_export( $mContents, true ) . "\n?>";
00634 }
00635
00636 return $mContents;
00637 }
00638
00645 public function commitFileCache()
00646 {
00647 if ( count( $this->_aLockedFileHandles[LOCK_EX] ) ) {
00648 startProfile("!__SAVING CACHE__! (warning)");
00649 foreach ( $this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle ) {
00650 if ( $rHandle !== false && isset( $this->_aFileCacheContents[$sKey] ) ) {
00651
00652
00653 fwrite( $rHandle, $this->_processCache( $sKey, $this->_aFileCacheContents[$sKey] ) );
00654
00655
00656 $this->_releaseFile( $sKey );
00657 }
00658 }
00659
00660 stopProfile("!__SAVING CACHE__! (warning)");
00661
00662
00663 $this->_aFileCacheContents = array();
00664 }
00665 }
00666
00676 protected function _lockFile( $sFilePath, $sIdent, $iLockMode = LOCK_EX )
00677 {
00678 $rHandle = isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
00679 if ( $rHandle === null ) {
00680
00681 $blLocked = false;
00682 $rHandle = @fopen( $sFilePath, "a+" );
00683
00684 if ( $rHandle !== false ) {
00685
00686 if ( flock( $rHandle, $iLockMode | LOCK_NB ) ) {
00687 if ( $iLockMode === LOCK_EX ) {
00688
00689 $blLocked = ftruncate( $rHandle, 0 );
00690 } else {
00691
00692 $blLocked = fseek( $rHandle, 0 ) === 0;
00693 }
00694 }
00695
00696
00697 if ( !$blLocked ) {
00698 fclose( $rHandle );
00699 $rHandle = false;
00700 }
00701 }
00702
00703
00704 if ( !$blLocked && $iLockMode === LOCK_EX ) {
00705
00706
00707 if ( count( $this->_aLockedFileHandles ) == 0 ) {
00708 clearstatcache();
00709 }
00710
00711
00712 if (!( file_exists( $sFilePath ) && !filesize( $sFilePath ) && abs( time() - filectime( $sFilePath ) < 40 ) ) ) {
00713 $rHandle = @fopen( $sFilePath, "w" );
00714 }
00715 }
00716
00717 $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
00718 }
00719
00720 return $rHandle;
00721 }
00722
00731 protected function _releaseFile( $sIdent, $iLockMode = LOCK_EX )
00732 {
00733 $blSuccess = true;
00734 if ( isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) &&
00735 $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false ) {
00736
00737
00738 $blSuccess = flock( $this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN ) &&
00739 fclose( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00740 unset( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
00741 }
00742
00743 return $blSuccess;
00744 }
00745
00753 public function oxResetFileCache()
00754 {
00755 $aPathes = glob( $this->getCacheFilePath( null, true ) . '*' );
00756 if ( is_array( $aPathes ) ) {
00757
00758 $aPathes = preg_grep( $this->_sPermanentCachePattern, $aPathes, PREG_GREP_INVERT );
00759 foreach ( $aPathes as $sFilename ) {
00760 @unlink( $sFilename );
00761 }
00762 }
00763 }
00764
00774 public function getRemoteCachePath($sRemote, $sLocal)
00775 {
00776 clearstatcache();
00777 if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
00778 return $sLocal;
00779 }
00780 $hRemote = @fopen( $sRemote, "rb");
00781 $blSuccess = false;
00782 if ( isset( $hRemote) && $hRemote ) {
00783 $hLocal = fopen( $sLocal, "wb");
00784 stream_copy_to_stream($hRemote, $hLocal);
00785 fclose($hRemote);
00786 fclose($hLocal);
00787 $blSuccess = true;
00788 } else {
00789
00790 $aUrl = @parse_url( $sRemote);
00791 if ( !empty( $aUrl["host"])) {
00792 $sPath = $aUrl["path"];
00793 if ( empty( $sPath ) ) {
00794 $sPath = "/";
00795 }
00796 $sHost = $aUrl["host"];
00797
00798 $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
00799 if ( $hSocket) {
00800 fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
00801 $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
00802 if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
00803 rewind($hLocal);
00804
00805
00806 fwrite ( $hLocal, stream_get_contents( $hSocket ) );
00807 fclose( $hLocal );
00808 fclose( $hSocket );
00809 $blSuccess = true;
00810 }
00811 }
00812 }
00813 }
00814 if ( $blSuccess || file_exists( $sLocal ) ) {
00815 return $sLocal;
00816 }
00817 return false;
00818 }
00819
00825 public function checkAccessRights()
00826 {
00827 $myConfig = $this->getConfig();
00828
00829 $blIsAuth = false;
00830
00831 $sUserID = oxSession::getVar( "auth");
00832
00833
00834 oxSession::setVar( "malladmin", 0);
00835 oxSession::setVar( "blIsAdmin", 0);
00836 oxSession::deleteVar( "blIsAdmin" );
00837 $myConfig->setConfigParam( 'blMallAdmin', false );
00838
00839 $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
00840
00841 if ( $sUserID) {
00842
00843 $oDb = oxDb::getDb();
00844 $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
00845
00846 if ( $sRights != "user") {
00847
00848 if ( $sRights == "malladmin") {
00849 oxSession::setVar( "malladmin", 1);
00850 $myConfig->setConfigParam( 'blMallAdmin', true );
00851
00852
00853
00854 $myConfig->setConfigParam( 'blAllowSharedEdit', true );
00855
00856 $sShop = oxSession::getVar( "actshop");
00857 if ( !isset($sShop)) {
00858 oxSession::setVar( "actshop", $myConfig->getBaseShopId());
00859 }
00860 $blIsAuth = true;
00861 } else {
00862
00863 $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
00864 if ( isset( $sShopID) && $sShopID) {
00865
00866
00867 oxSession::setVar( "actshop", $sRights);
00868 oxSession::setVar( "currentadminshop", $sRights);
00869 oxSession::setVar( "shp", $sRights);
00870
00871
00872 if ('chshp' == oxConfig::getParameter( 'fnc' )) {
00873
00874 $blIsAuth = false;
00875 } else {
00876 $blIsAuth = true;
00877
00878 $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
00879 foreach ($aShopIdVars as $sShopIdVar) {
00880 if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
00881 if ($sGotShop != $sRights) {
00882 $blIsAuth = false;
00883 break;
00884 }
00885 }
00886 }
00887 }
00888 }
00889 }
00890
00891 oxSession::setVar( "blIsAdmin", 1);
00892 }
00893 }
00894 return $blIsAuth;
00895 }
00896
00906 public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
00907 {
00908 if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
00909 return $this->_blSeoIsActive;
00910 }
00911
00912 $myConfig = $this->getConfig();
00913
00914 if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
00915 $this->_blSeoIsActive = true;
00916
00917 $aSeoModes = $myConfig->getconfigParam( 'aSeoModes' );
00918 $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
00919 $iActLang = $iActLang ? $iActLang : (int) oxLang::getInstance()->getBaseLanguage();
00920
00921
00922 if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
00923 $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
00924 }
00925 }
00926
00927 return $this->_blSeoIsActive;
00928 }
00929
00939 public function getShopBit( $iShopId )
00940 {
00941 $iShopId = (int) $iShopId;
00942
00943 $iRes = oxDb::getDb()->getOne( "select 1 << ( $iShopId - 1 ) as shopbit" );
00944
00945
00946 return $iRes;
00947 }
00948
00958 public function bitwiseAnd( $iVal1, $iVal2 )
00959 {
00960
00961 $iRes = oxDb::getDb()->getOne( "select ($iVal1 & $iVal2) as bitwiseAnd" );
00962
00963
00964 return $iRes;
00965 }
00966
00976 public function bitwiseOr( $iVal1, $iVal2 )
00977 {
00978
00979 $iRes = oxDb::getDb()->getOne( "select ($iVal1 | $iVal2) as bitwiseOr" );
00980
00981
00982 return $iRes;
00983 }
00984
00992 public function isValidAlpha( $sField )
00993 {
00994 return (boolean) getStr()->preg_match( "#^[\w]*$#", $sField );
00995 }
00996
01006 protected function _simpleRedirect( $sUrl, $sHeaderCode )
01007 {
01008 header( $sHeaderCode );
01009 header( "Location: $sUrl" );
01010 header( "Connection: close" );
01011 }
01012
01022 public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 301 )
01023 {
01024
01025
01026 if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
01027 return;
01028 }
01029
01030 if ( $blAddRedirectParam ) {
01031 $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
01032 }
01033
01034 $sUrl = str_ireplace( "&", "&", $sUrl );
01035
01036 $sHeaderCode = '';
01037 switch ($iHeaderCode) {
01038 case 301:
01039 $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
01040 break;
01041 case 302:
01042 default:
01043 $sHeaderCode = "HTTP/1.1 302 Found";
01044 }
01045
01046 $this->_simpleRedirect( $sUrl, $sHeaderCode );
01047
01048 try {
01049 $this->getSession()->freeze();
01050 } catch( oxException $oEx ) {
01051 $oEx->debugOut();
01052
01053 }
01054
01055 if ( defined( 'OXID_PHP_UNIT' ) ) {
01056 return;
01057 }
01058
01059 $this->showMessageAndExit( '' );
01060 }
01061
01069 public function showMessageAndExit( $sMsg )
01070 {
01071 $this->getSession()->freeze();
01072 $this->commitFileCache();
01073
01074 if ( defined( 'OXID_PHP_UNIT' ) ) {
01075 return;
01076 }
01077
01078 exit( $sMsg );
01079 }
01080
01088 public function setHeader($sHeader)
01089 {
01090 header($sHeader);
01091 }
01092
01101 protected function _addUrlParameters( $sUrl, $aParams )
01102 {
01103 $sDelim = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
01104 foreach ( $aParams as $sName => $sVal ) {
01105 $sUrl = $sUrl . $sDelim . $sName . '=' . $sVal;
01106 $sDelim = '&';
01107 }
01108
01109 return $sUrl;
01110 }
01111
01123 protected function _fillExplodeArray( $aName, $dVat = null)
01124 {
01125 $myConfig = $this->getConfig();
01126 $oObject = new OxstdClass();
01127 $aPrice = explode( '!P!', $aName[0]);
01128
01129 if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
01130
01131
01132 $oObject->price = $aPrice[1];
01133 $aName[0] = $aPrice[0];
01134
01135 $iPercPos = getStr()->strpos( $oObject->price, '%' );
01136 if ( $iPercPos !== false ) {
01137 $oObject->priceUnit = '%';
01138 $oObject->fprice = $oObject->price;
01139 $oObject->price = substr( $oObject->price, 0, $iPercPos );
01140 } else {
01141 $oCur = $myConfig->getActShopCurrencyObject();
01142 $oObject->price = str_replace(',', '.', $oObject->price);
01143 $oObject->fprice = oxLang::getInstance()->formatCurrency( $oObject->price * $oCur->rate, $oCur);
01144 $oObject->priceUnit = 'abs';
01145 }
01146
01147
01148 if ( !$this->isAdmin() && $oObject->price != 0 ) {
01149 $aName[0] .= " ";
01150 if ( $oObject->price > 0 ) {
01151 $aName[0] .= "+";
01152 }
01153
01154 if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
01155 $oPrice = oxNew('oxPrice');
01156 $oPrice->setPrice($oObject->price, $dVat);
01157 $aName[0] .= oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() * $oCur->rate, $oCur);
01158 } else {
01159 $aName[0] .= $oObject->fprice;
01160 }
01161 if ( $oObject->priceUnit == 'abs' ) {
01162 $aName[0] .= " ".$oCur->sign;
01163 }
01164 }
01165 } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
01166
01167 $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
01168 }
01169
01170 $oObject->name = $aName[0];
01171 $oObject->value = $aName[1];
01172 return $oObject;
01173 }
01174
01182 public function oxMimeContentType( $sFileName )
01183 {
01184 $sFileName = strtolower( $sFileName );
01185 $iLastDot = strrpos( $sFileName, '.' );
01186
01187 if ( $iLastDot !== false ) {
01188 $sType = substr( $sFileName, $iLastDot + 1 );
01189 switch ( $sType ) {
01190 case 'gif':
01191 $sType = 'image/gif';
01192 break;
01193 case 'jpeg':
01194 case 'jpg':
01195 $sType = 'image/jpeg';
01196 break;
01197 case 'png':
01198 $sType = 'image/png';
01199 break;
01200 default:
01201 $sType = false;
01202 break;
01203 }
01204 }
01205 return $sType;
01206 }
01207
01216 public function logger( $sText, $blNewline = false )
01217 { $myConfig = $this->getConfig();
01218
01219 if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
01220 if ( gettype( $sText ) != 'string' ) {
01221 $sText = var_export( $sText, true);
01222 }
01223 $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
01224 $this->writeToLog( $sLogMsg, "log.txt" );
01225 }
01226
01227 }
01228
01236 protected function _stripQuotes($mInput)
01237 {
01238 return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
01239 }
01240
01248 public function strRot13( $sStr )
01249 {
01250 $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
01251 $sTo = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
01252
01253 return strtr( $sStr, $sFrom, $sTo );
01254 }
01255
01256
01267 public function prepareUrlForNoSession( $sUrl )
01268 {
01269 if ( $this->seoIsActive() ) {
01270 return $sUrl;
01271 }
01272
01273 return oxUtilsUrl::getInstance()->prepareUrlForNoSession( $sUrl );
01274 }
01275
01286 protected function _getCacheFilePath( $sCacheName, $blPathOnly = false )
01287 {
01288 return $this->getCacheFilePath( $sCacheName, $blPathOnly );
01289 }
01290
01300 public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
01301 {
01302 $sVersionPrefix = "";
01303
01304
01305 $sVersionPrefix = 'pe';
01306
01307 $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
01308
01309 if (!$sPath) {
01310 return false;
01311 }
01312
01313 return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
01314 }
01315
01323 public function getLangCache( $sCacheName )
01324 {
01325 $aLangCache = null;
01326 $sFilePath = $this->getCacheFilePath( $sCacheName );
01327 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
01328 include $sFilePath;
01329 }
01330 return $aLangCache;
01331 }
01332
01341 public function setLangCache( $sCacheName, $aLangCache )
01342 {
01343 $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";";
01344 $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache);
01345 return $blRes;
01346 }
01347
01355 public function checkUrlEndingSlash( $sUrl )
01356 {
01357 if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
01358 $sUrl .= '/';
01359 }
01360
01361 return $sUrl;
01362 }
01363
01372 public function writeToLog( $sLogMessage, $sLogFileName )
01373 {
01374 $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
01375 $blOk = false;
01376
01377 if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
01378 fwrite( $oHandle, $sLogMessage );
01379 $blOk = fclose( $oHandle );
01380 }
01381
01382 return $blOk;
01383 }
01384
01392 public function handlePageNotFoundError($sUrl = '')
01393 {
01394 $this->setHeader("HTTP/1.0 404 Not Found");
01395 $sReturn = "Page not found.";
01396 try {
01397 $oView = oxNew('oxubase');
01398 $oView->init();
01399 $oView->render();
01400 $oView->addTplParam('sUrl', $sUrl);
01401 if ($sRet = oxUtilsView::getInstance()->getTemplateOutput('err_404.tpl', $oView)) {
01402 $sReturn = $sRet;
01403 }
01404 } catch (Exception $e) {
01405 }
01406 $this->showMessageAndExit( $sReturn );
01407 }
01408
01416 public function extractDomain( $sHost )
01417 {
01418 $oStr = getStr();
01419 if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
01420 ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
01421 $iLen = $oStr->strlen( $sHost );
01422 if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
01423 $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
01424 }
01425 }
01426
01427 return $sHost;
01428 }
01429 }