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         // if no cache was stored before we should generate it
00271         if ( !$blIsCached ) {
00272             $sOutput = $this->_render($oViewObject, $blIsCachable);
00273         }
00274 
00275 
00276         // #M1047 Firefox duplicated GET fix
00277         $myUtils->setHeader( "Content-Type: text/html; charset=".$oViewObject->getCharSet() );
00278 
00279         // show output
00280         $this->_output( $sOutput );
00281 
00282         $myConfig->pageClose();
00283 
00284         // stopping resource monitor
00285         $this->_stopMonitor( $blIsCachable, $blIsCached, $sViewID, $oViewObject->getViewData() );
00286     }
00287 
00296     protected function _render($oViewObject, $blRenderForCache = false)
00297     {
00298         // get Smarty is important here as it sets template directory correct
00299         $oSmarty = oxUtilsView::getInstance()->getSmarty();
00300 
00301         // render it
00302         $sTemplateName = $oViewObject->render();
00303 
00304         // check if template dir exists
00305         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00306         if ( !file_exists( $sTemplateFile)) {
00307             $oEx = oxNew( 'oxSystemComponentException' );
00308             $oLang = oxLang::getInstance();
00309             $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND', $oLang->getBaseLanguage() ), $sTemplateFile) );
00310             $oEx->setComponent( $sTemplateName );
00311             throw $oEx;
00312         }
00313         $aViewData = $oViewObject->getViewData();
00314 
00315         //Output processing. This is useful for modules. As sometimes you may want to process output manually.
00316         $oOutput = oxNew( 'oxoutput' );
00317         $aViewData = $oOutput->processViewArray( $aViewData, $oViewObject->getClassName() );
00318         $oViewObject->setViewData( $aViewData );
00319 
00320         //add all exceptions to display
00321         if ( ( $aErrors = oxSession::getVar( 'Errors' ) ) ) {
00322             oxUtilsView::getInstance()->passAllErrorsToView( $aViewData, $aErrors );
00323 
00324             // resetting errors after displaying them
00325             oxSession::setVar( 'Errors', array() );
00326         }
00327 
00328         foreach ( array_keys( $aViewData ) as $sViewName ) {
00329             $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00330         }
00331 
00332         // passing current view object to smarty
00333         $oSmarty->oxobject = $oViewObject;
00334 
00335 
00336         $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00337 
00338         //Output processing - useful for modules as sometimes you may want to process output manually.
00339         $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00340 
00341         return $oOutput->addVersionTags( $sOutput );
00342     }
00343 
00351     protected function _output( $sOutput )
00352     {
00353         echo $sOutput;
00354     }
00355 
00362     protected function _runOnce()
00363     {
00364         $myConfig = $this->getConfig();
00365         $blProductive = true;
00366         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00367 
00368             $iErrorReporting = error_reporting();
00369             if ( version_compare(PHP_VERSION, '5.3.0', '>=') ) {
00370                 // some 3rd party libraries still use deprecated functions
00371                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00372             } else {
00373                 $iErrorReporting = E_ALL ^ E_NOTICE;
00374             }
00375             // A. is it the right place for this code ?
00376             // productive mode ?
00377             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00378                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00379                     $myConfig->setConfigParam( 'iDebug', -1 );
00380                 }
00381             } else {
00382                 // disable error logging if server is misconfigured
00383                 if ( !ini_get( 'log_errors' ) ) {
00384                     $iErrorReporting = E_NONE;
00385                 }
00386             }
00387             error_reporting($iErrorReporting);
00388 
00389 
00390         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00391 
00392             $sTpl = false;
00393             // perform stuff - check if setup is still there
00394             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00395                 $sTpl = 'err_setup.tpl';
00396             }
00397 
00398             if ( $sTpl ) {
00399                 $oActView = oxNew( 'oxubase' );
00400                 $oSmarty = oxUtilsView::getInstance()->getSmarty();
00401                 $oSmarty->assign('oView', $oActView );
00402                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00403                 oxUtils::getInstance()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00404             }
00405 
00406             oxSession::setVar( 'blRunOnceExecuted', true );
00407         }
00408     }
00409 }