OXID eShop CE  4.9.9
 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 
30  return;
31  }
32 
33  static $aClassPaths;
34 
35  if (isset($aClassPaths[$sClass])) {
36  stopProfile("oxAutoload");
37  include $aClassPaths[$sClass];
38 
39  return;
40  }
41 
42  $sBasePath = getShopBasePath();
43 
44 
45  // initializing paths
46  if ($aClassDirs == null) {
47  $aClassDirs = getClassDirs($sBasePath);
48  }
49 
50  foreach ($aClassDirs as $sDir) {
51  $sFilename = $sDir . $sClass . '.php';
52  if (file_exists($sFilename)) {
53  if (!isset($aClassPaths[$sClass])) {
54  $aClassPaths[$sClass] = $sFilename;
55  }
56  stopProfile("oxAutoload");
57  include $sFilename;
58 
59  return;
60  }
61  }
62 
63 
64  // Files registered by modules
65  //$aModuleFiles = oxRegistry::getConfig()->getConfigParam( 'aModuleFiles' );
66  $aModuleFiles = oxUtilsObject::getInstance()->getModuleVar('aModuleFiles');
67  if (is_array($aModuleFiles)) {
68  $sBasePath = getShopBasePath();
69  $oModulelist = oxNew('oxmodulelist');
70  $aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
71  if (is_array($aActiveModuleInfo)) {
72  foreach ($aModuleFiles as $sModuleId => $aModules) {
73  if (isset($aModules[$sClass]) && isset($aActiveModuleInfo[$sModuleId])) {
74  $sPath = $aModules[$sClass];
75  $sFilename = $sBasePath . 'modules/' . $sPath;
76  if (file_exists($sFilename)) {
77  if (!isset($aClassPaths[$sClass])) {
78  $aClassPaths[$sClass] = $sFilename;
79  }
80  stopProfile("oxAutoload");
81  include $sFilename;
82 
83  return;
84  }
85  }
86  }
87  }
88  }
89 
90  // in case module parent class (*_parent) is required
91  $sClass = preg_replace('/_parent$/i', '', $sClass);
92 
93  // special case
94  if (!in_array($sClass, $aTriedClasses) && is_array($aModules = oxUtilsObject::getInstance()->getModuleVar('aModules'))) {
95 
96  $myUtilsObject = oxUtilsObject::getInstance();
97  foreach ($aModules as $sParentName => $sModuleName) {
98  // looking for module parent class
99  if (preg_match('/\b' . $sClass . '($|\&)/i', $sModuleName)) {
100  $myUtilsObject->getClassName($sParentName);
101  break;
102  }
103  $aTriedClasses[] = $sClass;
104  }
105  }
106 
107  stopProfile("oxAutoload");
108 }
109 
117 function getClassDirs($sBasePath)
118 {
119  $aClassDirs = array($sBasePath . 'core/',
120  $sBasePath . 'application/components/widgets/',
121  $sBasePath . 'application/components/services/',
122  $sBasePath . 'application/components/',
123  $sBasePath . 'application/models/',
124  $sBasePath . 'application/controllers/',
125  $sBasePath . 'application/controllers/admin/',
126  $sBasePath . 'application/controllers/admin/reports/',
127  $sBasePath . 'views/',
128  $sBasePath . 'core/exception/',
129  $sBasePath . 'core/interface/',
130  $sBasePath . 'core/cache/',
131  $sBasePath . 'core/cache/connectors/',
132  $sBasePath . 'core/wysiwigpro/',
133  $sBasePath . 'admin/reports/',
134  $sBasePath . 'admin/',
135  $sBasePath . 'modules/',
136  $sBasePath
137  );
138 
139  return $aClassDirs;
140 }
141 
142 
143 if (!function_exists('registerShopAutoLoad')) {
147  function registerShopAutoLoad()
148  {
149  spl_autoload_register("oxAutoload");
150  }
151 }
152 
153 if (!function_exists('registerComposerAutoLoad')) {
157  function registerComposerAutoLoad()
158  {
159  $autoloaderPath = __DIR__ . '/../modules/vendor/autoload.php';
160  if (file_exists($autoloaderPath)) {
161  include_once $autoloaderPath;
162  }
163  }
164 }
165 
166 if (!function_exists('getShopBasePath')) {
172  function getShopBasePath()
173  {
174  return OX_BASE_PATH;
175  }
176 }
177 
183 function isAdmin()
184 {
185  if (defined('OX_IS_ADMIN')) {
186  return OX_IS_ADMIN;
187  }
188 
189  return false;
190 }
191 
197 function setPhpIniParams()
198 {
199  //setting required PHP configuration parameters
200  ini_set('session.name', 'sid');
201  ini_set('session.use_cookies', 0);
202  ini_set('session.use_trans_sid', 0);
203  ini_set('url_rewriter.tags', '');
204 }
205 
206 if (!function_exists('error_404_handler')) {
214  function error_404_handler($sUrl = '')
215  {
216  oxRegistry::getUtils()->handlePageNotFoundError($sUrl);
217  }
218 }
219 
229 function warningHandler($iErrorNr, $sErrorText)
230 {
231  echo "<div class='error_box'>" . oxRegistry::getLang()->translateString('userError') . "<code>[$iErrorNr] $sErrorText</code></div>";
232 }
233 
240 function dumpVar($mVar, $blToFile = false)
241 {
243  if ($blToFile) {
244  $out = var_export($mVar, true);
245  $f = fopen($myConfig->getConfigParam('sCompileDir') . "/vardump.txt", "a");
246  fwrite($f, $out);
247  fclose($f);
248  } else {
249  echo '<pre>';
250  var_export($mVar);
251  echo '</pre>';
252  }
253 }
254 
255 if (!function_exists('isSearchEngineUrl')) {
256 
262  function isSearchEngineUrl()
263  {
264  return false;
265  }
266 }
267 
273 function debug($mVar)
274 {
275  $f = fopen('out.txt', 'a');
276  $sString = var_export($mVar, true);
277  fputs($f, $sString . "\n---------------------------------------------\n");
278  fclose($f);
279 }
280 
289 function cmpart($a, $b)
290 {
291  // sorting for crossselling
292  if ($a->cnt == $b->cnt) {
293  return 0;
294  }
295 
296  return ($a->cnt < $b->cnt) ? -1 : 1;
297 }
298 
299 if (!function_exists('startProfile')) {
305  function startProfile($sProfileName)
306  {
307  global $aStartTimes;
308  global $aExecutionCounts;
309  if (!isset($aExecutionCounts[$sProfileName])) {
310  $aExecutionCounts[$sProfileName] = 0;
311  }
312  if (!isset($aStartTimes[$sProfileName])) {
313  $aStartTimes[$sProfileName] = 0;
314  }
315  $aExecutionCounts[$sProfileName]++;
316  $aStartTimes[$sProfileName] = microtime(true);
317  }
318 }
319 
320 if (!function_exists('stopProfile')) {
326  function stopProfile($sProfileName)
327  {
328  global $aProfileTimes;
329  global $aStartTimes;
330  if (!isset($aProfileTimes[$sProfileName])) {
331  $aProfileTimes[$sProfileName] = 0;
332  }
333  $aProfileTimes[$sProfileName] += microtime(true) - $aStartTimes[$sProfileName];
334  }
335 }
336 
347 function oxNew($sClassName)
348 {
349  startProfile('oxNew');
350  $aArgs = func_get_args();
351  $oRes = call_user_func_array(array(oxUtilsObject::getInstance(), "oxNew"), $aArgs);
352  stopProfile('oxNew');
353 
354  return $oRes;
355 }
356 
364 function getDb($blAssoc = true)
365 {
366  return oxDb::getDb($blAssoc);
367 }
368 
374 function getStr()
375 {
376  return oxStr::getStr();
377 }
378 
388 function ox_get_template($sTplName, &$sTplSource, $oSmarty)
389 {
390  $sTplSource = $oSmarty->oxidcache->value;
391  if (oxRegistry::getConfig()->isDemoShop()) {
392  $oSmarty->security = true;
393  }
394 
395  return true;
396 }
397 
407 function ox_get_timestamp($sTplName, &$iTplTimestamp, $oSmarty)
408 {
409  if (isset($oSmarty->oxidtimecache->value)) {
410  // use stored timestamp
411  $iTplTimestamp = $oSmarty->oxidtimecache->value;
412  } else {
413  // always compile
414  $iTplTimestamp = time();
415  }
416 
417  return true;
418 }
419 
428 function ox_get_secure($sTplName, $oSmarty)
429 {
430  // assume all templates are secure
431  return true;
432 }
433 
440 function ox_get_trusted($sTplName, $oSmarty)
441 {
442  // not used for templates
443 }
444 
445 
446 if (!function_exists('getLangTableIdx')) {
447 
455  function getLangTableIdx($iLangId)
456  {
457  $iLangPerTable = oxRegistry::getConfig()->getConfigParam("iLangPerTable");
458  //#0002718 min language count per table 2
459  $iLangPerTable = ($iLangPerTable > 1) ? $iLangPerTable : 8;
460 
461  $iTableIdx = (int) ($iLangId / $iLangPerTable);
462 
463  return $iTableIdx;
464  }
465 }
466 
467 if (!function_exists('getLangTableName')) {
468 
477  function getLangTableName($sTable, $iLangId)
478  {
479  $iTableIdx = getLangTableIdx($iLangId);
480  if ($iTableIdx && in_array($sTable, oxRegistry::getLang()->getMultiLangTables())) {
481  $sLangTableSuffix = oxRegistry::getConfig()->getConfigParam("sLangTableSuffix");
482  $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
483 
484  $sTable .= $sLangTableSuffix . $iTableIdx;
485  }
486 
487  return $sTable;
488  }
489 }
490 
491 if (!function_exists('getViewName')) {
492 
502  function getViewName($sTable, $iLangId = null, $sShopId = null)
503  {
505 
506  //This config option should only be used in emergency case.
507  //Originally it was planned for the case when admin area is not reached due to the broken views.
508  if (!$myConfig->getConfigParam('blSkipViewUsage')) {
509  $sViewSfx = '';
510 
511 
512  $blIsMultiLang = in_array($sTable, oxRegistry::getLang()->getMultiLangTables());
513  if ($iLangId != -1 && $blIsMultiLang) {
514  $oLang = oxRegistry::getLang();
515  $iLangId = $iLangId !== null ? $iLangId : oxRegistry::getLang()->getBaseLanguage();
516  $sAbbr = $oLang->getLanguageAbbr($iLangId);
517  $sViewSfx .= "_{$sAbbr}";
518  }
519 
520  if ($sViewSfx || (($iLangId == -1 || $sShopId == -1) && $blIsMultiLang)) {
521  return "oxv_{$sTable}{$sViewSfx}";
522  }
523 
524  }
525 
526  return $sTable;
527  }
528 }
529 
530 if (!function_exists('getRequestUrl')) {
539  function getRequestUrl($sParams = '', $blReturnUrl = false)
540  {
541  if ($_SERVER["REQUEST_METHOD"] != "POST") {
542 
543  if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']) {
544  $sRequest = $_SERVER['REQUEST_URI'];
545  } else {
546  // try something else
547  $sRequest = $_SERVER['SCRIPT_URI'];
548  }
549 
550  // trying to resolve controller file name
551  if ($sRequest && ($iPos = stripos($sRequest, '?')) !== false) {
552 
553  $oStr = getStr();
554  // formatting request url
555  $sRequest = 'index.php' . $oStr->substr($sRequest, $iPos);
556 
557  // removing possible session id
558  $sRequest = $oStr->preg_replace('/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest);
559  $sRequest = $oStr->preg_replace('/(&|\?)stoken=[^&]*&?/', '$1', $sRequest);
560  $sRequest = $oStr->preg_replace('/&$/', '', $sRequest);
561 
562  return str_replace('&', '&amp;', $sRequest);
563  }
564  }
565  }
566 }
567 
568 registerComposerAutoLoad();
569 registerShopAutoLoad();