OXID eShop CE  4.10.4
 All Classes Namespaces 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 {
14 
20  protected $_iCurPrecision = null;
21 
29  protected $_sPermanentCachePattern = "/c_fieldnames_|c_tbdsc_|_allfields_/";
30 
36  protected $_sLanguageCachePattern = "/c_langcache_/i";
37 
43  protected $_sMenuCachePattern = "/c_menu_/i";
44 
50  protected $_aLockedFileHandles = array();
51 
57  protected $_aFileCacheContents = array();
58 
64  protected $_blIsSe = null;
65 
71  protected $_aStaticCache;
72 
78  protected $_blSeoIsActive = null;
79 
90  public function strMan($sVal, $sKey = null)
91  {
92  $oEncryptor = oxNew('oxEncryptor');
93  $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
94 
95  return $oEncryptor->encrypt($sVal, $sKey);
96  }
97 
108  public function strRem($sVal, $sKey = null)
109  {
110  $oDecryptor = oxNew('oxDecryptor');
111  $sKey = $sKey ? $sKey : $this->getConfig()->getConfigParam('sConfigKey');
112 
113  return $oDecryptor->decrypt($sVal, $sKey);
114  }
115 
123  public function getArrFldName($sName)
124  {
125  return str_replace(".", "__", $sName);
126  }
127 
136  public function assignValuesFromText($sIn, $dVat = null)
137  {
138  $aRet = array();
139  $aPieces = explode('@@', $sIn);
140  while (list($sKey, $sVal) = each($aPieces)) {
141  if ($sVal) {
142  $aName = explode('__', $sVal);
143  if (isset($aName[0]) && isset($aName[1])) {
144  $aRet[] = $this->_fillExplodeArray($aName, $dVat);
145  }
146  }
147  }
148 
149  return $aRet;
150  }
151 
159  public function assignValuesToText($aIn)
160  {
161  $sRet = "";
162  reset($aIn);
163  while (list($sKey, $sVal) = each($aIn)) {
164  $sRet .= $sKey;
165  $sRet .= "__";
166  $sRet .= $sVal;
167  $sRet .= "@@";
168  }
169 
170  return $sRet;
171  }
172 
180  public function currency2Float($sValue)
181  {
182  $fRet = $sValue;
183  $iPos = strrpos($sValue, ".");
184  if ($iPos && ((strlen($sValue) - 1 - $iPos) < 2 + 1)) {
185  // replace decimal with ","
186  $fRet = substr_replace($fRet, ",", $iPos, 1);
187  }
188  // remove thousands
189  $fRet = str_replace(array(" ", "."), "", $fRet);
190 
191  $fRet = str_replace(",", ".", $fRet);
192 
193  return (float) $fRet;
194  }
195 
203  public function string2Float($sValue)
204  {
205  $fRet = str_replace(" ", "", $sValue);
206  $iCommaPos = strpos($fRet, ",");
207  $iDotPos = strpos($fRet, ".");
208  if (!$iDotPos xor !$iCommaPos) {
209  if (substr_count($fRet, ",") > 1 || substr_count($fRet, ".") > 1) {
210  $fRet = str_replace(array(",", "."), "", $fRet);
211  } else {
212  $fRet = str_replace(",", ".", $fRet);
213  }
214  } else {
215  if ($iDotPos < $iCommaPos) {
216  $fRet = str_replace(".", "", $fRet);
217  $fRet = str_replace(",", ".", $fRet);
218  }
219  }
220  // remove thousands
221  $fRet = str_replace(array(" ", ","), "", $fRet);
222 
223  return (float) $fRet;
224  }
225 
233  public function isSearchEngine($sClient = null)
234  {
235  if (is_null($this->_blIsSe)) {
236  $this->setSearchEngine(null, $sClient);
237  }
238 
239  return $this->_blIsSe;
240  }
241 
250  public function setSearchEngine($blIsSe = null, $sClient = null)
251  {
252  if (isset($blIsSe)) {
253  $this->_blIsSe = $blIsSe;
254 
255  return;
256  }
257  startProfile("isSearchEngine");
258 
259  $myConfig = $this->getConfig();
260  $blIsSe = false;
261 
262  if (!($myConfig->getConfigParam('iDebug') && $this->isAdmin())) {
263  $aRobots = $myConfig->getConfigParam('aRobots');
264  $aRobots = is_array($aRobots) ? $aRobots : array();
265 
266  $aRobotsExcept = $myConfig->getConfigParam('aRobotsExcept');
267  $aRobotsExcept = is_array($aRobotsExcept) ? $aRobotsExcept : array();
268 
269  $sClient = $sClient ? $sClient : strtolower(getenv('HTTP_USER_AGENT'));
270  $blIsSe = false;
271  $aRobots = array_merge($aRobots, $aRobotsExcept);
272  foreach ($aRobots as $sRobot) {
273  if (strpos($sClient, $sRobot) !== false) {
274  $blIsSe = true;
275  break;
276  }
277  }
278  }
279 
280  $this->_blIsSe = $blIsSe;
281 
282  stopProfile("isSearchEngine");
283  }
284 
295  public function isValidEmail($sEmail)
296  {
297  $oMailValidator = oxNew('oxMailValidator');
298 
299  return $oMailValidator->isValidEmail($sEmail);
300  }
301 
309  public function loadAdminProfile($aInterfaceProfiles)
310  {
311  // improved #533
312  // checking for available profiles list
313  if (is_array($aInterfaceProfiles)) {
314  //checking for previous profiles
315  $sPrevProfile = oxRegistry::get("oxUtilsServer")->getOxCookie('oxidadminprofile');
316  if (isset($sPrevProfile)) {
317  $aPrevProfile = @explode("@", trim($sPrevProfile));
318  }
319 
320  //array to store profiles
321  $aProfiles = array();
322  foreach ($aInterfaceProfiles as $iPos => $sProfile) {
323  $aProfileSettings = array($iPos, $sProfile);
324  $aProfiles[] = $aProfileSettings;
325  }
326  // setting previous used profile as active
327  if (isset($aPrevProfile[0]) && isset($aProfiles[$aPrevProfile[0]])) {
328  $aProfiles[$aPrevProfile[0]][2] = 1;
329  }
330 
331  oxRegistry::getSession()->setVariable("aAdminProfiles", $aProfiles);
332 
333  return $aProfiles;
334  }
335 
336  return null;
337  }
338 
347  public function fRound($sVal, $oCur = null)
348  {
349  startProfile('fround');
350 
351  //cached currency precision, this saves about 1% of execution time
352  $iCurPrecision = null;
353  if (!defined('OXID_PHP_UNIT')) {
354  $iCurPrecision = $this->_iCurPrecision;
355  }
356 
357  if (is_null($iCurPrecision)) {
358  if (!$oCur) {
359  $oCur = $this->getConfig()->getActShopCurrencyObject();
360  }
361 
362  $iCurPrecision = $oCur->decimal;
363  $this->_iCurPrecision = $iCurPrecision;
364  }
365 
366  // if < 5.3.x this is a workaround for #36008 bug in php - incorrect round() & number_format() result (R)
367  static $dprez = null;
368  if (!$dprez) {
369  $prez = @ini_get("precision");
370  if (!$prez || $prez > 12) {
371  $prez = 12;
372  }
373  $dprez = pow(10, -$prez);
374  }
375  stopProfile('fround');
376 
377  return round($sVal + $dprez * ($sVal >= 0 ? 1 : -1), $iCurPrecision);
378  }
379 
394  public function arrayStringSearch($sNeedle, $aHaystack)
395  {
396  $sResult = array_search((string) $sNeedle, $aHaystack);
397  $sSecond = array_search((string) $sNeedle, $aHaystack, true);
398 
399  //got a different result when using strict and not strict?
400  //do a detail check
401  if( $sResult != $sSecond) {
402  $aStringstack = array();
403  foreach ($aHaystack as $sValue) {
404  $aStringstack[] = (string) $sValue;
405  }
406  $sResult = array_search((string) $sNeedle, $aStringstack, true);
407  }
408  return $sResult;
409  }
410 
418  public function toStaticCache($sName, $sContent, $sKey = null)
419  {
420  // if it's an array then we add
421  if ($sKey) {
422  $this->_aStaticCache[$sName][$sKey] = $sContent;
423  } else {
424  $this->_aStaticCache[$sName] = $sContent;
425  }
426  }
427 
435  public function fromStaticCache($sName)
436  {
437  if (isset($this->_aStaticCache[$sName])) {
438  return $this->_aStaticCache[$sName];
439  }
440 
441  return null;
442  }
443 
449  public function cleanStaticCache($sCacheName = null)
450  {
451  if ($sCacheName) {
452  unset($this->_aStaticCache[$sCacheName]);
453  } else {
454  $this->_aStaticCache = null;
455  }
456  }
457 
465  public function toPhpFileCache($sKey, $mContents)
466  {
467  //only simple arrays are supported
468  if (is_array($mContents) && ($sCachePath = $this->getCacheFilePath($sKey, false, 'php'))) {
469 
470  // setting meta
471  $this->setCacheMeta($sKey, array("serialize" => false, "cachepath" => $sCachePath));
472 
473  // caching..
474  $this->toFileCache($sKey, $mContents);
475  }
476  }
477 
485  public function fromPhpFileCache($sKey)
486  {
487  // setting meta
488  $this->setCacheMeta($sKey, array("include" => true, "cachepath" => $this->getCacheFilePath($sKey, false, 'php')));
489 
490  return $this->fromFileCache($sKey);
491  }
492 
500  public function getCacheMeta($sKey)
501  {
502  return isset($this->_aFileCacheMeta[$sKey]) ? $this->_aFileCacheMeta[$sKey] : false;
503  }
504 
511  public function setCacheMeta($sKey, $aMeta)
512  {
513  // cache meta data
514  $this->_aFileCacheMeta[$sKey] = $aMeta;
515  }
516 
527  public function toFileCache($sKey, $mContents, $iTtl = 0)
528  {
529  $aCacheData['content'] = $mContents;
530  $aMeta = $this->getCacheMeta($sKey);
531  if ($iTtl) {
532  $aCacheData['ttl'] = $iTtl;
533  $aCacheData['timestamp'] = oxRegistry::get("oxUtilsDate")->getTime();
534  }
535  $this->_aFileCacheContents[$sKey] = $aCacheData;
536 
537  // looking for cache meta
538  $sCachePath = isset($aMeta["cachepath"]) ? $aMeta["cachepath"] : $this->getCacheFilePath($sKey);
539 
540  return ( bool ) $this->_lockFile($sCachePath, $sKey);
541  }
542 
550  public function fromFileCache($sKey)
551  {
552  if (!array_key_exists($sKey, $this->_aFileCacheContents)) {
553  $sRes = null;
554 
555  $aMeta = $this->getCacheMeta($sKey);
556  $blInclude = isset($aMeta["include"]) ? $aMeta["include"] : false;
557  $sCachePath = isset($aMeta["cachepath"]) ? $aMeta["cachepath"] : $this->getCacheFilePath($sKey);
558 
559  // trying to lock
560  $this->_lockFile($sCachePath, $sKey, LOCK_SH);
561 
562  clearstatcache();
563  if (is_readable($sCachePath)) {
564  $sRes = $blInclude ? $this->_includeFile($sCachePath) : $this->_readFile($sCachePath);
565  }
566 
567  if (isset($sRes['ttl']) && $sRes['ttl'] != 0) {
568  $iTimestamp = $sRes['timestamp'];
569  $iTtl = $sRes['ttl'];
570 
571  $iTime = oxRegistry::get("oxUtilsDate")->getTime();
572  if ($iTime > $iTimestamp + $iTtl) {
573  return null;
574  }
575  }
576  // release lock
577  $this->_releaseFile($sKey, LOCK_SH);
578 
579  // caching
580  $this->_aFileCacheContents[$sKey] = $sRes;
581  }
582 
583  return $this->_aFileCacheContents[$sKey]['content'];
584  }
585 
593  protected function _readFile($sFilePath)
594  {
595  $sRes = file_get_contents($sFilePath);
596 
597  return $sRes ? unserialize($sRes) : null;
598  }
599 
607  protected function _includeFile($sFilePath)
608  {
609  $_aCacheContents = null;
610  include $sFilePath;
611 
612  return $_aCacheContents;
613  }
614 
623  protected function _processCache($sKey, $mContents)
624  {
625  // looking for cache meta
626  $aCacheMeta = $this->getCacheMeta($sKey);
627  $blSerialize = isset($aCacheMeta["serialize"]) ? $aCacheMeta["serialize"] : true;
628 
629  if ($blSerialize) {
630  $mContents = serialize($mContents);
631  } else {
632  $mContents = "<?php\n//automatically generated file\n//" . date("Y-m-d H:i:s") . "\n\n\$_aCacheContents = " . var_export($mContents, true) . "\n?>";
633  }
634 
635  return $mContents;
636  }
637 
642  public function commitFileCache()
643  {
644  if (!empty($this->_aLockedFileHandles[LOCK_EX])) {
645  startProfile("!__SAVING CACHE__! (warning)");
646  foreach ($this->_aLockedFileHandles[LOCK_EX] as $sKey => $rHandle) {
647  if ($rHandle !== false && isset($this->_aFileCacheContents[$sKey])) {
648 
649  // #0002931A truncate file once more before writing
650  ftruncate($rHandle, 0);
651 
652  // writing cache
653  fwrite($rHandle, $this->_processCache($sKey, $this->_aFileCacheContents[$sKey]));
654 
655  // releasing locks
656  $this->_releaseFile($sKey);
657  }
658  }
659 
660  stopProfile("!__SAVING CACHE__! (warning)");
661 
662  //empty buffer
663  $this->_aFileCacheContents = array();
664  }
665  }
666 
676  protected function _lockFile($sFilePath, $sIdent, $iLockMode = LOCK_EX)
677  {
678  $rHandle = isset($this->_aLockedFileHandles[$iLockMode][$sIdent]) ? $this->_aLockedFileHandles[$iLockMode][$sIdent] : null;
679  if ($rHandle === null) {
680 
681  $blLocked = false;
682  $rHandle = @fopen($sFilePath, "a+");
683 
684  if ($rHandle !== false) {
685 
686  if (flock($rHandle, $iLockMode | LOCK_NB)) {
687  if ($iLockMode === LOCK_EX) {
688  // truncate file
689  $blLocked = ftruncate($rHandle, 0);
690  } else {
691  // move to a start position
692  $blLocked = fseek($rHandle, 0) === 0;
693  }
694  }
695 
696  // on failure - closing and setting false..
697  if (!$blLocked) {
698  fclose($rHandle);
699  $rHandle = false;
700  }
701  }
702 
703  // in case system does not support file locking
704  if (!$blLocked && $iLockMode === LOCK_EX) {
705 
706  // clearing on first call
707  if (count($this->_aLockedFileHandles) == 0) {
708  clearstatcache();
709  }
710 
711  // start a blank file to inform other processes we are dealing with it.
712  if (!(file_exists($sFilePath) && !filesize($sFilePath) && abs(time() - filectime($sFilePath) < 40))) {
713  $rHandle = @fopen($sFilePath, "w");
714  }
715  }
716 
717  $this->_aLockedFileHandles[$iLockMode][$sIdent] = $rHandle;
718  }
719 
720  return $rHandle;
721  }
722 
731  protected function _releaseFile($sIdent, $iLockMode = LOCK_EX)
732  {
733  $blSuccess = true;
734  if (isset($this->_aLockedFileHandles[$iLockMode][$sIdent]) &&
735  $this->_aLockedFileHandles[$iLockMode][$sIdent] !== false
736  ) {
737 
738  // release the lock and close file
739  $blSuccess = flock($this->_aLockedFileHandles[$iLockMode][$sIdent], LOCK_UN) &&
740  fclose($this->_aLockedFileHandles[$iLockMode][$sIdent]);
741  unset($this->_aLockedFileHandles[$iLockMode][$sIdent]);
742  }
743 
744  return $blSuccess;
745  }
746 
752  public function oxResetFileCache()
753  {
754  $aFiles = glob($this->getCacheFilePath(null, true) . '*');
755  if (is_array($aFiles)) {
756  // delete all the files, except cached tables field names
757  $aFiles = preg_grep($this->_sPermanentCachePattern, $aFiles, PREG_GREP_INVERT);
758  foreach ($aFiles as $sFile) {
759  @unlink($sFile);
760  }
761  }
762  }
763 
769  public function resetTemplateCache($aTemplates)
770  {
771  $sSmartyDir = oxRegistry::get("oxUtilsView")->getSmartyDir();
772  //$aFiles = glob( $this->getCacheFilePath( null, true ) . '*' );
773  $aFiles = glob($sSmartyDir . '*');
774 
775  if (is_array($aFiles) && is_array($aTemplates) && count($aTemplates)) {
776  // delete all template cache files
777  foreach ($aTemplates as &$sTemplate) {
778  $sTemplate = preg_quote(basename(strtolower($sTemplate), '.tpl'));
779  }
780 
781  $sPattern = sprintf("/%%(%s)\.tpl\.php$/i", implode('|', $aTemplates));
782  $aFiles = preg_grep($sPattern, $aFiles);
783 
784  if (is_array($aFiles)) {
785  foreach ($aFiles as $sFile) {
786  @unlink($sFile);
787  }
788  }
789  }
790 
791  }
792 
796  public function resetLanguageCache()
797  {
798  $aFiles = glob($this->getCacheFilePath(null, true) . '*');
799  if (is_array($aFiles)) {
800  // delete all language cache files
801  $sPattern = $this->_sLanguageCachePattern;
802  $aFiles = preg_grep($sPattern, $aFiles);
803  foreach ($aFiles as $sFile) {
804  @unlink($sFile);
805  }
806  }
807  }
808 
812  public function resetMenuCache()
813  {
814  $aFiles = glob($this->getCacheFilePath(null, true) . '*');
815  if (is_array($aFiles)) {
816  // delete all menu cache files
817  $sPattern = $this->_sMenuCachePattern;
818  $aFiles = preg_grep($sPattern, $aFiles);
819  foreach ($aFiles as $sFile) {
820  @unlink($sFile);
821  }
822  }
823  }
824 
834  public function getRemoteCachePath($sRemote, $sLocal)
835  {
836  clearstatcache();
837  if (file_exists($sLocal) && filemtime($sLocal) && filemtime($sLocal) > time() - 86400) {
838  return $sLocal;
839  }
840  $hRemote = @fopen($sRemote, "rb");
841  $blSuccess = false;
842  if (isset($hRemote) && $hRemote) {
843  $hLocal = fopen($sLocal, "wb");
844  stream_copy_to_stream($hRemote, $hLocal);
845  fclose($hRemote);
846  fclose($hLocal);
847  $blSuccess = true;
848  } else {
849  // try via fsockopen
850  $aUrl = @parse_url($sRemote);
851  if (!empty($aUrl["host"])) {
852  $sPath = $aUrl["path"];
853  if (empty($sPath)) {
854  $sPath = "/";
855  }
856  $sHost = $aUrl["host"];
857 
858  $hSocket = @fsockopen($sHost, 80, $iErrorNumber, $iErrStr, 5);
859  if ($hSocket) {
860  fputs($hSocket, "GET " . $sPath . " HTTP/1.0\r\nHost: $sHost\r\n\r\n");
861  $headers = stream_get_line($hSocket, 4096, "\r\n\r\n");
862  if (($hLocal = @fopen($sLocal, "wb")) !== false) {
863  rewind($hLocal);
864  // does not copy all the data
865  // stream_copy_to_stream($hSocket, $hLocal);
866  fwrite($hLocal, stream_get_contents($hSocket));
867  fclose($hLocal);
868  fclose($hSocket);
869  $blSuccess = true;
870  }
871  }
872  }
873  }
874  if ($blSuccess || file_exists($sLocal)) {
875  return $sLocal;
876  }
877 
878  return false;
879  }
880 
886  public function canPreview()
887  {
888  $blCan = null;
889  if (($sPrevId = oxRegistry::getConfig()->getRequestParameter('preview')) &&
890  ($sAdminSid = oxRegistry::get("oxUtilsServer")->getOxCookie('admin_sid'))
891  ) {
892 
893  $sTable = getViewName('oxuser');
894  $oDb = oxDb::getDb();
895  $sQ = "select 1 from $sTable where MD5( CONCAT( " . $oDb->quote($sAdminSid) . ", {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = " . oxDb::getDb()->quote($sPrevId);
896  $blCan = (bool) $oDb->getOne($sQ);
897  }
898 
899  return $blCan;
900  }
901 
907  public function getPreviewId()
908  {
909  $sAdminSid = oxRegistry::get("oxUtilsServer")->getOxCookie('admin_sid');
910  if (($oUser = $this->getUser())) {
911  return md5($sAdminSid . $oUser->getId() . $oUser->oxuser__oxpassword->value . $oUser->oxuser__oxrights->value);
912  }
913  }
914 
920  public function checkAccessRights()
921  {
922  $myConfig = $this->getConfig();
923 
924  $blIsAuth = false;
925 
926  $sUserID = oxRegistry::getSession()->getVariable("auth");
927 
928  // deleting admin marker
929  oxRegistry::getSession()->setVariable("malladmin", 0);
930  oxRegistry::getSession()->setVariable("blIsAdmin", 0);
931  oxRegistry::getSession()->deleteVariable("blIsAdmin");
932  $myConfig->setConfigParam('blMallAdmin', false);
933  //#1552T
934  $myConfig->setConfigParam('blAllowInheritedEdit', false);
935 
936  if ($sUserID) {
937  // escaping
938  $oDb = oxDb::getDb();
939  $sRights = $oDb->getOne("select oxrights from oxuser where oxid = " . $oDb->quote($sUserID));
940 
941  if ($sRights != "user") {
942  // malladmin ?
943  if ($sRights == "malladmin") {
944  oxRegistry::getSession()->setVariable("malladmin", 1);
945  $myConfig->setConfigParam('blMallAdmin', true);
946 
947  //#1552T
948  //So far this blAllowSharedEdit is Equal to blMallAdmin but in future to be solved over rights and roles
949  $myConfig->setConfigParam('blAllowSharedEdit', true);
950 
951  $sShop = oxRegistry::getSession()->getVariable("actshop");
952  if (!isset($sShop)) {
953  oxRegistry::getSession()->setVariable("actshop", $myConfig->getBaseShopId());
954  }
955  $blIsAuth = true;
956  } else {
957  // Shopadmin... check if this shop is valid and exists
958  $sShopID = $oDb->getOne("select oxid from oxshops where oxid = " . $oDb->quote($sRights));
959  if (isset($sShopID) && $sShopID) {
960  // success, this shop exists
961 
962  oxRegistry::getSession()->setVariable("actshop", $sRights);
963  oxRegistry::getSession()->setVariable("currentadminshop", $sRights);
964  oxRegistry::getSession()->setVariable("shp", $sRights);
965 
966  // check if this subshop admin is evil.
967  if ('chshp' == oxRegistry::getConfig()->getRequestParameter('fnc')) {
968  // dont allow this call
969  $blIsAuth = false;
970  } else {
971  $blIsAuth = true;
972 
973  $aShopIdVars = array('actshop', 'shp', 'currentadminshop');
974  foreach ($aShopIdVars as $sShopIdVar) {
975  if ($sGotShop = oxRegistry::getConfig()->getRequestParameter($sShopIdVar)) {
976  if ($sGotShop != $sRights) {
977  $blIsAuth = false;
978  break;
979  }
980  }
981  }
982  }
983  }
984  }
985  // marking user as admin
986  oxRegistry::getSession()->setVariable("blIsAdmin", 1);
987  }
988  }
989 
990  return $blIsAuth;
991  }
992 
1002  public function seoIsActive($blReset = false, $sShopId = null, $iActLang = null)
1003  {
1004  if (!is_null($this->_blSeoIsActive) && !$blReset) {
1005  return $this->_blSeoIsActive;
1006  }
1007 
1008  $myConfig = $this->getConfig();
1009 
1010  if (($this->_blSeoIsActive = $myConfig->getConfigParam('blSeoMode')) === null) {
1011  $this->_blSeoIsActive = true;
1012 
1013  $aSeoModes = $myConfig->getconfigParam('aSeoModes');
1014  $sActShopId = $sShopId ? $sShopId : $myConfig->getActiveShop()->getId();
1015  $iActLang = $iActLang ? $iActLang : (int) oxRegistry::getLang()->getBaseLanguage();
1016 
1017  // checking special config param for active shop and language
1018  if (is_array($aSeoModes) && isset($aSeoModes[$sActShopId]) && isset($aSeoModes[$sActShopId][$iActLang])) {
1019  $this->_blSeoIsActive = (bool) $aSeoModes[$sActShopId][$iActLang];
1020  }
1021  }
1022 
1023  return $this->_blSeoIsActive;
1024  }
1025 
1033  public function isValidAlpha($sField)
1034  {
1035  return (boolean) getStr()->preg_match('/^[a-zA-Z0-9_]*$/', $sField);
1036  }
1037 
1045  protected function _simpleRedirect($sUrl, $sHeaderCode)
1046  {
1047  $oHeader = oxNew("oxHeader");
1048  $oHeader->setHeader($sHeaderCode);
1049  $oHeader->setHeader("Location: $sUrl");
1050  $oHeader->setHeader("Connection: close");
1051  $oHeader->sendHeader();
1052  }
1053 
1059  public function redirectOffline($iHeaderCode = 302)
1060  {
1061  $sUrl = $this->getConfig()->getShopUrl() . 'offline.html';
1062  $this->redirect($sUrl, false, $iHeaderCode);
1063  }
1064 
1074  public function redirect($sUrl, $blAddRedirectParam = true, $iHeaderCode = 302)
1075  {
1076  //preventing possible cyclic redirection
1077  //#M341 and check only if redirect parameter must be added
1078  if ($blAddRedirectParam && oxRegistry::getConfig()->getRequestParameter('redirected')) {
1079  return;
1080  }
1081 
1082  if ($blAddRedirectParam) {
1083  $sUrl = $this->_addUrlParameters($sUrl, array('redirected' => 1));
1084  }
1085 
1086  $sUrl = str_ireplace("&amp;", "&", $sUrl);
1087 
1088  switch ($iHeaderCode) {
1089  case 301:
1090  $sHeaderCode = "HTTP/1.1 301 Moved Permanently";
1091  break;
1092  case 500:
1093  $sHeaderCode = "HTTP/1.1 500 Internal Server Error";
1094  break;
1095  case 302:
1096  default:
1097  $sHeaderCode = "HTTP/1.1 302 Found";
1098  }
1099 
1100  $this->_simpleRedirect($sUrl, $sHeaderCode);
1101 
1102  try { //may occur in case db is lost
1103  $this->getSession()->freeze();
1104  } catch (oxException $oEx) {
1105  $oEx->debugOut();
1106  //do nothing else to make sure the redirect takes place
1107  }
1108 
1109  if (defined('OXID_PHP_UNIT')) {
1110  return;
1111  }
1112 
1113  $this->showMessageAndExit('');
1114  }
1115 
1124  public function showMessageAndExit($sMsg)
1125  {
1126  $this->getSession()->freeze();
1127  $this->commitFileCache();
1128 
1129  if (defined('OXID_PHP_UNIT')) {
1130  return;
1131  }
1132 
1133 
1134  exit($sMsg);
1135  }
1136 
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 
1230  return $oObject;
1231  }
1232 
1241  protected function _preparePrice($dPrice, $dVat)
1242  {
1243  $blCalculationModeNetto = $this->_isPriceViewModeNetto();
1244 
1245  $oCurrency = $this->getConfig()->getActShopCurrencyObject();
1246 
1247  $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
1248  if ($blCalculationModeNetto && !$blEnterNetPrice) {
1249  $dPrice = round(oxPrice::brutto2Netto($dPrice, $dVat), $oCurrency->decimal);
1250  } elseif (!$blCalculationModeNetto && $blEnterNetPrice) {
1251  $dPrice = round(oxPrice::netto2Brutto($dPrice, $dVat), $oCurrency->decimal);
1252  }
1253 
1254  return $dPrice;
1255  }
1256 
1262  protected function _isPriceViewModeNetto()
1263  {
1264  $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
1265  $oUser = $this->_getArticleUser();
1266  if ($oUser) {
1267  $blResult = $oUser->isPriceViewModeNetto();
1268  }
1269 
1270  return $blResult;
1271  }
1272 
1278  protected function _getArticleUser()
1279  {
1280  if ($this->_oUser) {
1281  return $this->_oUser;
1282  }
1283 
1284  return $this->getUser();
1285  }
1286 
1294  public function oxMimeContentType($sFileName)
1295  {
1296  $sFileName = strtolower($sFileName);
1297  $iLastDot = strrpos($sFileName, '.');
1298 
1299  if ($iLastDot !== false) {
1300  $sType = substr($sFileName, $iLastDot + 1);
1301  switch ($sType) {
1302  case 'gif':
1303  $sType = 'image/gif';
1304  break;
1305  case 'jpeg':
1306  case 'jpg':
1307  $sType = 'image/jpeg';
1308  break;
1309  case 'png':
1310  $sType = 'image/png';
1311  break;
1312  default:
1313  $sType = false;
1314  break;
1315  }
1316  }
1317 
1318  return $sType;
1319  }
1320 
1329  public function logger($sText, $blNewline = false)
1330  {
1331  $myConfig = $this->getConfig();
1332 
1333  if ($myConfig->getConfigParam('iDebug') == -2) {
1334  if (gettype($sText) != 'string') {
1335  $sText = var_export($sText, true);
1336  }
1337  $sLogMsg = "----------------------------------------------\n{$sText}" . (($blNewline) ? "\n" : "") . "\n";
1338  $this->writeToLog($sLogMsg, "log.txt");
1339  }
1340 
1341  }
1342 
1350  public function strRot13($sStr)
1351  {
1352  $sFrom = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
1353  $sTo = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
1354 
1355  return strtr($sStr, $sFrom, $sTo);
1356  }
1357 
1367  public function getCacheFilePath($sCacheName, $blPathOnly = false, $sExtension = 'txt')
1368  {
1369 
1370  $sVersionPrefix = 'pe';
1371 
1372  $sPath = realpath($this->getConfig()->getConfigParam('sCompileDir'));
1373 
1374  if (!$sPath) {
1375  return false;
1376  }
1377 
1378  return $blPathOnly ? "{$sPath}/" : "{$sPath}/ox{$sVersionPrefix}c_{$sCacheName}." . $sExtension;
1379  }
1380 
1388  public function getLangCache($sCacheName)
1389  {
1390  $aLangCache = null;
1391  $sFilePath = $this->getCacheFilePath($sCacheName);
1392  if (file_exists($sFilePath) && is_readable($sFilePath)) {
1393  include $sFilePath;
1394  }
1395 
1396  return $aLangCache;
1397  }
1398 
1407  public function setLangCache($sCacheName, $aLangCache)
1408  {
1409  $sCache = "<?php\n\$aLangCache = " . var_export($aLangCache, true) . ";\n?>";
1410  $blRes = file_put_contents($this->getCacheFilePath($sCacheName), $sCache, LOCK_EX);
1411 
1412  return $blRes;
1413  }
1414 
1422  public function checkUrlEndingSlash($sUrl)
1423  {
1424  if (!getStr()->preg_match("/\/$/", $sUrl)) {
1425  $sUrl .= '/';
1426  }
1427 
1428  return $sUrl;
1429  }
1430 
1441  public function writeToLog($sLogMessage, $sLogFileName)
1442  {
1443  $sLogDist = $this->getConfig()->getLogsDir() . $sLogFileName;
1444  $blOk = false;
1445 
1446  if (($oHandle = fopen($sLogDist, 'a')) !== false) {
1447  fwrite($oHandle, $sLogMessage);
1448  $blOk = fclose($oHandle);
1449  }
1450 
1451  return $blOk;
1452  }
1453 
1459  public function handlePageNotFoundError($sUrl = '')
1460  {
1461  $this->setHeader("HTTP/1.0 404 Not Found");
1462  if (oxRegistry::getConfig()->isUtf()) {
1463  $this->setHeader("Content-Type: text/html; charset=UTF-8");
1464  }
1465 
1466  $sReturn = "Page not found.";
1467  try {
1468  $oView = oxNew('oxUBase');
1469  $oView->init();
1470  $oView->render();
1471  $oView->setClassName('oxUBase');
1472  $oView->addTplParam('sUrl', $sUrl);
1473  if ($sRet = oxRegistry::get("oxUtilsView")->getTemplateOutput('message/err_404.tpl', $oView)) {
1474  $sReturn = $sRet;
1475  }
1476  } catch (Exception $e) {
1477  }
1478  $this->showMessageAndExit($sReturn);
1479  }
1480 
1488  public function extractDomain($sHost)
1489  {
1490  $oStr = getStr();
1491  if (!$oStr->preg_match('/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $sHost) &&
1492  ($iLastDot = strrpos($sHost, '.')) !== false
1493  ) {
1494  $iLen = $oStr->strlen($sHost);
1495  if (($iNextDot = strrpos($sHost, '.', ($iLen - $iLastDot + 1) * -1)) !== false) {
1496  $sHost = trim($oStr->substr($sHost, $iNextDot), '.');
1497  }
1498  }
1499 
1500  return $sHost;
1501  }
1502 }