oxfunctions.php

Go to the documentation of this file.
00001 <?php
00002 
00010 function oxAutoload($sClass)
00011 {
00012     startProfile("oxAutoload");
00013     $sClass = basename($sClass);
00014     $sClass = strtolower($sClass);
00015 
00016     static $sBasePath = null;
00017     static $aClassDirs = null;
00018 
00019     // preventing infinite loop
00020     static $aTriedClasses = array();
00021 
00022     //loading very base classes. We can do this as we know they exists,
00023     //moreover even further method code could not execute without them
00024     $sBaseClassLocation = null;
00025     $aBaseClasses = array("oxutils", "oxsupercfg", "oxutilsobject");
00026     if (in_array($sClass, $aBaseClasses)) {
00027         $sFilename = getShopBasePath() . "core/" . $sClass . ".php";
00028         include $sFilename;
00029 
00030         return;
00031     }
00032 
00033     static $aClassPaths;
00034 
00035     if (isset($aClassPaths[$sClass])) {
00036         stopProfile("oxAutoload");
00037         include $aClassPaths[$sClass];
00038 
00039         return;
00040     }
00041 
00042     $sBasePath = getShopBasePath();
00043 
00044 
00045     // initializing paths
00046     if ($aClassDirs == null) {
00047         $aClassDirs = getClassDirs($sBasePath);
00048     }
00049 
00050     foreach ($aClassDirs as $sDir) {
00051         $sFilename = $sDir . $sClass . '.php';
00052         if (file_exists($sFilename)) {
00053             if (!isset($aClassPaths[$sClass])) {
00054                 $aClassPaths[$sClass] = $sFilename;
00055             }
00056             stopProfile("oxAutoload");
00057             include $sFilename;
00058 
00059             return;
00060         }
00061     }
00062 
00063 
00064     // Files registered by modules
00065     //$aModuleFiles = oxRegistry::getConfig()->getConfigParam( 'aModuleFiles' );
00066     $aModuleFiles = oxUtilsObject::getInstance()->getModuleVar('aModuleFiles');
00067     if (is_array($aModuleFiles)) {
00068         $sBasePath = getShopBasePath();
00069         $oModulelist = oxNew('oxmodulelist');
00070         $aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
00071         if (is_array($aActiveModuleInfo)) {
00072             foreach ($aModuleFiles as $sModuleId => $aModules) {
00073                 if (isset($aModules[$sClass]) && isset($aActiveModuleInfo[$sModuleId])) {
00074                     $sPath = $aModules[$sClass];
00075                     $sFilename = $sBasePath . 'modules/' . $sPath;
00076                     if (file_exists($sFilename)) {
00077                         if (!isset($aClassPaths[$sClass])) {
00078                             $aClassPaths[$sClass] = $sFilename;
00079                         }
00080                         stopProfile("oxAutoload");
00081                         include $sFilename;
00082 
00083                         return;
00084                     }
00085                 }
00086             }
00087         }
00088     }
00089 
00090     // in case module parent class (*_parent) is required
00091     $sClass = preg_replace('/_parent$/i', '', $sClass);
00092 
00093     // special case
00094     if (!in_array($sClass, $aTriedClasses) && is_array($aModules = oxUtilsObject::getInstance()->getModuleVar('aModules'))) {
00095 
00096         $myUtilsObject = oxUtilsObject::getInstance();
00097         foreach ($aModules as $sParentName => $sModuleName) {
00098             // looking for module parent class
00099             if (preg_match('/\b' . $sClass . '($|\&)/i', $sModuleName)) {
00100                 $myUtilsObject->getClassName($sParentName);
00101                 break;
00102             }
00103             $aTriedClasses[] = $sClass;
00104         }
00105     }
00106 
00107     stopProfile("oxAutoload");
00108 }
00109 
00117 function getClassDirs($sBasePath)
00118 {
00119     $aClassDirs = array($sBasePath . 'core/',
00120                         $sBasePath . 'application/components/widgets/',
00121                         $sBasePath . 'application/components/services/',
00122                         $sBasePath . 'application/components/',
00123                         $sBasePath . 'application/models/',
00124                         $sBasePath . 'application/controllers/',
00125                         $sBasePath . 'application/controllers/admin/',
00126                         $sBasePath . 'application/controllers/admin/reports/',
00127                         $sBasePath . 'views/',
00128                         $sBasePath . 'core/exception/',
00129                         $sBasePath . 'core/interface/',
00130                         $sBasePath . 'core/cache/',
00131                         $sBasePath . 'core/cache/connectors/',
00132                         $sBasePath . 'core/wysiwigpro/',
00133                         $sBasePath . 'admin/reports/',
00134                         $sBasePath . 'admin/',
00135                         $sBasePath . 'modules/',
00136                         $sBasePath
00137     );
00138 
00139     return $aClassDirs;
00140 }
00141 
00142 
00143 if (!function_exists('getShopBasePath')) {
00149     function getShopBasePath()
00150     {
00151         return OX_BASE_PATH;
00152     }
00153 }
00154 
00160 function isAdmin()
00161 {
00162     if (defined('OX_IS_ADMIN')) {
00163         return OX_IS_ADMIN;
00164     }
00165 
00166     return false;
00167 }
00168 
00174 function setPhpIniParams()
00175 {
00176     //setting required PHP configuration parameters
00177     ini_set('session.name', 'sid');
00178     ini_set('session.use_cookies', 0);
00179     ini_set('session.use_trans_sid', 0);
00180     ini_set('url_rewriter.tags', '');
00181 }
00182 
00183 if (!function_exists('error_404_handler')) {
00191     function error_404_handler($sUrl = '')
00192     {
00193         oxRegistry::getUtils()->handlePageNotFoundError($sUrl);
00194     }
00195 }
00196 
00206 function warningHandler($iErrorNr, $sErrorText)
00207 {
00208     echo "<div class='error_box'>" . oxRegistry::getLang()->translateString('userError') . "<code>[$iErrorNr] $sErrorText</code></div>";
00209 }
00210 
00217 function dumpVar($mVar, $blToFile = false)
00218 {
00219     $myConfig = oxRegistry::getConfig();
00220     if ($blToFile) {
00221         $out = var_export($mVar, true);
00222         $f = fopen($myConfig->getConfigParam('sCompileDir') . "/vardump.txt", "a");
00223         fwrite($f, $out);
00224         fclose($f);
00225     } else {
00226         echo '<pre>';
00227         var_export($mVar);
00228         echo '</pre>';
00229     }
00230 }
00231 
00232 if (!function_exists('isSearchEngineUrl')) {
00233 
00239     function isSearchEngineUrl()
00240     {
00241         return false;
00242     }
00243 }
00244 
00250 function debug($mVar)
00251 {
00252     $f = fopen('out.txt', 'a');
00253     $sString = var_export($mVar, true);
00254     fputs($f, $sString . "\n---------------------------------------------\n");
00255     fclose($f);
00256 }
00257 
00266 function cmpart($a, $b)
00267 {
00268     // sorting for crossselling
00269     if ($a->cnt == $b->cnt) {
00270         return 0;
00271     }
00272 
00273     return ($a->cnt < $b->cnt) ? -1 : 1;
00274 }
00275 
00276 if (!function_exists('startProfile')) {
00282     function startProfile($sProfileName)
00283     {
00284         global $aStartTimes;
00285         global $aExecutionCounts;
00286         if (!isset($aExecutionCounts[$sProfileName])) {
00287             $aExecutionCounts[$sProfileName] = 0;
00288         }
00289         if (!isset($aStartTimes[$sProfileName])) {
00290             $aStartTimes[$sProfileName] = 0;
00291         }
00292         $aExecutionCounts[$sProfileName]++;
00293         $aStartTimes[$sProfileName] = microtime(true);
00294     }
00295 }
00296 
00297 if (!function_exists('stopProfile')) {
00303     function stopProfile($sProfileName)
00304     {
00305         global $aProfileTimes;
00306         global $aStartTimes;
00307         if (!isset($aProfileTimes[$sProfileName])) {
00308             $aProfileTimes[$sProfileName] = 0;
00309         }
00310         $aProfileTimes[$sProfileName] += microtime(true) - $aStartTimes[$sProfileName];
00311     }
00312 }
00313 
00324 function oxNew($sClassName)
00325 {
00326     startProfile('oxNew');
00327     $aArgs = func_get_args();
00328     $oRes = call_user_func_array(array(oxUtilsObject::getInstance(), "oxNew"), $aArgs);
00329     stopProfile('oxNew');
00330 
00331     return $oRes;
00332 }
00333 
00341 function getDb($blAssoc = true)
00342 {
00343     return oxDb::getDb($blAssoc);
00344 }
00345 
00351 function getStr()
00352 {
00353     return oxStr::getStr();
00354 }
00355 
00365 function ox_get_template($sTplName, &$sTplSource, $oSmarty)
00366 {
00367     $sTplSource = $oSmarty->oxidcache->value;
00368     if (oxRegistry::getConfig()->isDemoShop()) {
00369         $oSmarty->security = true;
00370     }
00371 
00372     return true;
00373 }
00374 
00384 function ox_get_timestamp($sTplName, &$iTplTimestamp, $oSmarty)
00385 {
00386     if (isset($oSmarty->oxidtimecache->value)) {
00387         // use stored timestamp
00388         $iTplTimestamp = $oSmarty->oxidtimecache->value;
00389     } else {
00390         // always compile
00391         $iTplTimestamp = time();
00392     }
00393 
00394     return true;
00395 }
00396 
00405 function ox_get_secure($sTplName, $oSmarty)
00406 {
00407     // assume all templates are secure
00408     return true;
00409 }
00410 
00417 function ox_get_trusted($sTplName, $oSmarty)
00418 {
00419     // not used for templates
00420 }
00421 
00422 
00423 if (!function_exists('getLangTableIdx')) {
00424 
00432     function getLangTableIdx($iLangId)
00433     {
00434         $iLangPerTable = oxRegistry::getConfig()->getConfigParam("iLangPerTable");
00435         //#0002718 min language count per table 2
00436         $iLangPerTable = ($iLangPerTable > 1) ? $iLangPerTable : 8;
00437 
00438         $iTableIdx = (int) ($iLangId / $iLangPerTable);
00439 
00440         return $iTableIdx;
00441     }
00442 }
00443 
00444 if (!function_exists('getLangTableName')) {
00445 
00454     function getLangTableName($sTable, $iLangId)
00455     {
00456         $iTableIdx = getLangTableIdx($iLangId);
00457         if ($iTableIdx && in_array($sTable, oxRegistry::getLang()->getMultiLangTables())) {
00458             $sLangTableSuffix = oxRegistry::getConfig()->getConfigParam("sLangTableSuffix");
00459             $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
00460 
00461             $sTable .= $sLangTableSuffix . $iTableIdx;
00462         }
00463 
00464         return $sTable;
00465     }
00466 }
00467 
00468 if (!function_exists('getViewName')) {
00469 
00479     function getViewName($sTable, $iLangId = null, $sShopId = null)
00480     {
00481         $myConfig = oxRegistry::getConfig();
00482 
00483         //This config option should only be used in emergency case.
00484         //Originally it was planned for the case when admin area is not reached due to the broken views.
00485         if (!$myConfig->getConfigParam('blSkipViewUsage')) {
00486             $sViewSfx = '';
00487 
00488 
00489             $blIsMultiLang = in_array($sTable, oxRegistry::getLang()->getMultiLangTables());
00490             if ($iLangId != -1 && $blIsMultiLang) {
00491                 $oLang = oxRegistry::getLang();
00492                 $iLangId = $iLangId !== null ? $iLangId : oxRegistry::getLang()->getBaseLanguage();
00493                 $sAbbr = $oLang->getLanguageAbbr($iLangId);
00494                 $sViewSfx .= "_{$sAbbr}";
00495             }
00496 
00497             if ($sViewSfx || (($iLangId == -1 || $sShopId == -1) && $blIsMultiLang)) {
00498                 return "oxv_{$sTable}{$sViewSfx}";
00499             }
00500 
00501         }
00502 
00503         return $sTable;
00504     }
00505 }
00506 
00507 if (!function_exists('getRequestUrl')) {
00516     function getRequestUrl($sParams = '', $blReturnUrl = false)
00517     {
00518         if ($_SERVER["REQUEST_METHOD"] != "POST") {
00519 
00520             if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']) {
00521                 $sRequest = $_SERVER['REQUEST_URI'];
00522             } else {
00523                 // try something else
00524                 $sRequest = $_SERVER['SCRIPT_URI'];
00525             }
00526 
00527             // trying to resolve controller file name
00528             if ($sRequest && ($iPos = stripos($sRequest, '?')) !== false) {
00529 
00530                 $oStr = getStr();
00531                 // formatting request url
00532                 $sRequest = 'index.php' . $oStr->substr($sRequest, $iPos);
00533 
00534                 // removing possible session id
00535                 $sRequest = $oStr->preg_replace('/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest);
00536                 $sRequest = $oStr->preg_replace('/(&|\?)stoken=[^&]*&?/', '$1', $sRequest);
00537                 $sRequest = $oStr->preg_replace('/&$/', '', $sRequest);
00538 
00539                 return str_replace('&', '&amp;', $sRequest);
00540             }
00541         }
00542     }
00543 }
00544 
00545 //registering oxAutoload() as autoload handler
00546 spl_autoload_register("oxAutoload");