OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxshopcontrol.php
Go to the documentation of this file.
1 <?php
2 
8 class oxShopControl extends oxSuperCfg
9 {
10 
11 
17  protected $_blHandlerSet = null;
18 
24  protected $_blMainTasksExecuted = null;
25 
31  protected $_dTimeStart = null;
32 
38  protected $_dTimeEnd = null;
39 
47  protected $_aErrors = null;
48 
56  protected $_aAllErrors = null;
57 
65  protected $_aControllerErrors = null;
66 
67 
75  protected $_oOutput = null;
76 
82  protected $_oCache = null;
83 
95  public function start($sClass = null, $sFunction = null, $aParams = null, $aViewsChain = null)
96  {
97  //sets default exception handler
99 
100  try {
101  $this->_runOnce();
102 
103  $sFunction = !is_null($sFunction) ? $sFunction : oxRegistry::getConfig()->getRequestParameter('fnc');
104  $sClass = !is_null($sClass) ? $sClass : $this->_getStartController($sClass);
105 
106  $this->_process($sClass, $sFunction, $aParams, $aViewsChain);
107  } catch (oxSystemComponentException $oEx) {
108  $this->_handleSystemException($oEx);
109  } catch (oxCookieException $oEx) {
110  $this->_handleCookieException($oEx);
111 
112  }
113  catch (oxConnectionException $oEx) {
114  $this->_handleDbConnectionException($oEx);
115  } catch (oxException $oEx) {
116  $this->_handleBaseException($oEx);
117  }
118  }
119 
129  protected function _setDefaultExceptionHandler()
130  {
131  if (isset($this->_blHandlerSet)) {
132  return;
133  }
134 
135  set_exception_handler(array(oxNew('oxexceptionhandler', $this->_isDebugMode()), 'handleUncaughtException'));
136  }
137 
147  protected function _log($sClass, $sFnc)
148  {
149  $oDb = oxDb::getDb();
150  $oConfig = $this->getConfig();
151  $oSession = $this->getSession();
152 
153  $sShopID = $oSession->getVariable('actshop');
154  $sTime = date('Y-m-d H:i:s');
155  $sSidQuoted = $oDb->quote($oSession->getId());
156  $sUserIDQuoted = $oDb->quote($oSession->getVariable('usr'));
157 
158  $sCnid = $oConfig->getRequestParameter('cnid');
159  $sAnid = $oConfig->getRequestParameter('aid') ? $oConfig->getRequestParameter('aid') : $oConfig->getRequestParameter('anid');
160 
161  $sParameter = '';
162 
163  if ($sClass == 'content') {
164  $sParameter = str_replace('.tpl', '', $oConfig->getRequestParameter('tpl'));
165  } elseif ($sClass == 'search') {
166  $sParameter = $oConfig->getRequestParameter('searchparam');
167  }
168 
169  $sFncQuoted = $oDb->quote($sFnc);
170  $sClassQuoted = $oDb->quote($sClass);
171  $sParameterQuoted = $oDb->quote($sParameter);
172 
173  $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) " .
174  "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, " . $oDb->quote($sCnid) . ", " . $oDb->quote($sAnid) . ", $sParameterQuoted )";
175 
176  $oDb->execute($sQ);
177  }
178 
179  // OXID : add timing
183  protected function _startMonitor()
184  {
185  if ($this->_isDebugMode()) {
186  $this->_dTimeStart = microtime(true);
187  }
188  }
189 
198  protected function _stopMonitor($blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array())
199  {
200  if ($this->_isDebugMode() && !$this->isAdmin()) {
201  /* @var $oDebugInfo oxDebugInfo */
202  $iDebug = $this->getConfig()->getConfigParam('iDebug');
203  $oDebugInfo = oxNew('oxDebugInfo');
204 
205  $blHidden = ($iDebug == -1);
206 
207  $sLog = '';
208  $sLogId = md5(time() . rand() . rand());
209  $sLog .= "<div id='oxidDebugInfo_$sLogId'>";
210 
211  $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>";
212  $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:" . ($blHidden ? 'none' : 'block') . "' class='debugInfoBlock' align='left'>";
213 
214 
215  // outputting template params
216  if ($iDebug == 4) {
217  $sLog .= $oDebugInfo->formatTemplateData($aViewData);
218  }
219 
220  // output timing
221  $this->_dTimeEnd = microtime(true);
222 
223 
224  $sLog .= $oDebugInfo->formatMemoryUsage();
225 
226  $sLog .= $oDebugInfo->formatTimeStamp();
227 
228 
229  $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
230 
231  // @deprecated since v5.3.0 (2016-06-07).
232  // The SQL logging feature is deprecated. This feature will be removed.
233  if ($iDebug == 7) {
234  $sLog .= $oDebugInfo->formatDbInfo();
235  }
236  // END deprecated
237 
238  // @deprecated since v5.3.0 (2016-06-07).
239  // The SQL performance monitor feature is deprecated. This feature will be removed.
240  if ($iDebug == 2 || $iDebug == 3 || $iDebug == 4) {
241  $sLog .= $oDebugInfo->formatAdoDbPerf();
242  }
243  // END deprecated
244 
245  $sLog .= '</div>';
246 
247  $sLog .= "<script type='text/javascript'>
248  var b = document.getElementById('oxidDebugInfo_$sLogId');
249  var c = document.body;
250  if (c) { c.appendChild(b.parentNode.removeChild(b));}
251  </script>";
252 
253  $sLog .= "</div>";
254 
255  $this->_getOutputManager()->output('debuginfo', $sLog);
256  }
257  }
258 
264  public function getTotalTime()
265  {
266  if ($this->_dTimeEnd && $this->_dTimeStart) {
267  return $this->_dTimeEnd - $this->_dTimeStart;
268  }
269 
270  return 0;
271  }
272 
278  protected function _executeMaintenanceTasks()
279  {
280  if (isset($this->_blMainTasksExecuted)) {
281  return;
282  }
283 
284  startProfile('executeMaintenanceTasks');
285  oxNew("oxArticleList")->updateUpcomingPrices();
286  stopProfile('executeMaintenanceTasks');
287  }
288 
304  protected function _process($sClass, $sFunction, $aParams = null, $aViewsChain = null)
305  {
306  startProfile('process');
307  $myConfig = $this->getConfig();
308 
309  // executing maintenance tasks
310  $this->_executeMaintenanceTasks();
311 
312  $oUtils = oxRegistry::getUtils();
313  $sViewID = null;
314 
315  if (!$oUtils->isSearchEngine() &&
316  !($this->isAdmin() || !$myConfig->getConfigParam('blLogging'))
317  ) {
318  $this->_log($sClass, $sFunction);
319  }
320 
321  // starting resource monitor
322  $this->_startMonitor();
323 
324  // caching params ...
325  $sOutput = null;
326  $blIsCached = false;
327 
328  // Initialize view object and it's components.
329  $oViewObject = $this->_initializeViewObject($sClass, $sFunction, $aParams, $aViewsChain);
330 
331  if (!$this->_canExecuteFunction($oViewObject, $oViewObject->getFncName())) {
333  $oException = oxNew('oxSystemComponentException', 'Non public method cannot be accessed');
334  throw $oException;
335  }
336 
337  // executing user defined function
338  $oViewObject->executeFunction($oViewObject->getFncName());
339 
340 
341 
342  // if no cache was stored before we should generate it
343  if (!$blIsCached) {
344  $sOutput = $this->_render($oViewObject);
345  }
346 
347 
348  $oOutput = $this->_getOutputManager();
349  $oOutput->setCharset($oViewObject->getCharSet());
350 
351  if (oxRegistry::getConfig()->getRequestParameter('renderPartial')) {
352  $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
353  $oOutput->output('errors', $this->_getFormattedErrors($oViewObject->getClassName()));
354  }
355 
356  $oOutput->sendHeaders();
357 
358 
359  $oOutput->output('content', $sOutput);
360 
361  $myConfig->pageClose();
362 
363  stopProfile('process');
364 
365  // stopping resource monitor
366  $this->_stopMonitor($oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData());
367 
368  // flush output (finalize)
369  $oOutput->flushOutput();
370  }
371 
382  protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
383  {
384  $myConfig = $this->getConfig();
385 
386  // creating current view object
387  $oViewObject = oxNew($sClass);
388 
389  // store this call
390  $oViewObject->setClassName($sClass);
391  $oViewObject->setFncName($sFunction);
392  $oViewObject->setViewParameters($aParams);
393 
394  $myConfig->setActiveView($oViewObject);
395 
396 
397  // init class
398  $oViewObject->init();
399 
400  return $oViewObject;
401  }
402 
411  protected function _canExecuteFunction($oClass, $sFunction)
412  {
413  $blCanExecute = true;
414 
415  if (method_exists($oClass, $sFunction)) {
416  $oReflectionMethod = new ReflectionMethod($oClass, $sFunction);
417  if (!$oReflectionMethod->isPublic()) {
418  $blCanExecute = false;
419  }
420  }
421 
422  return $blCanExecute;
423  }
424 
425 
433  protected function _getFormattedErrors($sControllerName)
434  {
435  $aErrors = $this->_getErrors($sControllerName);
436  $aFmtErrors = array();
437  if (is_array($aErrors) && count($aErrors)) {
438  foreach ($aErrors as $sLocation => $aEx2) {
439  foreach ($aEx2 as $sKey => $oEr) {
440  $oErr = unserialize($oEr);
441  $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
442  }
443  }
444  }
445 
446  return $aFmtErrors;
447  }
448 
456  protected function _render($oViewObject)
457  {
458  // get Smarty is important here as it sets template directory correct
459  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
460 
461  // render it
462  $sTemplateName = $oViewObject->render();
463 
464  // check if template dir exists
465  $sTemplateFile = $this->getConfig()->getTemplatePath($sTemplateName, $this->isAdmin());
466  if (!file_exists($sTemplateFile)) {
467 
468  $oEx = oxNew('oxSystemComponentException');
469  $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND');
470  $oEx->setComponent($sTemplateName);
471 
472  $sTemplateName = "message/exception.tpl";
473 
474  if ($this->_isDebugMode()) {
475  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
476  }
477  $oEx->debugOut();
478  }
479 
480  // Output processing. This is useful for modules. As sometimes you may want to process output manually.
481  $oOutput = $this->_getOutputManager();
482  $aViewData = $oOutput->processViewArray($oViewObject->getViewData(), $oViewObject->getClassName());
483  $oViewObject->setViewData($aViewData);
484 
485  //add all exceptions to display
486  $aErrors = $this->_getErrors($oViewObject->getClassName());
487  if (is_array($aErrors) && count($aErrors)) {
488  oxRegistry::get("oxUtilsView")->passAllErrorsToView($aViewData, $aErrors);
489  }
490 
491  foreach (array_keys($aViewData) as $sViewName) {
492  $oSmarty->assign_by_ref($sViewName, $aViewData[$sViewName]);
493  }
494 
495  // passing current view object to smarty
496  $oSmarty->oxobject = $oViewObject;
497 
498 
499  $sOutput = $oSmarty->fetch($sTemplateName, $oViewObject->getViewId());
500 
501  //Output processing - useful for modules as sometimes you may want to process output manually.
502  $sOutput = $oOutput->process($sOutput, $oViewObject->getClassName());
503 
504  return $oOutput->addVersionTags($sOutput);
505  }
506 
512  protected function _getOutputManager()
513  {
514  if (!$this->_oOutput) {
515  $this->_oOutput = oxNew('oxOutput');
516  }
517 
518  return $this->_oOutput;
519  }
520 
528  protected function _getErrors($sCurrentControllerName)
529  {
530  if (null === $this->_aErrors) {
531  $this->_aErrors = oxRegistry::getSession()->getVariable('Errors');
532  $this->_aControllerErrors = oxRegistry::getSession()->getVariable('ErrorController');
533  if (null === $this->_aErrors) {
534  $this->_aErrors = array();
535  }
536  $this->_aAllErrors = $this->_aErrors;
537  }
538  // resetting errors of current controller or widget from session
539  if (is_array($this->_aControllerErrors) && !empty($this->_aControllerErrors)) {
540  foreach ($this->_aControllerErrors as $sErrorName => $sControllerName) {
541  if ($sControllerName == $sCurrentControllerName) {
542  unset($this->_aAllErrors[$sErrorName]);
543  unset($this->_aControllerErrors[$sErrorName]);
544  }
545  }
546  } else {
547  $this->_aAllErrors = array();
548  }
549  oxRegistry::getSession()->setVariable('ErrorController', $this->_aControllerErrors);
550  oxRegistry::getSession()->setVariable('Errors', $this->_aAllErrors);
551 
552  return $this->_aErrors;
553  }
554 
561  protected function _runOnce()
562  {
563  $oConfig = $this->getConfig();
564 
565  error_reporting($this->_getErrorReportingLevel());
566 
567 
568  $blRunOnceExecuted = oxRegistry::getSession()->getVariable('blRunOnceExecuted');
569  if (!$blRunOnceExecuted && !$this->isAdmin() && $oConfig->isProductiveMode()) {
570 
571  // check if setup is still there
572  if (file_exists($oConfig->getConfigParam('sShopDir') . '/setup/index.php')) {
573  $sTpl = 'message/err_setup.tpl';
574  $oActView = oxNew('oxubase');
575  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
576  $oSmarty->assign('oView', $oActView);
577  $oSmarty->assign('oViewConf', $oActView->getViewConfig());
578  oxRegistry::getUtils()->showMessageAndExit($oSmarty->fetch($sTpl));
579  }
580 
581  oxRegistry::getSession()->setVariable('blRunOnceExecuted', true);
582  }
583  }
584 
591  protected function _getErrorReportingLevel()
592  {
593  $sOldReporting = error_reporting();
594 
595  $iErrorReporting = E_ALL ^ E_NOTICE;
596  // some 3rd party libraries still use deprecated functions
597  if (defined('E_DEPRECATED')) {
598  $iErrorReporting = $iErrorReporting ^ E_DEPRECATED;
599  }
600 
601  if ($this->getConfig()->isProductiveMode() && !ini_get('log_errors')) {
602  $iErrorReporting = 0;
603  }
604 
605 
606  return $iErrorReporting;
607  }
608 
614  protected function _isDebugMode()
615  {
616  if (OxRegistry::get("OxConfigFile")->getVar('iDebug')) {
617  return true;
618  }
619 
620  return false;
621  }
622 
623 
624 
631  protected function _handleSystemException($oEx)
632  {
633  $oEx->debugOut();
634 
635  if ($this->_isDebugMode()) {
636  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
637  $this->_process('exceptionError', 'displayExceptionError');
638  } else {
639  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
640  }
641  }
642 
648  protected function _handleCookieException($oEx)
649  {
650  if ($this->_isDebugMode()) {
651  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
652  }
653  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
654  }
655 
661  protected function _handleAccessRightsException($oEx)
662  {
663  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=content&tpl=err_accessdenied.tpl', true, 302);
664  }
665 
673  protected function _handleDbConnectionException($oEx)
674  {
675  $oEx->debugOut();
676 
677  if ($this->_isDebugMode()) {
678  oxRegistry::getUtils()->showMessageAndExit($oEx->getString());
679  }
680  oxRegistry::getUtils()->redirectOffline();
681  }
682 
688  protected function _handleBaseException($oEx)
689  {
690  $oEx->debugOut();
691 
692  if ($this->_isDebugMode()) {
693  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
694  $this->_process('exceptionError', 'displayExceptionError');
695  }
696  }
697 
703  protected function _getStartController()
704  {
705  $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
706 
707  if (!$sClass) {
708  $sClass = $this->isAdmin() ? 'login' : $this->_getFrontendStartController();
709  oxRegistry::getSession()->setVariable('cl', $sClass);
710  }
711 
712  return $sClass;
713  }
714 
721  protected function _getFrontendStartController()
722  {
723  $sClass = 'start';
724  if ($this->getConfig()->isMall()) {
725  $sClass = $this->_getFrontendMallStartController();
726  }
727 
728  return $sClass;
729  }
730 
737  protected function _getFrontendMallStartController()
738  {
739  $iShopCount = oxDb::getDb()->getOne('select count(*) from oxshops where oxactive = 1');
740 
741  $sMallShopURL = $this->getConfig()->getConfigParam('sMallShopURL');
742 
743  $sClass = 'start';
744  if ($iShopCount && $iShopCount > 1 && $this->getConfig()->getConfigParam('iMallMode') != 0 && !$sMallShopURL) {
745  $sClass = 'mallstart';
746  }
747 
748  return $sClass;
749  }
750 }