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 whether we have to display mall start screen 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 
00388        $oOutput->sendHeaders();
00389 
00390 
00391         $oOutput->output('content', $sOutput);
00392 
00393         $myConfig->pageClose();
00394 
00395         stopProfile('process');
00396 
00397         // stopping resource monitor
00398         $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
00399 
00400         // flush output (finalize)
00401         $oOutput->flushOutput();
00402     }
00403 
00414     protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
00415     {
00416         $myConfig = $this->getConfig();
00417 
00418         // creating current view object
00419         $oViewObject = oxNew( $sClass );
00420 
00421         // store this call
00422         $oViewObject->setClassName( $sClass );
00423         $oViewObject->setFncName( $sFunction );
00424         $oViewObject->setViewParameters( $aParams );
00425 
00426         $myConfig->setActiveView( $oViewObject );
00427 
00428 
00429         // init class
00430         $oViewObject->init();
00431 
00432         return $oViewObject;
00433     }
00434 
00435 
00443     protected function _getFormattedErrors( $sControllerName )
00444     {
00445         $aErrors = $this->_getErrors( $sControllerName );
00446         $aFmtErrors = array();
00447         if ( is_array($aErrors) && count($aErrors) ) {
00448             foreach ( $aErrors as $sLocation => $aEx2 ) {
00449                 foreach ( $aEx2 as $sKey => $oEr ) {
00450                     $oErr = unserialize( $oEr );
00451                     $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
00452                 }
00453             }
00454         }
00455         return $aFmtErrors;
00456     }
00457 
00465     protected function _render($oViewObject)
00466     {
00467         // get Smarty is important here as it sets template directory correct
00468         $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00469 
00470         // render it
00471         $sTemplateName = $oViewObject->render();
00472 
00473         // check if template dir exists
00474         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00475         if ( !file_exists( $sTemplateFile)) {
00476 
00477             $oEx = oxNew( 'oxSystemComponentException' );
00478             $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
00479             $oEx->setComponent( $sTemplateName );
00480 
00481             $sTemplateName = "message/exception.tpl";
00482 
00483             if ( $this->_isDebugMode() ) {
00484                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00485             }
00486             $oEx->debugOut();
00487         }
00488 
00489         // Output processing. This is useful for modules. As sometimes you may want to process output manually.
00490         $oOutput = $this->_getOutputManager();
00491         $aViewData = $oOutput->processViewArray( $oViewObject->getViewData(), $oViewObject->getClassName() );
00492         $oViewObject->setViewData( $aViewData );
00493 
00494         //add all exceptions to display
00495         $aErrors = $this->_getErrors( $oViewObject->getClassName() );
00496         if ( is_array($aErrors) && count($aErrors) ) {
00497             oxRegistry::get("oxUtilsView")->passAllErrorsToView( $aViewData, $aErrors );
00498         }
00499 
00500         foreach ( array_keys( $aViewData ) as $sViewName ) {
00501             $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00502         }
00503 
00504         // passing current view object to smarty
00505         $oSmarty->oxobject = $oViewObject;
00506 
00507 
00508         $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00509 
00510         //Output processing - useful for modules as sometimes you may want to process output manually.
00511         $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00512         return $oOutput->addVersionTags( $sOutput );
00513     }
00514 
00520     protected function _getOutputManager()
00521     {
00522         if (!$this->_oOutput) {
00523             $this->_oOutput = oxNew('oxOutput');
00524         }
00525         return $this->_oOutput;
00526     }
00527 
00535     protected function _getErrors( $sCurrentControllerName )
00536     {
00537         if ( null === $this->_aErrors ) {
00538             $this->_aErrors = oxRegistry::getSession()->getVariable( 'Errors' );
00539             $this->_aControllerErrors = oxRegistry::getSession()->getVariable( 'ErrorController' );
00540             if ( null === $this->_aErrors ) {
00541                 $this->_aErrors = array();
00542             }
00543             $this->_aAllErrors = $this->_aErrors;
00544         }
00545         // resetting errors of current controller or widget from session
00546         if ( is_array($this->_aControllerErrors) && !empty($this->_aControllerErrors) ) {
00547             foreach ( $this->_aControllerErrors as $sErrorName => $sControllerName ) {
00548                 if ( $sControllerName == $sCurrentControllerName ) {
00549                     unset( $this->_aAllErrors[$sErrorName] );
00550                     unset( $this->_aControllerErrors[$sErrorName] );
00551                 }
00552             }
00553         } else {
00554             $this->_aAllErrors = array();
00555         }
00556         oxRegistry::getSession()->setVariable( 'ErrorController', $this->_aControllerErrors );
00557         oxRegistry::getSession()->setVariable( 'Errors', $this->_aAllErrors );
00558         return $this->_aErrors;
00559     }
00560 
00567     protected function _runOnce()
00568     {
00569         $myConfig = $this->getConfig();
00570         $blProductive = true;
00571         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00572 
00573             $iErrorReporting = error_reporting();
00574             if ( defined( 'E_DEPRECATED' ) ) {
00575                 // some 3rd party libraries still use deprecated functions
00576                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00577             } else {
00578                 $iErrorReporting = E_ALL ^ E_NOTICE;
00579             }
00580             // A. is it the right place for this code ?
00581             // productive mode ?
00582             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00583                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00584                     $myConfig->setConfigParam( 'iDebug', -1 );
00585                 }
00586             } else {
00587                 // disable error logging if server is misconfigured
00588                 // #2015 E_NONE replaced with 0
00589                 if ( !ini_get( 'log_errors' ) ) {
00590                     $iErrorReporting = 0;
00591                 }
00592             }
00593             error_reporting($iErrorReporting);
00594 
00595 
00596         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00597 
00598             $sTpl = false;
00599             // perform stuff - check if setup is still there
00600             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00601                 $sTpl = 'message/err_setup.tpl';
00602             }
00603 
00604             if ( $sTpl ) {
00605                 $oActView = oxNew( 'oxubase' );
00606                 $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
00607                 $oSmarty->assign('oView', $oActView );
00608                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00609                 oxRegistry::getUtils()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00610             }
00611 
00612             oxSession::setVar( 'blRunOnceExecuted', true );
00613         }
00614     }
00615 
00621     protected function _isDebugMode()
00622     {
00623         if ( OxRegistry::get("OxConfigFile")->getVar('iDebug') ) {
00624             return true;
00625         }
00626 
00627         return false;
00628     }
00629 
00630 }