OXID eShop CE  4.10.2
 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 
126  protected function _setDefaultExceptionHandler()
127  {
128  if (isset($this->_blHandlerSet)) {
129  return;
130  }
131 
132  set_exception_handler(array(oxNew('oxexceptionhandler', $this->_isDebugMode()), 'handleUncaughtException'));
133  }
134 
144  protected function _log($sClass, $sFnc)
145  {
146  $oDb = oxDb::getDb();
147  $oConfig = $this->getConfig();
148  $oSession = $this->getSession();
149 
150  $sShopID = $oSession->getVariable('actshop');
151  $sTime = date('Y-m-d H:i:s');
152  $sSidQuoted = $oDb->quote($oSession->getId());
153  $sUserIDQuoted = $oDb->quote($oSession->getVariable('usr'));
154 
155  $sCnid = $oConfig->getRequestParameter('cnid');
156  $sAnid = $oConfig->getRequestParameter('aid') ? $oConfig->getRequestParameter('aid') : $oConfig->getRequestParameter('anid');
157 
158  $sParameter = '';
159 
160  if ($sClass == 'content') {
161  $sParameter = str_replace('.tpl', '', $oConfig->getRequestParameter('tpl'));
162  } elseif ($sClass == 'search') {
163  $sParameter = $oConfig->getRequestParameter('searchparam');
164  }
165 
166  $sFncQuoted = $oDb->quote($sFnc);
167  $sClassQuoted = $oDb->quote($sClass);
168  $sParameterQuoted = $oDb->quote($sParameter);
169 
170  $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) " .
171  "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, " . $oDb->quote($sCnid) . ", " . $oDb->quote($sAnid) . ", $sParameterQuoted )";
172 
173  $oDb->execute($sQ);
174  }
175 
176  // OXID : add timing
180  protected function _startMonitor()
181  {
182  if ($this->_isDebugMode()) {
183  $this->_dTimeStart = microtime(true);
184  }
185  }
186 
195  protected function _stopMonitor($blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array())
196  {
197  if ($this->_isDebugMode() && !$this->isAdmin()) {
198  /* @var $oDebugInfo oxDebugInfo */
199  $iDebug = $this->getConfig()->getConfigParam('iDebug');
200  $oDebugInfo = oxNew('oxDebugInfo');
201 
202  $blHidden = ($iDebug == -1);
203 
204  $sLog = '';
205  $sLogId = md5(time() . rand() . rand());
206  $sLog .= "<div id='oxidDebugInfo_$sLogId'>";
207 
208  $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>";
209  $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:" . ($blHidden ? 'none' : 'block') . "' class='debugInfoBlock' align='left'>";
210 
211 
212  // outputting template params
213  if ($iDebug == 4) {
214  $sLog .= $oDebugInfo->formatTemplateData($aViewData);
215  }
216 
217  // output timing
218  $this->_dTimeEnd = microtime(true);
219 
220 
221  $sLog .= $oDebugInfo->formatMemoryUsage();
222 
223  $sLog .= $oDebugInfo->formatTimeStamp();
224 
225 
226  $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
227 
228  // @deprecated since v5.3.0 (2016-06-07).
229  // The SQL logging feature is deprecated. This feature will be removed.
230  if ($iDebug == 7) {
231  $sLog .= $oDebugInfo->formatDbInfo();
232  }
233  // END deprecated
234 
235  // @deprecated since v5.3.0 (2016-06-07).
236  // The SQL performance monitor feature is deprecated. This feature will be removed.
237  if ($iDebug == 2 || $iDebug == 3 || $iDebug == 4) {
238  $sLog .= $oDebugInfo->formatAdoDbPerf();
239  }
240  // END deprecated
241 
242  $sLog .= '</div>';
243 
244  $sLog .= "<script type='text/javascript'>
245  var b = document.getElementById('oxidDebugInfo_$sLogId');
246  var c = document.body;
247  if (c) { c.appendChild(b.parentNode.removeChild(b));}
248  </script>";
249 
250  $sLog .= "</div>";
251 
252  $this->_getOutputManager()->output('debuginfo', $sLog);
253  }
254  }
255 
261  public function getTotalTime()
262  {
263  if ($this->_dTimeEnd && $this->_dTimeStart) {
264  return $this->_dTimeEnd - $this->_dTimeStart;
265  }
266 
267  return 0;
268  }
269 
275  protected function _executeMaintenanceTasks()
276  {
277  if (isset($this->_blMainTasksExecuted)) {
278  return;
279  }
280 
281  startProfile('executeMaintenanceTasks');
282  oxNew("oxArticleList")->updateUpcomingPrices();
283  stopProfile('executeMaintenanceTasks');
284  }
285 
301  protected function _process($sClass, $sFunction, $aParams = null, $aViewsChain = null)
302  {
303  startProfile('process');
304  $myConfig = $this->getConfig();
305 
306  // executing maintenance tasks
307  $this->_executeMaintenanceTasks();
308 
309  $oUtils = oxRegistry::getUtils();
310  $sViewID = null;
311 
312  if (!$oUtils->isSearchEngine() &&
313  !($this->isAdmin() || !$myConfig->getConfigParam('blLogging'))
314  ) {
315  $this->_log($sClass, $sFunction);
316  }
317 
318  // starting resource monitor
319  $this->_startMonitor();
320 
321  // caching params ...
322  $sOutput = null;
323  $blIsCached = false;
324 
325  // Initialize view object and it's components.
326  $oViewObject = $this->_initializeViewObject($sClass, $sFunction, $aParams, $aViewsChain);
327 
328  if (!$this->_canExecuteFunction($oViewObject, $oViewObject->getFncName())) {
330  $oException = oxNew('oxSystemComponentException', 'Non public method cannot be accessed');
331  throw $oException;
332  }
333 
334  // executing user defined function
335  $oViewObject->executeFunction($oViewObject->getFncName());
336 
337 
338 
339  // if no cache was stored before we should generate it
340  if (!$blIsCached) {
341  $sOutput = $this->_render($oViewObject);
342  }
343 
344 
345  $oOutput = $this->_getOutputManager();
346  $oOutput->setCharset($oViewObject->getCharSet());
347 
348  if (oxRegistry::getConfig()->getRequestParameter('renderPartial')) {
349  $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
350  $oOutput->output('errors', $this->_getFormattedErrors($oViewObject->getClassName()));
351  }
352 
353  $oOutput->sendHeaders();
354 
355 
356  $oOutput->output('content', $sOutput);
357 
358  $myConfig->pageClose();
359 
360  stopProfile('process');
361 
362  // stopping resource monitor
363  $this->_stopMonitor($oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData());
364 
365  // flush output (finalize)
366  $oOutput->flushOutput();
367  }
368 
379  protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
380  {
381  $myConfig = $this->getConfig();
382 
383  // creating current view object
384  $oViewObject = oxNew($sClass);
385 
386  // store this call
387  $oViewObject->setClassName($sClass);
388  $oViewObject->setFncName($sFunction);
389  $oViewObject->setViewParameters($aParams);
390 
391  $myConfig->setActiveView($oViewObject);
392 
393 
394  // init class
395  $oViewObject->init();
396 
397  return $oViewObject;
398  }
399 
408  protected function _canExecuteFunction($oClass, $sFunction)
409  {
410  $blCanExecute = true;
411 
412  if (method_exists($oClass, $sFunction)) {
413  $oReflectionMethod = new ReflectionMethod($oClass, $sFunction);
414  if (!$oReflectionMethod->isPublic()) {
415  $blCanExecute = false;
416  }
417  }
418 
419  return $blCanExecute;
420  }
421 
422 
430  protected function _getFormattedErrors($sControllerName)
431  {
432  $aErrors = $this->_getErrors($sControllerName);
433  $aFmtErrors = array();
434  if (is_array($aErrors) && count($aErrors)) {
435  foreach ($aErrors as $sLocation => $aEx2) {
436  foreach ($aEx2 as $sKey => $oEr) {
437  $oErr = unserialize($oEr);
438  $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
439  }
440  }
441  }
442 
443  return $aFmtErrors;
444  }
445 
453  protected function _render($oViewObject)
454  {
455  // get Smarty is important here as it sets template directory correct
456  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
457 
458  // render it
459  $sTemplateName = $oViewObject->render();
460 
461  // check if template dir exists
462  $sTemplateFile = $this->getConfig()->getTemplatePath($sTemplateName, $this->isAdmin());
463  if (!file_exists($sTemplateFile)) {
464 
465  $oEx = oxNew('oxSystemComponentException');
466  $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND');
467  $oEx->setComponent($sTemplateName);
468 
469  $sTemplateName = "message/exception.tpl";
470 
471  if ($this->_isDebugMode()) {
472  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
473  }
474  $oEx->debugOut();
475  }
476 
477  // Output processing. This is useful for modules. As sometimes you may want to process output manually.
478  $oOutput = $this->_getOutputManager();
479  $aViewData = $oOutput->processViewArray($oViewObject->getViewData(), $oViewObject->getClassName());
480  $oViewObject->setViewData($aViewData);
481 
482  //add all exceptions to display
483  $aErrors = $this->_getErrors($oViewObject->getClassName());
484  if (is_array($aErrors) && count($aErrors)) {
485  oxRegistry::get("oxUtilsView")->passAllErrorsToView($aViewData, $aErrors);
486  }
487 
488  foreach (array_keys($aViewData) as $sViewName) {
489  $oSmarty->assign_by_ref($sViewName, $aViewData[$sViewName]);
490  }
491 
492  // passing current view object to smarty
493  $oSmarty->oxobject = $oViewObject;
494 
495 
496  $sOutput = $oSmarty->fetch($sTemplateName, $oViewObject->getViewId());
497 
498  //Output processing - useful for modules as sometimes you may want to process output manually.
499  $sOutput = $oOutput->process($sOutput, $oViewObject->getClassName());
500 
501  return $oOutput->addVersionTags($sOutput);
502  }
503 
509  protected function _getOutputManager()
510  {
511  if (!$this->_oOutput) {
512  $this->_oOutput = oxNew('oxOutput');
513  }
514 
515  return $this->_oOutput;
516  }
517 
525  protected function _getErrors($sCurrentControllerName)
526  {
527  if (null === $this->_aErrors) {
528  $this->_aErrors = oxRegistry::getSession()->getVariable('Errors');
529  $this->_aControllerErrors = oxRegistry::getSession()->getVariable('ErrorController');
530  if (null === $this->_aErrors) {
531  $this->_aErrors = array();
532  }
533  $this->_aAllErrors = $this->_aErrors;
534  }
535  // resetting errors of current controller or widget from session
536  if (is_array($this->_aControllerErrors) && !empty($this->_aControllerErrors)) {
537  foreach ($this->_aControllerErrors as $sErrorName => $sControllerName) {
538  if ($sControllerName == $sCurrentControllerName) {
539  unset($this->_aAllErrors[$sErrorName]);
540  unset($this->_aControllerErrors[$sErrorName]);
541  }
542  }
543  } else {
544  $this->_aAllErrors = array();
545  }
546  oxRegistry::getSession()->setVariable('ErrorController', $this->_aControllerErrors);
547  oxRegistry::getSession()->setVariable('Errors', $this->_aAllErrors);
548 
549  return $this->_aErrors;
550  }
551 
558  protected function _runOnce()
559  {
560  $oConfig = $this->getConfig();
561 
562  error_reporting($this->_getErrorReportingLevel());
563 
564 
565  $blRunOnceExecuted = oxRegistry::getSession()->getVariable('blRunOnceExecuted');
566  if (!$blRunOnceExecuted && !$this->isAdmin() && $oConfig->isProductiveMode()) {
567 
568  // check if setup is still there
569  if (file_exists($oConfig->getConfigParam('sShopDir') . '/setup/index.php')) {
570  $sTpl = 'message/err_setup.tpl';
571  $oActView = oxNew('oxubase');
572  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
573  $oSmarty->assign('oView', $oActView);
574  $oSmarty->assign('oViewConf', $oActView->getViewConfig());
575  oxRegistry::getUtils()->showMessageAndExit($oSmarty->fetch($sTpl));
576  }
577 
578  oxRegistry::getSession()->setVariable('blRunOnceExecuted', true);
579  }
580  }
581 
588  protected function _getErrorReportingLevel()
589  {
590  $sOldReporting = error_reporting();
591 
592  $iErrorReporting = E_ALL ^ E_NOTICE;
593  // some 3rd party libraries still use deprecated functions
594  if (defined('E_DEPRECATED')) {
595  $iErrorReporting = $iErrorReporting ^ E_DEPRECATED;
596  }
597 
598  if ($this->getConfig()->isProductiveMode() && !ini_get('log_errors')) {
599  $iErrorReporting = 0;
600  }
601 
602 
603  return $iErrorReporting;
604  }
605 
611  protected function _isDebugMode()
612  {
613  if (OxRegistry::get("OxConfigFile")->getVar('iDebug')) {
614  return true;
615  }
616 
617  return false;
618  }
619 
620 
621 
628  protected function _handleSystemException($oEx)
629  {
630  $oEx->debugOut();
631 
632  if ($this->_isDebugMode()) {
633  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
634  $this->_process('exceptionError', 'displayExceptionError');
635  } else {
636  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
637  }
638  }
639 
645  protected function _handleCookieException($oEx)
646  {
647  if ($this->_isDebugMode()) {
648  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
649  }
650  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
651  }
652 
658  protected function _handleAccessRightsException($oEx)
659  {
660  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=content&tpl=err_accessdenied.tpl', true, 302);
661  }
662 
668  protected function _handleDbConnectionException($oEx)
669  {
670  $oEx->debugOut();
671 
672  if ($this->_isDebugMode()) {
673  oxRegistry::getUtils()->showMessageAndExit($oEx->getString());
674  }
675  oxRegistry::getUtils()->redirectOffline();
676  }
677 
683  protected function _handleBaseException($oEx)
684  {
685  $oEx->debugOut();
686 
687  if ($this->_isDebugMode()) {
688  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
689  $this->_process('exceptionError', 'displayExceptionError');
690  }
691  }
692 
698  protected function _getStartController()
699  {
700  $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
701 
702  if (!$sClass) {
703  $sClass = $this->isAdmin() ? 'login' : $this->_getFrontendStartController();
704  oxRegistry::getSession()->setVariable('cl', $sClass);
705  }
706 
707  return $sClass;
708  }
709 
716  protected function _getFrontendStartController()
717  {
718  $sClass = 'start';
719  if ($this->getConfig()->isMall()) {
720  $sClass = $this->_getFrontendMallStartController();
721  }
722 
723  return $sClass;
724  }
725 
732  protected function _getFrontendMallStartController()
733  {
734  $iShopCount = oxDb::getDb()->getOne('select count(*) from oxshops where oxactive = 1');
735 
736  $sMallShopURL = $this->getConfig()->getConfigParam('sMallShopURL');
737 
738  $sClass = 'start';
739  if ($iShopCount && $iShopCount > 1 && $this->getConfig()->getConfigParam('iMallMode') != 0 && !$sMallShopURL) {
740  $sClass = 'mallstart';
741  }
742 
743  return $sClass;
744  }
745 }