OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxshopcontrol.php
Go to the documentation of this file.
1 <?php
2 
8 class oxShopControl extends oxSuperCfg
9 {
10 
16  protected $_blHandlerSet = null;
17 
23  protected $_blMainTasksExecuted = null;
24 
30  protected $_dTimeStart = null;
31 
37  protected $_dTimeEnd = null;
38 
46  protected $_aErrors = null;
47 
55  protected $_aAllErrors = null;
56 
64  protected $_aControllerErrors = null;
65 
66 
74  protected $_oOutput = null;
75 
76  protected $_oCache = null;
77 
94  public function start( $sClass = null, $sFunction = null, $aParams = null, $aViewsChain = null )
95  {
96  //sets default exception handler
98  try {
99  //perform tasks once per session
100  $this->_runOnce();
101  $sFunction = ( isset( $sFunction ) ) ? $sFunction : oxRegistry::getConfig()->getRequestParameter( 'fnc' );
102  $sClass = $this->_getControllerToLoad( $sClass );
103  $this->_process( $sClass, $sFunction, $aParams, $aViewsChain );
104  } catch( oxSystemComponentException $oEx ) {
105  $this->_handleSystemException( $oEx );
106  } catch ( oxCookieException $oEx ) {
107  $this->_handleCookieException( $oEx );
108  }
109  catch ( oxConnectionException $oEx) {
110  $this->_handleDbConnectionException( $oEx );
111  }
112  catch ( oxException $oEx) {
113  $this->_handleBaseException( $oEx );
114  }
115  }
116 
123  protected function _setDefaultExceptionHandler()
124  {
125  if (isset($this->_blHandlerSet)) {
126  return;
127  }
128  set_exception_handler(array(oxNew('oxexceptionhandler', $this->_isDebugMode()), 'handleUncaughtException'));
129  }
130 
140  protected function _log( $sClass, $sFnc )
141  {
142  $oDb = oxDb::getDb();
143  $sShopID = oxSession::getVar( 'actshop' );
144  $sTime = date( 'Y-m-d H:i:s' );
145  $sSidQuoted = $oDb->quote( $this->getSession()->getId() );
146  $sUserIDQuoted = $oDb->quote( oxSession::getVar( 'usr' ) );
147  $sCnid = oxConfig::getParameter( 'cnid' );
148  $sAnid = oxConfig::getParameter( 'aid' ) ? oxConfig::getParameter( 'aid' ) : oxConfig::getParameter( 'anid' );
149  $sParameter = '';
150 
151  if ( $sClass == 'content' ) {
152  $sParameter = str_replace( '.tpl', '', oxConfig::getParameter('tpl') );
153  } elseif ( $sClass == 'search' ) {
154  $sParameter = oxConfig::getParameter( 'searchparam' );
155  }
156 
157  $sFncQuoted = $oDb->quote( $sFnc );
158  $sClassQuoted = $oDb->quote( $sClass );
159  $sParameterQuoted = $oDb->quote( $sParameter );
160 
161 
162  $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) ".
163  "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, ".$oDb->quote( $sCnid ).", ".$oDb->quote( $sAnid ).", $sParameterQuoted )";
164 
165  $oDb->execute( $sQ );
166  }
167 
168  // OXID : add timing
174  protected function _startMonitor()
175  {
176  if ( $this->_isDebugMode() ) {
177  $this->_dTimeStart = microtime(true);
178  }
179  }
180 
191  protected function _stopMonitor( $blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array() )
192  {
193  if ( $this->_isDebugMode() && !$this->isAdmin() ) {
194  /* @var $oDebugInfo oxDebugInfo */
195  $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
196  $oDebugInfo = oxNew('oxDebugInfo');
197 
198  $blHidden = ($iDebug == -1);
199 
200  $sLog = '';
201  $sLogId = md5(time().rand().rand());
202  $sLog .= "<div id='oxidDebugInfo_$sLogId'>";
203 
204  $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>";
205  $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:".($blHidden?'none':'block')."' class='debugInfoBlock' align='left'>";
206 
207 
208  // outputting template params
209  if ( $iDebug == 4 ) {
210  $sLog .= $oDebugInfo->formatTemplateData($aViewData);
211  }
212 
213  // output timing
214  $this->_dTimeEnd = microtime(true);
215 
216 
217  $sLog .= $oDebugInfo->formatMemoryUsage();
218 
219  $sLog .= $oDebugInfo->formatTimeStamp();
220 
221 
222  $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
223 
224  if ( $iDebug == 7 ) {
225  $sLog .= $oDebugInfo->formatDbInfo();
226  }
227 
228  if ( $iDebug == 2 || $iDebug == 3 || $iDebug == 4 ) {
229  $sLog .= $oDebugInfo->formatAdoDbPerf();
230  }
231 
232  $sLog .= '</div>';
233 
234  $sLog .= "<script type='text/javascript'>
235  var b = document.getElementById('oxidDebugInfo_$sLogId');
236  var c = document.body;
237  if (c) { c.appendChild(b.parentNode.removeChild(b));}
238  </script>";
239 
240  $sLog .= "</div>";
241 
242  $this->_getOutputManager()->output('debuginfo', $sLog);
243  }
244  }
245 
251  public function getTotalTime()
252  {
253  if ($this->_dTimeEnd && $this->_dTimeStart) {
254  return $this->_dTimeEnd - $this->_dTimeStart;
255  }
256 
257  return 0;
258  }
259 
265  protected function _executeMaintenanceTasks()
266  {
267  if (isset($this->_blMainTasksExecuted)) {
268  return;
269  }
270 
271  startProfile('executeMaintenanceTasks');
272  oxNew("oxArticleList")->updateUpcomingPrices();
273  stopProfile('executeMaintenanceTasks');
274  }
275 
293  protected function _process( $sClass, $sFunction, $aParams = null, $aViewsChain = null )
294  {
295  startProfile('process');
296  $myConfig = $this->getConfig();
297 
298  // executing maintenance tasks
299  $this->_executeMaintenanceTasks();
300 
301  $oUtils = oxRegistry::getUtils();
302  $sViewID = null;
303 
304  if ( !$oUtils->isSearchEngine() &&
305  !( $this->isAdmin() || !$myConfig->getConfigParam( 'blLogging' ) ) ) {
306  $this->_log( $sClass, $sFunction );
307  }
308 
309  // starting resource monitor
310  $this->_startMonitor();
311 
312  // caching params ...
313  $sOutput = null;
314  $blIsCached = false;
315 
316  // Initialize view object and it's components.
317  $oViewObject = $this->_initializeViewObject($sClass, $sFunction, $aParams, $aViewsChain);
318 
319  if ( !$this->_canExecuteFunction( $oViewObject, $oViewObject->getFncName() ) ) {
320  throw oxNew( 'oxSystemComponentException', 'Non public method cannot be accessed' );
321  }
322 
323  // executing user defined function
324  $oViewObject->executeFunction( $oViewObject->getFncName() );
325 
326 
327 
328  // if no cache was stored before we should generate it
329  if ( !$blIsCached ) {
330  $sOutput = $this->_render($oViewObject);
331  }
332 
333 
334  $oOutput = $this->_getOutputManager();
335  $oOutput->setCharset($oViewObject->getCharSet());
336 
337  if (oxConfig::getParameter('renderPartial')) {
338  $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
339  $oOutput->output('errors', $this->_getFormattedErrors( $oViewObject->getClassName() ));
340  }
341 
342  $oOutput->sendHeaders();
343 
344 
345  $oOutput->output('content', $sOutput);
346 
347  $myConfig->pageClose();
348 
349  stopProfile('process');
350 
351  // stopping resource monitor
352  $this->_stopMonitor( $oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData() );
353 
354  // flush output (finalize)
355  $oOutput->flushOutput();
356  }
357 
368  protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
369  {
370  $myConfig = $this->getConfig();
371 
372  // creating current view object
373  $oViewObject = oxNew( $sClass );
374 
375  // store this call
376  $oViewObject->setClassName( $sClass );
377  $oViewObject->setFncName( $sFunction );
378  $oViewObject->setViewParameters( $aParams );
379 
380  $myConfig->setActiveView( $oViewObject );
381 
382 
383  // init class
384  $oViewObject->init();
385 
386  return $oViewObject;
387  }
388 
396  protected function _canExecuteFunction( $oClass, $sFunction )
397  {
398  $blCanExecute = true;
399 
400  if ( method_exists( $oClass, $sFunction ) ) {
401  $oReflectionMethod = new ReflectionMethod( $oClass, $sFunction );
402  if ( !$oReflectionMethod->isPublic() ) {
403  $blCanExecute = false;
404  }
405  }
406  return $blCanExecute;
407  }
408 
409 
417  protected function _getFormattedErrors( $sControllerName )
418  {
419  $aErrors = $this->_getErrors( $sControllerName );
420  $aFmtErrors = array();
421  if ( is_array($aErrors) && count($aErrors) ) {
422  foreach ( $aErrors as $sLocation => $aEx2 ) {
423  foreach ( $aEx2 as $sKey => $oEr ) {
424  $oErr = unserialize( $oEr );
425  $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
426  }
427  }
428  }
429  return $aFmtErrors;
430  }
431 
439  protected function _render($oViewObject)
440  {
441  // get Smarty is important here as it sets template directory correct
442  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
443 
444  // render it
445  $sTemplateName = $oViewObject->render();
446 
447  // check if template dir exists
448  $sTemplateFile = $this->getConfig()->getTemplatePath( $sTemplateName, $this->isAdmin() ) ;
449  if ( !file_exists( $sTemplateFile)) {
450 
451  $oEx = oxNew( 'oxSystemComponentException' );
452  $oEx->setMessage( 'EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND' );
453  $oEx->setComponent( $sTemplateName );
454 
455  $sTemplateName = "message/exception.tpl";
456 
457  if ( $this->_isDebugMode() ) {
458  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
459  }
460  $oEx->debugOut();
461  }
462 
463  // Output processing. This is useful for modules. As sometimes you may want to process output manually.
464  $oOutput = $this->_getOutputManager();
465  $aViewData = $oOutput->processViewArray( $oViewObject->getViewData(), $oViewObject->getClassName() );
466  $oViewObject->setViewData( $aViewData );
467 
468  //add all exceptions to display
469  $aErrors = $this->_getErrors( $oViewObject->getClassName() );
470  if ( is_array($aErrors) && count($aErrors) ) {
471  oxRegistry::get("oxUtilsView")->passAllErrorsToView( $aViewData, $aErrors );
472  }
473 
474  foreach ( array_keys( $aViewData ) as $sViewName ) {
475  $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
476  }
477 
478  // passing current view object to smarty
479  $oSmarty->oxobject = $oViewObject;
480 
481 
482  $sOutput = $oSmarty->fetch( $sTemplateName, $oViewObject->getViewId() );
483 
484  //Output processing - useful for modules as sometimes you may want to process output manually.
485  $sOutput = $oOutput->process( $sOutput, $oViewObject->getClassName() );
486  return $oOutput->addVersionTags( $sOutput );
487  }
488 
494  protected function _getOutputManager()
495  {
496  if (!$this->_oOutput) {
497  $this->_oOutput = oxNew('oxOutput');
498  }
499  return $this->_oOutput;
500  }
501 
509  protected function _getErrors( $sCurrentControllerName )
510  {
511  if ( null === $this->_aErrors ) {
512  $this->_aErrors = oxRegistry::getSession()->getVariable( 'Errors' );
513  $this->_aControllerErrors = oxRegistry::getSession()->getVariable( 'ErrorController' );
514  if ( null === $this->_aErrors ) {
515  $this->_aErrors = array();
516  }
517  $this->_aAllErrors = $this->_aErrors;
518  }
519  // resetting errors of current controller or widget from session
520  if ( is_array($this->_aControllerErrors) && !empty($this->_aControllerErrors) ) {
521  foreach ( $this->_aControllerErrors as $sErrorName => $sControllerName ) {
522  if ( $sControllerName == $sCurrentControllerName ) {
523  unset( $this->_aAllErrors[$sErrorName] );
524  unset( $this->_aControllerErrors[$sErrorName] );
525  }
526  }
527  } else {
528  $this->_aAllErrors = array();
529  }
530  oxRegistry::getSession()->setVariable( 'ErrorController', $this->_aControllerErrors );
531  oxRegistry::getSession()->setVariable( 'Errors', $this->_aAllErrors );
532  return $this->_aErrors;
533  }
534 
541  protected function _runOnce()
542  {
543  $myConfig = $this->getConfig();
544  $blProductive = true;
545  $blRunOnceExecuted = oxSession::getVar( 'blRunOnceExecuted' );
546 
547  $iErrorReporting = error_reporting();
548  if ( defined( 'E_DEPRECATED' ) ) {
549  // some 3rd party libraries still use deprecated functions
550  $iErrorReporting = E_ALL ^ E_NOTICE ^ E_DEPRECATED;
551  } else {
552  $iErrorReporting = E_ALL ^ E_NOTICE;
553  }
554  // A. is it the right place for this code ?
555  // productive mode ?
556  if ( ! ( $blProductive = $myConfig->isProductiveMode() ) ) {
557  if ( is_null($myConfig->getConfigParam( 'iDebug' )) ) {
558  $myConfig->setConfigParam( 'iDebug', -1 );
559  }
560  } else {
561  // disable error logging if server is misconfigured
562  // #2015 E_NONE replaced with 0
563  if ( !ini_get( 'log_errors' ) ) {
564  $iErrorReporting = 0;
565  }
566  }
567  error_reporting($iErrorReporting);
568 
569 
570  if ( !$blRunOnceExecuted && !$this->isAdmin() && $blProductive ) {
571 
572  $sTpl = false;
573  // perform stuff - check if setup is still there
574  if ( file_exists( $myConfig->getConfigParam( 'sShopDir' ) . '/setup/index.php' ) ) {
575  $sTpl = 'message/err_setup.tpl';
576  }
577 
578  if ( $sTpl ) {
579  $oActView = oxNew( 'oxubase' );
580  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
581  $oSmarty->assign('oView', $oActView );
582  $oSmarty->assign('oViewConf', $oActView->getViewConfig() );
583  oxRegistry::getUtils()->showMessageAndExit( $oSmarty->fetch( $sTpl ) );
584  }
585 
586  oxSession::setVar( 'blRunOnceExecuted', true );
587  }
588  }
589 
595  protected function _isDebugMode()
596  {
597  if ( OxRegistry::get("OxConfigFile")->getVar('iDebug') ) {
598  return true;
599  }
600 
601  return false;
602  }
603 
604 
611  protected function _handleSystemException( $oEx )
612  {
613  //possible reason: class does not exist etc. --> just redirect to start page
614  if ( $this->_isDebugMode() ) {
615  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
616  $this->_process( 'exceptionError', 'displayExceptionError' );
617  }
618  $oEx->debugOut();
619 
620  $myConfig = $this->getConfig();
621  if ( !$myConfig->getConfigParam( 'iDebug' ) ) {
622  oxRegistry::getUtils()->redirect( $myConfig->getShopHomeUrl() .'cl=start', true, 302 );
623  }
624  }
625 
631  protected function _handleCookieException( $oEx )
632  {
633  if ( $this->_isDebugMode() ) {
634  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
635  }
636  oxRegistry::getUtils()->redirect( $this->getConfig()->getShopHomeUrl() .'cl=start', true, 302 );
637  }
638 
644  protected function _handleAccessRightsException( $oEx )
645  {
646  oxRegistry::getUtils()->redirect( $this->getConfig()->getShopHomeUrl() .'cl=content&tpl=err_accessdenied.tpl', true, 302 );
647  }
648 
654  protected function _handleDbConnectionException( $oEx )
655  {
656  $oEx->debugOut();
657  if ( $this->_isDebugMode() ) {
658  oxUtils::getInstance()->showMessageAndExit( $oEx->getString() );
659  } else {
660  oxRegistry::getUtils()->redirectOffline();
661  }
662  }
663 
669  protected function _handleBaseException( $oEx )
670  {
671  $oEx->debugOut();
672  if ( $this->_isDebugMode() ) {
673  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
674  $this->_process( 'exceptionError', 'displayExceptionError' );
675  }
676  }
677 
685  protected function _getControllerToLoad( $sClass = null )
686  {
687  $oConfig = $this->getConfig();
688  $sClass = ( isset( $sClass ) ) ? $sClass : oxRegistry::getConfig()->getRequestParameter( 'cl' );
689  if ( !$sClass ) {
690 
691  if ( !$this->isAdmin() ) {
692 
693  // first start of the shop
694  // check wether we have to display mall startscreen or not
695  if ( $oConfig->isMall() ) {
696 
697  $iShopCount = oxDb::getDb()->getOne( 'select count(*) from oxshops where oxactive = 1' );
698 
699  $sMallShopURL = $oConfig->getConfigParam( 'sMallShopURL' );
700  if ( $iShopCount && $iShopCount > 1 && $oConfig->getConfigParam( 'iMallMode' ) != 0 && !$sMallShopURL ) {
701  // no class specified so we need to change back to baseshop
702  $sClass = 'mallstart';
703  }
704  }
705 
706  if ( !$sClass ) {
707  $sClass = 'start';
708  }
709  } else {
710  $sClass = 'login';
711  }
712 
713  oxRegistry::getSession()->setVariable( 'cl', $sClass );
714  }
715 
716  return $sClass;
717  }
718 }