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         $aClassPaths = oxUtils::getInstance()->fromPhpFileCache("class_file_paths");
00036     }
00037 
00038     if (isset($aClassPaths[$sClass])) {
00039         stopProfile("oxAutoload");
00040         include $aClassPaths[$sClass];
00041         return;
00042     }
00043 
00044     // initializing paths
00045     if ( $aClassDirs == null ) {
00046         $sBasePath = getShopBasePath();
00047         $aClassDirs = array( $sBasePath . 'core/',
00048                              $sBasePath . 'views/',
00049                              $sBasePath . 'core/exception/',
00050                              $sBasePath . 'core/interface/',
00051                              $sBasePath . 'admin/reports/',
00052                              $sBasePath . 'admin/',
00053                              $sBasePath . 'modules/',
00054                              $sBasePath
00055                             );
00056     }
00057 
00058     foreach ( $aClassDirs as $sDir ) {
00059         $sFilename = $sDir .  $sClass . '.php';
00060         if ( file_exists( $sFilename ) ) {
00061             if (!isset($aClassPaths[$sClass])) {
00062                 $aClassPaths[$sClass] = $sFilename;
00063                 oxUtils::getInstance()->toPhpFileCache("class_file_paths", $aClassPaths);
00064             }
00065             stopProfile("oxAutoload");
00066             include $sFilename;
00067             return;
00068         }
00069     }
00070 
00071     // in case module parent class (*_parent) is required
00072     $sClass = preg_replace( '/_parent$/i', '', $sClass );
00073 
00074     // special case
00075     if ( !in_array( $sClass, $aTriedClasses ) && is_array( $aModules = oxConfig::getInstance()->getConfigParam( 'aModules' ) ) ) {
00076 
00077         $myUtilsObject = oxUtilsObject::getInstance();
00078         foreach ( $aModules as $sParentName => $sModuleName ) {
00079             // looking for module parent class
00080             if ( stripos( $sModuleName, $sClass ) !== false ) {
00081                 $myUtilsObject->getClassName( $sParentName );
00082                 break;
00083             }
00084         }
00085         $aTriedClasses[] = $sClass;
00086     }
00087 
00088     stopProfile("oxAutoload");
00089 }
00090 
00091 if ( !function_exists( 'error_404_handler' ) ) {
00099     function error_404_handler($sUrl = '')
00100     {
00101         oxUtils::getInstance()->handlePageNotFoundError($sUrl);
00102     }
00103 }
00104 
00116 function warningHandler($iErrorNr, $sErrorText)
00117 {
00118     echo "<div class='error_box'>".oxLang::getInstance()->translateString('userError')."<code>[$iErrorNr] $sErrorText</code></div>";
00119 }
00120 
00129 function dumpVar( $mVar, $blToFile = false )
00130 {
00131     $myConfig = oxConfig::getInstance();
00132     if ( $blToFile ) {
00133         $out = var_export( $mVar, true );
00134         $f = fopen( $myConfig->getConfigParam( 'sCompileDir' )."/vardump.txt", "a" );
00135         fwrite( $f, $out );
00136         fclose( $f );
00137     } else {
00138         echo '<pre>';
00139         print_r( $mVar );
00140         echo '</pre>';
00141     }
00142 }
00143 
00144 if ( !function_exists( 'isAdmin' ) ) {
00152     function isAdmin()
00153     {
00154         //additionally checking maybe oxConfig::blAdmin is already set
00155         if ( class_exists( 'oxConfig', false ) ) {
00156             $blAdmin = oxConfig::getInstance()->getConfigParam( 'blAdmin' );
00157             if ( isset( $blAdmin ) ) {
00158                 stopProfile('isAdmin');
00159                 return $blAdmin;
00160             }
00161         }
00162         return false;
00163     }
00164 }
00165 
00166 if ( !function_exists( 'isSearchEngineUrl' ) ) {
00167 
00173     function isSearchEngineUrl()
00174     {
00175         return false;
00176     }
00177 }
00178 
00186 function debug( $mVar )
00187 {
00188     $f = fopen( 'out.txt', 'a' );
00189     $sString = var_export( $mVar, true );
00190     fputs( $f, $sString."\n---------------------------------------------\n" );
00191     fclose( $f );
00192 }
00193 
00202 function cmpart( $a, $b )
00203 {
00204     // sorting for crossselling
00205     if ( $a->cnt == $b->cnt )
00206         return 0;
00207     return ( $a->cnt < $b->cnt ) ? -1 : 1;
00208 }
00209 
00210 if ( !function_exists( 'startProfile' ) ) {
00218     function startProfile( $sProfileName )
00219     {
00220         global $aStartTimes;
00221         global $aExecutionCounts;
00222         if (!isset($aExecutionCounts[$sProfileName])) {
00223             $aExecutionCounts[$sProfileName] = 0;
00224         }
00225         if (!isset($aStartTimes[$sProfileName])) {
00226             $aStartTimes[$sProfileName] = 0;
00227         }
00228         $aExecutionCounts[$sProfileName]++;
00229         $aStartTimes[$sProfileName] = microtime(true);
00230     }
00231 }
00232 
00233 if ( !function_exists( 'stopProfile' ) ) {
00241     function stopProfile( $sProfileName )
00242     {
00243         global $aProfileTimes;
00244         global $aStartTimes;
00245         if (!isset($aProfileTimes[$sProfileName])) {
00246             $aProfileTimes[$sProfileName] = 0;
00247         }
00248         $aProfileTimes[$sProfileName] += microtime( true ) - $aStartTimes[$sProfileName];
00249     }
00250 }
00251 
00262 function oxNew( $sClassName )
00263 {
00264     startProfile( 'oxNew' );
00265     $aArgs = func_get_args();
00266     $oRes = call_user_func_array( array( oxUtilsObject::getInstance(), "oxNew" ), $aArgs );
00267     stopProfile( 'oxNew' );
00268     return $oRes;
00269 }
00270 
00278 function oxNewArticle( $sArtId )
00279 {
00280     return oxUtilsObject::getInstance()->oxNewArticle( $sArtId );
00281 }
00282 
00290 function getDb($blAssoc = true)
00291 {
00292     return oxDb::getDb($blAssoc);
00293 }
00294 
00300 function getStr()
00301 {
00302     return oxStr::getStr();
00303 }
00304 
00314 function ox_get_template( $sTplName, &$sTplSource, $oSmarty )
00315 {
00316     $sTplSource = $oSmarty->oxidcache->value;
00317     if ( oxConfig::getInstance()->isDemoShop() ) {
00318         $oSmarty->security = true;
00319     }
00320 
00321     return true;
00322 }
00323 
00333 function ox_get_timestamp( $sTplName, &$iTplTimestamp, $oSmarty )
00334 {
00335     if ( isset( $oSmarty->oxidtimecache->value ) ) {
00336         // use stored timestamp
00337         $iTplTimestamp = $oSmarty->oxidtimecache->value;
00338     } else {
00339         // always compile
00340         $iTplTimestamp = time();
00341     }
00342 
00343     return true;
00344 }
00345 
00354 function ox_get_secure( $sTplName, $oSmarty )
00355 {
00356     // assume all templates are secure
00357     return true;
00358 }
00359 
00368 function ox_get_trusted( $sTplName, $oSmarty )
00369 {
00370     // not used for templates
00371 }
00372 
00373 
00374 if ( !function_exists( 'getLangTableIdx' ) ) {
00375 
00383     function getLangTableIdx( $iLangId )
00384     {
00385         $iLangPerTable = oxConfig::getInstance()->getConfigParam( "iLangPerTable" );
00386         //#0002718 min language count per table 2
00387         $iLangPerTable = ( $iLangPerTable > 1 ) ? $iLangPerTable : 8;
00388 
00389         $iTableIdx = (int) ( $iLangId / $iLangPerTable );
00390         return $iTableIdx;
00391     }
00392 }
00393 
00394 if ( !function_exists( 'getLangTableName' ) ) {
00395 
00404     function getLangTableName( $sTable, $iLangId )
00405     {
00406         $iTableIdx = getLangTableIdx( $iLangId );
00407         if ( $iTableIdx && in_array($sTable, oxLang::getInstance()->getMultiLangTables())) {
00408             $sLangTableSuffix = oxConfig::getInstance()->getConfigParam( "sLangTableSuffix" );
00409             $sLangTableSuffix = $sLangTableSuffix ? $sLangTableSuffix : "_set";
00410 
00411             $sTable .= $sLangTableSuffix . $iTableIdx;
00412         }
00413 
00414         return $sTable;
00415     }
00416 }
00417 
00418 if ( !function_exists( 'getViewName' ) ) {
00419 
00429     function getViewName( $sTable, $iLangId = null, $sShopId = null )
00430     {
00431         $myConfig = oxConfig::getInstance();
00432         if ( !$myConfig->getConfigParam( 'blSkipViewUsage' ) ) {
00433             $sViewSfx = '';
00434 
00435 
00436             $blIsMultiLang = in_array( $sTable, oxLang::getInstance()->getMultiLangTables() );
00437             if ( $iLangId != -1 && $blIsMultiLang ) {
00438                 $oLang = oxLang::getInstance();
00439                 $iLangId = $iLangId !== null ? $iLangId : oxLang::getInstance()->getBaseLanguage();
00440                 $sAbbr = $oLang->getLanguageAbbr( $iLangId );
00441                 $sViewSfx .= "_{$sAbbr}";
00442             }
00443 
00444             if ( $sViewSfx || (($iLangId == -1 || $sShopId == -1 ) && $blIsMultiLang)) {
00445                 return "oxv_{$sTable}{$sViewSfx}";
00446             }
00447 
00448         }
00449 
00450         return $sTable;
00451     }
00452 }
00453 
00454 if ( !function_exists( 'getRequestUrl' ) ) {
00463     function getRequestUrl( $sParams = '', $blReturnUrl = false )
00464     {
00465         if ($_SERVER["REQUEST_METHOD"] != "POST" ) {
00466 
00467             if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00468                 $sRequest = $_SERVER['REQUEST_URI'];
00469             } else {
00470                 // try something else
00471                 $sRequest = $_SERVER['SCRIPT_URI'];
00472             }
00473 
00474             // trying to resolve controller file name
00475             if ( $sRequest && ( $iPos = stripos( $sRequest, '?' ) ) !== false ) {
00476 
00477                 $oStr = getStr();
00478                 // formatting request url
00479                 $sRequest = 'index.php' . $oStr->substr( $sRequest, $iPos );
00480 
00481                 // removing possible session id
00482                 $sRequest = $oStr->preg_replace( '/(&|\?)(force_)?(admin_)?sid=[^&]*&?/', '$1', $sRequest );
00483                 $sRequest = $oStr->preg_replace( '/(&|\?)stoken=[^&]*&?/', '$1', $sRequest );
00484                 $sRequest = $oStr->preg_replace( '/&$/', '', $sRequest );
00485                 return str_replace( '&', '&amp;', $sRequest );
00486             }
00487         }
00488     }
00489 }
00490 
00491 //registering oxAutoload() as autoload handler
00492 spl_autoload_register("oxAutoload");