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