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 ) ) {
00102         $aModules = oxConfig::getInstance()->getConfigParam( 'aModules' );
00103         if ( is_array( $aModules ) ) {
00104             $myUtilsObject = oxUtilsObject::getInstance();
00105             foreach ( $aModules as $sParentName => $sModuleName ) {
00106                 // looking for module parent class
00107                 if ( stripos( $sModuleName, $sClass ) !== false ) {
00108                     $myUtilsObject->getClassName( $sParentName );
00109                     break;
00110                 }
00111             }
00112             $aTriedClasses[] = $sClass;
00113         }
00114     }
00115 
00116     stopProfile("oxAutoload");
00117 }
00118 
00119 if ( !function_exists( 'error_404_handler' ) ) {
00127     function error_404_handler($sUrl = '')
00128     {
00129         oxUtils::getInstance()->handlePageNotFoundError($sUrl);
00130     }
00131 }
00132 
00144 function warningHandler($iErrorNr, $sErrorText)
00145 {
00146     echo "<div class='error_box'>".oxLang::getInstance()->translateString('userError')."<code>[$iErrorNr] $sErrorText</code></div>";
00147 }
00148 
00157 function dumpVar( $mVar, $blToFile = false )
00158 {
00159     $myConfig = oxConfig::getInstance();
00160     if ( $blToFile ) {
00161         $out = var_export( $mVar, true );
00162         $f = fopen( $myConfig->getConfigParam( 'sCompileDir' )."/vardump.txt", "a" );
00163         fwrite( $f, $out );
00164         fclose( $f );
00165     } else {
00166         echo '<pre>';
00167         print_r( $mVar );
00168         echo '</pre>';
00169     }
00170 }
00171 
00172 if ( !function_exists( 'isAdmin' ) ) {
00180     function isAdmin()
00181     {
00182         //additionally checking maybe oxConfig::blAdmin is already set
00183         if ( class_exists( 'oxConfig', false ) ) {
00184             $blAdmin = oxConfig::getInstance()->getConfigParam( 'blAdmin' );
00185             if ( isset( $blAdmin ) ) {
00186                 stopProfile('isAdmin');
00187                 return $blAdmin;
00188             }
00189         }
00190         return false;
00191     }
00192 }
00193 
00194 if ( !function_exists( 'isSearchEngineUrl' ) ) {
00195 
00201     function isSearchEngineUrl()
00202     {
00203         return false;
00204     }
00205 }
00206 
00214 function debug( $mVar )
00215 {
00216     $f = fopen( 'out.txt', 'a' );
00217     $sString = var_export( $mVar, true );
00218     fputs( $f, $sString."\n---------------------------------------------\n" );
00219     fclose( $f );
00220 }
00221 
00230 function cmpart( $a, $b )
00231 {
00232     // sorting for crossselling
00233     if ( $a->cnt == $b->cnt )
00234         return 0;
00235     return ( $a->cnt < $b->cnt ) ? -1 : 1;
00236 }
00237 
00238 if ( !function_exists( 'startProfile' ) ) {
00246     function startProfile( $sProfileName )
00247     {
00248         global $aStartTimes;
00249         global $aExecutionCounts;
00250         if (!isset($aExecutionCounts[$sProfileName])) {
00251             $aExecutionCounts[$sProfileName] = 0;
00252         }
00253         if (!isset($aStartTimes[$sProfileName])) {
00254             $aStartTimes[$sProfileName] = 0;
00255         }
00256         $aExecutionCounts[$sProfileName]++;
00257         $aStartTimes[$sProfileName] = microtime(true);
00258     }
00259 }
00260 
00261 if ( !function_exists( 'stopProfile' ) ) {
00269     function stopProfile( $sProfileName )
00270     {
00271         global $aProfileTimes;
00272         global $aStartTimes;
00273         if (!isset($aProfileTimes[$sProfileName])) {
00274             $aProfileTimes[$sProfileName] = 0;
00275         }
00276         $aProfileTimes[$sProfileName] += microtime( true ) - $aStartTimes[$sProfileName];
00277     }
00278 }
00279 
00290 function oxNew( $sClassName )
00291 {
00292     startProfile( 'oxNew' );
00293     $aArgs = func_get_args();
00294     $oRes = call_user_func_array( array( oxUtilsObject::getInstance(), "oxNew" ), $aArgs );
00295     stopProfile( 'oxNew' );
00296     return $oRes;
00297 }
00298 
00306 function oxNewArticle( $sArtId )
00307 {
00308     return oxUtilsObject::getInstance()->oxNewArticle( $sArtId );
00309 }
00310 
00318 function getDb($blAssoc = true)
00319 {
00320     return oxDb::getDb($blAssoc);
00321 }
00322 
00328 function getStr()
00329 {
00330     return oxStr::getStr();
00331 }
00332 
00342 function ox_get_template( $sTplName, &$sTplSource, $oSmarty )
00343 {
00344     $sTplSource = $oSmarty->oxidcache->value;
00345     if ( oxConfig::getInstance()->isDemoShop() ) {
00346         $oSmarty->security = true;
00347     }
00348 
00349     return true;
00350 }
00351 
00361 function ox_get_timestamp( $sTplName, &$iTplTimestamp, $oSmarty )
00362 {
00363     if ( isset( $oSmarty->oxidtimecache->value ) ) {
00364         // use stored timestamp
00365         $iTplTimestamp = $oSmarty->oxidtimecache->value;
00366     } else {
00367         // always compile
00368         $iTplTimestamp = time();
00369     }
00370 
00371     return true;
00372 }
00373 
00382 function ox_get_secure( $sTplName, $oSmarty )
00383 {
00384     // assume all templates are secure
00385     return true;
00386 }
00387 
00396 function ox_get_trusted( $sTplName, $oSmarty )
00397 {
00398     // not used for templates
00399 }
00400 
00401 
00402 if ( !function_exists( 'getLangTableIdx' ) ) {
00403 
00411     function getLangTableIdx( $iLangId )
00412     {
00413         $iLangPerTable = oxConfig::getInstance()->getConfigParam( "iLangPerTable" );
00414         //#0002718 min language count per table 2
00415         $iLangPerTable = ( $iLangPerTable > 1 ) ? $iLangPerTable : 8;
00416 
00417         $iTableIdx = (int) ( $iLangId / $iLangPerTable );
00418         return $iTableIdx;
00419     }
00420 }
00421 
00422 if ( !function_exists( 'getLangTableName' ) ) {
00423 
00432     function getLangTableName( $sTable, $iLangId )
00433     {
00434         $iTableIdx = getLangTableIdx( $iLangId );
00435         if ( $iTableIdx && in_array($sTable, oxLang::getInstance()->getMultiLangTables())) {
00436             $sLangTableSuffix = oxConfig::getInstance()->getConfigParam( "sLangTableSuffix" );
00437             $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
00438 
00439             $sTable .= $sLangTableSuffix . $iTableIdx;
00440         }
00441 
00442         return $sTable;
00443     }
00444 }
00445 
00446 if ( !function_exists( 'getViewName' ) ) {
00447 
00457     function getViewName( $sTable, $iLangId = null, $sShopId = null )
00458     {
00459         $myConfig = oxConfig::getInstance();
00460         if ( !$myConfig->getConfigParam( 'blSkipViewUsage' ) ) {
00461             $sViewSfx = '';
00462 
00463 
00464             $blIsMultiLang = in_array( $sTable, oxLang::getInstance()->getMultiLangTables() );
00465             if ( $iLangId != -1 && $blIsMultiLang ) {
00466                 $oLang = oxLang::getInstance();
00467                 $iLangId = $iLangId !== null ? $iLangId : oxLang::getInstance()->getBaseLanguage();
00468                 $sAbbr = $oLang->getLanguageAbbr( $iLangId );
00469                 $sViewSfx .= "_{$sAbbr}";
00470             }
00471 
00472             if ( $sViewSfx || (($iLangId == -1 || $sShopId == -1 ) && $blIsMultiLang)) {
00473                 return "oxv_{$sTable}{$sViewSfx}";
00474             }
00475 
00476         }
00477 
00478         return $sTable;
00479     }
00480 }
00481 
00482 if ( !function_exists( 'getRequestUrl' ) ) {
00491     function getRequestUrl( $sParams = '', $blReturnUrl = false )
00492     {
00493         if ($_SERVER["REQUEST_METHOD"] != "POST" ) {
00494 
00495             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00496                 $sRequest = $_SERVER['REQUEST_URI'];
00497             } else {
00498                 // try something else
00499                 $sRequest = $_SERVER['SCRIPT_URI'];
00500             }
00501 
00502             // trying to resolve controller file name
00503             if ( $sRequest && ( $iPos = stripos( $sRequest, '?' ) ) !== false ) {
00504 
00505                 $oStr = getStr();
00506                 // formatting request url
00507                 $sRequest = 'index.php' . $oStr->substr( $sRequest, $iPos );
00508 
00509                 // removing possible session id
00510                 $sRequest = $oStr->preg_replace( '/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest );
00511                 $sRequest = $oStr->preg_replace( '/(&|\?)stoken=[^&]*&?/', '$1', $sRequest );
00512                 $sRequest = $oStr->preg_replace( '/&$/', '', $sRequest );
00513                 return str_replace( '&', '&amp;', $sRequest );
00514             }
00515         }
00516     }
00517 }
00518 
00519 //registering oxAutoload() as autoload handler
00520 spl_autoload_register("oxAutoload");