oxshopcontrol.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxShopControl extends oxSuperCfg
00009 {
00015     protected $_dTimeStart = null;
00016 
00022     protected $_dTimeEnd = null;
00023 
00031     protected $_aErrors = null;
00032 
00040     protected $_oOutput = null;
00041 
00042     protected $_oCache = null;
00043 
00054     public function start()
00055     {
00056         $myConfig = $this->getConfig();
00057 
00058         //perform tasks once per session
00059         $this->_runOnce();
00060 
00061         $sClass    = oxConfig::getParameter( 'cl' );
00062         $sFunction = oxConfig::getParameter( 'fnc' );
00063 
00064         if ( !$sClass ) {
00065 
00066             if ( !$this->isAdmin() ) {
00067 
00068                 // first start of the shop
00069                 // check wether we have to display mall startscreen or not
00070                 if ( $myConfig->isMall() ) {
00071 
00072                     $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
00073 
00074                     $sMallShopURL = $myConfig->getConfigParam( 'sMallShopURL' );
00075                     if ( $iShopCount && $iShopCount > 1 && $myConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
00076                         // no class specified so we need to change back to baseshop
00077                         $sClass = 'mallstart';
00078                     }
00079                 }
00080 
00081                 if ( !$sClass ) {
00082                     $sClass = 'start';
00083                 }
00084             } else {
00085                 $sClass = 'login';
00086             }
00087 
00088             oxSession::setVar( 'cl', $sClass );
00089         }
00090 
00091         try {
00092             $this->_process( $sClass, $sFunction );
00093         } catch( oxSystemComponentException $oEx ) {
00094             //possible reason: class does not exist etc. --> just redirect to start page
00095             if ( $this->_isDebugMode() ) {
00096                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00097             }
00098             $oEx->debugOut();
00099             oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start' );
00100         } catch ( oxCookieException $oEx ) {
00101             // redirect to start page and display the error
00102             if ( $this->_isDebugMode() ) {
00103                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00104             }
00105             oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start' );
00106         }
00107     }
00108 
00118     protected function _log( $sClass, $sFnc )
00119     {
00120         $oDb = oxDb::getDb();
00121         $sShopID = oxSession::getVar( 'actshop' );
00122         $sTime   = date( 'Y-m-d H:i:s' );
00123         $sSidQuoted    = $oDb->quote( $this->getSession()->getId() );
00124         $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00125         $sCnid = oxConfig::getParameter( 'cnid' );
00126         $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00127         $sParameter = '';
00128 
00129         if ( $sClass == 'info' ) {
00130             $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00131         } elseif ( $sClass == 'search' ) {
00132             $sParameter = oxConfig::getParameter( 'searchparam' );
00133         }
00134 
00135         $sFncQuoted = $oDb->quote( $sFnc );
00136         $sClassQuoted = $oDb->quote( $sClass );
00137         $sParameterQuoted = $oDb->quote( $sParameter );
00138 
00139         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00140               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, '$sCnid', '$sAnid', $sParameterQuoted )";
00141         $oDb->execute( $sQ );
00142     }
00143 
00144     // OXID : add timing
00150     protected function _startMonitor()
00151     {
00152         if ( $this->_isDebugMode() ) {
00153             $this->_dTimeStart = microtime(true);
00154         }
00155     }
00156 
00167     protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00168     {
00169         if ( $this->_isDebugMode() ) {
00170             $myConfig = $this->getConfig();
00171             /* @var $oDebugInfo oxDebugInfo */
00172             $oDebugInfo = oxNew('oxDebugInfo');
00173 
00174             $blHidden = ($this->getConfig()->getConfigParam( 'iDebug' ) == -1);
00175 
00176             $sLog = '';
00177             $sLogId = md5(time().rand().rand());
00178             $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>";
00179             $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:".($blHidden?'none':'block')."' class='debugInfoBlock' align='left'>";
00180 
00181             // outputting template params
00182             if ( $myConfig->getConfigParam( 'iDebug' ) == 4 ) {
00183                 $sLog .= $oDebugInfo->formatTemplateData($aViewData);
00184             }
00185 
00186             // output timing
00187             $this->_dTimeEnd = microtime(true);
00188 
00189 
00190             $sLog .= $oDebugInfo->formatMemoryUsage();
00191             $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
00192 
00193             if (!isAdmin() && ($iDebug == 7)) {
00194                 $sLog .= $oDebugInfo->formatDbInfo();
00195             }
00196 
00197             if (!isAdmin() && ($iDebug == 2 || $iDebug == 3 || $iDebug == 4)) {
00198                 $sLog .= $oDebugInfo->formatAdoDbPerf();
00199             }
00200 
00201             $sLog .= '</div>';
00202 
00203             $this->_getOutputManager()->output('debuginfo', $sLog);
00204         }
00205     }
00206 
00212     public function getTotalTime()
00213     {
00214         if ($this->_dTimeEnd && $this->_dTimeStart) {
00215             return $this->_dTimeEnd - $this->_dTimeStart;
00216         }
00217 
00218         return 0;
00219     }
00220 
00236     protected function _process( $sClass, $sFunction )
00237     {
00238         startProfile('process');
00239         $myConfig = $this->getConfig();
00240         $myUtils  = oxUtils::getInstance();
00241         $sViewID = null;
00242 
00243         if ( !$myUtils->isSearchEngine() &&
00244              !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00245             $this->_log( $sClass, $sFunction );
00246         }
00247 
00248         // starting resource monitor
00249         $this->_startMonitor();
00250 
00251         // caching params ...
00252         $sOutput      = null;
00253         $blIsCached   = false;
00254 
00255         $oViewObject = $this->_initializeViewObject($sClass, $sFunction);
00256 
00257         // executing user defined function
00258         $oViewObject->executeFunction( $oViewObject->getFncName() );
00259 
00260 
00261         // if no cache was stored before we should generate it
00262         if ( !$blIsCached ) {
00263             $sOutput = $this->_render($oViewObject);
00264         }
00265 
00266 
00267         $oOutput = $this->_getOutputManager();
00268         $oOutput->setCharset($oViewObject->getCharSet());
00269 
00270         if (oxConfig::getParameter('renderPartial')) {
00271             $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
00272             $oOutput->output('errors', $this->_getFormattedErrors());
00273         }
00274 
00275         $oOutput->sendHeaders();
00276 
00277         $oOutput->output('content', $sOutput);
00278 
00279         $myConfig->pageClose();
00280 
00281         stopProfile('process');
00282 
00283         // stopping resource monitor
00284         $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
00285 
00286         // flush output (finalize)
00287         $oOutput->flushOutput();
00288     }
00289 
00298     protected function _initializeViewObject($sClass, $sFunction)
00299     {
00300         $myConfig = $this->getConfig();
00301 
00302         // creating current view object
00303         $oViewObject = oxNew( $sClass );
00304 
00305         // store this call
00306         $oViewObject->setClassName( $sClass );
00307         $oViewObject->setFncName( $sFunction );
00308 
00309         $myConfig->setActiveView( $oViewObject );
00310 
00311 
00312         // init class
00313         $oViewObject->init();
00314 
00315         return $oViewObject;
00316     }
00317 
00318 
00324     protected function _getFormattedErrors()
00325     {
00326         $aErrors = $this->_getErrors();
00327         $aFmtErrors = array();
00328         if ( is_array($aErrors) && count($aErrors) ) {
00329             foreach ( $aErrors as $sLocation => $aEx2 ) {
00330                 foreach ( $aEx2 as $sKey => $oEr ) {
00331                     $oErr = unserialize( $oEr );
00332                     $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
00333                 }
00334             }
00335         }
00336         return $aFmtErrors;
00337     }
00338 
00346     protected function _render($oViewObject)
00347     {
00348         // get Smarty is important here as it sets template directory correct
00349         $oSmarty = oxUtilsView::getInstance()->getSmarty();
00350 
00351         // render it
00352         $sTemplateName = $oViewObject->render();
00353 
00354         // check if template dir exists
00355         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00356         if ( !file_exists( $sTemplateFile)) {
00357             $oEx = oxNew( 'oxSystemComponentException' );
00358             $oLang = oxLang::getInstance();
00359             $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
00360             $oEx->setComponent( $sTemplateName );
00361             throw $oEx;
00362         }
00363         $aViewData = $oViewObject->getViewData();
00364 
00365         // Output processing. This is useful for modules. As sometimes you may want to process output manually.
00366         $oOutput = $this->_getOutputManager();
00367         $aViewData = $oOutput->processViewArray( $aViewData, $oViewObject->getClassName() );
00368         $oViewObject->setViewData( $aViewData );
00369 
00370         //add all exceptions to display
00371         $aErrors = $this->_getErrors();
00372         if ( is_array($aErrors) && count($aErrors) ) {
00373             oxUtilsView::getInstance()->passAllErrorsToView( $aViewData, $aErrors );
00374         }
00375 
00376         foreach ( array_keys( $aViewData ) as $sViewName ) {
00377             $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00378         }
00379 
00380         // passing current view object to smarty
00381         $oSmarty->oxobject = $oViewObject;
00382 
00383 
00384         $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00385 
00386         //Output processing - useful for modules as sometimes you may want to process output manually.
00387         $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00388         return $oOutput->addVersionTags( $sOutput );
00389     }
00390 
00396     protected function _getOutputManager()
00397     {
00398         if (!$this->_oOutput) {
00399             $this->_oOutput = oxNew('oxOutput');
00400         }
00401         return $this->_oOutput;
00402     }
00403 
00415     protected function _output( $sOutput )
00416     {
00417         echo $sOutput;
00418     }
00419 
00425     protected function _getErrors()
00426     {
00427         if (null === $this->_aErrors) {
00428             $this->_aErrors = oxSession::getVar( 'Errors' );
00429             if (null === $this->_aErrors) {
00430                 $this->_aErrors = array();
00431             }
00432             // resetting errors from session
00433             oxSession::setVar( 'Errors', array() );
00434         }
00435         return $this->_aErrors;
00436     }
00437 
00444     protected function _runOnce()
00445     {
00446         $myConfig = $this->getConfig();
00447         $blProductive = true;
00448         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00449 
00450             $iErrorReporting = error_reporting();
00451             if ( version_compare(PHP_VERSION, '5.3.0', '>=') ) {
00452                 // some 3rd party libraries still use deprecated functions
00453                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00454             } else {
00455                 $iErrorReporting = E_ALL ^ E_NOTICE;
00456             }
00457             // A. is it the right place for this code ?
00458             // productive mode ?
00459             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00460                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00461                     $myConfig->setConfigParam( 'iDebug', -1 );
00462                 }
00463             } else {
00464                 // disable error logging if server is misconfigured
00465                 if ( !ini_get( 'log_errors' ) ) {
00466                     $iErrorReporting = E_NONE;
00467                 }
00468             }
00469             error_reporting($iErrorReporting);
00470 
00471 
00472         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00473 
00474             $sTpl = false;
00475             // perform stuff - check if setup is still there
00476             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00477                 $sTpl = 'message/err_setup.tpl';
00478             }
00479 
00480             if ( $sTpl ) {
00481                 $oActView = oxNew( 'oxubase' );
00482                 $oSmarty = oxUtilsView::getInstance()->getSmarty();
00483                 $oSmarty->assign('oView', $oActView );
00484                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00485                 oxUtils::getInstance()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00486             }
00487 
00488             oxSession::setVar( 'blRunOnceExecuted', true );
00489         }
00490     }
00491 
00497     protected function _isDebugMode()
00498     {
00499         if ( !$this->isAdmin() && $this->getConfig()->getConfigParam( 'iDebug' ) ) {
00500             return true;
00501         }
00502 
00503         return false;
00504     }
00505 }