oxshopcontrol.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxShopControl extends oxSuperCfg
00009 {
00015     protected $_blHandlerSet = null;
00016 
00022     protected $_blMainTasksExecuted = null;
00023 
00029     protected $_dTimeStart = null;
00030 
00036     protected $_dTimeEnd = null;
00037 
00045     protected $_aErrors = null;
00046 
00054     protected $_oOutput = null;
00055 
00056     protected $_oCache = null;
00057 
00074     public function start( $sClass = null, $sFunction = null, $aParams = null, $aViewsChain = null )
00075     {
00076         //sets default exception handler
00077         $this->_setDefaultExceptionHandler();
00078 
00079         $myConfig = $this->getConfig();
00080 
00081         //perform tasks once per session
00082         $this->_runOnce();
00083 
00084         $sClass    = ( isset( $sClass ) ) ? $sClass : oxConfig::getParameter( 'cl' );
00085         $sFunction = ( isset( $sFunction ) ) ? $sFunction : oxConfig::getParameter( 'fnc' );
00086 
00087         if ( !$sClass ) {
00088 
00089             if ( !$this->isAdmin() ) {
00090 
00091                 // first start of the shop
00092                 // check wether we have to display mall startscreen or not
00093                 if ( $myConfig->isMall() ) {
00094 
00095                     $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
00096 
00097                     $sMallShopURL = $myConfig->getConfigParam( 'sMallShopURL' );
00098                     if ( $iShopCount && $iShopCount > 1 && $myConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
00099                         // no class specified so we need to change back to baseshop
00100                         $sClass = 'mallstart';
00101                     }
00102                 }
00103 
00104                 if ( !$sClass ) {
00105                     $sClass = 'start';
00106                 }
00107             } else {
00108                 $sClass = 'login';
00109             }
00110 
00111             oxSession::setVar( 'cl', $sClass );
00112         }
00113 
00114         try {
00115             $this->_process( $sClass, $sFunction, $aParams, $aViewsChain );
00116         } catch( oxSystemComponentException $oEx ) {
00117             //possible reason: class does not exist etc. --> just redirect to start page
00118             if ( $this->_isDebugMode() ) {
00119                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00120                 $this->_process( 'exceptionError', 'displayExceptionError' );
00121             }
00122             $oEx->debugOut();
00123 
00124             if ( !$myConfig->getConfigParam( 'iDebug' ) ) {
00125                 oxRegistry::getUtils()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00126             }
00127         } catch ( oxCookieException $oEx ) {
00128             // redirect to start page and display the error
00129             if ( $this->_isDebugMode() ) {
00130                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00131             }
00132             oxRegistry::getUtils()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00133         }
00134 
00135         catch ( oxException $oEx) {
00136             //catching other not cought exceptions
00137             if ( $this->_isDebugMode() ) {
00138                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00139                 $this->_process( 'exceptionError', 'displayExceptionError' );
00140             }
00141 
00142             // log the exception
00143             $oEx->debugOut();
00144         }
00145     }
00146 
00153     protected function _setDefaultExceptionHandler()
00154     {
00155         if (isset($this->_blHandlerSet)) {
00156             return;
00157         }
00158         set_exception_handler(array(oxNew('oxexceptionhandler', $this->_isDebugMode()), 'handleUncaughtException'));
00159     }
00160 
00170     protected function _log( $sClass, $sFnc )
00171     {
00172         $oDb = oxDb::getDb();
00173         $sShopID = oxSession::getVar( 'actshop' );
00174         $sTime   = date( 'Y-m-d H:i:s' );
00175         $sSidQuoted    = $oDb->quote( $this->getSession()->getId() );
00176         $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00177         $sCnid = oxConfig::getParameter( 'cnid' );
00178         $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00179         $sParameter = '';
00180 
00181         if ( $sClass == 'content' ) {
00182             $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00183         } elseif ( $sClass == 'search' ) {
00184             $sParameter = oxConfig::getParameter( 'searchparam' );
00185         }
00186 
00187         $sFncQuoted = $oDb->quote( $sFnc );
00188         $sClassQuoted = $oDb->quote( $sClass );
00189         $sParameterQuoted = $oDb->quote( $sParameter );
00190 
00191 
00192         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00193               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, ".$oDb->quote( $sCnid ).", ".$oDb->quote( $sAnid ).", $sParameterQuoted )";
00194 
00195         $oDb->execute( $sQ );
00196     }
00197 
00198     // OXID : add timing
00204     protected function _startMonitor()
00205     {
00206         if ( $this->_isDebugMode() ) {
00207             $this->_dTimeStart = microtime(true);
00208         }
00209     }
00210 
00221     protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00222     {
00223         if ( $this->_isDebugMode() && !$this->isAdmin() ) {
00224             /* @var $oDebugInfo oxDebugInfo */
00225             $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00226             $oDebugInfo = oxNew('oxDebugInfo');
00227 
00228             $blHidden = ($iDebug == -1);
00229 
00230             $sLog = '';
00231             $sLogId = md5(time().rand().rand());
00232             $sLog .= "<div id='oxidDebugInfo_$sLogId'>";
00233 
00234             $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>";
00235             $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:".($blHidden?'none':'block')."' class='debugInfoBlock' align='left'>";
00236 
00237 
00238             // outputting template params
00239             if ( $iDebug == 4 ) {
00240                 $sLog .= $oDebugInfo->formatTemplateData($aViewData);
00241             }
00242 
00243             // output timing
00244             $this->_dTimeEnd = microtime(true);
00245 
00246 
00247             $sLog .= $oDebugInfo->formatMemoryUsage();
00248 
00249             $sLog .= $oDebugInfo->formatTimeStamp();
00250 
00251 
00252             $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
00253 
00254             if ( $iDebug == 7 ) {
00255                 $sLog .= $oDebugInfo->formatDbInfo();
00256             }
00257 
00258             if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 ) {
00259                 $sLog .= $oDebugInfo->formatAdoDbPerf();
00260             }
00261 
00262             $sLog .= '</div>';
00263 
00264             $sLog .= "<script type='text/javascript'>
00265                 var b = document.getElementById('oxidDebugInfo_$sLogId');
00266                 var c = document.body;
00267                 if (c) { c.appendChild(b.parentNode.removeChild(b));}
00268             </script>";
00269 
00270             $sLog .= "</div>";
00271 
00272             $this->_getOutputManager()->output('debuginfo', $sLog);
00273         }
00274     }
00275 
00281     public function getTotalTime()
00282     {
00283         if ($this->_dTimeEnd && $this->_dTimeStart) {
00284             return $this->_dTimeEnd - $this->_dTimeStart;
00285         }
00286 
00287         return 0;
00288     }
00289 
00295     protected function _executeMaintenanceTasks()
00296     {
00297         if (isset($this->_blMainTasksExecuted)) {
00298             return;
00299         }
00300 
00301         startProfile('executeMaintenanceTasks');
00302         oxNew("oxarticlelist")->updateUpcomingPrices();
00303         stopProfile('executeMaintenanceTasks');
00304     }
00305 
00323     protected function _process( $sClass, $sFunction, $aParams = null, $aViewsChain = null )
00324     {
00325         startProfile('process');
00326         $myConfig = $this->getConfig();
00327 
00328         // executing maintenance tasks
00329         $this->_executeMaintenanceTasks();
00330 
00331         $oUtils  = oxRegistry::getUtils();
00332         $sViewID = null;
00333 
00334         if ( !$oUtils->isSearchEngine() &&
00335              !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00336             $this->_log( $sClass, $sFunction );
00337         }
00338 
00339         // starting resource monitor
00340         $this->_startMonitor();
00341 
00342         // caching params ...
00343         $sOutput      = null;
00344         $blIsCached   = false;
00345 
00346         $oViewObject = $this->_initializeViewObject($sClass, $sFunction, $aParams, $aViewsChain);
00347 
00348         // executing user defined function
00349         $oViewObject->executeFunction( $oViewObject->getFncName() );
00350 
00351 
00352 
00353         // if no cache was stored before we should generate it
00354         if ( !$blIsCached ) {
00355             $sOutput = $this->_render($oViewObject);
00356         }
00357 
00358 
00359         $oOutput = $this->_getOutputManager();
00360         $oOutput->setCharset($oViewObject->getCharSet());
00361 
00362         if (oxConfig::getParameter('renderPartial')) {
00363             $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
00364             $oOutput->output('errors', $this->_getFormattedErrors( $oViewObject->getClassName() ));
00365         }
00366 
00367        $oOutput->sendHeaders();
00368 
00369 
00370         $oOutput->output('content', $sOutput);
00371 
00372         $myConfig->pageClose();
00373 
00374         stopProfile('process');
00375 
00376         // stopping resource monitor
00377         $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
00378 
00379         // flush output (finalize)
00380         $oOutput->flushOutput();
00381     }
00382 
00393     protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
00394     {
00395         $myConfig = $this->getConfig();
00396 
00397         // creating current view object
00398         $oViewObject = oxNew( $sClass );
00399 
00400         // store this call
00401         $oViewObject->setClassName( $sClass );
00402         $oViewObject->setFncName( $sFunction );
00403         $oViewObject->setViewParameters( $aParams );
00404 
00405         $myConfig->setActiveView( $oViewObject );
00406 
00407 
00408         // init class
00409         $oViewObject->init();
00410 
00411         return $oViewObject;
00412     }
00413 
00414 
00422     protected function _getFormattedErrors( $sControllerName )
00423     {
00424         $aErrors = $this->_getErrors( $sControllerName );
00425         $aFmtErrors = array();
00426         if ( is_array($aErrors) && count($aErrors) ) {
00427             foreach ( $aErrors as $sLocation => $aEx2 ) {
00428                 foreach ( $aEx2 as $sKey => $oEr ) {
00429                     $oErr = unserialize( $oEr );
00430                     $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
00431                 }
00432             }
00433         }
00434         return $aFmtErrors;
00435     }
00436 
00444     protected function _render($oViewObject)
00445     {
00446         // get Smarty is important here as it sets template directory correct
00447         $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00448 
00449         // render it
00450         $sTemplateName = $oViewObject->render();
00451 
00452         // check if template dir exists
00453         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00454         if ( !file_exists( $sTemplateFile)) {
00455 
00456             $oEx = oxNew( 'oxSystemComponentException' );
00457             $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
00458             $oEx->setComponent( $sTemplateName );
00459 
00460             $sTemplateName = "message/exception.tpl";
00461 
00462             if ( $this->_isDebugMode() ) {
00463                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00464             }
00465             $oEx->debugOut();
00466         }
00467 
00468         // Output processing. This is useful for modules. As sometimes you may want to process output manually.
00469         $oOutput = $this->_getOutputManager();
00470         $aViewData = $oOutput->processViewArray( $oViewObject->getViewData(), $oViewObject->getClassName() );
00471         $oViewObject->setViewData( $aViewData );
00472 
00473         //add all exceptions to display
00474         $aErrors = $this->_getErrors( $oViewObject->getClassName() );
00475         if ( is_array($aErrors) && count($aErrors) ) {
00476             oxRegistry::get("oxUtilsView")->passAllErrorsToView( $aViewData, $aErrors );
00477         }
00478 
00479         foreach ( array_keys( $aViewData ) as $sViewName ) {
00480             $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00481         }
00482 
00483         // passing current view object to smarty
00484         $oSmarty->oxobject = $oViewObject;
00485 
00486 
00487         $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00488 
00489         //Output processing - useful for modules as sometimes you may want to process output manually.
00490         $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00491         return $oOutput->addVersionTags( $sOutput );
00492     }
00493 
00499     protected function _getOutputManager()
00500     {
00501         if (!$this->_oOutput) {
00502             $this->_oOutput = oxNew('oxOutput');
00503         }
00504         return $this->_oOutput;
00505     }
00506 
00514     protected function _getErrors( $sCurrentControllerName )
00515     {
00516         if (null === $this->_aErrors) {
00517             $this->_aErrors = oxSession::getVar( 'Errors' );
00518             if (null === $this->_aErrors) {
00519                 $this->_aErrors = array();
00520             }
00521             // resetting errors of current controller or widget from session
00522             $aControllerErrors = oxSession::getVar( 'ErrorController' );
00523             $aAllErrors = $this->_aErrors;
00524             if ( is_array($aControllerErrors) && !empty($aControllerErrors)) {
00525                 foreach ( $aControllerErrors as $sErrorName => $sControllerName ) {
00526                     if ( $sControllerName == $sCurrentControllerName ) {
00527                         unset($aAllErrors[$sErrorName]);
00528                         unset($aControllerErrors[$sErrorName]);
00529                     }
00530                 }
00531             } else {
00532                 $aAllErrors = array();
00533             }
00534             oxSession::setVar( 'ErrorController', $aControllerErrors );
00535             oxSession::setVar( 'Errors', $aAllErrors );
00536         }
00537         return $this->_aErrors;
00538     }
00539 
00546     protected function _runOnce()
00547     {
00548         $myConfig = $this->getConfig();
00549         $blProductive = true;
00550         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00551 
00552             $iErrorReporting = error_reporting();
00553             if ( defined( 'E_DEPRECATED' ) ) {
00554                 // some 3rd party libraries still use deprecated functions
00555                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00556             } else {
00557                 $iErrorReporting = E_ALL ^ E_NOTICE;
00558             }
00559             // A. is it the right place for this code ?
00560             // productive mode ?
00561             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00562                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00563                     $myConfig->setConfigParam( 'iDebug', -1 );
00564                 }
00565             } else {
00566                 // disable error logging if server is misconfigured
00567                 // #2015 E_NONE replaced with 0
00568                 if ( !ini_get( 'log_errors' ) ) {
00569                     $iErrorReporting = 0;
00570                 }
00571             }
00572             error_reporting($iErrorReporting);
00573 
00574 
00575         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00576 
00577             $sTpl = false;
00578             // perform stuff - check if setup is still there
00579             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00580                 $sTpl = 'message/err_setup.tpl';
00581             }
00582 
00583             if ( $sTpl ) {
00584                 $oActView = oxNew( 'oxubase' );
00585                 $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00586                 $oSmarty->assign('oView', $oActView );
00587                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00588                 oxRegistry::getUtils()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00589             }
00590 
00591             oxSession::setVar( 'blRunOnceExecuted', true );
00592         }
00593     }
00594 
00600     protected function _isDebugMode()
00601     {
00602         if ( OxRegistry::get("OxConfigFile")->getVar('iDebug') ) {
00603             return true;
00604         }
00605 
00606         return false;
00607     }
00608 
00609 }