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