OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxutils.php
Go to the documentation of this file.
1 <?php
2 
6 require_once getShopBasePath()."core/smarty/Smarty.class.php";
7 
12 class oxUtils extends oxSuperCfg
13 {
19  protected $_iCurPrecision = null;
20 
28  protected $_sPermanentCachePattern = "/c_fieldnames_|c_tbdsc_|_allfields_/";
29 
35  protected $_sLanguageCachePattern = "/c_langcache_/i";
36 
42  protected $_sMenuCachePattern = "/c_menu_/i";
43 
49  protected $_aLockedFileHandles = array();
50 
56  protected $_aFileCacheContents = array();
57 
63  protected $_blIsSe = null;
64 
72  public static function getInstance()
73  {
74  return oxRegistry::getUtils();
75  }
76 
82  protected $_aStaticCache;
83 
89  protected $_blSeoIsActive = null;
90 
96  public function stripGpcMagicQuotes()
97  {
98  if (!get_magic_quotes_gpc()) {
99  return;
100  }
101  $_REQUEST = self::_stripQuotes($_REQUEST);
102  $_POST = self::_stripQuotes($_POST);
103  $_GET = self::_stripQuotes($_GET);
104  $_COOKIE = self::_stripQuotes($_COOKIE);
105  }
106 
117  public function strMan( $sVal, $sKey = null )
118  {
119  $oEncryptor = oxNew('oxEncryptor');
120  $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
121 
122  return $oEncryptor->encrypt($sVal, $sKey);
123  }
124 
135  public function strRem( $sVal, $sKey = null )
136  {
137  $oDecryptor = oxNew('oxDecryptor');
138  $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
139 
140  return $oDecryptor->decrypt($sVal, $sKey);
141  }
142 
150  public function getArrFldName( $sName )
151  {
152  return str_replace( ".", "__", $sName);
153  }
154 
163  public function assignValuesFromText( $sIn, $dVat = null )
164  {
165  $aRet = array();
166  $aPieces = explode( '@@', $sIn );
167  while ( list( $sKey, $sVal ) = each( $aPieces ) ) {
168  if ( $sVal ) {
169  $aName = explode( '__', $sVal );
170  if ( isset( $aName[0] ) && isset( $aName[1] ) ) {
171  $aRet[] = $this->_fillExplodeArray( $aName, $dVat );
172  }
173  }
174  }
175  return $aRet;
176  }
177 
185  public function assignValuesToText( $aIn)
186  {
187  $sRet = "";
188  reset( $aIn );
189  while (list($sKey, $sVal) = each($aIn)) {
190  $sRet .= $sKey;
191  $sRet .= "__";
192  $sRet .= $sVal;
193  $sRet .= "@@";
194  }
195  return $sRet;
196  }
197 
205  public function currency2Float( $sValue)
206  {
207  $fRet = $sValue;
208  $iPos = strrpos( $sValue, ".");
209  if ($iPos && ((strlen($sValue)-1-$iPos) < 2+1)) {
210  // replace decimal with ","
211  $fRet = substr_replace( $fRet, ",", $iPos, 1);
212  }
213  // remove thousands
214  $fRet = str_replace( array(" ","."), "", $fRet);
215 
216  $fRet = str_replace( ",", ".", $fRet);
217  return (float) $fRet;
218  }
219 
227  public function string2Float( $sValue)
228  {
229  $fRet = str_replace( " ", "", $sValue);
230  $iCommaPos = strpos( $fRet, ",");
231  $iDotPos = strpos( $fRet, ".");
232  if (!$iDotPos xor !$iCommaPos) {
233  if (substr_count( $fRet, ",") > 1 || substr_count( $fRet, ".") > 1) {
234  $fRet = str_replace( array(",","."), "", $fRet);
235  } else {
236  $fRet = str_replace( ",", ".", $fRet);
237  }
238  } else if ( $iDotPos < $iCommaPos ) {
239  $fRet = str_replace( ".", "", $fRet);
240  $fRet = str_replace( ",", ".", $fRet);
241  }
242  // remove thousands
243  $fRet = str_replace( array(" ",","), "", $fRet);
244  return (float) $fRet;
245  }
246 
254  public function isSearchEngine( $sClient = null )
255  {
256  if (is_null($this->_blIsSe)) {
257  $this->setSearchEngine( null, $sClient );
258  }
259  return $this->_blIsSe;
260  }
261 
270  public function setSearchEngine( $blIsSe = null, $sClient = null )
271  {
272  if (isset($blIsSe)) {
273  $this->_blIsSe = $blIsSe;
274  return;
275  }
276  startProfile("isSearchEngine");
277 
278  $myConfig = $this->getConfig();
279  $blIsSe = false;
280 
281  if ( !( $myConfig->getConfigParam( 'iDebug' ) && $this->isAdmin() ) ) {
282  $aRobots = $myConfig->getConfigParam( 'aRobots' );
283  $aRobots = is_array( $aRobots )?$aRobots:array();
284 
285  $aRobotsExcept = $myConfig->getConfigParam( 'aRobotsExcept' );
286  $aRobotsExcept = is_array( $aRobotsExcept )?$aRobotsExcept:array();
287 
288  $sClient = $sClient?$sClient:strtolower( getenv( 'HTTP_USER_AGENT' ) );
289  $blIsSe = false;
290  $aRobots = array_merge( $aRobots, $aRobotsExcept );
291  foreach ( $aRobots as $sRobot ) {
292  if ( strpos( $sClient, $sRobot ) !== false ) {
293  $blIsSe = true;
294  break;
295  }
296  }
297  }
298 
299  $this->_blIsSe = $blIsSe;
300 
301  stopProfile("isSearchEngine");
302  }
303 
312  public function isValidEmail( $sEmail )
313  {
314  $blValid = true;
315  if ( $sEmail != 'admin' ) {
316  $sEmailTpl = "/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i";
317  $blValid = ( getStr()->preg_match( $sEmailTpl, $sEmail ) != 0 );
318  }
319 
320  return $blValid;
321  }
322 
330  public function loadAdminProfile($aInterfaceProfiles)
331  {
332  // improved #533
333  // checking for available profiles list
334  if ( is_array( $aInterfaceProfiles ) ) {
335  //checking for previous profiles
336  $sPrevProfile = oxRegistry::get("oxUtilsServer")->getOxCookie('oxidadminprofile');
337  if (isset($sPrevProfile)) {
338  $aPrevProfile = @explode("@", trim($sPrevProfile));
339  }
340 
341  //array to store profiles
342  $aProfiles = array();
343  foreach ( $aInterfaceProfiles as $iPos => $sProfile) {
344  $aProfileSettings = array($iPos, $sProfile);
345  $aProfiles[] = $aProfileSettings;
346  }
347  // setting previous used profile as active
348  if (isset($aPrevProfile[0]) && isset($aProfiles[$aPrevProfile[0]])) {
349  $aProfiles[$aPrevProfile[0]][2] = 1;
350  }
351 
352  oxSession::setVar("aAdminProfiles", $aProfiles);
353  return $aProfiles;
354  }
355  return null;
356  }
357 
366  public function fRound($sVal, $oCur = null)
367  {
368  startProfile('fround');
369 
370  //cached currency precision, this saves about 1% of execution time
371  $iCurPrecision = null;
372  if (! defined('OXID_PHP_UNIT')) {
373  $iCurPrecision = $this->_iCurPrecision;
374  }
375 
376  if (is_null($iCurPrecision)) {
377  if ( !$oCur ) {
378  $oCur = $this->getConfig()->getActShopCurrencyObject();
379  }
380 
381  $iCurPrecision = $oCur->decimal;
382  $this->_iCurPrecision = $iCurPrecision;
383  }
384 
385  // if < 5.3.x this is a workaround for #36008 bug in php - incorrect round() & number_format() result (R)
386  static $dprez = null;
387  if (!$dprez) {
388  $prez = @ini_get("precision");
389  if (!$prez || $prez > 12 ) {
390  $prez = 12;
391  }
392  $dprez = pow(10, -$prez);
393  }
394  stopProfile('fround');
395  return round($sVal + $dprez * ( $sVal >= 0 ? 1 : -1 ), $iCurPrecision);
396  }
397 
407  public function toStaticCache( $sName, $sContent, $sKey = null )
408  {
409  // if it's an array then we add
410  if ( $sKey ) {
411  $this->_aStaticCache[$sName][$sKey] = $sContent;
412  } else {
413  $this->_aStaticCache[$sName] = $sContent;
414  }
415  }
416 
424  public function fromStaticCache( $sName)
425  {
426  if ( isset( $this->_aStaticCache[$sName])) {
427  return $this->_aStaticCache[$sName];
428  }
429  return null;
430  }
431 
439  public function cleanStaticCache($sCacheName = null)
440  {
441  if ($sCacheName) {
442  unset($this->_aStaticCache[$sCacheName]);
443  } else {
444  $this->_aStaticCache = null;
445  }
446  }
447 
457  public function toPhpFileCache( $sKey, $mContents )
458  {
459  //only simple arrays are supported
460  if ( is_array( $mContents ) && ( $sCachePath = $this->getCacheFilePath( $sKey, false, 'php' ) ) ) {
461 
462  // setting meta
463  $this->setCacheMeta( $sKey, array( "serialize" => false, "cachepath" => $sCachePath ) );
464 
465  // caching..
466  $this->toFileCache( $sKey, $mContents );
467  }
468  }
469 
477  public function fromPhpFileCache( $sKey )
478  {
479  // setting meta
480  $this->setCacheMeta( $sKey, array( "include" => true, "cachepath" => $this->getCacheFilePath( $sKey, false, 'php' ) ) );
481  return $this->fromFileCache( $sKey );
482  }
483 
491  public function getCacheMeta( $sKey )
492  {
493  return isset( $this->_aFileCacheMeta[$sKey] ) ? $this->_aFileCacheMeta[$sKey] : false;
494  }
495 
504  public function setCacheMeta( $sKey, $aMeta )
505  {
506  // cache meta data
507  $this->_aFileCacheMeta[$sKey] = $aMeta;
508  }
509 
520  public function toFileCache( $sKey, $mContents, $iTtl = 0 )
521  {
522  $aCacheData['content'] = $mContents;
523  $aMeta = $this->getCacheMeta( $sKey );
524  if ( $iTtl ) {
525  $aCacheData['ttl'] = $iTtl;
526  $aCacheData['timestamp'] = oxRegistry::get("oxUtilsDate")->getTime();
527  }
528  $this->_aFileCacheContents[$sKey] = $aCacheData;
529 
530  // looking for cache meta
531  $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
532  return ( bool ) $this->_lockFile( $sCachePath, $sKey );
533  }
534 
542  public function fromFileCache( $sKey )
543  {
544  if ( !array_key_exists( $sKey, $this->_aFileCacheContents ) ) {
545  $sRes = null;
546 
547  $aMeta = $this->getCacheMeta( $sKey );
548  $blInclude = isset( $aMeta["include"] ) ? $aMeta["include"] : false;
549  $sCachePath = isset( $aMeta["cachepath"] ) ? $aMeta["cachepath"] : $this->getCacheFilePath( $sKey );
550 
551  // trying to lock
552  $this->_lockFile( $sCachePath, $sKey, LOCK_SH );
553 
554  clearstatcache();
555  if ( is_readable( $sCachePath ) ) {
556  $sRes = $blInclude ? $this->_includeFile( $sCachePath ) : $this->_readFile( $sCachePath );
557  }
558 
559  if ( isset( $sRes['ttl'] ) && $sRes['ttl'] != 0 ) {
560  $iTimestamp = $sRes['timestamp'];
561  $iTtl = $sRes['ttl'];
562 
563  $iTime = oxRegistry::get("oxUtilsDate")->getTime();
564  if ( $iTime > $iTimestamp + $iTtl ) {
565  return null;
566  }
567  }
568  // release lock
569  $this->_releaseFile( $sKey, LOCK_SH );
570 
571  // caching
572  $this->_aFileCacheContents[$sKey] = $sRes;
573  }
574 
575  return $this->_aFileCacheContents[$sKey]['content'];
576  }
577 
585  protected function _readFile( $sFilePath )
586  {
587  $sRes = file_get_contents( $sFilePath );
588  return $sRes ? unserialize( $sRes ) : null;
589  }
590 
598  protected function _includeFile( $sFilePath )
599  {
600  $_aCacheContents = null;
601  include $sFilePath;
602  return $_aCacheContents;
603  }
604 
613  protected function _processCache( $sKey, $mContents )
614  {
615  // looking for cache meta
616  $aCacheMeta = $this->getCacheMeta( $sKey );
617  $blSerialize = isset( $aCacheMeta["serialize"] ) ? $aCacheMeta["serialize"] : true;
618 
619  if ( $blSerialize ) {
620  $mContents = serialize( $mContents );
621  } else {
622  $mContents = "<?php\n//automatically generated file\n//" . date( "Y-m-d H:i:s" ) . "\n\n\$_aCacheContents = " . var_export( $mContents, true ) . "\n?>";
623  }
624 
625  return $mContents;
626  }
627 
634  public function commitFileCache()
635  {
636  if ( count( $this->_aLockedFileHandles[LOCK_EX] ) ) {
637  startProfile("!__SAVING CACHE__! (warning)");
638  foreach ( $this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle ) {
639  if ( $rHandle !== false && isset( $this->_aFileCacheContents[$sKey] ) ) {
640 
641  // #0002931A truncate file once more before writing
642  ftruncate( $rHandle, 0 );
643 
644  // writing cache
645  fwrite( $rHandle, $this->_processCache( $sKey, $this->_aFileCacheContents[$sKey] ) );
646 
647  // releasing locks
648  $this->_releaseFile( $sKey );
649  }
650  }
651 
652  stopProfile("!__SAVING CACHE__! (warning)");
653 
654  //empty buffer
655  $this->_aFileCacheContents = array();
656  }
657  }
658 
668  protected function _lockFile( $sFilePath, $sIdent, $iLockMode = LOCK_EX )
669  {
670  $rHandle = isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
671  if ( $rHandle === null ) {
672 
673  $blLocked = false;
674  $rHandle = @fopen( $sFilePath, "a+" );
675 
676  if ( $rHandle !== false ) {
677 
678  if ( flock( $rHandle, $iLockMode | LOCK_NB ) ) {
679  if ( $iLockMode === LOCK_EX ) {
680  // truncate file
681  $blLocked = ftruncate( $rHandle, 0 );
682  } else {
683  // move to a start position
684  $blLocked = fseek( $rHandle, 0 ) === 0;
685  }
686  }
687 
688  // on failure - closing and setting false..
689  if ( !$blLocked ) {
690  fclose( $rHandle );
691  $rHandle = false;
692  }
693  }
694 
695  // in case system does not support file locking
696  if ( !$blLocked && $iLockMode === LOCK_EX ) {
697 
698  // clearing on first call
699  if ( count( $this->_aLockedFileHandles ) == 0 ) {
700  clearstatcache();
701  }
702 
703  // start a blank file to inform other processes we are dealing with it.
704  if (!( file_exists( $sFilePath ) && !filesize( $sFilePath ) && abs( time() - filectime( $sFilePath ) < 40 ) ) ) {
705  $rHandle = @fopen( $sFilePath, "w" );
706  }
707  }
708 
709  $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
710  }
711 
712  return $rHandle;
713  }
714 
723  protected function _releaseFile( $sIdent, $iLockMode = LOCK_EX )
724  {
725  $blSuccess = true;
726  if ( isset( $this->_aLockedFileHandles[$iLockMode][$sIdent] ) &&
727  $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false ) {
728 
729  // release the lock and close file
730  $blSuccess = flock( $this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN ) &&
731  fclose( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
732  unset( $this->_aLockedFileHandles[$iLockMode][$sIdent] );
733  }
734 
735  return $blSuccess;
736  }
737 
745  public function oxResetFileCache()
746  {
747  $aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
748  if ( is_array( $aFiles ) ) {
749  // delete all the files, except cached tables field names
750  $aFiles = preg_grep( $this->_sPermanentCachePattern, $aFiles, PREG_GREP_INVERT );
751  foreach ( $aFiles as $sFile ) {
752  @unlink( $sFile );
753  }
754  }
755  }
756 
764  public function resetTemplateCache($aTemplates)
765  {
766  $sSmartyDir = oxRegistry::get("oxUtilsView")->getSmartyDir();
767  //$aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
768  $aFiles = glob( $sSmartyDir . '*' );
769 
770  if ( is_array( $aFiles ) && is_array( $aTemplates ) && count($aTemplates) ) {
771  // delete all template cache files
772  foreach ($aTemplates as &$sTemplate) {
773  $sTemplate = preg_quote(basename(strtolower($sTemplate), '.tpl'));
774  }
775 
776  $sPattern = sprintf("/%%(%s)\.tpl\.php$/i", implode('|', $aTemplates));
777  $aFiles = preg_grep( $sPattern, $aFiles );
778 
779  if (is_array( $aFiles ) ) {
780  foreach ( $aFiles as $sFile ) {
781  @unlink( $sFile );
782  }
783  }
784  }
785 
786  }
787 
793  public function resetLanguageCache()
794  {
795  $aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
796  if ( is_array( $aFiles ) ) {
797  // delete all language cache files
798  $sPattern = $this->_sLanguageCachePattern;
799  $aFiles = preg_grep( $sPattern, $aFiles );
800  foreach ( $aFiles as $sFile ) {
801  @unlink( $sFile );
802  }
803  }
804  }
805 
811  public function resetMenuCache()
812  {
813  $aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
814  if ( is_array( $aFiles ) ) {
815  // delete all menu cache files
816  $sPattern = $this->_sMenuCachePattern;
817  $aFiles = preg_grep( $sPattern, $aFiles );
818  foreach ( $aFiles as $sFile ) {
819  @unlink( $sFile );
820  }
821  }
822  }
823 
833  public function getRemoteCachePath($sRemote, $sLocal)
834  {
835  clearstatcache();
836  if ( file_exists( $sLocal ) && filemtime( $sLocal ) && filemtime( $sLocal ) > time() - 86400 ) {
837  return $sLocal;
838  }
839  $hRemote = @fopen( $sRemote, "rb");
840  $blSuccess = false;
841  if ( isset( $hRemote) && $hRemote ) {
842  $hLocal = fopen( $sLocal, "wb");
843  stream_copy_to_stream($hRemote, $hLocal);
844  fclose($hRemote);
845  fclose($hLocal);
846  $blSuccess = true;
847  } else {
848  // try via fsockopen
849  $aUrl = @parse_url( $sRemote);
850  if ( !empty( $aUrl["host"])) {
851  $sPath = $aUrl["path"];
852  if ( empty( $sPath ) ) {
853  $sPath = "/";
854  }
855  $sHost = $aUrl["host"];
856 
857  $hSocket = @fsockopen( $sHost, 80, $iErrorNumber, $iErrStr, 5);
858  if ( $hSocket) {
859  fputs( $hSocket, "GET ".$sPath." HTTP/1.0\r\nHost: $sHost\r\n\r\n");
860  $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
861  if ( ( $hLocal = @fopen( $sLocal, "wb") ) !== false ) {
862  rewind($hLocal);
863  // does not copy all the data
864  // stream_copy_to_stream($hSocket, $hLocal);
865  fwrite ( $hLocal, stream_get_contents( $hSocket ) );
866  fclose( $hLocal );
867  fclose( $hSocket );
868  $blSuccess = true;
869  }
870  }
871  }
872  }
873  if ( $blSuccess || file_exists( $sLocal ) ) {
874  return $sLocal;
875  }
876  return false;
877  }
878 
884  public function canPreview()
885  {
886  $blCan = null;
887  if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
888  ( $sAdminSid = oxRegistry::get("oxUtilsServer")->getOxCookie( 'admin_sid' ) ) ) {
889 
890  $sTable = getViewName( 'oxuser' );
891  $oDb = oxDb::getDb();
892  $sQ = "select 1 from $sTable where MD5( CONCAT( ".$oDb->quote($sAdminSid).", {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = ".oxDb::getDb()->quote($sPrevId);
893  $blCan = (bool) $oDb->getOne( $sQ );
894  }
895 
896  return $blCan;
897  }
898 
904  public function getPreviewId()
905  {
906  $sAdminSid = oxRegistry::get("oxUtilsServer")->getOxCookie( 'admin_sid' );
907  if ( ( $oUser = $this->getUser() ) ) {
908  return md5( $sAdminSid . $oUser->getId() . $oUser->oxuser__oxpassword->value . $oUser->oxuser__oxrights->value );
909  }
910  }
911 
917  public function checkAccessRights()
918  {
919  $myConfig = $this->getConfig();
920 
921  $blIsAuth = false;
922 
923  $sUserID = oxSession::getVar( "auth");
924 
925  // deleting admin marker
926  oxSession::setVar( "malladmin", 0);
927  oxSession::setVar( "blIsAdmin", 0);
928  oxSession::deleteVar( "blIsAdmin" );
929  $myConfig->setConfigParam( 'blMallAdmin', false );
930  //#1552T
931  $myConfig->setConfigParam( 'blAllowInheritedEdit', false );
932 
933  if ( $sUserID) {
934  // escaping
935  $oDb = oxDb::getDb();
936  $sRights = $oDb->getOne("select oxrights from oxuser where oxid = ".$oDb->quote($sUserID));
937 
938  if ( $sRights != "user") {
939  // malladmin ?
940  if ( $sRights == "malladmin") {
941  oxSession::setVar( "malladmin", 1);
942  $myConfig->setConfigParam( 'blMallAdmin', true );
943 
944  //#1552T
945  //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
946  $myConfig->setConfigParam( 'blAllowSharedEdit', true );
947 
948  $sShop = oxSession::getVar( "actshop");
949  if ( !isset($sShop)) {
950  oxSession::setVar( "actshop", $myConfig->getBaseShopId());
951  }
952  $blIsAuth = true;
953  } else {
954  // Shopadmin... check if this shop is valid and exists
955  $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote( $sRights ) );
956  if ( isset( $sShopID) && $sShopID) {
957  // success, this shop exists
958 
959  oxSession::setVar( "actshop", $sRights);
960  oxSession::setVar( "currentadminshop", $sRights);
961  oxSession::setVar( "shp", $sRights);
962 
963  // check if this subshop admin is evil.
964  if ('chshp' == oxConfig::getParameter( 'fnc' )) {
965  // dont allow this call
966  $blIsAuth = false;
967  } else {
968  $blIsAuth = true;
969 
970  $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
971  foreach ($aShopIdVars as $sShopIdVar) {
972  if ($sGotShop = oxConfig::getParameter( $sShopIdVar )) {
973  if ($sGotShop != $sRights) {
974  $blIsAuth = false;
975  break;
976  }
977  }
978  }
979  }
980  }
981  }
982  // marking user as admin
983  oxSession::setVar( "blIsAdmin", 1);
984  }
985  }
986  return $blIsAuth;
987  }
988 
998  public function seoIsActive( $blReset = false, $sShopId = null, $iActLang = null )
999  {
1000  if ( !is_null( $this->_blSeoIsActive ) && !$blReset ) {
1001  return $this->_blSeoIsActive;
1002  }
1003 
1004  $myConfig = $this->getConfig();
1005 
1006  if ( ( $this->_blSeoIsActive = $myConfig->getConfigParam( 'blSeoMode' ) ) === null ) {
1007  $this->_blSeoIsActive = true;
1008 
1009  $aSeoModes = $myConfig->getconfigParam( 'aSeoModes' );
1010  $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
1011  $iActLang = $iActLang ? $iActLang : (int) oxRegistry::getLang()->getBaseLanguage();
1012 
1013  // checking special config param for active shop and language
1014  if ( is_array( $aSeoModes ) && isset( $aSeoModes[$sActShopId] ) && isset( $aSeoModes[$sActShopId][$iActLang] ) ) {
1015  $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
1016  }
1017  }
1018 
1019  return $this->_blSeoIsActive;
1020  }
1021 
1029  public function isValidAlpha( $sField )
1030  {
1031  return (boolean) getStr()->preg_match( '/^[a-zA-Z0-9_]*$/', $sField );
1032  }
1033 
1043  protected function _simpleRedirect( $sUrl, $sHeaderCode )
1044  {
1045  $oHeader = oxNew( "oxHeader" );
1046  $oHeader->setHeader( $sHeaderCode );
1047  $oHeader->setHeader( "Location: $sUrl" );
1048  $oHeader->setHeader( "Connection: close" );
1049  $oHeader->sendHeader();
1050  }
1051 
1057  public function redirectOffline($iHeaderCode = 302)
1058  {
1059  $sUrl = $this->getConfig()->getShopUrl() .'offline.html';
1060  $this->redirect($sUrl, false, $iHeaderCode);
1061  }
1062 
1072  public function redirect( $sUrl, $blAddRedirectParam = true, $iHeaderCode = 302 )
1073  {
1074  //preventing possible cyclic redirection
1075  //#M341 and check only if redirect parameter must be added
1076  if ( $blAddRedirectParam && oxConfig::getParameter( 'redirected' ) ) {
1077  return;
1078  }
1079 
1080  if ( $blAddRedirectParam ) {
1081  $sUrl = $this->_addUrlParameters( $sUrl, array( 'redirected' => 1 ) );
1082  }
1083 
1084  $sUrl = str_ireplace( "&amp;", "&", $sUrl );
1085 
1086  switch ($iHeaderCode) {
1087  case 301:
1088  $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
1089  break;
1090  case 500:
1091  $sHeaderCode = "HTTP/1.1 500 Internal Server Error";
1092  break;
1093  case 302:
1094  default:
1095  $sHeaderCode = "HTTP/1.1 302 Found";
1096  }
1097 
1098  $this->_simpleRedirect( $sUrl, $sHeaderCode );
1099 
1100  try {//may occur in case db is lost
1101  $this->getSession()->freeze();
1102  } catch( oxException $oEx ) {
1103  $oEx->debugOut();
1104  //do nothing else to make sure the redirect takes place
1105  }
1106 
1107  if ( defined( 'OXID_PHP_UNIT' ) ) {
1108  return;
1109  }
1110 
1111  $this->showMessageAndExit( '' );
1112  }
1113 
1122  public function showMessageAndExit( $sMsg )
1123  {
1124  $this->getSession()->freeze();
1125  $this->commitFileCache();
1126 
1127  if ( defined( 'OXID_PHP_UNIT' ) ) {
1128  return;
1129  }
1130 
1131 
1132  exit( $sMsg );
1133  }
1134 
1142  public function setHeader($sHeader)
1143  {
1144  header($sHeader);
1145  }
1146 
1155  protected function _addUrlParameters( $sUrl, $aParams )
1156  {
1157  $sDelimiter = ( ( getStr()->strpos( $sUrl, '?' ) !== false ) )?'&':'?';
1158  foreach ( $aParams as $sName => $sVal ) {
1159  $sUrl = $sUrl . $sDelimiter . $sName . '=' . $sVal;
1160  $sDelimiter = '&';
1161  }
1162 
1163  return $sUrl;
1164  }
1165 
1177  protected function _fillExplodeArray( $aName, $dVat = null)
1178  {
1179  $myConfig = $this->getConfig();
1180  $oObject = new stdClass();
1181  $aPrice = explode( '!P!', $aName[0]);
1182 
1183  if ( ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) && isset( $aPrice[0] ) && isset( $aPrice[1] ) ) || $this->isAdmin() ) {
1184 
1185  // yes, price is there
1186  $oObject->price = isset( $aPrice[1] ) ? $aPrice[1] : 0;
1187  $aName[0] = isset( $aPrice[0] ) ? $aPrice[0] : '';
1188 
1189  $iPercPos = getStr()->strpos( $oObject->price, '%' );
1190  if ( $iPercPos !== false ) {
1191  $oObject->priceUnit = '%';
1192  $oObject->fprice = $oObject->price;
1193  $oObject->price = substr( $oObject->price, 0, $iPercPos );
1194  } else {
1195  $oCur = $myConfig->getActShopCurrencyObject();
1196  $oObject->price = str_replace(',', '.', $oObject->price);
1197  $oObject->fprice = oxRegistry::getLang()->formatCurrency( $oObject->price * $oCur->rate, $oCur);
1198  $oObject->priceUnit = 'abs';
1199  }
1200 
1201  // add price info into list
1202  if ( !$this->isAdmin() && $oObject->price != 0 ) {
1203  $aName[0] .= " ";
1204 
1205  $dPrice = $this->_preparePrice( $oObject->price, $dVat );
1206 
1207  if ( $oObject->price > 0 ) {
1208  $aName[0] .= "+";
1209  }
1210  //V FS#2616
1211  if ( $dVat != null && $oObject->priceUnit == 'abs' ) {
1212  $oPrice = oxNew('oxPrice');
1213  $oPrice->setPrice($oObject->price, $dVat);
1214  $aName[0] .= oxRegistry::getLang()->formatCurrency( $dPrice * $oCur->rate, $oCur);
1215  } else {
1216  $aName[0] .= $oObject->fprice;
1217  }
1218  if ( $oObject->priceUnit == 'abs' ) {
1219  $aName[0] .= " ".$oCur->sign;
1220  }
1221  }
1222  } elseif ( isset( $aPrice[0] ) && isset($aPrice[1] ) ) {
1223  // A. removing unused part of information
1224  $aName[0] = getStr()->preg_replace( "/!P!.*/", "", $aName[0] );
1225  }
1226 
1227  $oObject->name = $aName[0];
1228  $oObject->value = $aName[1];
1229  return $oObject;
1230  }
1231 
1240  protected function _preparePrice( $dPrice, $dVat )
1241  {
1242  $blCalculationModeNetto = $this->_isPriceViewModeNetto();
1243 
1244  $oCurrency = $this->getConfig()->getActShopCurrencyObject();
1245 
1246  $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
1247  if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
1248  $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), $oCurrency->decimal );
1249  } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
1250  $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), $oCurrency->decimal );
1251  }
1252 
1253  return $dPrice;
1254  }
1255 
1261  protected function _isPriceViewModeNetto()
1262  {
1263  $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
1264  $oUser = $this->_getArticleUser();
1265  if ($oUser) {
1266  $blResult = $oUser->isPriceViewModeNetto();
1267  }
1268 
1269  return $blResult;
1270  }
1271 
1277  protected function _getArticleUser()
1278  {
1279  if ($this->_oUser) {
1280  return $this->_oUser;
1281  }
1282 
1283  return $this->getUser();
1284  }
1285 
1293  public function oxMimeContentType( $sFileName )
1294  {
1295  $sFileName = strtolower( $sFileName );
1296  $iLastDot = strrpos( $sFileName, '.' );
1297 
1298  if ( $iLastDot !== false ) {
1299  $sType = substr( $sFileName, $iLastDot + 1 );
1300  switch ( $sType ) {
1301  case 'gif':
1302  $sType = 'image/gif';
1303  break;
1304  case 'jpeg':
1305  case 'jpg':
1306  $sType = 'image/jpeg';
1307  break;
1308  case 'png':
1309  $sType = 'image/png';
1310  break;
1311  default:
1312  $sType = false;
1313  break;
1314  }
1315  }
1316  return $sType;
1317  }
1318 
1327  public function logger( $sText, $blNewline = false )
1328  { $myConfig = $this->getConfig();
1329 
1330  if ( $myConfig->getConfigParam( 'iDebug' ) == -2) {
1331  if ( gettype( $sText ) != 'string' ) {
1332  $sText = var_export( $sText, true);
1333  }
1334  $sLogMsg = "----------------------------------------------\n{$sText}".( ( $blNewline ) ?"\n":"" )."\n";
1335  $this->writeToLog( $sLogMsg, "log.txt" );
1336  }
1337 
1338  }
1339 
1347  protected function _stripQuotes($mInput)
1348  {
1349  return is_array($mInput) ? array_map( array( $this, '_stripQuotes' ), $mInput) : stripslashes( $mInput );
1350  }
1351 
1359  public function strRot13( $sStr )
1360  {
1361  $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
1362  $sTo = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
1363 
1364  return strtr( $sStr, $sFrom, $sTo );
1365  }
1366 
1376  public function getCacheFilePath( $sCacheName, $blPathOnly = false, $sExtension = 'txt' )
1377  {
1378 
1379  $sVersionPrefix = 'pe';
1380 
1381  $sPath = realpath($this->getConfig()->getConfigParam( 'sCompileDir' ));
1382 
1383  if (!$sPath) {
1384  return false;
1385  }
1386 
1387  return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
1388  }
1389 
1397  public function getLangCache( $sCacheName )
1398  {
1399  $aLangCache = null;
1400  $sFilePath = $this->getCacheFilePath( $sCacheName );
1401  if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
1402  include $sFilePath;
1403  }
1404  return $aLangCache;
1405  }
1406 
1415  public function setLangCache( $sCacheName, $aLangCache )
1416  {
1417  $sCache = "<?php\n\$aLangCache = ".var_export( $aLangCache, true ).";\n?>";
1418  $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache, LOCK_EX);
1419  return $blRes;
1420  }
1421 
1429  public function checkUrlEndingSlash( $sUrl )
1430  {
1431  if ( !getStr()->preg_match("/\/$/", $sUrl) ) {
1432  $sUrl .= '/';
1433  }
1434 
1435  return $sUrl;
1436  }
1437 
1446  public function writeToLog( $sLogMessage, $sLogFileName )
1447  {
1448  $sLogDist = $this->getConfig()->getLogsDir().$sLogFileName;
1449  $blOk = false;
1450 
1451  if ( ( $oHandle = fopen( $sLogDist, 'a' ) ) !== false ) {
1452  fwrite( $oHandle, $sLogMessage );
1453  $blOk = fclose( $oHandle );
1454  }
1455 
1456  return $blOk;
1457  }
1458 
1466  public function handlePageNotFoundError($sUrl = '')
1467  {
1468  $this->setHeader("HTTP/1.0 404 Not Found");
1469  if ( oxRegistry::getConfig()->isUtf() ) {
1470  $this->setHeader("Content-Type: text/html; charset=UTF-8");
1471  }
1472 
1473  $sReturn = "Page not found.";
1474  try {
1475  $oView = oxNew('oxUBase');
1476  $oView->init();
1477  $oView->render();
1478  $oView->setClassName( 'oxUBase' );
1479  $oView->addTplParam('sUrl', $sUrl);
1480  if ($sRet = oxRegistry::get("oxUtilsView")->getTemplateOutput('message/err_404.tpl', $oView)) {
1481  $sReturn = $sRet;
1482  }
1483  } catch (Exception $e) {
1484  }
1485  $this->showMessageAndExit( $sReturn );
1486  }
1487 
1495  public function extractDomain( $sHost )
1496  {
1497  $oStr = getStr();
1498  if ( !$oStr->preg_match( '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost ) &&
1499  ( $iLastDot = strrpos( $sHost, '.' ) ) !== false ) {
1500  $iLen = $oStr->strlen( $sHost );
1501  if ( ( $iNextDot = strrpos( $sHost, '.', ( $iLen - $iLastDot + 1 ) * - 1 ) ) !== false ) {
1502  $sHost = trim( $oStr->substr( $sHost, $iNextDot ), '.' );
1503  }
1504  }
1505 
1506  return $sHost;
1507  }
1508 }