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         try {
00057             //perform tasks once per session
00058             $this->_runOnce();
00059 
00060             $sFunction = oxConfig::getParameter( 'fnc' );
00061             $sClass = $this->_getControllerToLoad();
00062 
00063             $this->_process( $sClass, $sFunction );
00064         } catch( oxSystemComponentException $oEx ) {
00065             $this->_handleSystemException( $oEx );
00066         } catch ( oxCookieException $oEx ) {
00067             $this->_handleCookieException( $oEx );
00068         }
00069         catch ( oxConnectionException $oEx) {
00070             $this->_handleDbConnectionException( $oEx );
00071         }
00072         catch ( oxException $oEx) {
00073             $this->_handleBaseException( $oEx );
00074         }
00075     }
00076 
00086     protected function _log( $sClass, $sFnc )
00087     {
00088         $oDb = oxDb::getDb();
00089         $sShopID = oxSession::getVar( 'actshop' );
00090         $sTime   = date( 'Y-m-d H:i:s' );
00091         $sSidQuoted    = $oDb->quote( $this->getSession()->getId() );
00092         $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00093         $sCnid = oxConfig::getParameter( 'cnid' );
00094         $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00095         $sParameter = '';
00096 
00097         if ( $sClass == 'info' ) {
00098             $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00099         } elseif ( $sClass == 'search' ) {
00100             $sParameter = oxConfig::getParameter( 'searchparam' );
00101         }
00102 
00103         $sFncQuoted = $oDb->quote( $sFnc );
00104         $sClassQuoted = $oDb->quote( $sClass );
00105         $sParameterQuoted = $oDb->quote( $sParameter );
00106 
00107         $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00108               "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, ".$oDb->quote( $sCnid ).", ".$oDb->quote( $sAnid ).", $sParameterQuoted )";
00109         $oDb->execute( $sQ );
00110     }
00111 
00112     // OXID : add timing
00118     protected function _startMonitor()
00119     {
00120         if ( $this->_isDebugMode() ) {
00121             $this->_dTimeStart = microtime(true);
00122         }
00123     }
00124 
00135     protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00136     {
00137         if ( $this->_isDebugMode() && !$this->isAdmin() ) {
00138             /* @var $oDebugInfo oxDebugInfo */
00139             $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00140             $oDebugInfo = oxNew('oxDebugInfo');
00141 
00142             $blHidden = ($iDebug == -1);
00143 
00144             $sLog = '';
00145             $sLogId = md5(time().rand().rand());
00146             $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>";
00147             $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:".($blHidden?'none':'block')."' class='debugInfoBlock' align='left'>";
00148 
00149             // outputting template params
00150             if ( $iDebug == 4 ) {
00151                 $sLog .= $oDebugInfo->formatTemplateData($aViewData);
00152             }
00153 
00154             // output timing
00155             $this->_dTimeEnd = microtime(true);
00156 
00157 
00158             $sLog .= $oDebugInfo->formatMemoryUsage();
00159             $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
00160 
00161             if ( $iDebug == 7 ) {
00162                 $sLog .= $oDebugInfo->formatDbInfo();
00163             }
00164 
00165             if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 ) {
00166                 $sLog .= $oDebugInfo->formatAdoDbPerf();
00167             }
00168 
00169             $sLog .= '</div>';
00170 
00171             $this->_getOutputManager()->output('debuginfo', $sLog);
00172         }
00173     }
00174 
00180     public function getTotalTime()
00181     {
00182         if ($this->_dTimeEnd && $this->_dTimeStart) {
00183             return $this->_dTimeEnd - $this->_dTimeStart;
00184         }
00185 
00186         return 0;
00187     }
00188 
00194     protected function _executeMaintenanceTasks()
00195     {
00196         startProfile('executeMaintenanceTasks');
00197         oxNew("oxarticlelist")->updateUpcomingPrices();
00198         stopProfile('executeMaintenanceTasks');
00199     }
00200 
00216     protected function _process( $sClass, $sFunction )
00217     {
00218         startProfile('process');
00219         $myConfig = $this->getConfig();
00220 
00221         // executing maintenance tasks
00222         $this->_executeMaintenanceTasks();
00223 
00224         $myUtils  = oxUtils::getInstance();
00225         $sViewID = null;
00226 
00227         if ( !$myUtils->isSearchEngine() &&
00228              !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00229             $this->_log( $sClass, $sFunction );
00230         }
00231 
00232         // starting resource monitor
00233         $this->_startMonitor();
00234 
00235         // caching params ...
00236         $sOutput      = null;
00237         $blIsCached   = false;
00238 
00239         $oViewObject = $this->_initializeViewObject($sClass, $sFunction);
00240 
00241         if ( !$this->_canExecuteFunction( $oViewObject, $oViewObject->getFncName() ) ) {
00242             throw oxNew( 'oxSystemComponentException', 'Non public method cannot be accessed' );
00243         }
00244 
00245         // executing user defined function
00246         $oViewObject->executeFunction( $oViewObject->getFncName() );
00247 
00248 
00249         // if no cache was stored before we should generate it
00250         if ( !$blIsCached ) {
00251             $sOutput = $this->_render($oViewObject);
00252         }
00253 
00254 
00255         $oOutput = $this->_getOutputManager();
00256         $oOutput->setCharset($oViewObject->getCharSet());
00257 
00258         if (oxConfig::getParameter('renderPartial')) {
00259             $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
00260             $oOutput->output('errors', $this->_getFormattedErrors());
00261         }
00262 
00263         $oOutput->sendHeaders();
00264 
00265         $oOutput->output('content', $sOutput);
00266 
00267         $myConfig->pageClose();
00268 
00269         stopProfile('process');
00270 
00271         // stopping resource monitor
00272         $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
00273 
00274         // flush output (finalize)
00275         $oOutput->flushOutput();
00276     }
00277 
00286     protected function _initializeViewObject($sClass, $sFunction)
00287     {
00288         $myConfig = $this->getConfig();
00289 
00290         // creating current view object
00291         $oViewObject = oxNew( $sClass );
00292 
00293         // store this call
00294         $oViewObject->setClassName( $sClass );
00295         $oViewObject->setFncName( $sFunction );
00296 
00297         $myConfig->setActiveView( $oViewObject );
00298 
00299 
00300         // init class
00301         $oViewObject->init();
00302 
00303         return $oViewObject;
00304     }
00305 
00313     protected function _canExecuteFunction( $oClass, $sFunction )
00314     {
00315         $blCanExecute = true;
00316 
00317         if ( method_exists( $oClass, $sFunction ) ) {
00318             $oReflectionMethod = new ReflectionMethod( $oClass, $sFunction );
00319             if ( !$oReflectionMethod->isPublic() ) {
00320                 $blCanExecute = false;
00321             }
00322         }
00323         return $blCanExecute;
00324     }
00325 
00326 
00332     protected function _getFormattedErrors()
00333     {
00334         $aErrors = $this->_getErrors();
00335         $aFmtErrors = array();
00336         if ( is_array($aErrors) && count($aErrors) ) {
00337             foreach ( $aErrors as $sLocation => $aEx2 ) {
00338                 foreach ( $aEx2 as $sKey => $oEr ) {
00339                     $oErr = unserialize( $oEr );
00340                     $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
00341                 }
00342             }
00343         }
00344         return $aFmtErrors;
00345     }
00346 
00354     protected function _render($oViewObject)
00355     {
00356         // get Smarty is important here as it sets template directory correct
00357         $oSmarty = oxUtilsView::getInstance()->getSmarty();
00358 
00359         // render it
00360         $sTemplateName = $oViewObject->render();
00361 
00362         // check if template dir exists
00363         $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00364         if ( !file_exists( $sTemplateFile)) {
00365 
00366             $oEx = oxNew( 'oxSystemComponentException' );
00367             $oLang = oxLang::getInstance();
00368             $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
00369             $oEx->setComponent( $sTemplateName );
00370 
00371             $sTemplateName = "message/exception.tpl";
00372 
00373             if ( $this->_isDebugMode() ) {
00374                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00375             }
00376             $oEx->debugOut();
00377         }
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 ( defined( 'E_DEPRECATED' ) ) {
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->getConfig()->getConfigParam( 'iDebug' ) ) {
00517             return true;
00518         }
00519 
00520         return false;
00521     }
00522 
00529     protected function _handleSystemException( $oEx )
00530     {
00531         $oEx->debugOut();
00532 
00533         if ( $this->_isDebugMode() ) {
00534             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00535             $this->_process( 'exceptionError', 'displayExceptionError' );
00536         }
00537         if ( !$this->getConfig()->getConfigParam( 'iDebug' ) ) {
00538             oxUtils::getInstance()->redirect( $this->getConfig()->getShopHomeUrl() .'cl=start', true, 302 );
00539         }
00540     }
00541 
00545     protected function _handleCookieException( $oEx )
00546     {
00547         // redirect to start page and display the error
00548         if ( $this->_isDebugMode() ) {
00549             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00550         }
00551         oxUtils::getInstance()->redirect( $this->getConfig()->getShopHomeUrl() .'cl=start', true, 302 );
00552     }
00553 
00559     protected function _handleAccessRightsException( $oEx )
00560     {
00561         oxUtils::getInstance()->redirect( $this->getConfig()->getShopHomeUrl() . 'cl=content&tpl=err_accessdenied.tpl', true, 302 );
00562     }
00563 
00569     protected function _handleDbConnectionException( $oEx )
00570     {
00571         $oEx->debugOut();
00572         if ( $this->_isDebugMode() ) {
00573             oxUtils::getInstance()->showMessageAndExit( $oEx->getString() );
00574         } else {
00575             header( "HTTP/1.1 500 Internal Server Error");
00576             header( "Location: offline.html");
00577             header( "Connection: close");
00578         }
00579     }
00580 
00586     protected function _handleBaseException( $oEx )
00587     {
00588         $oEx->debugOut();
00589         if ( $this->_isDebugMode() ) {
00590             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00591             $this->_process( 'exceptionError', 'displayExceptionError' );
00592         }
00593     }
00594 
00600     protected function _getControllerToLoad()
00601     {
00602         $oConfig = $this->getConfig();
00603         $sClass = oxConfig::getParameter( 'cl' );
00604 
00605         if ( !$sClass ) {
00606 
00607             if ( !$this->isAdmin() ) {
00608 
00609                 // first start of the shop
00610                 // check wether we have to display mall startscreen or not
00611                 if ( $oConfig->isMall() ) {
00612 
00613                     $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
00614 
00615                     $sMallShopURL = $oConfig->getConfigParam( 'sMallShopURL' );
00616                     if ( $iShopCount && $iShopCount > 1 && $oConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
00617                         // no class specified so we need to change back to baseshop
00618                         $sClass = 'mallstart';
00619                     }
00620                 }
00621 
00622                 if ( !$sClass ) {
00623                     $sClass = 'start';
00624                 }
00625             } else {
00626                 $sClass = 'login';
00627             }
00628 
00629             oxSession::setVar( 'cl', $sClass );
00630         }
00631         return $sClass;
00632     }
00633 }