oxshopcontrol.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxShopControl extends oxSuperCfg
00009 {
00015     protected $_dTimeStart = null;
00016 
00022     protected $_dTimeEnd = null;
00023 
00034     public function start()
00035     {
00036         $myConfig = $this->getConfig();
00037 
00038         //perform tasks once per session
00039         $this->_runOnce();
00040 
00041         $sClass    = oxConfig::getParameter( 'cl' );
00042         $sFunction = oxConfig::getParameter( 'fnc' );
00043 
00044         if ( !$sClass ) {
00045 
00046             if ( !$this->isAdmin() ) {
00047 
00048                 // first start of the shop
00049                 // check wether we have to display mall startscreen or not
00050                 if ( $myConfig->isMall() ) {
00051 
00052                     $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
00053 
00054                     $sMallShopURL = $myConfig->getConfigParam( 'sMallShopURL' );
00055                     if ( $iShopCount && $iShopCount > 1 && $myConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
00056                         // no class specified so we need to change back to baseshop
00057                         $sClass = 'mallstart';
00058                     }
00059                 }
00060 
00061                 if ( !$sClass ) {
00062                     $sClass = 'start';
00063                 }
00064             } else {
00065                 $sClass = 'login';
00066             }
00067 
00068             oxSession::setVar( 'cl', $sClass );
00069         }
00070 
00071         // baseshop always active if there is no mall
00072         if ( !oxSession::getVar( 'actshop' ) ) {
00073             oxSession::setVar( 'actshop', $myConfig->getShopId() );
00074         }
00075 
00076         try {
00077             $this->_process( $sClass, $sFunction );
00078         } catch( oxSystemComponentException $oEx ) {
00079             //possible reason: class does not exist etc. --> just redirect to start page
00080             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00081             oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start' );
00082         } catch ( oxCookieException $oEx ) {
00083             // redirect to start page and display the error
00084             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00085             oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start' );
00086         }
00087     }
00088 
00098     protected function _log( $sClass, $sFnc )
00099     {
00100         $oDb = oxDb::getDb();
00101         $sShopID = oxSession::getVar( 'actshop' );
00102         $sTime   = date( 'Y-m-d H:i:s' );
00103         $sSidQuoted    = $oDb->quote( $this->getSession()->getId() );
00104         $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00105         $sCnid = oxConfig::getParameter( 'cnid' );
00106         $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00107         $sParameter = '';
00108 
00109         if ( $sClass == 'info' ) {
00110             $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00111         } elseif ( $sClass == 'search' ) {
00112             $sParameter = oxConfig::getParameter( 'searchparam' );
00113         }
00114 
00115         $sFncQuoted = $oDb->quote( $sFnc );
00116         $sClassQuoted = $oDb->quote( $sClass );
00117         $sParameterQuoted = $oDb->quote( $sParameter );
00118 
00119         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00120               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, '$sCnid', '$sAnid', $sParameterQuoted )";
00121         $oDb->execute( $sQ );
00122     }
00123 
00124     // OXID : add timing
00130     protected function _startMonitor()
00131     {
00132         if ( !$this->isAdmin() && $this->getConfig()->getConfigParam( 'iDebug' ) ) {
00133             list ( $sUsec, $sSec ) = explode( ' ', microtime() );
00134             $this->_dTimeStart = ( ( float ) $sUsec + ( float ) $sSec );
00135         }
00136     }
00137 
00148     protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00149     {
00150         $myConfig = $this->getConfig();
00151         if ( !$this->isAdmin() && $myConfig->getConfigParam( 'iDebug' ) != 0 ) {
00152             $sLog = '<div align="left">';
00153 
00154             // outputting template params
00155             if ( $myConfig->getConfigParam( 'iDebug' ) == 4 ) {
00156 
00157                 reset( $aViewData );
00158                 while ( list( $sViewName, $oViewData ) = each( $aViewData ) ) {
00159                     // show debbuging information
00160                         $sLog .= "TemplateData[$sViewName] : <br />\n";
00161                         print_r( $oViewData );
00162                 }
00163             }
00164 
00165             // output timing
00166             list( $sUsec, $sSec ) = explode( ' ', microtime() );
00167             $this->_dTimeEnd = ( ( float ) $sUsec + ( float ) $sSec );
00168             //getting rid of float precission shift
00169             $dTimeSpent = ((int)( $this->_dTimeEnd * 10000 - $this->_dTimeStart * 10000 ) ) / 10000;
00170 
00171             $sLog .= 'Execution time :'.$dTimeSpent.'<br />';
00172 
00173             // memory usage info
00174             if ( function_exists( 'memory_get_usage' ) ) {
00175                 $iKb = ( int ) ( memory_get_usage() / 1024 );
00176                 $iMb = round($iKb / 1024, 3);
00177                 $sLog .= 'Memory usage: '.$iMb.' MB';
00178 
00179                 if ( function_exists( 'memory_get_peak_usage' ) ) {
00180                     $iPeakKb = ( int ) ( memory_get_peak_usage() / 1024 );
00181                     $iPeakMb = round($iPeakKb / 1024, 3);
00182                     $sLog .= ' (peak: '.$iPeakMb.' MB)';
00183                 }
00184                 $sLog .= '<br />';
00185 
00186                 if ( version_compare( PHP_VERSION, '5.2.0', '>=' ) ) {
00187                     $iKb = ( int ) ( memory_get_usage( true ) / 1024 );
00188                     $iMb = round($iKb / 1024, 3);
00189                     $sLog .= 'System memory usage: '.$iMb.' MB';
00190 
00191                     if ( function_exists( 'memory_get_peak_usage' ) ) {
00192                         $iPeakKb = ( int ) ( memory_get_peak_usage( true ) / 1024 );
00193                         $iPeakMb = round($iPeakKb / 1024, 3);
00194                         $sLog .= ' (peak: '.$iPeakMb.' MB)';
00195                     }
00196                     $sLog .= '<br />';
00197                 }
00198             }
00199 
00200             $sLog .= '</div>';
00201             $this->_output( $sLog );
00202         }
00203     }
00204 
00210     public function getTotalTime()
00211     {
00212         if ($this->_dTimeEnd && $this->_dTimeStart) {
00213             return $this->_dTimeEnd - $this->_dTimeStart;
00214         }
00215 
00216         return 0;
00217     }
00218 
00234     protected function _process( $sClass, $sFunction )
00235     {
00236         $myConfig = $this->getConfig();
00237         $myUtils  = oxUtils::getInstance();
00238         $sViewID = null;
00239 
00240         if ( !$myUtils->isSearchEngine() &&
00241              !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00242             $this->_log( $sClass, $sFunction );
00243         }
00244 
00245         // starting resource monitor
00246         $this->_startMonitor();
00247 
00248         // creating current view object
00249         $oViewObject = oxNew( $sClass );
00250 
00251         // store this call
00252         $oViewObject->setClassName( $sClass );
00253         $oViewObject->setFncName( $sFunction );
00254 
00255         $myConfig->setActiveView( $oViewObject );
00256 
00257         // caching params ...
00258         $sOutput      = null;
00259         $blIsCached   = false;
00260         $blIsCachable = false;
00261 
00262 
00263         // init class
00264         $oViewObject->init();
00265 
00266         // executing user defined function
00267         $oViewObject->executeFunction( $oViewObject->getFncName() );
00268 
00269 
00270         // get Smarty is important here as it sets template directory correct
00271         $oSmarty = oxUtilsView::getInstance()->getSmarty();
00272 
00273         // if no cache was stored before we should generate it
00274         if ( !$blIsCached ) {
00275 
00276             // render it
00277             $sTemplateName = $oViewObject->render();
00278 
00279             // check if template dir exists
00280             $sTemplateFile = $myConfig->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00281             if ( !file_exists( $sTemplateFile)) {
00282                 $oEx = oxNew( 'oxSystemComponentException' );
00283                 $oLang = oxLang::getInstance();
00284                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND', $oLang->getBaseLanguage() ), $sTemplateFile) );
00285                 $oEx->setComponent( $sTemplateName );
00286                 throw $oEx;
00287             }
00288             $aViewData = $oViewObject->getViewData();
00289 
00290             //Output processing. This is useful for modules. As sometimes you may want to process output manually.
00291             $oOutput = oxNew( 'oxoutput' );
00292             $aViewData = $oOutput->processViewArray( $aViewData, $oViewObject->getClassName() );
00293             $oViewObject->setViewData( $aViewData );
00294 
00295             //add all exceptions to display
00296             if ( ( $aErrors = oxSession::getVar( 'Errors' ) ) ) {
00297                 oxUtilsView::getInstance()->passAllErrorsToView( $aViewData, $aErrors );
00298 
00299                 // resetting errors after displaying them
00300                 oxSession::setVar( 'Errors', array() );
00301             }
00302 
00303             foreach ( array_keys( $aViewData ) as $sViewName ) {
00304                 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00305             }
00306 
00307             // passing current view object to smarty
00308             $oSmarty->oxobject = $oViewObject;
00309 
00310 
00311             $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00312 
00313             //Output processing - useful for modules as sometimes you may want to process output manually.
00314             $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00315             $sOutput = $oOutput->addVersionTags( $sOutput );
00316         }
00317 
00318 
00319         // #M1047 Firefox duplicated GET fix
00320         $myUtils->setHeader( "Content-Type: text/html; charset=".oxLang::getInstance()->translateString( 'charset' ) );
00321 
00322         // show output
00323         $this->_output( $sOutput );
00324 
00325         $myConfig->pageClose();
00326 
00327         // stopping resource monitor
00328         $this->_stopMonitor( $blIsCachable, $blIsCached, $sViewID, $oViewObject->getViewData() );
00329     }
00330 
00338     protected function _output( $sOutput )
00339     {
00340         echo $sOutput;
00341     }
00342 
00349     protected function _runOnce()
00350     {
00351         $myConfig = $this->getConfig();
00352         $blProductive = true;
00353         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00354 
00355             $iErrorReporting = error_reporting();
00356             if ( version_compare(PHP_VERSION, '5.3.0', '>=') ) {
00357                 // some 3rd party libraries still use deprecated functions
00358                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00359             } else {
00360                 $iErrorReporting = E_ALL ^ E_NOTICE;
00361             }
00362             // A. is it the right place for this code ?
00363             // productive mode ?
00364             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00365                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00366                     $myConfig->setConfigParam( 'iDebug', -1 );
00367                 }
00368             } else {
00369                 // disable error logging if server is misconfigured
00370                 if ( !ini_get( 'log_errors' ) ) {
00371                     $iErrorReporting = E_NONE;
00372                 }
00373             }
00374             error_reporting($iErrorReporting);
00375 
00376 
00377         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00378 
00379             $sTpl = false;
00380             // perform stuff - check if setup is still there
00381             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00382                 $sTpl = 'err_setup.tpl';
00383             }
00384 
00385             if ( $sTpl ) {
00386                 $oActView = oxNew( 'oxubase' );
00387                 $oSmarty = oxUtilsView::getInstance()->getSmarty();
00388                 $oSmarty->assign('oView', $oActView );
00389                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00390                 oxUtils::getInstance()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00391             }
00392 
00393             oxSession::setVar( 'blRunOnceExecuted', true );
00394         }
00395     }
00396 }