OXID eShop CE  4.10.6
 All Classes Namespaces 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 . 'core/exception/',
128  $sBasePath . 'core/interface/',
129  $sBasePath . 'core/cache/',
130  $sBasePath . 'core/cache/connectors/',
131  $sBasePath . 'core/wysiwigpro/',
132  $sBasePath . 'admin/reports/',
133  $sBasePath
134  );
135 
136  return $aClassDirs;
137 }
138 
139 
140 if (!function_exists('registerShopAutoLoad')) {
144  function registerShopAutoLoad()
145  {
146  spl_autoload_register("oxAutoload");
147  }
148 }
149 
150 if (!function_exists('registerComposerAutoLoad')) {
154  function registerComposerAutoLoad()
155  {
156  $autoloaderPath = __DIR__ . '/../modules/vendor/autoload.php';
157  if (file_exists($autoloaderPath)) {
158  include_once $autoloaderPath;
159  }
160  }
161 }
162 
163 if (!function_exists('getShopBasePath')) {
169  function getShopBasePath()
170  {
171  return OX_BASE_PATH;
172  }
173 }
174 
180 function isAdmin()
181 {
182  if (defined('OX_IS_ADMIN')) {
183  return OX_IS_ADMIN;
184  }
185 
186  return false;
187 }
188 
194 function setPhpIniParams()
195 {
196  //setting required PHP configuration parameters
197  ini_set('session.name', 'sid');
198  ini_set('session.use_cookies', 0);
199  ini_set('session.use_trans_sid', 0);
200  ini_set('url_rewriter.tags', '');
201 }
202 
203 if (!function_exists('error_404_handler')) {
211  function error_404_handler($sUrl = '')
212  {
213  oxRegistry::getUtils()->handlePageNotFoundError($sUrl);
214  }
215 }
216 
226 function warningHandler($iErrorNr, $sErrorText)
227 {
228  echo "<div class='error_box'>" . oxRegistry::getLang()->translateString('userError') . "<code>[$iErrorNr] $sErrorText</code></div>";
229 }
230 
237 function dumpVar($mVar, $blToFile = false)
238 {
240  if ($blToFile) {
241  $out = var_export($mVar, true);
242  $f = fopen($myConfig->getConfigParam('sCompileDir') . "/vardump.txt", "a");
243  fwrite($f, $out);
244  fclose($f);
245  } else {
246  echo '<pre>';
247  var_export($mVar);
248  echo '</pre>';
249  }
250 }
251 
252 if (!function_exists('isSearchEngineUrl')) {
253 
259  function isSearchEngineUrl()
260  {
261  return false;
262  }
263 }
264 
270 function debug($mVar)
271 {
272  $f = fopen('out.txt', 'a');
273  $sString = var_export($mVar, true);
274  fputs($f, $sString . "\n---------------------------------------------\n");
275  fclose($f);
276 }
277 
286 function cmpart($a, $b)
287 {
288  // sorting for crossselling
289  if ($a->cnt == $b->cnt) {
290  return 0;
291  }
292 
293  return ($a->cnt < $b->cnt) ? -1 : 1;
294 }
295 
296 if (!function_exists('startProfile')) {
302  function startProfile($sProfileName)
303  {
304  global $aStartTimes;
305  global $aExecutionCounts;
306  if (!isset($aExecutionCounts[$sProfileName])) {
307  $aExecutionCounts[$sProfileName] = 0;
308  }
309  if (!isset($aStartTimes[$sProfileName])) {
310  $aStartTimes[$sProfileName] = 0;
311  }
312  $aExecutionCounts[$sProfileName]++;
313  $aStartTimes[$sProfileName] = microtime(true);
314  }
315 }
316 
317 if (!function_exists('stopProfile')) {
323  function stopProfile($sProfileName)
324  {
325  global $aProfileTimes;
326  global $aStartTimes;
327  if (!isset($aProfileTimes[$sProfileName])) {
328  $aProfileTimes[$sProfileName] = 0;
329  }
330  $aProfileTimes[$sProfileName] += microtime(true) - $aStartTimes[$sProfileName];
331  }
332 }
333 
344 function oxNew($sClassName)
345 {
346  startProfile('oxNew');
347  $aArgs = func_get_args();
348  $oRes = call_user_func_array(array(oxUtilsObject::getInstance(), "oxNew"), $aArgs);
349  stopProfile('oxNew');
350 
351  return $oRes;
352 }
353 
361 function getDb($blAssoc = true)
362 {
363  return oxDb::getDb($blAssoc);
364 }
365 
371 function getStr()
372 {
373  return oxStr::getStr();
374 }
375 
385 function ox_get_template($sTplName, &$sTplSource, $oSmarty)
386 {
387  $sTplSource = $oSmarty->oxidcache->value;
388  if (oxRegistry::getConfig()->isDemoShop()) {
389  $oSmarty->security = true;
390  }
391 
392  return true;
393 }
394 
404 function ox_get_timestamp($sTplName, &$iTplTimestamp, $oSmarty)
405 {
406  if (isset($oSmarty->oxidtimecache->value)) {
407  // use stored timestamp
408  $iTplTimestamp = $oSmarty->oxidtimecache->value;
409  } else {
410  // always compile
411  $iTplTimestamp = time();
412  }
413 
414  return true;
415 }
416 
425 function ox_get_secure($sTplName, $oSmarty)
426 {
427  // assume all templates are secure
428  return true;
429 }
430 
437 function ox_get_trusted($sTplName, $oSmarty)
438 {
439  // not used for templates
440 }
441 
442 
443 if (!function_exists('getLangTableIdx')) {
444 
452  function getLangTableIdx($iLangId)
453  {
454  $iLangPerTable = oxRegistry::getConfig()->getConfigParam("iLangPerTable");
455  //#0002718 min language count per table 2
456  $iLangPerTable = ($iLangPerTable > 1) ? $iLangPerTable : 8;
457 
458  $iTableIdx = (int) ($iLangId / $iLangPerTable);
459 
460  return $iTableIdx;
461  }
462 }
463 
464 if (!function_exists('getLangTableName')) {
465 
474  function getLangTableName($sTable, $iLangId)
475  {
476  $iTableIdx = getLangTableIdx($iLangId);
477  if ($iTableIdx && in_array($sTable, oxRegistry::getLang()->getMultiLangTables())) {
478  $sLangTableSuffix = oxRegistry::getConfig()->getConfigParam("sLangTableSuffix");
479  $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
480 
481  $sTable .= $sLangTableSuffix . $iTableIdx;
482  }
483 
484  return $sTable;
485  }
486 }
487 
488 if (!function_exists('getViewName')) {
489 
499  function getViewName($sTable, $iLangId = null, $sShopId = null)
500  {
502 
503  //This config option should only be used in emergency case.
504  //Originally it was planned for the case when admin area is not reached due to the broken views.
505  if (!$myConfig->getConfigParam('blSkipViewUsage')) {
506  $sViewSfx = '';
507 
508 
509  $blIsMultiLang = in_array($sTable, oxRegistry::getLang()->getMultiLangTables());
510  if ($iLangId != -1 && $blIsMultiLang) {
511  $oLang = oxRegistry::getLang();
512  $iLangId = $iLangId !== null ? $iLangId : oxRegistry::getLang()->getBaseLanguage();
513  $sAbbr = $oLang->getLanguageAbbr($iLangId);
514  $sViewSfx .= "_{$sAbbr}";
515  }
516 
517  if ($sViewSfx || (($iLangId == -1 || $sShopId == -1) && $blIsMultiLang)) {
518  return "oxv_{$sTable}{$sViewSfx}";
519  }
520 
521  }
522 
523  return $sTable;
524  }
525 }
526 
527 if (!function_exists('getRequestUrl')) {
536  function getRequestUrl($sParams = '', $blReturnUrl = false)
537  {
538  if ($_SERVER["REQUEST_METHOD"] != "POST") {
539 
540  if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']) {
541  $sRequest = $_SERVER['REQUEST_URI'];
542  } else {
543  // try something else
544  $sRequest = $_SERVER['SCRIPT_URI'];
545  }
546 
547  // trying to resolve controller file name
548  if ($sRequest && ($iPos = stripos($sRequest, '?')) !== false) {
549 
550  $oStr = getStr();
551  // formatting request url
552  $sRequest = 'index.php' . $oStr->substr($sRequest, $iPos);
553 
554  // removing possible session id
555  $sRequest = $oStr->preg_replace('/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest);
556  $sRequest = $oStr->preg_replace('/(&|\?)stoken=[^&]*&?/', '$1', $sRequest);
557  $sRequest = $oStr->preg_replace('/&$/', '', $sRequest);
558 
559  return str_replace('&', '&amp;', $sRequest);
560  }
561  }
562  }
563 }
564 
565 registerComposerAutoLoad();
566 registerShopAutoLoad();