00001 <?php
00002
00008 class oxShopControl extends oxSuperCfg
00009 {
00015 protected $_dTimeStart = null;
00016
00022 protected $_dTimeEnd = null;
00023
00034 public function start()
00035 {
00036 $myConfig = $this->getConfig();
00037
00038
00039 $this->_runOnce();
00040
00041 $sClass = oxConfig::getParameter( 'cl' );
00042 $sFunction = oxConfig::getParameter( 'fnc' );
00043
00044 if ( !$sClass ) {
00045
00046 if ( !$this->isAdmin() ) {
00047
00048
00049
00050 if ( $myConfig->isMall() ) {
00051
00052 $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
00053
00054 $sMallShopURL = $myConfig->getConfigParam( 'sMallShopURL' );
00055 if ( $iShopCount && $iShopCount > 1 && $myConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
00056
00057 $sClass = 'mallstart';
00058 }
00059 }
00060
00061 if ( !$sClass ) {
00062 $sClass = 'start';
00063 }
00064 } else {
00065 $sClass = 'login';
00066 }
00067
00068 oxSession::setVar( 'cl', $sClass );
00069 }
00070
00071
00072 if ( !oxSession::getVar( 'actshop' ) ) {
00073 oxSession::setVar( 'actshop', $myConfig->getShopId() );
00074 }
00075
00076 try {
00077 $this->_process( $sClass, $sFunction );
00078 } catch( oxSystemComponentException $oEx ) {
00079
00080 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00081 oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start' );
00082 } catch ( oxCookieException $oEx ) {
00083
00084 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00085 oxUtils::getInstance()->redirect( $myConfig->getShopHomeUrl() .'cl=start' );
00086 }
00087 }
00088
00098 protected function _log( $sClass, $sFnc )
00099 {
00100 $oDb = oxDb::getDb();
00101 $sShopID = oxSession::getVar( 'actshop' );
00102 $sTime = date( 'Y-m-d H:i:s' );
00103 $sSidQuoted = $oDb->quote( $this->getSession()->getId() );
00104 $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
00105 $sCnid = oxConfig::getParameter( 'cnid' );
00106 $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
00107 $sParameter = '';
00108
00109 if ( $sClass == 'info' ) {
00110 $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
00111 } elseif ( $sClass == 'search' ) {
00112 $sParameter = oxConfig::getParameter( 'searchparam' );
00113 }
00114
00115 $sFncQuoted = $oDb->quote( $sFnc );
00116 $sClassQuoted = $oDb->quote( $sClass );
00117 $sParameterQuoted = $oDb->quote( $sParameter );
00118
00119 $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
00120 "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, '$sCnid', '$sAnid', $sParameterQuoted )";
00121 $oDb->execute( $sQ );
00122 }
00123
00124
00130 protected function _startMonitor()
00131 {
00132 if ( !$this->isAdmin() && $this->getConfig()->getConfigParam( 'iDebug' ) ) {
00133 list ( $sUsec, $sSec ) = explode( ' ', microtime() );
00134 $this->_dTimeStart = ( ( float ) $sUsec + ( float ) $sSec );
00135 }
00136 }
00137
00148 protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
00149 {
00150 $myConfig = $this->getConfig();
00151 if ( !$this->isAdmin() && $myConfig->getConfigParam( 'iDebug' ) != 0 ) {
00152 $sLog = '<div align="left">';
00153
00154
00155 if ( $myConfig->getConfigParam( 'iDebug' ) == 4 ) {
00156
00157 reset( $aViewData );
00158 while ( list( $sViewName, $oViewData ) = each( $aViewData ) ) {
00159
00160 $sLog .= "TemplateData[$sViewName] : <br />\n";
00161 print_r( $oViewData );
00162 }
00163 }
00164
00165
00166 list( $sUsec, $sSec ) = explode( ' ', microtime() );
00167 $this->_dTimeEnd = ( ( float ) $sUsec + ( float ) $sSec );
00168
00169 $dTimeSpent = ((int)( $this->_dTimeEnd * 10000 - $this->_dTimeStart * 10000 ) ) / 10000;
00170
00171 $sLog .= 'Execution time :'.$dTimeSpent.'<br />';
00172
00173
00174 if ( function_exists( 'memory_get_usage' ) ) {
00175 $iKb = ( int ) ( memory_get_usage() / 1024 );
00176 $iMb = round($iKb / 1024, 3);
00177 $sLog .= 'Memory usage: '.$iMb.' MB';
00178
00179 if ( function_exists( 'memory_get_peak_usage' ) ) {
00180 $iPeakKb = ( int ) ( memory_get_peak_usage() / 1024 );
00181 $iPeakMb = round($iPeakKb / 1024, 3);
00182 $sLog .= ' (peak: '.$iPeakMb.' MB)';
00183 }
00184 $sLog .= '<br />';
00185
00186 if ( version_compare( PHP_VERSION, '5.2.0', '>=' ) ) {
00187 $iKb = ( int ) ( memory_get_usage( true ) / 1024 );
00188 $iMb = round($iKb / 1024, 3);
00189 $sLog .= 'System memory usage: '.$iMb.' MB';
00190
00191 if ( function_exists( 'memory_get_peak_usage' ) ) {
00192 $iPeakKb = ( int ) ( memory_get_peak_usage( true ) / 1024 );
00193 $iPeakMb = round($iPeakKb / 1024, 3);
00194 $sLog .= ' (peak: '.$iPeakMb.' MB)';
00195 }
00196 $sLog .= '<br />';
00197 }
00198 }
00199
00200 $sLog .= '</div>';
00201 $this->_output( $sLog );
00202 }
00203 }
00204
00210 public function getTotalTime()
00211 {
00212 if ($this->_dTimeEnd && $this->_dTimeStart) {
00213 return $this->_dTimeEnd - $this->_dTimeStart;
00214 }
00215
00216 return 0;
00217 }
00218
00234 protected function _process( $sClass, $sFunction )
00235 {
00236 $myConfig = $this->getConfig();
00237 $myUtils = oxUtils::getInstance();
00238 $sViewID = null;
00239
00240 if ( !$myUtils->isSearchEngine() &&
00241 !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
00242 $this->_log( $sClass, $sFunction );
00243 }
00244
00245
00246 $this->_startMonitor();
00247
00248
00249 $oViewObject = oxNew( $sClass );
00250
00251
00252 $oViewObject->setClassName( $sClass );
00253 $oViewObject->setFncName( $sFunction );
00254
00255 $myConfig->setActiveView( $oViewObject );
00256
00257
00258 $sOutput = null;
00259 $blIsCached = false;
00260 $blIsCachable = false;
00261
00262
00263
00264 $oViewObject->init();
00265
00266
00267 $oViewObject->executeFunction( $oViewObject->getFncName() );
00268
00269
00270
00271 $oSmarty = oxUtilsView::getInstance()->getSmarty();
00272
00273
00274 if ( !$blIsCached ) {
00275
00276
00277 $sTemplateName = $oViewObject->render();
00278
00279
00280 $sTemplateFile = $myConfig->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
00281 if ( !file_exists( $sTemplateFile)) {
00282 $oEx = oxNew( 'oxSystemComponentException' );
00283 $oLang = oxLang::getInstance();
00284 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND', $oLang->getBaseLanguage() ), $sTemplateFile) );
00285 $oEx->setComponent( $sTemplateName );
00286 throw $oEx;
00287 }
00288 $aViewData = $oViewObject->getViewData();
00289
00290
00291 $oOutput = oxNew( 'oxoutput' );
00292 $aViewData = $oOutput->processViewArray( $aViewData, $oViewObject->getClassName() );
00293 $oViewObject->setViewData( $aViewData );
00294
00295
00296 if ( ( $aErrors = oxSession::getVar( 'Errors' ) ) ) {
00297 oxUtilsView::getInstance()->passAllErrorsToView( $aViewData, $aErrors );
00298
00299
00300 oxSession::setVar( 'Errors', array() );
00301 }
00302
00303 foreach ( array_keys( $aViewData ) as $sViewName ) {
00304 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00305 }
00306
00307
00308 $oSmarty->oxobject = $oViewObject;
00309
00310
00311 $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
00312
00313
00314 $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
00315 $sOutput = $oOutput->addVersionTags( $sOutput );
00316 }
00317
00318
00319
00320 $myUtils->setHeader( "Content-Type: text/html; charset=".oxLang::getInstance()->translateString( 'charset' ) );
00321
00322
00323 $this->_output( $sOutput );
00324
00325 $myConfig->pageClose();
00326
00327
00328 $this->_stopMonitor( $blIsCachable, $blIsCached, $sViewID, $oViewObject->getViewData() );
00329 }
00330
00338 protected function _output( $sOutput )
00339 {
00340 echo $sOutput;
00341 }
00342
00349 protected function _runOnce()
00350 {
00351 $myConfig = $this->getConfig();
00352 $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
00353 $blProductive = true;
00354
00355
00356
00357
00358 if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
00359 if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
00360 $myConfig->setConfigParam( 'iDebug', -1 );
00361 }
00362
00363 error_reporting( E_ALL ^ E_NOTICE );
00364 } else {
00365
00366
00367 if ( !ini_get( 'log_errors' ) ) {
00368 error_reporting( E_NONE );
00369 } else {
00370 error_reporting( E_ALL ^ E_NOTICE );
00371 }
00372 }
00373
00374
00375
00376 if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
00377
00378 $sTpl = false;
00379
00380 if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
00381 $sTpl = 'err_setup.tpl';
00382 } elseif ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/updateApp' ) ) {
00383 $sTpl = 'err_update.tpl';
00384 }
00385
00386 if ( $sTpl ) {
00387 $oActView = oxNew( 'oxubase' );
00388 $oSmarty = oxUtilsView::getInstance()->getSmarty();
00389 $oSmarty->assign('oView', $oActView );
00390 $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
00391 oxUtils::getInstance()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
00392 }
00393
00394 oxSession::setVar( 'blRunOnceExecuted', true );
00395 }
00396 }
00397 }