oxshopcontrol.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxShopControl extends oxSuperCfg
00009 {
00010 
00016     protected $_blHandlerSet = null;
00017 
00023     protected $_blMainTasksExecuted = null;
00024 
00030     protected $_dTimeStart = null;
00031 
00037     protected $_dTimeEnd = null;
00038 
00046     protected $_aErrors = null;
00047 
00055     protected $_aAllErrors = null;
00056 
00064     protected $_aControllerErrors = null;
00065 
00066 
00074     protected $_oOutput = null;
00075 
00076     protected $_oCache = null;
00077 
00094     public function start( $sClass = null, $sFunction = null, $aParams = null, $aViewsChain = null )
00095     {
00096         //sets default exception handler
00097         $this->_setDefaultExceptionHandler();
00098 
00099         $myConfig = $this->getConfig();
00100 
00101         //perform tasks once per session
00102         $this->_runOnce();
00103 
00104         $sClass    = ( isset( $sClass ) ) ? $sClass : oxConfig::getParameter( 'cl' );
00105         $sFunction = ( isset( $sFunction ) ) ? $sFunction : oxConfig::getParameter( 'fnc' );
00106 
00107         if ( !$sClass ) {
00108 
00109             if ( !$this->isAdmin() ) {
00110 
00111                 // first start of the shop
00112                 // check wether we have to display mall startscreen or not
00113                 if ( $myConfig->isMall() ) {
00114 
00115                     $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
00116 
00117                     $sMallShopURL = $myConfig->getConfigParam( 'sMallShopURL' );
00118                     if ( $iShopCount && $iShopCount > 1 && $myConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
00119                         // no class specified so we need to change back to baseshop
00120                         $sClass = 'mallstart';
00121                     }
00122                 }
00123 
00124                 if ( !$sClass ) {
00125                     $sClass = 'start';
00126                 }
00127             } else {
00128                 $sClass = 'login';
00129             }
00130 
00131             oxSession::setVar( 'cl', $sClass );
00132         }
00133 
00134         try {
00135             $this->_process( $sClass, $sFunction, $aParams, $aViewsChain );
00136         } catch( oxSystemComponentException $oEx ) {
00137             //possible reason: class does not exist etc. --> just redirect to start page
00138             if ( $this->_isDebugMode() ) {
00139                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00140                 $this->_process( 'exceptionError', 'displayExceptionError' );
00141             }
00142             $oEx->debugOut();
00143 
00144             if ( !$myConfig->getConfigParam( 'iDebug' ) ) {
00145                 oxRegistry::getUtils()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00146             }
00147         } catch ( oxCookieException $oEx ) {
00148             // redirect to start page and display the error
00149             if ( $this->_isDebugMode() ) {
00150                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00151             }
00152             oxRegistry::getUtils()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00153         }
00154 
00155         catch ( oxException $oEx) {
00156             //catching other not cought exceptions
00157             if ( $this->_isDebugMode() ) {
00158                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00159                 $this->_process( 'exceptionError', 'displayExceptionError' );
00160             }
00161 
00162             // log the exception
00163             $oEx->debugOut();
00164         }
00165     }
00166 
00173     protected function _setDefaultExceptionHandler()
00174     {
00175         if (isset($this->_blHandlerSet)) {
00176             return;
00177         }
00178         set_exception_handler(array(oxNew('oxexceptionhandler', $this->_isDebugMode()), 'handleUncaughtException'));
00179     }
00180 
00190     protected function _log( $sClass, $sFnc )
00191     {
00192         $oDb = oxDb::getDb();
00193         $sShopID = oxSession::getVar( 'actshop' );
00194         $sTime   = date( 'Y-m-d H:i:s' );
00195         $sSidQuoted    = $oDb->quote( $this->getSession()->getId() );
00196         $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00197         $sCnid = oxConfig::getParameter( 'cnid' );
00198         $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00199         $sParameter = '';
00200 
00201         if ( $sClass == 'content' ) {
00202             $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00203         } elseif ( $sClass == 'search' ) {
00204             $sParameter = oxConfig::getParameter( 'searchparam' );
00205         }
00206 
00207         $sFncQuoted = $oDb->quote( $sFnc );
00208         $sClassQuoted = $oDb->quote( $sClass );
00209         $sParameterQuoted = $oDb->quote( $sParameter );
00210 
00211 
00212         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00213               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, ".$oDb->quote( $sCnid ).", ".$oDb->quote( $sAnid ).", $sParameterQuoted )";
00214 
00215         $oDb->execute( $sQ );
00216     }
00217 
00218     // OXID : add timing
00224     protected function _startMonitor()
00225     {
00226         if ( $this->_isDebugMode() ) {
00227             $this->_dTimeStart = microtime(true);
00228         }
00229     }
00230 
00241     protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00242     {
00243         if ( $this->_isDebugMode() && !$this->isAdmin() ) {
00244             /* @var $oDebugInfo oxDebugInfo */
00245             $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00246             $oDebugInfo = oxNew('oxDebugInfo');
00247 
00248             $blHidden = ($iDebug == -1);
00249 
00250             $sLog = '';
00251             $sLogId = md5(time().rand().rand());
00252             $sLog .= "<div id='oxidDebugInfo_$sLogId'>";
00253 
00254             $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>";
00255             $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:".($blHidden?'none':'block')."' class='debugInfoBlock' align='left'>";
00256 
00257 
00258             // outputting template params
00259             if ( $iDebug == 4 ) {
00260                 $sLog .= $oDebugInfo->formatTemplateData($aViewData);
00261             }
00262 
00263             // output timing
00264             $this->_dTimeEnd = microtime(true);
00265 
00266 
00267             $sLog .= $oDebugInfo->formatMemoryUsage();
00268 
00269             $sLog .= $oDebugInfo->formatTimeStamp();
00270 
00271 
00272             $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
00273 
00274             if ( $iDebug == 7 ) {
00275                 $sLog .= $oDebugInfo->formatDbInfo();
00276             }
00277 
00278             if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 ) {
00279                 $sLog .= $oDebugInfo->formatAdoDbPerf();
00280             }
00281 
00282             $sLog .= '</div>';
00283 
00284             $sLog .= "<script type='text/javascript'>
00285                 var b = document.getElementById('oxidDebugInfo_$sLogId');
00286                 var c = document.body;
00287                 if (c) { c.appendChild(b.parentNode.removeChild(b));}
00288             </script>";
00289 
00290             $sLog .= "</div>";
00291 
00292             $this->_getOutputManager()->output('debuginfo', $sLog);
00293         }
00294     }
00295 
00301     public function getTotalTime()
00302     {
00303         if ($this->_dTimeEnd && $this->_dTimeStart) {
00304             return $this->_dTimeEnd - $this->_dTimeStart;
00305         }
00306 
00307         return 0;
00308     }
00309 
00315     protected function _executeMaintenanceTasks()
00316     {
00317         if (isset($this->_blMainTasksExecuted)) {
00318             return;
00319         }
00320 
00321         startProfile('executeMaintenanceTasks');
00322         oxNew("oxArticleList")->updateUpcomingPrices();
00323         stopProfile('executeMaintenanceTasks');
00324     }
00325 
00343     protected function _process( $sClass, $sFunction, $aParams = null, $aViewsChain = null )
00344     {
00345         startProfile('process');
00346         $myConfig = $this->getConfig();
00347 
00348         // executing maintenance tasks
00349         $this->_executeMaintenanceTasks();
00350 
00351         $oUtils  = oxRegistry::getUtils();
00352         $sViewID = null;
00353 
00354         if ( !$oUtils->isSearchEngine() &&
00355              !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00356             $this->_log( $sClass, $sFunction );
00357         }
00358 
00359         // starting resource monitor
00360         $this->_startMonitor();
00361 
00362         // caching params ...
00363         $sOutput      = null;
00364         $blIsCached   = false;
00365 
00366         $oViewObject = $this->_initializeViewObject($sClass, $sFunction, $aParams, $aViewsChain);
00367 
00368         // executing user defined function
00369         $oViewObject->executeFunction( $oViewObject->getFncName() );
00370 
00371 
00372 
00373         // if no cache was stored before we should generate it
00374         if ( !$blIsCached ) {
00375             $sOutput = $this->_render($oViewObject);
00376         }
00377 
00378 
00379         $oOutput = $this->_getOutputManager();
00380         $oOutput->setCharset($oViewObject->getCharSet());
00381 
00382         if (oxConfig::getParameter('renderPartial')) {
00383             $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
00384             $oOutput->output('errors', $this->_getFormattedErrors( $oViewObject->getClassName() ));
00385         }
00386 
00387        $oOutput->sendHeaders();
00388 
00389 
00390         $oOutput->output('content', $sOutput);
00391 
00392         $myConfig->pageClose();
00393 
00394         stopProfile('process');
00395 
00396         // stopping resource monitor
00397         $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
00398 
00399         // flush output (finalize)
00400         $oOutput->flushOutput();
00401     }
00402 
00413     protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
00414     {
00415         $myConfig = $this->getConfig();
00416 
00417         // creating current view object
00418         $oViewObject = oxNew( $sClass );
00419 
00420         // store this call
00421         $oViewObject->setClassName( $sClass );
00422         $oViewObject->setFncName( $sFunction );
00423         $oViewObject->setViewParameters( $aParams );
00424 
00425         $myConfig->setActiveView( $oViewObject );
00426 
00427 
00428         // init class
00429         $oViewObject->init();
00430 
00431         return $oViewObject;
00432     }
00433 
00434 
00442     protected function _getFormattedErrors( $sControllerName )
00443     {
00444         $aErrors = $this->_getErrors( $sControllerName );
00445         $aFmtErrors = array();
00446         if ( is_array($aErrors) && count($aErrors) ) {
00447             foreach ( $aErrors as $sLocation => $aEx2 ) {
00448                 foreach ( $aEx2 as $sKey => $oEr ) {
00449                     $oErr = unserialize( $oEr );
00450                     $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
00451                 }
00452             }
00453         }
00454         return $aFmtErrors;
00455     }
00456 
00464     protected function _render($oViewObject)
00465     {
00466         // get Smarty is important here as it sets template directory correct
00467         $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00468 
00469         // render it
00470         $sTemplateName = $oViewObject->render();
00471 
00472         // check if template dir exists
00473         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00474         if ( !file_exists( $sTemplateFile)) {
00475 
00476             $oEx = oxNew( 'oxSystemComponentException' );
00477             $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
00478             $oEx->setComponent( $sTemplateName );
00479 
00480             $sTemplateName = "message/exception.tpl";
00481 
00482             if ( $this->_isDebugMode() ) {
00483                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00484             }
00485             $oEx->debugOut();
00486         }
00487 
00488         // Output processing. This is useful for modules. As sometimes you may want to process output manually.
00489         $oOutput = $this->_getOutputManager();
00490         $aViewData = $oOutput->processViewArray( $oViewObject->getViewData(), $oViewObject->getClassName() );
00491         $oViewObject->setViewData( $aViewData );
00492 
00493         //add all exceptions to display
00494         $aErrors = $this->_getErrors( $oViewObject->getClassName() );
00495         if ( is_array($aErrors) && count($aErrors) ) {
00496             oxRegistry::get("oxUtilsView")->passAllErrorsToView( $aViewData, $aErrors );
00497         }
00498 
00499         foreach ( array_keys( $aViewData ) as $sViewName ) {
00500             $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00501         }
00502 
00503         // passing current view object to smarty
00504         $oSmarty->oxobject = $oViewObject;
00505 
00506 
00507         $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00508 
00509         //Output processing - useful for modules as sometimes you may want to process output manually.
00510         $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00511         return $oOutput->addVersionTags( $sOutput );
00512     }
00513 
00519     protected function _getOutputManager()
00520     {
00521         if (!$this->_oOutput) {
00522             $this->_oOutput = oxNew('oxOutput');
00523         }
00524         return $this->_oOutput;
00525     }
00526 
00534     protected function _getErrors( $sCurrentControllerName )
00535     {
00536         if ( null === $this->_aErrors ) {
00537             $this->_aErrors = oxRegistry::getSession()->getVariable( 'Errors' );
00538             $this->_aControllerErrors = oxRegistry::getSession()->getVariable( 'ErrorController' );
00539             if ( null === $this->_aErrors ) {
00540                 $this->_aErrors = array();
00541             }
00542             $this->_aAllErrors = $this->_aErrors;
00543         }
00544         // resetting errors of current controller or widget from session
00545         if ( is_array($this->_aControllerErrors) && !empty($this->_aControllerErrors) ) {
00546             foreach ( $this->_aControllerErrors as $sErrorName => $sControllerName ) {
00547                 if ( $sControllerName == $sCurrentControllerName ) {
00548                     unset( $this->_aAllErrors[$sErrorName] );
00549                     unset( $this->_aControllerErrors[$sErrorName] );
00550                 }
00551             }
00552         } else {
00553             $this->_aAllErrors = array();
00554         }
00555         oxRegistry::getSession()->setVariable( 'ErrorController', $this->_aControllerErrors );
00556         oxRegistry::getSession()->setVariable( 'Errors', $this->_aAllErrors );
00557         return $this->_aErrors;
00558     }
00559 
00566     protected function _runOnce()
00567     {
00568         $myConfig = $this->getConfig();
00569         $blProductive = true;
00570         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00571 
00572             $iErrorReporting = error_reporting();
00573             if ( defined( 'E_DEPRECATED' ) ) {
00574                 // some 3rd party libraries still use deprecated functions
00575                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00576             } else {
00577                 $iErrorReporting = E_ALL ^ E_NOTICE;
00578             }
00579             // A. is it the right place for this code ?
00580             // productive mode ?
00581             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00582                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00583                     $myConfig->setConfigParam( 'iDebug', -1 );
00584                 }
00585             } else {
00586                 // disable error logging if server is misconfigured
00587                 // #2015 E_NONE replaced with 0
00588                 if ( !ini_get( 'log_errors' ) ) {
00589                     $iErrorReporting = 0;
00590                 }
00591             }
00592             error_reporting($iErrorReporting);
00593 
00594 
00595         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00596 
00597             $sTpl = false;
00598             // perform stuff - check if setup is still there
00599             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00600                 $sTpl = 'message/err_setup.tpl';
00601             }
00602 
00603             if ( $sTpl ) {
00604                 $oActView = oxNew( 'oxubase' );
00605                 $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00606                 $oSmarty->assign('oView', $oActView );
00607                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00608                 oxRegistry::getUtils()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00609             }
00610 
00611             oxSession::setVar( 'blRunOnceExecuted', true );
00612         }
00613     }
00614 
00620     protected function _isDebugMode()
00621     {
00622         if ( OxRegistry::get("OxConfigFile")->getVar('iDebug') ) {
00623             return true;
00624         }
00625 
00626         return false;
00627     }
00628 
00629 }