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", "oxconfig");
00026     if (in_array($sClass, $aBaseClasses)) {
00027         $sFilename = getShopBasePath() ."core/" . $sClass . ".php" ;
00028         include $sFilename;
00029         return;
00030     }
00031 
00032     static $aClassPaths;
00033 
00034     if (!$aClassPaths) {
00035         //removed due to #2931
00036         //$aClassPaths = oxUtils::getInstance()->fromPhpFileCache("class_file_paths");
00037     }
00038 
00039     if (isset($aClassPaths[$sClass])) {
00040         stopProfile("oxAutoload");
00041         include $aClassPaths[$sClass];
00042         return;
00043     }
00044 
00045     // initializing paths
00046     if ( $aClassDirs == null ) {
00047         $sBasePath = getShopBasePath();
00048         $aClassDirs = array( $sBasePath . 'core/',
00049                              $sBasePath . 'views/',
00050                              $sBasePath . 'core/exception/',
00051                              $sBasePath . 'core/interface/',
00052                              $sBasePath . 'admin/reports/',
00053                              $sBasePath . 'admin/',
00054                              $sBasePath . 'modules/',
00055                              $sBasePath
00056                             );
00057     }
00058 
00059     foreach ( $aClassDirs as $sDir ) {
00060         $sFilename = $sDir .  $sClass . '.php';
00061         if ( file_exists( $sFilename ) ) {
00062             if (!isset($aClassPaths[$sClass])) {
00063                 $aClassPaths[$sClass] = $sFilename;
00064                 //oxUtils::getInstance()->toPhpFileCache("class_file_paths", $aClassPaths);
00065             }
00066             stopProfile("oxAutoload");
00067             include $sFilename;
00068             return;
00069         }
00070     }
00071 
00072     // Files registered by modules
00073     $aModuleFiles = oxConfig::getInstance()->getConfigParam( 'aModuleFiles' );
00074     if ( is_array( $aModuleFiles ) ) {
00075         $sBasePath   = getShopBasePath();
00076         $oModulelist = oxNew('oxmodulelist');
00077         $aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
00078         if (is_array($aActiveModuleInfo)) {
00079             foreach ($aModuleFiles as $sModuleId => $aModules) {
00080                 if (isset($aModules[$sClass]) && isset($aActiveModuleInfo[$sModuleId])) {
00081                     $sPath = $aModules[$sClass];
00082                     $sFilename = $sBasePath. 'modules/'.  $sPath;
00083                     if ( file_exists( $sFilename ) ) {
00084                         if (!isset($aClassPaths[$sClass])) {
00085                             $aClassPaths[$sClass] = $sFilename;
00086                             oxUtils::getInstance()->toPhpFileCache("class_file_paths", $aClassPaths);
00087                         }
00088                         stopProfile("oxAutoload");
00089                         include $sFilename;
00090                         return;
00091                     }
00092                 }
00093             }
00094         }
00095     }
00096 
00097     // in case module parent class (*_parent) is required
00098     $sClass = preg_replace( '/_parent$/i', '', $sClass );
00099 
00100     // special case
00101     if ( !in_array( $sClass, $aTriedClasses ) && is_array( $aModules = oxConfig::getInstance()->getConfigParam( 'aModules' ) ) ) {
00102 
00103         $myUtilsObject = oxUtilsObject::getInstance();
00104         foreach ( $aModules as $sParentName => $sModuleName ) {
00105             // looking for module parent class
00106             if (  preg_match('/\b'.$sClass.'($|\&)/i', $sModuleName )  ) {  
00107                 $myUtilsObject->getClassName( $sParentName );
00108                 break;
00109             }
00110             $aTriedClasses[] = $sClass;
00111         }
00112     }
00113 
00114     stopProfile("oxAutoload");
00115 }
00116 
00117 if ( !function_exists( 'error_404_handler' ) ) {
00125     function error_404_handler($sUrl = '')
00126     {
00127         oxUtils::getInstance()->handlePageNotFoundError($sUrl);
00128     }
00129 }
00130 
00142 function warningHandler($iErrorNr, $sErrorText)
00143 {
00144     echo "<div class='error_box'>".oxLang::getInstance()->translateString('userError')."<code>[$iErrorNr] $sErrorText</code></div>";
00145 }
00146 
00155 function dumpVar( $mVar, $blToFile = false )
00156 {
00157     $myConfig = oxConfig::getInstance();
00158     if ( $blToFile ) {
00159         $out = var_export( $mVar, true );
00160         $f = fopen( $myConfig->getConfigParam( 'sCompileDir' )."/vardump.txt", "a" );
00161         fwrite( $f, $out );
00162         fclose( $f );
00163     } else {
00164         echo '<pre>';
00165         print_r( $mVar );
00166         echo '</pre>';
00167     }
00168 }
00169 
00170 if ( !function_exists( 'isAdmin' ) ) {
00178     function isAdmin()
00179     {
00180         //additionally checking maybe oxConfig::blAdmin is already set
00181         if ( class_exists( 'oxConfig', false ) ) {
00182             $blAdmin = oxConfig::getInstance()->getConfigParam( 'blAdmin' );
00183             if ( isset( $blAdmin ) ) {
00184                 stopProfile('isAdmin');
00185                 return $blAdmin;
00186             }
00187         }
00188         return false;
00189     }
00190 }
00191 
00192 if ( !function_exists( 'isSearchEngineUrl' ) ) {
00193 
00199     function isSearchEngineUrl()
00200     {
00201         return false;
00202     }
00203 }
00204 
00212 function debug( $mVar )
00213 {
00214     $f = fopen( 'out.txt', 'a' );
00215     $sString = var_export( $mVar, true );
00216     fputs( $f, $sString."\n---------------------------------------------\n" );
00217     fclose( $f );
00218 }
00219 
00228 function cmpart( $a, $b )
00229 {
00230     // sorting for crossselling
00231     if ( $a->cnt == $b->cnt )
00232         return 0;
00233     return ( $a->cnt < $b->cnt ) ? -1 : 1;
00234 }
00235 
00236 if ( !function_exists( 'startProfile' ) ) {
00244     function startProfile( $sProfileName )
00245     {
00246         global $aStartTimes;
00247         global $aExecutionCounts;
00248         if (!isset($aExecutionCounts[$sProfileName])) {
00249             $aExecutionCounts[$sProfileName] = 0;
00250         }
00251         if (!isset($aStartTimes[$sProfileName])) {
00252             $aStartTimes[$sProfileName] = 0;
00253         }
00254         $aExecutionCounts[$sProfileName]++;
00255         $aStartTimes[$sProfileName] = microtime(true);
00256     }
00257 }
00258 
00259 if ( !function_exists( 'stopProfile' ) ) {
00267     function stopProfile( $sProfileName )
00268     {
00269         global $aProfileTimes;
00270         global $aStartTimes;
00271         if (!isset($aProfileTimes[$sProfileName])) {
00272             $aProfileTimes[$sProfileName] = 0;
00273         }
00274         $aProfileTimes[$sProfileName] += microtime( true ) - $aStartTimes[$sProfileName];
00275     }
00276 }
00277 
00288 function oxNew( $sClassName )
00289 {
00290     startProfile( 'oxNew' );
00291     $aArgs = func_get_args();
00292     $oRes = call_user_func_array( array( oxUtilsObject::getInstance(), "oxNew" ), $aArgs );
00293     stopProfile( 'oxNew' );
00294     return $oRes;
00295 }
00296 
00304 function oxNewArticle( $sArtId )
00305 {
00306     return oxUtilsObject::getInstance()->oxNewArticle( $sArtId );
00307 }
00308 
00316 function getDb($blAssoc = true)
00317 {
00318     return oxDb::getDb($blAssoc);
00319 }
00320 
00326 function getStr()
00327 {
00328     return oxStr::getStr();
00329 }
00330 
00340 function ox_get_template( $sTplName, &$sTplSource, $oSmarty )
00341 {
00342     $sTplSource = $oSmarty->oxidcache->value;
00343     if ( oxConfig::getInstance()->isDemoShop() ) {
00344         $oSmarty->security = true;
00345     }
00346 
00347     return true;
00348 }
00349 
00359 function ox_get_timestamp( $sTplName, &$iTplTimestamp, $oSmarty )
00360 {
00361     if ( isset( $oSmarty->oxidtimecache->value ) ) {
00362         // use stored timestamp
00363         $iTplTimestamp = $oSmarty->oxidtimecache->value;
00364     } else {
00365         // always compile
00366         $iTplTimestamp = time();
00367     }
00368 
00369     return true;
00370 }
00371 
00380 function ox_get_secure( $sTplName, $oSmarty )
00381 {
00382     // assume all templates are secure
00383     return true;
00384 }
00385 
00394 function ox_get_trusted( $sTplName, $oSmarty )
00395 {
00396     // not used for templates
00397 }
00398 
00399 
00400 if ( !function_exists( 'getLangTableIdx' ) ) {
00401 
00409     function getLangTableIdx( $iLangId )
00410     {
00411         $iLangPerTable = oxConfig::getInstance()->getConfigParam( "iLangPerTable" );
00412         //#0002718 min language count per table 2
00413         $iLangPerTable = ( $iLangPerTable > 1 ) ? $iLangPerTable : 8;
00414 
00415         $iTableIdx = (int) ( $iLangId / $iLangPerTable );
00416         return $iTableIdx;
00417     }
00418 }
00419 
00420 if ( !function_exists( 'getLangTableName' ) ) {
00421 
00430     function getLangTableName( $sTable, $iLangId )
00431     {
00432         $iTableIdx = getLangTableIdx( $iLangId );
00433         if ( $iTableIdx && in_array($sTable, oxLang::getInstance()->getMultiLangTables())) {
00434             $sLangTableSuffix = oxConfig::getInstance()->getConfigParam( "sLangTableSuffix" );
00435             $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
00436 
00437             $sTable .= $sLangTableSuffix . $iTableIdx;
00438         }
00439 
00440         return $sTable;
00441     }
00442 }
00443 
00444 if ( !function_exists( 'getViewName' ) ) {
00445 
00455     function getViewName( $sTable, $iLangId = null, $sShopId = null )
00456     {
00457         $myConfig = oxConfig::getInstance();
00458         if ( !$myConfig->getConfigParam( 'blSkipViewUsage' ) ) {
00459             $sViewSfx = '';
00460 
00461 
00462             $blIsMultiLang = in_array( $sTable, oxLang::getInstance()->getMultiLangTables() );
00463             if ( $iLangId != -1 && $blIsMultiLang ) {
00464                 $oLang = oxLang::getInstance();
00465                 $iLangId = $iLangId !== null ? $iLangId : oxLang::getInstance()->getBaseLanguage();
00466                 $sAbbr = $oLang->getLanguageAbbr( $iLangId );
00467                 $sViewSfx .= "_{$sAbbr}";
00468             }
00469 
00470             if ( $sViewSfx || (($iLangId == -1 || $sShopId == -1 ) && $blIsMultiLang)) {
00471                 return "oxv_{$sTable}{$sViewSfx}";
00472             }
00473 
00474         }
00475 
00476         return $sTable;
00477     }
00478 }
00479 
00480 if ( !function_exists( 'getRequestUrl' ) ) {
00489     function getRequestUrl( $sParams = '', $blReturnUrl = false )
00490     {
00491         if ($_SERVER["REQUEST_METHOD"] != "POST" ) {
00492 
00493             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00494                 $sRequest = $_SERVER['REQUEST_URI'];
00495             } else {
00496                 // try something else
00497                 $sRequest = $_SERVER['SCRIPT_URI'];
00498             }
00499 
00500             // trying to resolve controller file name
00501             if ( $sRequest && ( $iPos = stripos( $sRequest, '?' ) ) !== false ) {
00502 
00503                 $oStr = getStr();
00504                 // formatting request url
00505                 $sRequest = 'index.php' . $oStr->substr( $sRequest, $iPos );
00506 
00507                 // removing possible session id
00508                 $sRequest = $oStr->preg_replace( '/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest );
00509                 $sRequest = $oStr->preg_replace( '/(&|\?)stoken=[^&]*&?/', '$1', $sRequest );
00510                 $sRequest = $oStr->preg_replace( '/&$/', '', $sRequest );
00511                 return str_replace( '&', '&amp;', $sRequest );
00512             }
00513         }
00514     }
00515 }
00516 
00517 //registering oxAutoload() as autoload handler
00518 spl_autoload_register("oxAutoload");