OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxfunctions.php
Go to the documentation of this file.
1 <?php
2 
10 function oxAutoload( $sClass )
11 {
12  startProfile("oxAutoload");
13  $sClass = basename( $sClass );
14  $sClass = strtolower($sClass);
15 
16  static $sBasePath = null;
17  static $aClassDirs = null;
18 
19  // preventing infinite loop
20  static $aTriedClasses = array();
21 
22  //loading very base classes. We can do this as we know they exists,
23  //moreover even further method code could not execute without them
24  $sBaseClassLocation = null;
25  $aBaseClasses = array("oxutils", "oxsupercfg", "oxutilsobject");
26  if (in_array($sClass, $aBaseClasses)) {
27  $sFilename = getShopBasePath() ."core/" . $sClass . ".php" ;
28  include $sFilename;
29  return;
30  }
31 
32  static $aClassPaths;
33 
34  if (isset($aClassPaths[$sClass])) {
35  stopProfile("oxAutoload");
36  include $aClassPaths[$sClass];
37  return;
38  }
39 
40  $sBasePath = getShopBasePath();
41 
42 
43  // initializing paths
44  if ( $aClassDirs == null ) {
45  $aClassDirs = getClassDirs ( $sBasePath );
46  }
47 
48  foreach ( $aClassDirs as $sDir ) {
49  $sFilename = $sDir . $sClass . '.php';
50  if ( file_exists( $sFilename ) ) {
51  if (!isset($aClassPaths[$sClass])) {
52  $aClassPaths[$sClass] = $sFilename;
53  //oxRegistry::getUtils()->toPhpFileCache("class_file_paths", $aClassPaths);
54  }
55  stopProfile("oxAutoload");
56  include $sFilename;
57  return;
58  }
59  }
60 
61 
62 
63  // Files registered by modules
64  //$aModuleFiles = oxRegistry::getConfig()->getConfigParam( 'aModuleFiles' );
65  $aModuleFiles = oxUtilsObject::getInstance()->getModuleVar( 'aModuleFiles' );
66  if ( is_array( $aModuleFiles ) ) {
67  $sBasePath = getShopBasePath();
68  $oModulelist = oxNew('oxmodulelist');
69  $aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
70  if (is_array($aActiveModuleInfo)) {
71  foreach ($aModuleFiles as $sModuleId => $aModules) {
72  if (isset($aModules[$sClass]) && isset($aActiveModuleInfo[$sModuleId])) {
73  $sPath = $aModules[$sClass];
74  $sFilename = $sBasePath. 'modules/'. $sPath;
75  if ( file_exists( $sFilename ) ) {
76  if (!isset($aClassPaths[$sClass])) {
77  $aClassPaths[$sClass] = $sFilename;
78  oxRegistry::getUtils()->toPhpFileCache("class_file_paths", $aClassPaths);
79  }
80  stopProfile("oxAutoload");
81  include $sFilename;
82  return;
83  }
84  }
85  }
86  }
87  }
88 
89  // in case module parent class (*_parent) is required
90  $sClass = preg_replace( '/_parent$/i', '', $sClass );
91 
92  // special case
93  if ( !in_array( $sClass, $aTriedClasses ) && is_array( $aModules = oxUtilsObject::getInstance()->getModuleVar( 'aModules' ) ) ) {
94 
95  $myUtilsObject = oxUtilsObject::getInstance();
96  foreach ( $aModules as $sParentName => $sModuleName ) {
97  // looking for module parent class
98  if ( preg_match('/\b'.$sClass.'($|\&)/i', $sModuleName ) ) {
99  $myUtilsObject->getClassName( $sParentName );
100  break;
101  }
102  $aTriedClasses[] = $sClass;
103  }
104  }
105 
106  stopProfile("oxAutoload");
107 }
108 
116 function getClassDirs($sBasePath)
117 {
118  $aClassDirs = array( $sBasePath . 'core/',
119  $sBasePath . 'application/components/widgets/',
120  $sBasePath . 'application/components/',
121  $sBasePath . 'application/models/',
122  $sBasePath . 'application/controllers/',
123  $sBasePath . 'application/controllers/admin/',
124  $sBasePath . 'application/controllers/admin/reports/',
125  $sBasePath . 'views/',
126  $sBasePath . 'core/exception/',
127  $sBasePath . 'core/interface/',
128  $sBasePath . 'core/cache/',
129  $sBasePath . 'core/cache/connectors/',
130  $sBasePath . 'core/wysiwigpro/',
131  $sBasePath . 'admin/reports/',
132  $sBasePath . 'admin/',
133  $sBasePath . 'modules/',
134  $sBasePath
135  );
136  return $aClassDirs;
137 }
138 
139 
140 if ( !function_exists( 'getShopBasePath' ) ) {
146  function getShopBasePath()
147  {
148  return OX_BASE_PATH;
149  }
150 }
151 
157 function isAdmin()
158 {
159  if (defined('OX_IS_ADMIN')) {
160  return OX_IS_ADMIN;
161  }
162 
163  return false;
164 }
165 
171 function setPhpIniParams()
172 {
173  //setting required PHP configuration parameters
174  ini_set('session.name', 'sid');
175  ini_set('session.use_cookies', 0);
176  ini_set('session.use_trans_sid', 0);
177  ini_set('url_rewriter.tags', '');
178  ini_set('magic_quotes_runtime', 0);
179 }
180 
187 {
188  if (!get_magic_quotes_gpc()) {
189  return;
190  }
191  $_REQUEST = _stripMagicQuotes($_REQUEST);
192  $_POST = _stripMagicQuotes($_POST);
193  $_GET = _stripMagicQuotes($_GET);
194  $_COOKIE = _stripMagicQuotes($_COOKIE);
195 }
196 
204 function _stripMagicQuotes($mInput)
205 {
206  return is_array($mInput) ? array_map( '_stripMagicQuotes', $mInput ) : stripslashes( $mInput );
207 }
208 
209 if ( !function_exists( 'error_404_handler' ) ) {
217  function error_404_handler($sUrl = '')
218  {
219  oxRegistry::getUtils()->handlePageNotFoundError($sUrl);
220  }
221 }
222 
234 function warningHandler($iErrorNr, $sErrorText)
235 {
236  echo "<div class='error_box'>".oxRegistry::getLang()->translateString('userError')."<code>[$iErrorNr] $sErrorText</code></div>";
237 }
238 
247 function dumpVar( $mVar, $blToFile = false )
248 {
250  if ( $blToFile ) {
251  $out = var_export( $mVar, true );
252  $f = fopen( $myConfig->getConfigParam( 'sCompileDir' )."/vardump.txt", "a" );
253  fwrite( $f, $out );
254  fclose( $f );
255  } else {
256  echo '<pre>';
257  var_export( $mVar );
258  echo '</pre>';
259  }
260 }
261 
262 if ( !function_exists( 'isSearchEngineUrl' ) ) {
263 
269  function isSearchEngineUrl()
270  {
271  return false;
272  }
273 }
274 
282 function debug( $mVar )
283 {
284  $f = fopen( 'out.txt', 'a' );
285  $sString = var_export( $mVar, true );
286  fputs( $f, $sString."\n---------------------------------------------\n" );
287  fclose( $f );
288 }
289 
298 function cmpart( $a, $b )
299 {
300  // sorting for crossselling
301  if ( $a->cnt == $b->cnt )
302  return 0;
303  return ( $a->cnt < $b->cnt ) ? -1 : 1;
304 }
305 
306 if ( !function_exists( 'startProfile' ) ) {
314  function startProfile( $sProfileName )
315  {
316  global $aStartTimes;
317  global $aExecutionCounts;
318  if (!isset($aExecutionCounts[$sProfileName])) {
319  $aExecutionCounts[$sProfileName] = 0;
320  }
321  if (!isset($aStartTimes[$sProfileName])) {
322  $aStartTimes[$sProfileName] = 0;
323  }
324  $aExecutionCounts[$sProfileName]++;
325  $aStartTimes[$sProfileName] = microtime(true);
326  }
327 }
328 
329 if ( !function_exists( 'stopProfile' ) ) {
337  function stopProfile( $sProfileName )
338  {
339  global $aProfileTimes;
340  global $aStartTimes;
341  if (!isset($aProfileTimes[$sProfileName])) {
342  $aProfileTimes[$sProfileName] = 0;
343  }
344  $aProfileTimes[$sProfileName] += microtime( true ) - $aStartTimes[$sProfileName];
345  }
346 }
347 
358 function oxNew( $sClassName )
359 {
360  startProfile( 'oxNew' );
361  $aArgs = func_get_args();
362  $oRes = call_user_func_array( array( oxUtilsObject::getInstance(), "oxNew" ), $aArgs );
363  stopProfile( 'oxNew' );
364  return $oRes;
365 }
366 
376 function oxNewArticle( $sArtId )
377 {
378  return oxUtilsObject::getInstance()->oxNewArticle( $sArtId );
379 }
380 
388 function getDb($blAssoc = true)
389 {
390  return oxDb::getDb($blAssoc);
391 }
392 
398 function getStr()
399 {
400  return oxStr::getStr();
401 }
402 
412 function ox_get_template( $sTplName, &$sTplSource, $oSmarty )
413 {
414  $sTplSource = $oSmarty->oxidcache->value;
415  if ( oxRegistry::getConfig()->isDemoShop() ) {
416  $oSmarty->security = true;
417  }
418 
419  return true;
420 }
421 
431 function ox_get_timestamp( $sTplName, &$iTplTimestamp, $oSmarty )
432 {
433  if ( isset( $oSmarty->oxidtimecache->value ) ) {
434  // use stored timestamp
435  $iTplTimestamp = $oSmarty->oxidtimecache->value;
436  } else {
437  // always compile
438  $iTplTimestamp = time();
439  }
440 
441  return true;
442 }
443 
452 function ox_get_secure( $sTplName, $oSmarty )
453 {
454  // assume all templates are secure
455  return true;
456 }
457 
466 function ox_get_trusted( $sTplName, $oSmarty )
467 {
468  // not used for templates
469 }
470 
471 
472 if ( !function_exists( 'getLangTableIdx' ) ) {
473 
481  function getLangTableIdx( $iLangId )
482  {
483  $iLangPerTable = oxRegistry::getConfig()->getConfigParam( "iLangPerTable" );
484  //#0002718 min language count per table 2
485  $iLangPerTable = ( $iLangPerTable > 1 ) ? $iLangPerTable : 8;
486 
487  $iTableIdx = (int) ( $iLangId / $iLangPerTable );
488  return $iTableIdx;
489  }
490 }
491 
492 if ( !function_exists( 'getLangTableName' ) ) {
493 
502  function getLangTableName( $sTable, $iLangId )
503  {
504  $iTableIdx = getLangTableIdx( $iLangId );
505  if ( $iTableIdx && in_array($sTable, oxRegistry::getLang()->getMultiLangTables())) {
506  $sLangTableSuffix = oxRegistry::getConfig()->getConfigParam( "sLangTableSuffix" );
507  $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
508 
509  $sTable .= $sLangTableSuffix . $iTableIdx;
510  }
511 
512  return $sTable;
513  }
514 }
515 
516 if ( !function_exists( 'getViewName' ) ) {
517 
527  function getViewName( $sTable, $iLangId = null, $sShopId = null )
528  {
530 
531  //This config option should only be used in emergency case.
532  //Originally it was planned for the case when admin area is not reached due to the broken views.
533  if ( !$myConfig->getConfigParam( 'blSkipViewUsage' ) ) {
534  $sViewSfx = '';
535 
536 
537  $blIsMultiLang = in_array( $sTable, oxRegistry::getLang()->getMultiLangTables() );
538  if ( $iLangId != -1 && $blIsMultiLang ) {
539  $oLang = oxRegistry::getLang();
540  $iLangId = $iLangId !== null ? $iLangId : oxRegistry::getLang()->getBaseLanguage();
541  $sAbbr = $oLang->getLanguageAbbr( $iLangId );
542  $sViewSfx .= "_{$sAbbr}";
543  }
544 
545  if ( $sViewSfx || (($iLangId == -1 || $sShopId == -1 ) && $blIsMultiLang)) {
546  return "oxv_{$sTable}{$sViewSfx}";
547  }
548 
549  }
550 
551  return $sTable;
552  }
553 }
554 
555 if ( !function_exists( 'getRequestUrl' ) ) {
564  function getRequestUrl( $sParams = '', $blReturnUrl = false )
565  {
566  if ($_SERVER["REQUEST_METHOD"] != "POST" ) {
567 
568  if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
569  $sRequest = $_SERVER['REQUEST_URI'];
570  } else {
571  // try something else
572  $sRequest = $_SERVER['SCRIPT_URI'];
573  }
574 
575  // trying to resolve controller file name
576  if ( $sRequest && ( $iPos = stripos( $sRequest, '?' ) ) !== false ) {
577 
578  $oStr = getStr();
579  // formatting request url
580  $sRequest = 'index.php' . $oStr->substr( $sRequest, $iPos );
581 
582  // removing possible session id
583  $sRequest = $oStr->preg_replace( '/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest );
584  $sRequest = $oStr->preg_replace( '/(&|\?)stoken=[^&]*&?/', '$1', $sRequest );
585  $sRequest = $oStr->preg_replace( '/&$/', '', $sRequest );
586  return str_replace( '&', '&amp;', $sRequest );
587  }
588  }
589  }
590 }
591 
592 //registering oxAutoload() as autoload handler
593 spl_autoload_register("oxAutoload");