OXID eShop CE  4.9.5
 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('getShopBasePath')) {
149  function getShopBasePath()
150  {
151  return OX_BASE_PATH;
152  }
153 }
154 
160 function isAdmin()
161 {
162  if (defined('OX_IS_ADMIN')) {
163  return OX_IS_ADMIN;
164  }
165 
166  return false;
167 }
168 
174 function setPhpIniParams()
175 {
176  //setting required PHP configuration parameters
177  ini_set('session.name', 'sid');
178  ini_set('session.use_cookies', 0);
179  ini_set('session.use_trans_sid', 0);
180  ini_set('url_rewriter.tags', '');
181 }
182 
183 if (!function_exists('error_404_handler')) {
191  function error_404_handler($sUrl = '')
192  {
193  oxRegistry::getUtils()->handlePageNotFoundError($sUrl);
194  }
195 }
196 
206 function warningHandler($iErrorNr, $sErrorText)
207 {
208  echo "<div class='error_box'>" . oxRegistry::getLang()->translateString('userError') . "<code>[$iErrorNr] $sErrorText</code></div>";
209 }
210 
217 function dumpVar($mVar, $blToFile = false)
218 {
220  if ($blToFile) {
221  $out = var_export($mVar, true);
222  $f = fopen($myConfig->getConfigParam('sCompileDir') . "/vardump.txt", "a");
223  fwrite($f, $out);
224  fclose($f);
225  } else {
226  echo '<pre>';
227  var_export($mVar);
228  echo '</pre>';
229  }
230 }
231 
232 if (!function_exists('isSearchEngineUrl')) {
233 
239  function isSearchEngineUrl()
240  {
241  return false;
242  }
243 }
244 
250 function debug($mVar)
251 {
252  $f = fopen('out.txt', 'a');
253  $sString = var_export($mVar, true);
254  fputs($f, $sString . "\n---------------------------------------------\n");
255  fclose($f);
256 }
257 
266 function cmpart($a, $b)
267 {
268  // sorting for crossselling
269  if ($a->cnt == $b->cnt) {
270  return 0;
271  }
272 
273  return ($a->cnt < $b->cnt) ? -1 : 1;
274 }
275 
276 if (!function_exists('startProfile')) {
282  function startProfile($sProfileName)
283  {
284  global $aStartTimes;
285  global $aExecutionCounts;
286  if (!isset($aExecutionCounts[$sProfileName])) {
287  $aExecutionCounts[$sProfileName] = 0;
288  }
289  if (!isset($aStartTimes[$sProfileName])) {
290  $aStartTimes[$sProfileName] = 0;
291  }
292  $aExecutionCounts[$sProfileName]++;
293  $aStartTimes[$sProfileName] = microtime(true);
294  }
295 }
296 
297 if (!function_exists('stopProfile')) {
303  function stopProfile($sProfileName)
304  {
305  global $aProfileTimes;
306  global $aStartTimes;
307  if (!isset($aProfileTimes[$sProfileName])) {
308  $aProfileTimes[$sProfileName] = 0;
309  }
310  $aProfileTimes[$sProfileName] += microtime(true) - $aStartTimes[$sProfileName];
311  }
312 }
313 
324 function oxNew($sClassName)
325 {
326  startProfile('oxNew');
327  $aArgs = func_get_args();
328  $oRes = call_user_func_array(array(oxUtilsObject::getInstance(), "oxNew"), $aArgs);
329  stopProfile('oxNew');
330 
331  return $oRes;
332 }
333 
341 function getDb($blAssoc = true)
342 {
343  return oxDb::getDb($blAssoc);
344 }
345 
351 function getStr()
352 {
353  return oxStr::getStr();
354 }
355 
365 function ox_get_template($sTplName, &$sTplSource, $oSmarty)
366 {
367  $sTplSource = $oSmarty->oxidcache->value;
368  if (oxRegistry::getConfig()->isDemoShop()) {
369  $oSmarty->security = true;
370  }
371 
372  return true;
373 }
374 
384 function ox_get_timestamp($sTplName, &$iTplTimestamp, $oSmarty)
385 {
386  if (isset($oSmarty->oxidtimecache->value)) {
387  // use stored timestamp
388  $iTplTimestamp = $oSmarty->oxidtimecache->value;
389  } else {
390  // always compile
391  $iTplTimestamp = time();
392  }
393 
394  return true;
395 }
396 
405 function ox_get_secure($sTplName, $oSmarty)
406 {
407  // assume all templates are secure
408  return true;
409 }
410 
417 function ox_get_trusted($sTplName, $oSmarty)
418 {
419  // not used for templates
420 }
421 
422 
423 if (!function_exists('getLangTableIdx')) {
424 
432  function getLangTableIdx($iLangId)
433  {
434  $iLangPerTable = oxRegistry::getConfig()->getConfigParam("iLangPerTable");
435  //#0002718 min language count per table 2
436  $iLangPerTable = ($iLangPerTable > 1) ? $iLangPerTable : 8;
437 
438  $iTableIdx = (int) ($iLangId / $iLangPerTable);
439 
440  return $iTableIdx;
441  }
442 }
443 
444 if (!function_exists('getLangTableName')) {
445 
454  function getLangTableName($sTable, $iLangId)
455  {
456  $iTableIdx = getLangTableIdx($iLangId);
457  if ($iTableIdx && in_array($sTable, oxRegistry::getLang()->getMultiLangTables())) {
458  $sLangTableSuffix = oxRegistry::getConfig()->getConfigParam("sLangTableSuffix");
459  $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
460 
461  $sTable .= $sLangTableSuffix . $iTableIdx;
462  }
463 
464  return $sTable;
465  }
466 }
467 
468 if (!function_exists('getViewName')) {
469 
479  function getViewName($sTable, $iLangId = null, $sShopId = null)
480  {
482 
483  //This config option should only be used in emergency case.
484  //Originally it was planned for the case when admin area is not reached due to the broken views.
485  if (!$myConfig->getConfigParam('blSkipViewUsage')) {
486  $sViewSfx = '';
487 
488 
489  $blIsMultiLang = in_array($sTable, oxRegistry::getLang()->getMultiLangTables());
490  if ($iLangId != -1 && $blIsMultiLang) {
491  $oLang = oxRegistry::getLang();
492  $iLangId = $iLangId !== null ? $iLangId : oxRegistry::getLang()->getBaseLanguage();
493  $sAbbr = $oLang->getLanguageAbbr($iLangId);
494  $sViewSfx .= "_{$sAbbr}";
495  }
496 
497  if ($sViewSfx || (($iLangId == -1 || $sShopId == -1) && $blIsMultiLang)) {
498  return "oxv_{$sTable}{$sViewSfx}";
499  }
500 
501  }
502 
503  return $sTable;
504  }
505 }
506 
507 if (!function_exists('getRequestUrl')) {
516  function getRequestUrl($sParams = '', $blReturnUrl = false)
517  {
518  if ($_SERVER["REQUEST_METHOD"] != "POST") {
519 
520  if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']) {
521  $sRequest = $_SERVER['REQUEST_URI'];
522  } else {
523  // try something else
524  $sRequest = $_SERVER['SCRIPT_URI'];
525  }
526 
527  // trying to resolve controller file name
528  if ($sRequest && ($iPos = stripos($sRequest, '?')) !== false) {
529 
530  $oStr = getStr();
531  // formatting request url
532  $sRequest = 'index.php' . $oStr->substr($sRequest, $iPos);
533 
534  // removing possible session id
535  $sRequest = $oStr->preg_replace('/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest);
536  $sRequest = $oStr->preg_replace('/(&|\?)stoken=[^&]*&?/', '$1', $sRequest);
537  $sRequest = $oStr->preg_replace('/&$/', '', $sRequest);
538 
539  return str_replace('&', '&amp;', $sRequest);
540  }
541  }
542  }
543 }
544 
545 //registering oxAutoload() as autoload handler
546 spl_autoload_register("oxAutoload");