oxshopcontrol.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxShopControl extends oxSuperCfg
00009 {
00010 
00011 
00017     protected $_blHandlerSet = null;
00018 
00024     protected $_blMainTasksExecuted = null;
00025 
00031     protected $_dTimeStart = null;
00032 
00038     protected $_dTimeEnd = null;
00039 
00047     protected $_aErrors = null;
00048 
00056     protected $_aAllErrors = null;
00057 
00065     protected $_aControllerErrors = null;
00066 
00067 
00075     protected $_oOutput = null;
00076 
00082     protected $_oCache = null;
00083 
00095     public function start($sClass = null, $sFunction = null, $aParams = null, $aViewsChain = null)
00096     {
00097         //sets default exception handler
00098         $this->_setDefaultExceptionHandler();
00099 
00100         try {
00101             $this->_runOnce();
00102 
00103             $sFunction = !is_null($sFunction) ? $sFunction : oxRegistry::getConfig()->getRequestParameter('fnc');
00104             $sClass = !is_null($sClass) ? $sClass : $this->_getStartController($sClass);
00105 
00106             $this->_process($sClass, $sFunction, $aParams, $aViewsChain);
00107         } catch (oxSystemComponentException $oEx) {
00108             $this->_handleSystemException($oEx);
00109         } catch (oxCookieException $oEx) {
00110             $this->_handleCookieException($oEx);
00111 
00112         }
00113         catch (oxConnectionException $oEx) {
00114             $this->_handleDbConnectionException($oEx);
00115         } catch (oxException $oEx) {
00116             $this->_handleBaseException($oEx);
00117         }
00118     }
00119 
00126     protected function _setDefaultExceptionHandler()
00127     {
00128         if (isset($this->_blHandlerSet)) {
00129             return;
00130         }
00131 
00132         set_exception_handler(array(oxNew('oxexceptionhandler', $this->_isDebugMode()), 'handleUncaughtException'));
00133     }
00134 
00142     protected function _log($sClass, $sFnc)
00143     {
00144         $oDb = oxDb::getDb();
00145         $oConfig = $this->getConfig();
00146         $oSession = $this->getSession();
00147 
00148         $sShopID = $oSession->getVariable('actshop');
00149         $sTime = date('Y-m-d H:i:s');
00150         $sSidQuoted = $oDb->quote($oSession->getId());
00151         $sUserIDQuoted = $oDb->quote($oSession->getVariable('usr'));
00152 
00153         $sCnid = $oConfig->getRequestParameter('cnid');
00154         $sAnid = $oConfig->getRequestParameter('aid') ? $oConfig->getRequestParameter('aid') : $oConfig->getRequestParameter('anid');
00155 
00156         $sParameter = '';
00157 
00158         if ($sClass == 'content') {
00159             $sParameter = str_replace('.tpl', '', $oConfig->getRequestParameter('tpl'));
00160         } elseif ($sClass == 'search') {
00161             $sParameter = $oConfig->getRequestParameter('searchparam');
00162         }
00163 
00164         $sFncQuoted = $oDb->quote($sFnc);
00165         $sClassQuoted = $oDb->quote($sClass);
00166         $sParameterQuoted = $oDb->quote($sParameter);
00167 
00168         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) " .
00169               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, " . $oDb->quote($sCnid) . ", " . $oDb->quote($sAnid) . ", $sParameterQuoted )";
00170 
00171         $oDb->execute($sQ);
00172     }
00173 
00174     // OXID : add timing
00178     protected function _startMonitor()
00179     {
00180         if ($this->_isDebugMode()) {
00181             $this->_dTimeStart = microtime(true);
00182         }
00183     }
00184 
00193     protected function _stopMonitor($blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array())
00194     {
00195         if ($this->_isDebugMode() && !$this->isAdmin()) {
00196             /* @var $oDebugInfo oxDebugInfo */
00197             $iDebug = $this->getConfig()->getConfigParam('iDebug');
00198             $oDebugInfo = oxNew('oxDebugInfo');
00199 
00200             $blHidden = ($iDebug == -1);
00201 
00202             $sLog = '';
00203             $sLogId = md5(time() . rand() . rand());
00204             $sLog .= "<div id='oxidDebugInfo_$sLogId'>";
00205 
00206             $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>";
00207             $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:" . ($blHidden ? 'none' : 'block') . "' class='debugInfoBlock' align='left'>";
00208 
00209 
00210             // outputting template params
00211             if ($iDebug == 4) {
00212                 $sLog .= $oDebugInfo->formatTemplateData($aViewData);
00213             }
00214 
00215             // output timing
00216             $this->_dTimeEnd = microtime(true);
00217 
00218 
00219             $sLog .= $oDebugInfo->formatMemoryUsage();
00220 
00221             $sLog .= $oDebugInfo->formatTimeStamp();
00222 
00223 
00224             $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
00225 
00226             if ($iDebug == 7) {
00227                 $sLog .= $oDebugInfo->formatDbInfo();
00228             }
00229 
00230             if ($iDebug == 2 || $iDebug == 3 || $iDebug == 4) {
00231                 $sLog .= $oDebugInfo->formatAdoDbPerf();
00232             }
00233 
00234             $sLog .= '</div>';
00235 
00236             $sLog .= "<script type='text/javascript'>
00237                 var b = document.getElementById('oxidDebugInfo_$sLogId');
00238                 var c = document.body;
00239                 if (c) { c.appendChild(b.parentNode.removeChild(b));}
00240             </script>";
00241 
00242             $sLog .= "</div>";
00243 
00244             $this->_getOutputManager()->output('debuginfo', $sLog);
00245         }
00246     }
00247 
00253     public function getTotalTime()
00254     {
00255         if ($this->_dTimeEnd && $this->_dTimeStart) {
00256             return $this->_dTimeEnd - $this->_dTimeStart;
00257         }
00258 
00259         return 0;
00260     }
00261 
00267     protected function _executeMaintenanceTasks()
00268     {
00269         if (isset($this->_blMainTasksExecuted)) {
00270             return;
00271         }
00272 
00273         startProfile('executeMaintenanceTasks');
00274         oxNew("oxArticleList")->updateUpcomingPrices();
00275         stopProfile('executeMaintenanceTasks');
00276     }
00277 
00293     protected function _process($sClass, $sFunction, $aParams = null, $aViewsChain = null)
00294     {
00295         startProfile('process');
00296         $myConfig = $this->getConfig();
00297 
00298         // executing maintenance tasks
00299         $this->_executeMaintenanceTasks();
00300 
00301         $oUtils = oxRegistry::getUtils();
00302         $sViewID = null;
00303 
00304         if (!$oUtils->isSearchEngine() &&
00305             !($this->isAdmin() || !$myConfig->getConfigParam('blLogging'))
00306         ) {
00307             $this->_log($sClass, $sFunction);
00308         }
00309 
00310         // starting resource monitor
00311         $this->_startMonitor();
00312 
00313         // caching params ...
00314         $sOutput = null;
00315         $blIsCached = false;
00316 
00317         // Initialize view object and it's components.
00318         $oViewObject = $this->_initializeViewObject($sClass, $sFunction, $aParams, $aViewsChain);
00319 
00320         if (!$this->_canExecuteFunction($oViewObject, $oViewObject->getFncName())) {
00322             $oException = oxNew('oxSystemComponentException', 'Non public method cannot be accessed');
00323             throw $oException;
00324         }
00325 
00326         // executing user defined function
00327         $oViewObject->executeFunction($oViewObject->getFncName());
00328 
00329 
00330 
00331         // if no cache was stored before we should generate it
00332         if (!$blIsCached) {
00333             $sOutput = $this->_render($oViewObject);
00334         }
00335 
00336 
00337         $oOutput = $this->_getOutputManager();
00338         $oOutput->setCharset($oViewObject->getCharSet());
00339 
00340         if (oxRegistry::getConfig()->getRequestParameter('renderPartial')) {
00341             $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
00342             $oOutput->output('errors', $this->_getFormattedErrors($oViewObject->getClassName()));
00343         }
00344 
00345         $oOutput->sendHeaders();
00346 
00347 
00348         $oOutput->output('content', $sOutput);
00349 
00350         $myConfig->pageClose();
00351 
00352         stopProfile('process');
00353 
00354         // stopping resource monitor
00355         $this->_stopMonitor($oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData());
00356 
00357         // flush output (finalize)
00358         $oOutput->flushOutput();
00359     }
00360 
00371     protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
00372     {
00373         $myConfig = $this->getConfig();
00374 
00375         // creating current view object
00376         $oViewObject = oxNew($sClass);
00377 
00378         // store this call
00379         $oViewObject->setClassName($sClass);
00380         $oViewObject->setFncName($sFunction);
00381         $oViewObject->setViewParameters($aParams);
00382 
00383         $myConfig->setActiveView($oViewObject);
00384 
00385 
00386         // init class
00387         $oViewObject->init();
00388 
00389         return $oViewObject;
00390     }
00391 
00400     protected function _canExecuteFunction($oClass, $sFunction)
00401     {
00402         $blCanExecute = true;
00403 
00404         if (method_exists($oClass, $sFunction)) {
00405             $oReflectionMethod = new ReflectionMethod($oClass, $sFunction);
00406             if (!$oReflectionMethod->isPublic()) {
00407                 $blCanExecute = false;
00408             }
00409         }
00410 
00411         return $blCanExecute;
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 
00435         return $aFmtErrors;
00436     }
00437 
00445     protected function _render($oViewObject)
00446     {
00447         // get Smarty is important here as it sets template directory correct
00448         $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00449 
00450         // render it
00451         $sTemplateName = $oViewObject->render();
00452 
00453         // check if template dir exists
00454         $sTemplateFile = $this->getConfig()->getTemplatePath($sTemplateName, $this->isAdmin());
00455         if (!file_exists($sTemplateFile)) {
00456 
00457             $oEx = oxNew('oxSystemComponentException');
00458             $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND');
00459             $oEx->setComponent($sTemplateName);
00460 
00461             $sTemplateName = "message/exception.tpl";
00462 
00463             if ($this->_isDebugMode()) {
00464                 oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00465             }
00466             $oEx->debugOut();
00467         }
00468 
00469         // Output processing. This is useful for modules. As sometimes you may want to process output manually.
00470         $oOutput = $this->_getOutputManager();
00471         $aViewData = $oOutput->processViewArray($oViewObject->getViewData(), $oViewObject->getClassName());
00472         $oViewObject->setViewData($aViewData);
00473 
00474         //add all exceptions to display
00475         $aErrors = $this->_getErrors($oViewObject->getClassName());
00476         if (is_array($aErrors) && count($aErrors)) {
00477             oxRegistry::get("oxUtilsView")->passAllErrorsToView($aViewData, $aErrors);
00478         }
00479 
00480         foreach (array_keys($aViewData) as $sViewName) {
00481             $oSmarty->assign_by_ref($sViewName, $aViewData[$sViewName]);
00482         }
00483 
00484         // passing current view object to smarty
00485         $oSmarty->oxobject = $oViewObject;
00486 
00487 
00488         $sOutput = $oSmarty->fetch($sTemplateName, $oViewObject->getViewId());
00489 
00490         //Output processing - useful for modules as sometimes you may want to process output manually.
00491         $sOutput = $oOutput->process($sOutput, $oViewObject->getClassName());
00492 
00493         return $oOutput->addVersionTags($sOutput);
00494     }
00495 
00501     protected function _getOutputManager()
00502     {
00503         if (!$this->_oOutput) {
00504             $this->_oOutput = oxNew('oxOutput');
00505         }
00506 
00507         return $this->_oOutput;
00508     }
00509 
00517     protected function _getErrors($sCurrentControllerName)
00518     {
00519         if (null === $this->_aErrors) {
00520             $this->_aErrors = oxRegistry::getSession()->getVariable('Errors');
00521             $this->_aControllerErrors = oxRegistry::getSession()->getVariable('ErrorController');
00522             if (null === $this->_aErrors) {
00523                 $this->_aErrors = array();
00524             }
00525             $this->_aAllErrors = $this->_aErrors;
00526         }
00527         // resetting errors of current controller or widget from session
00528         if (is_array($this->_aControllerErrors) && !empty($this->_aControllerErrors)) {
00529             foreach ($this->_aControllerErrors as $sErrorName => $sControllerName) {
00530                 if ($sControllerName == $sCurrentControllerName) {
00531                     unset($this->_aAllErrors[$sErrorName]);
00532                     unset($this->_aControllerErrors[$sErrorName]);
00533                 }
00534             }
00535         } else {
00536             $this->_aAllErrors = array();
00537         }
00538         oxRegistry::getSession()->setVariable('ErrorController', $this->_aControllerErrors);
00539         oxRegistry::getSession()->setVariable('Errors', $this->_aAllErrors);
00540 
00541         return $this->_aErrors;
00542     }
00543 
00550     protected function _runOnce()
00551     {
00552         $oConfig = $this->getConfig();
00553 
00554         error_reporting($this->_getErrorReportingLevel());
00555 
00556 
00557         $blRunOnceExecuted = oxRegistry::getSession()->getVariable('blRunOnceExecuted');
00558         if (!$blRunOnceExecuted && !$this->isAdmin() && $oConfig->isProductiveMode()) {
00559 
00560             // check if setup is still there
00561             if (file_exists($oConfig->getConfigParam('sShopDir') . '/setup/index.php')) {
00562                 $sTpl = 'message/err_setup.tpl';
00563                 $oActView = oxNew('oxubase');
00564                 $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00565                 $oSmarty->assign('oView', $oActView);
00566                 $oSmarty->assign('oViewConf', $oActView->getViewConfig());
00567                 oxRegistry::getUtils()->showMessageAndExit($oSmarty->fetch($sTpl));
00568             }
00569 
00570             oxRegistry::getSession()->setVariable('blRunOnceExecuted', true);
00571         }
00572     }
00573 
00580     protected function _getErrorReportingLevel()
00581     {
00582         $sOldReporting = error_reporting();
00583 
00584         $iErrorReporting = E_ALL ^ E_NOTICE;
00585         // some 3rd party libraries still use deprecated functions
00586         if (defined('E_DEPRECATED')) {
00587             $iErrorReporting = $iErrorReporting ^ E_DEPRECATED;
00588         }
00589 
00590         if ($this->getConfig()->isProductiveMode() && !ini_get('log_errors')) {
00591             $iErrorReporting = 0;
00592         }
00593 
00594 
00595         return $iErrorReporting;
00596     }
00597 
00603     protected function _isDebugMode()
00604     {
00605         if (OxRegistry::get("OxConfigFile")->getVar('iDebug')) {
00606             return true;
00607         }
00608 
00609         return false;
00610     }
00611 
00612 
00613 
00620     protected function _handleSystemException($oEx)
00621     {
00622         $oEx->debugOut();
00623 
00624         if ($this->_isDebugMode()) {
00625             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00626             $this->_process('exceptionError', 'displayExceptionError');
00627         } else {
00628             oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
00629         }
00630     }
00631 
00637     protected function _handleCookieException($oEx)
00638     {
00639         if ($this->_isDebugMode()) {
00640             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00641         }
00642         oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
00643     }
00644 
00650     protected function _handleAccessRightsException($oEx)
00651     {
00652         oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=content&tpl=err_accessdenied.tpl', true, 302);
00653     }
00654 
00660     protected function _handleDbConnectionException($oEx)
00661     {
00662         $oEx->debugOut();
00663 
00664         if ($this->_isDebugMode()) {
00665             oxRegistry::getUtils()->showMessageAndExit($oEx->getString());
00666         }
00667         oxRegistry::getUtils()->redirectOffline();
00668     }
00669 
00675     protected function _handleBaseException($oEx)
00676     {
00677         $oEx->debugOut();
00678 
00679         if ($this->_isDebugMode()) {
00680             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00681             $this->_process('exceptionError', 'displayExceptionError');
00682         }
00683     }
00684 
00690     protected function _getStartController()
00691     {
00692         $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
00693 
00694         if (!$sClass) {
00695             $sClass = $this->isAdmin() ? 'login' : $this->_getFrontendStartController();
00696             oxRegistry::getSession()->setVariable('cl', $sClass);
00697         }
00698 
00699         return $sClass;
00700     }
00701 
00708     protected function _getFrontendStartController()
00709     {
00710         $sClass = 'start';
00711         if ($this->getConfig()->isMall()) {
00712             $sClass = $this->_getFrontendMallStartController();
00713         }
00714 
00715         return $sClass;
00716     }
00717 
00724     protected function _getFrontendMallStartController()
00725     {
00726         $iShopCount = oxDb::getDb()->getOne('select count(*) from oxshops where oxactive = 1');
00727 
00728         $sMallShopURL = $this->getConfig()->getConfigParam('sMallShopURL');
00729 
00730         $sClass = 'start';
00731         if ($iShopCount && $iShopCount > 1 && $this->getConfig()->getConfigParam('iMallMode') != 0 && !$sMallShopURL) {
00732             $sClass = 'mallstart';
00733         }
00734 
00735         return $sClass;
00736     }
00737 }