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 
00031     protected $_aErrors = null;
00032 
00040     protected $_oOutput = null;
00041 
00042     protected $_oCache = null;
00043 
00054     public function start()
00055     {
00056         $myConfig = $this->getConfig();
00057 
00058         //perform tasks once per session
00059         $this->_runOnce();
00060 
00061         $sClass    = oxConfig::getParameter( 'cl' );
00062         $sFunction = oxConfig::getParameter( 'fnc' );
00063 
00064         if ( !$sClass ) {
00065 
00066             if ( !$this->isAdmin() ) {
00067 
00068                 // first start of the shop
00069                 // check wether we have to display mall startscreen or not
00070                 if ( $myConfig->isMall() ) {
00071 
00072                     $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
00073 
00074                     $sMallShopURL = $myConfig->getConfigParam( 'sMallShopURL' );
00075                     if ( $iShopCount && $iShopCount > 1 && $myConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
00076                         // no class specified so we need to change back to baseshop
00077                         $sClass = 'mallstart';
00078                     }
00079                 }
00080 
00081                 if ( !$sClass ) {
00082                     $sClass = 'start';
00083                 }
00084             } else {
00085                 $sClass = 'login';
00086             }
00087 
00088             oxSession::setVar( 'cl', $sClass );
00089         }
00090 
00091         try {
00092             $this->_process( $sClass, $sFunction );
00093         } catch( oxSystemComponentException $oEx ) {
00094             //possible reason: class does not exist etc. --> just redirect to start page
00095             if ( $this->_isDebugMode() ) {
00096                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00097                 $this->_process( 'exceptionError', 'displayExceptionError' );
00098             }
00099             $oEx->debugOut();
00100 
00101             if ( !$myConfig->getConfigParam( 'iDebug' ) ) {
00102                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00103             }
00104         } catch ( oxCookieException $oEx ) {
00105             // redirect to start page and display the error
00106             if ( $this->_isDebugMode() ) {
00107                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00108             }
00109             oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00110         }
00111 
00112         catch ( oxException $oEx) {
00113             //catching other not cought exceptions
00114             if ( $this->_isDebugMode() ) {
00115                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00116                 $this->_process( 'exceptionError', 'displayExceptionError' );
00117             }
00118 
00119             // log the exception
00120             $oEx->debugOut();
00121         }
00122 
00123     }
00124 
00134     protected function _log( $sClass, $sFnc )
00135     {
00136         $oDb = oxDb::getDb();
00137         $sShopID = oxSession::getVar( 'actshop' );
00138         $sTime   = date( 'Y-m-d H:i:s' );
00139         $sSidQuoted    = $oDb->quote( $this->getSession()->getId() );
00140         $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00141         $sCnid = oxConfig::getParameter( 'cnid' );
00142         $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00143         $sParameter = '';
00144 
00145         if ( $sClass == 'info' ) {
00146             $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00147         } elseif ( $sClass == 'search' ) {
00148             $sParameter = oxConfig::getParameter( 'searchparam' );
00149         }
00150 
00151         $sFncQuoted = $oDb->quote( $sFnc );
00152         $sClassQuoted = $oDb->quote( $sClass );
00153         $sParameterQuoted = $oDb->quote( $sParameter );
00154 
00155         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00156               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, ".$oDb->quote( $sCnid ).", ".$oDb->quote( $sAnid ).", $sParameterQuoted )";
00157         $oDb->execute( $sQ );
00158     }
00159 
00160     // OXID : add timing
00166     protected function _startMonitor()
00167     {
00168         if ( $this->_isDebugMode() ) {
00169             $this->_dTimeStart = microtime(true);
00170         }
00171     }
00172 
00183     protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00184     {
00185         if ( $this->_isDebugMode() && !$this->isAdmin() ) {
00186             /* @var $oDebugInfo oxDebugInfo */
00187             $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00188             $oDebugInfo = oxNew('oxDebugInfo');
00189 
00190             $blHidden = ($iDebug == -1);
00191 
00192             $sLog = '';
00193             $sLogId = md5(time().rand().rand());
00194             $sLog .= "<div style='color:#630;margin:15px 0 0;cursor:pointer' onclick='var el=document.getElementById(\"debugInfoBlock_$sLogId\"); if (el.style.display==\"block\")el.style.display=\"none\"; else el.style.display = \"block\";'> ".$oDebugInfo->formatGeneralInfo()."(show/hide)</div>";
00195             $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:".($blHidden?'none':'block')."' class='debugInfoBlock' align='left'>";
00196 
00197             // outputting template params
00198             if ( $iDebug == 4 ) {
00199                 $sLog .= $oDebugInfo->formatTemplateData($aViewData);
00200             }
00201 
00202             // output timing
00203             $this->_dTimeEnd = microtime(true);
00204 
00205 
00206             $sLog .= $oDebugInfo->formatMemoryUsage();
00207             $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
00208 
00209             if ( $iDebug == 7 ) {
00210                 $sLog .= $oDebugInfo->formatDbInfo();
00211             }
00212 
00213             if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 ) {
00214                 $sLog .= $oDebugInfo->formatAdoDbPerf();
00215             }
00216 
00217             $sLog .= '</div>';
00218 
00219             $this->_getOutputManager()->output('debuginfo', $sLog);
00220         }
00221     }
00222 
00228     public function getTotalTime()
00229     {
00230         if ($this->_dTimeEnd && $this->_dTimeStart) {
00231             return $this->_dTimeEnd - $this->_dTimeStart;
00232         }
00233 
00234         return 0;
00235     }
00236 
00242     protected function _executeMaintenanceTasks()
00243     {
00244         startProfile('executeMaintenanceTasks');
00245         oxNew("oxarticlelist")->updateUpcomingPrices();
00246         stopProfile('executeMaintenanceTasks');
00247     }
00248 
00264     protected function _process( $sClass, $sFunction )
00265     {
00266         startProfile('process');
00267         $myConfig = $this->getConfig();
00268 
00269         // executing maintenance tasks
00270         $this->_executeMaintenanceTasks();
00271 
00272         $myUtils  = oxUtils::getInstance();
00273         $sViewID = null;
00274 
00275         if ( !$myUtils->isSearchEngine() &&
00276              !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00277             $this->_log( $sClass, $sFunction );
00278         }
00279 
00280         // starting resource monitor
00281         $this->_startMonitor();
00282 
00283         // caching params ...
00284         $sOutput      = null;
00285         $blIsCached   = false;
00286 
00287         $oViewObject = $this->_initializeViewObject($sClass, $sFunction);
00288 
00289         // executing user defined function
00290         $oViewObject->executeFunction( $oViewObject->getFncName() );
00291 
00292 
00293         // if no cache was stored before we should generate it
00294         if ( !$blIsCached ) {
00295             $sOutput = $this->_render($oViewObject);
00296         }
00297 
00298 
00299         $oOutput = $this->_getOutputManager();
00300         $oOutput->setCharset($oViewObject->getCharSet());
00301 
00302         if (oxConfig::getParameter('renderPartial')) {
00303             $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
00304             $oOutput->output('errors', $this->_getFormattedErrors());
00305         }
00306 
00307         $oOutput->sendHeaders();
00308 
00309         $oOutput->output('content', $sOutput);
00310 
00311         $myConfig->pageClose();
00312 
00313         stopProfile('process');
00314 
00315         // stopping resource monitor
00316         $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
00317 
00318         // flush output (finalize)
00319         $oOutput->flushOutput();
00320     }
00321 
00330     protected function _initializeViewObject($sClass, $sFunction)
00331     {
00332         $myConfig = $this->getConfig();
00333 
00334         // creating current view object
00335         $oViewObject = oxNew( $sClass );
00336 
00337         // store this call
00338         $oViewObject->setClassName( $sClass );
00339         $oViewObject->setFncName( $sFunction );
00340 
00341         $myConfig->setActiveView( $oViewObject );
00342 
00343 
00344         // init class
00345         $oViewObject->init();
00346 
00347         return $oViewObject;
00348     }
00349 
00350 
00356     protected function _getFormattedErrors()
00357     {
00358         $aErrors = $this->_getErrors();
00359         $aFmtErrors = array();
00360         if ( is_array($aErrors) && count($aErrors) ) {
00361             foreach ( $aErrors as $sLocation => $aEx2 ) {
00362                 foreach ( $aEx2 as $sKey => $oEr ) {
00363                     $oErr = unserialize( $oEr );
00364                     $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
00365                 }
00366             }
00367         }
00368         return $aFmtErrors;
00369     }
00370 
00378     protected function _render($oViewObject)
00379     {
00380         // get Smarty is important here as it sets template directory correct
00381         $oSmarty = oxUtilsView::getInstance()->getSmarty();
00382 
00383         // render it
00384         $sTemplateName = $oViewObject->render();
00385 
00386         // check if template dir exists
00387         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00388         if ( !file_exists( $sTemplateFile)) {
00389 
00390             $oEx = oxNew( 'oxSystemComponentException' );
00391             $oLang = oxLang::getInstance();
00392             $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
00393             $oEx->setComponent( $sTemplateName );
00394 
00395             $sTemplateName = "message/exception.tpl";
00396 
00397             if ( $this->_isDebugMode() ) {
00398                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00399             }
00400             $oEx->debugOut();
00401         }
00402 
00403         $aViewData = $oViewObject->getViewData();
00404 
00405         // Output processing. This is useful for modules. As sometimes you may want to process output manually.
00406         $oOutput = $this->_getOutputManager();
00407         $aViewData = $oOutput->processViewArray( $aViewData, $oViewObject->getClassName() );
00408         $oViewObject->setViewData( $aViewData );
00409 
00410         //add all exceptions to display
00411         $aErrors = $this->_getErrors();
00412         if ( is_array($aErrors) && count($aErrors) ) {
00413             oxUtilsView::getInstance()->passAllErrorsToView( $aViewData, $aErrors );
00414         }
00415 
00416         foreach ( array_keys( $aViewData ) as $sViewName ) {
00417             $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00418         }
00419 
00420         // passing current view object to smarty
00421         $oSmarty->oxobject = $oViewObject;
00422 
00423 
00424         $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00425 
00426         //Output processing - useful for modules as sometimes you may want to process output manually.
00427         $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00428         return $oOutput->addVersionTags( $sOutput );
00429     }
00430 
00436     protected function _getOutputManager()
00437     {
00438         if (!$this->_oOutput) {
00439             $this->_oOutput = oxNew('oxOutput');
00440         }
00441         return $this->_oOutput;
00442     }
00443 
00455     protected function _output( $sOutput )
00456     {
00457         echo $sOutput;
00458     }
00459 
00465     protected function _getErrors()
00466     {
00467         if (null === $this->_aErrors) {
00468             $this->_aErrors = oxSession::getVar( 'Errors' );
00469             if (null === $this->_aErrors) {
00470                 $this->_aErrors = array();
00471             }
00472             // resetting errors from session
00473             oxSession::setVar( 'Errors', array() );
00474         }
00475         return $this->_aErrors;
00476     }
00477 
00484     protected function _runOnce()
00485     {
00486         $myConfig = $this->getConfig();
00487         $blProductive = true;
00488         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00489 
00490             $iErrorReporting = error_reporting();
00491             if ( defined( 'E_DEPRECATED' ) ) {
00492                 // some 3rd party libraries still use deprecated functions
00493                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00494             } else {
00495                 $iErrorReporting = E_ALL ^ E_NOTICE;
00496             }
00497             // A. is it the right place for this code ?
00498             // productive mode ?
00499             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00500                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00501                     $myConfig->setConfigParam( 'iDebug', -1 );
00502                 }
00503             } else {
00504                 // disable error logging if server is misconfigured
00505                 // #2015 E_NONE replaced with 0
00506                 if ( !ini_get( 'log_errors' ) ) {
00507                     $iErrorReporting = 0;
00508                 }
00509             }
00510             error_reporting($iErrorReporting);
00511 
00512 
00513         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00514 
00515             $sTpl = false;
00516             // perform stuff - check if setup is still there
00517             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00518                 $sTpl = 'message/err_setup.tpl';
00519             }
00520 
00521             if ( $sTpl ) {
00522                 $oActView = oxNew( 'oxubase' );
00523                 $oSmarty = oxUtilsView::getInstance()->getSmarty();
00524                 $oSmarty->assign('oView', $oActView );
00525                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00526                 oxUtils::getInstance()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00527             }
00528 
00529             oxSession::setVar( 'blRunOnceExecuted', true );
00530         }
00531     }
00532 
00538     protected function _isDebugMode()
00539     {
00540         if ( $this->getConfig()->getConfigParam( 'iDebug' ) ) {
00541             return true;
00542         }
00543 
00544         return false;
00545     }
00546 }