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