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                 $this->_process( 'exceptionError', 'displayExceptionError' );
00098             }
00099             $oEx->debugOut();
00100 
00101             if ( !$myConfig->getConfigParam( 'iDebug' ) ) {
00102                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00103             }
00104         } catch ( oxCookieException $oEx ) {
00105             // redirect to start page and display the error
00106             if ( $this->_isDebugMode() ) {
00107                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00108             }
00109             oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
00110         }
00111 
00112         catch ( oxException $oEx) {
00113             //catching other not cought exceptions
00114             if ( $this->_isDebugMode() ) {
00115                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00116                 $this->_process( 'exceptionError', 'displayExceptionError' );
00117                 $oEx->debugOut();
00118             }
00119         }
00120 
00121     }
00122 
00132     protected function _log( $sClass, $sFnc )
00133     {
00134         $oDb = oxDb::getDb();
00135         $sShopID = oxSession::getVar( 'actshop' );
00136         $sTime   = date( 'Y-m-d H:i:s' );
00137         $sSidQuoted    = $oDb->quote( $this->getSession()->getId() );
00138         $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00139         $sCnid = oxConfig::getParameter( 'cnid' );
00140         $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00141         $sParameter = '';
00142 
00143         if ( $sClass == 'info' ) {
00144             $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00145         } elseif ( $sClass == 'search' ) {
00146             $sParameter = oxConfig::getParameter( 'searchparam' );
00147         }
00148 
00149         $sFncQuoted = $oDb->quote( $sFnc );
00150         $sClassQuoted = $oDb->quote( $sClass );
00151         $sParameterQuoted = $oDb->quote( $sParameter );
00152 
00153         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00154               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, ".$oDb->quote( $sCnid ).", ".$oDb->quote( $sAnid ).", $sParameterQuoted )";
00155         $oDb->execute( $sQ );
00156     }
00157 
00158     // OXID : add timing
00164     protected function _startMonitor()
00165     {
00166         if ( $this->_isDebugMode() ) {
00167             $this->_dTimeStart = microtime(true);
00168         }
00169     }
00170 
00181     protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00182     {
00183         if ( $this->_isDebugMode() ) {
00184             $myConfig = $this->getConfig();
00185             /* @var $oDebugInfo oxDebugInfo */
00186             $oDebugInfo = oxNew('oxDebugInfo');
00187 
00188             $blHidden = ($this->getConfig()->getConfigParam( 'iDebug' ) == -1);
00189 
00190             $sLog = '';
00191             $sLogId = md5(time().rand().rand());
00192             $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>";
00193             $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:".($blHidden?'none':'block')."' class='debugInfoBlock' align='left'>";
00194 
00195             // outputting template params
00196             if ( $myConfig->getConfigParam( 'iDebug' ) == 4 ) {
00197                 $sLog .= $oDebugInfo->formatTemplateData($aViewData);
00198             }
00199 
00200             // output timing
00201             $this->_dTimeEnd = microtime(true);
00202 
00203 
00204             $sLog .= $oDebugInfo->formatMemoryUsage();
00205             $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
00206 
00207             if (!isAdmin() && ($iDebug == 7)) {
00208                 $sLog .= $oDebugInfo->formatDbInfo();
00209             }
00210 
00211             if (!isAdmin() && ($iDebug == 2 || $iDebug == 3 || $iDebug == 4)) {
00212                 $sLog .= $oDebugInfo->formatAdoDbPerf();
00213             }
00214 
00215             $sLog .= '</div>';
00216 
00217             $this->_getOutputManager()->output('debuginfo', $sLog);
00218         }
00219     }
00220 
00226     public function getTotalTime()
00227     {
00228         if ($this->_dTimeEnd && $this->_dTimeStart) {
00229             return $this->_dTimeEnd - $this->_dTimeStart;
00230         }
00231 
00232         return 0;
00233     }
00234 
00250     protected function _process( $sClass, $sFunction )
00251     {
00252         startProfile('process');
00253         $myConfig = $this->getConfig();
00254         $myUtils  = oxUtils::getInstance();
00255         $sViewID = null;
00256 
00257         if ( !$myUtils->isSearchEngine() &&
00258              !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00259             $this->_log( $sClass, $sFunction );
00260         }
00261 
00262         // starting resource monitor
00263         $this->_startMonitor();
00264 
00265         // caching params ...
00266         $sOutput      = null;
00267         $blIsCached   = false;
00268 
00269         $oViewObject = $this->_initializeViewObject($sClass, $sFunction);
00270 
00271         // executing user defined function
00272         $oViewObject->executeFunction( $oViewObject->getFncName() );
00273 
00274 
00275         // if no cache was stored before we should generate it
00276         if ( !$blIsCached ) {
00277             $sOutput = $this->_render($oViewObject);
00278         }
00279 
00280 
00281         $oOutput = $this->_getOutputManager();
00282         $oOutput->setCharset($oViewObject->getCharSet());
00283 
00284         if (oxConfig::getParameter('renderPartial')) {
00285             $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
00286             $oOutput->output('errors', $this->_getFormattedErrors());
00287         }
00288 
00289         $oOutput->sendHeaders();
00290 
00291         $oOutput->output('content', $sOutput);
00292 
00293         $myConfig->pageClose();
00294 
00295         stopProfile('process');
00296 
00297         // stopping resource monitor
00298         $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
00299 
00300         // flush output (finalize)
00301         $oOutput->flushOutput();
00302     }
00303 
00312     protected function _initializeViewObject($sClass, $sFunction)
00313     {
00314         $myConfig = $this->getConfig();
00315 
00316         // creating current view object
00317         $oViewObject = oxNew( $sClass );
00318 
00319         // store this call
00320         $oViewObject->setClassName( $sClass );
00321         $oViewObject->setFncName( $sFunction );
00322 
00323         $myConfig->setActiveView( $oViewObject );
00324 
00325 
00326         // init class
00327         $oViewObject->init();
00328 
00329         return $oViewObject;
00330     }
00331 
00332 
00338     protected function _getFormattedErrors()
00339     {
00340         $aErrors = $this->_getErrors();
00341         $aFmtErrors = array();
00342         if ( is_array($aErrors) && count($aErrors) ) {
00343             foreach ( $aErrors as $sLocation => $aEx2 ) {
00344                 foreach ( $aEx2 as $sKey => $oEr ) {
00345                     $oErr = unserialize( $oEr );
00346                     $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
00347                 }
00348             }
00349         }
00350         return $aFmtErrors;
00351     }
00352 
00360     protected function _render($oViewObject)
00361     {
00362         // get Smarty is important here as it sets template directory correct
00363         $oSmarty = oxUtilsView::getInstance()->getSmarty();
00364 
00365         // render it
00366         $sTemplateName = $oViewObject->render();
00367 
00368         // check if template dir exists
00369         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00370         if ( !file_exists( $sTemplateFile)) {
00371             $oEx = oxNew( 'oxSystemComponentException' );
00372             $oLang = oxLang::getInstance();
00373             $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
00374             $oEx->setComponent( $sTemplateName );
00375             throw $oEx;
00376         }
00377         $aViewData = $oViewObject->getViewData();
00378 
00379         // Output processing. This is useful for modules. As sometimes you may want to process output manually.
00380         $oOutput = $this->_getOutputManager();
00381         $aViewData = $oOutput->processViewArray( $aViewData, $oViewObject->getClassName() );
00382         $oViewObject->setViewData( $aViewData );
00383 
00384         //add all exceptions to display
00385         $aErrors = $this->_getErrors();
00386         if ( is_array($aErrors) && count($aErrors) ) {
00387             oxUtilsView::getInstance()->passAllErrorsToView( $aViewData, $aErrors );
00388         }
00389 
00390         foreach ( array_keys( $aViewData ) as $sViewName ) {
00391             $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00392         }
00393 
00394         // passing current view object to smarty
00395         $oSmarty->oxobject = $oViewObject;
00396 
00397 
00398         $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00399 
00400         //Output processing - useful for modules as sometimes you may want to process output manually.
00401         $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00402         return $oOutput->addVersionTags( $sOutput );
00403     }
00404 
00410     protected function _getOutputManager()
00411     {
00412         if (!$this->_oOutput) {
00413             $this->_oOutput = oxNew('oxOutput');
00414         }
00415         return $this->_oOutput;
00416     }
00417 
00429     protected function _output( $sOutput )
00430     {
00431         echo $sOutput;
00432     }
00433 
00439     protected function _getErrors()
00440     {
00441         if (null === $this->_aErrors) {
00442             $this->_aErrors = oxSession::getVar( 'Errors' );
00443             if (null === $this->_aErrors) {
00444                 $this->_aErrors = array();
00445             }
00446             // resetting errors from session
00447             oxSession::setVar( 'Errors', array() );
00448         }
00449         return $this->_aErrors;
00450     }
00451 
00458     protected function _runOnce()
00459     {
00460         $myConfig = $this->getConfig();
00461         $blProductive = true;
00462         $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00463 
00464             $iErrorReporting = error_reporting();
00465             if ( version_compare(PHP_VERSION, '5.3.0', '>=') ) {
00466                 // some 3rd party libraries still use deprecated functions
00467                 $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
00468             } else {
00469                 $iErrorReporting = E_ALL ^ E_NOTICE;
00470             }
00471             // A. is it the right place for this code ?
00472             // productive mode ?
00473             if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00474                 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00475                     $myConfig->setConfigParam( 'iDebug', -1 );
00476                 }
00477             } else {
00478                 // disable error logging if server is misconfigured
00479                 if ( !ini_get( 'log_errors' ) ) {
00480                     $iErrorReporting = E_NONE;
00481                 }
00482             }
00483             error_reporting($iErrorReporting);
00484 
00485 
00486         if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00487 
00488             $sTpl = false;
00489             // perform stuff - check if setup is still there
00490             if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00491                 $sTpl = 'message/err_setup.tpl';
00492             }
00493 
00494             if ( $sTpl ) {
00495                 $oActView = oxNew( 'oxubase' );
00496                 $oSmarty = oxUtilsView::getInstance()->getSmarty();
00497                 $oSmarty->assign('oView', $oActView );
00498                 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00499                 oxUtils::getInstance()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00500             }
00501 
00502             oxSession::setVar( 'blRunOnceExecuted', true );
00503         }
00504     }
00505 
00511     protected function _isDebugMode()
00512     {
00513         if ( !$this->isAdmin() && $this->getConfig()->getConfigParam( 'iDebug' ) ) {
00514             return true;
00515         }
00516 
00517         return false;
00518     }
00519 }