OXID eShop CE  4.9.9
 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 
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 
142  protected function _log($sClass, $sFnc)
143  {
144  $oDb = oxDb::getDb();
145  $oConfig = $this->getConfig();
146  $oSession = $this->getSession();
147 
148  $sShopID = $oSession->getVariable('actshop');
149  $sTime = date('Y-m-d H:i:s');
150  $sSidQuoted = $oDb->quote($oSession->getId());
151  $sUserIDQuoted = $oDb->quote($oSession->getVariable('usr'));
152 
153  $sCnid = $oConfig->getRequestParameter('cnid');
154  $sAnid = $oConfig->getRequestParameter('aid') ? $oConfig->getRequestParameter('aid') : $oConfig->getRequestParameter('anid');
155 
156  $sParameter = '';
157 
158  if ($sClass == 'content') {
159  $sParameter = str_replace('.tpl', '', $oConfig->getRequestParameter('tpl'));
160  } elseif ($sClass == 'search') {
161  $sParameter = $oConfig->getRequestParameter('searchparam');
162  }
163 
164  $sFncQuoted = $oDb->quote($sFnc);
165  $sClassQuoted = $oDb->quote($sClass);
166  $sParameterQuoted = $oDb->quote($sParameter);
167 
168  $sQ = "insert into oxlogs (oxtime, oxshopid, oxuserid, oxsessid, oxclass, oxfnc, oxcnid, oxanid, oxparameter) " .
169  "values( '$sTime', '$sShopID', $sUserIDQuoted, $sSidQuoted, $sClassQuoted, $sFncQuoted, " . $oDb->quote($sCnid) . ", " . $oDb->quote($sAnid) . ", $sParameterQuoted )";
170 
171  $oDb->execute($sQ);
172  }
173 
174  // OXID : add timing
178  protected function _startMonitor()
179  {
180  if ($this->_isDebugMode()) {
181  $this->_dTimeStart = microtime(true);
182  }
183  }
184 
193  protected function _stopMonitor($blIsCache = false, $blIsCached = false, $sViewID = null, $aViewData = array())
194  {
195  if ($this->_isDebugMode() && !$this->isAdmin()) {
196  /* @var $oDebugInfo oxDebugInfo */
197  $iDebug = $this->getConfig()->getConfigParam('iDebug');
198  $oDebugInfo = oxNew('oxDebugInfo');
199 
200  $blHidden = ($iDebug == -1);
201 
202  $sLog = '';
203  $sLogId = md5(time() . rand() . rand());
204  $sLog .= "<div id='oxidDebugInfo_$sLogId'>";
205 
206  $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>";
207  $sLog .= "<div id='debugInfoBlock_$sLogId' style='display:" . ($blHidden ? 'none' : 'block') . "' class='debugInfoBlock' align='left'>";
208 
209 
210  // outputting template params
211  if ($iDebug == 4) {
212  $sLog .= $oDebugInfo->formatTemplateData($aViewData);
213  }
214 
215  // output timing
216  $this->_dTimeEnd = microtime(true);
217 
218 
219  $sLog .= $oDebugInfo->formatMemoryUsage();
220 
221  $sLog .= $oDebugInfo->formatTimeStamp();
222 
223 
224  $sLog .= $oDebugInfo->formatExecutionTime($this->getTotalTime());
225 
226  if ($iDebug == 7) {
227  $sLog .= $oDebugInfo->formatDbInfo();
228  }
229 
230  if ($iDebug == 2 || $iDebug == 3 || $iDebug == 4) {
231  $sLog .= $oDebugInfo->formatAdoDbPerf();
232  }
233 
234  $sLog .= '</div>';
235 
236  $sLog .= "<script type='text/javascript'>
237  var b = document.getElementById('oxidDebugInfo_$sLogId');
238  var c = document.body;
239  if (c) { c.appendChild(b.parentNode.removeChild(b));}
240  </script>";
241 
242  $sLog .= "</div>";
243 
244  $this->_getOutputManager()->output('debuginfo', $sLog);
245  }
246  }
247 
253  public function getTotalTime()
254  {
255  if ($this->_dTimeEnd && $this->_dTimeStart) {
256  return $this->_dTimeEnd - $this->_dTimeStart;
257  }
258 
259  return 0;
260  }
261 
267  protected function _executeMaintenanceTasks()
268  {
269  if (isset($this->_blMainTasksExecuted)) {
270  return;
271  }
272 
273  startProfile('executeMaintenanceTasks');
274  oxNew("oxArticleList")->updateUpcomingPrices();
275  stopProfile('executeMaintenanceTasks');
276  }
277 
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  ) {
307  $this->_log($sClass, $sFunction);
308  }
309 
310  // starting resource monitor
311  $this->_startMonitor();
312 
313  // caching params ...
314  $sOutput = null;
315  $blIsCached = false;
316 
317  // Initialize view object and it's components.
318  $oViewObject = $this->_initializeViewObject($sClass, $sFunction, $aParams, $aViewsChain);
319 
320  if (!$this->_canExecuteFunction($oViewObject, $oViewObject->getFncName())) {
322  $oException = oxNew('oxSystemComponentException', 'Non public method cannot be accessed');
323  throw $oException;
324  }
325 
326  // executing user defined function
327  $oViewObject->executeFunction($oViewObject->getFncName());
328 
329 
330 
331  // if no cache was stored before we should generate it
332  if (!$blIsCached) {
333  $sOutput = $this->_render($oViewObject);
334  }
335 
336 
337  $oOutput = $this->_getOutputManager();
338  $oOutput->setCharset($oViewObject->getCharSet());
339 
340  if (oxRegistry::getConfig()->getRequestParameter('renderPartial')) {
341  $oOutput->setOutputFormat(oxOutput::OUTPUT_FORMAT_JSON);
342  $oOutput->output('errors', $this->_getFormattedErrors($oViewObject->getClassName()));
343  }
344 
345  $oOutput->sendHeaders();
346 
347 
348  $oOutput->output('content', $sOutput);
349 
350  $myConfig->pageClose();
351 
352  stopProfile('process');
353 
354  // stopping resource monitor
355  $this->_stopMonitor($oViewObject->getIsCallForCache(), $blIsCached, $sViewID, $oViewObject->getViewData());
356 
357  // flush output (finalize)
358  $oOutput->flushOutput();
359  }
360 
371  protected function _initializeViewObject($sClass, $sFunction, $aParams = null, $aViewsChain = null)
372  {
373  $myConfig = $this->getConfig();
374 
375  // creating current view object
376  $oViewObject = oxNew($sClass);
377 
378  // store this call
379  $oViewObject->setClassName($sClass);
380  $oViewObject->setFncName($sFunction);
381  $oViewObject->setViewParameters($aParams);
382 
383  $myConfig->setActiveView($oViewObject);
384 
385 
386  // init class
387  $oViewObject->init();
388 
389  return $oViewObject;
390  }
391 
400  protected function _canExecuteFunction($oClass, $sFunction)
401  {
402  $blCanExecute = true;
403 
404  if (method_exists($oClass, $sFunction)) {
405  $oReflectionMethod = new ReflectionMethod($oClass, $sFunction);
406  if (!$oReflectionMethod->isPublic()) {
407  $blCanExecute = false;
408  }
409  }
410 
411  return $blCanExecute;
412  }
413 
414 
422  protected function _getFormattedErrors($sControllerName)
423  {
424  $aErrors = $this->_getErrors($sControllerName);
425  $aFmtErrors = array();
426  if (is_array($aErrors) && count($aErrors)) {
427  foreach ($aErrors as $sLocation => $aEx2) {
428  foreach ($aEx2 as $sKey => $oEr) {
429  $oErr = unserialize($oEr);
430  $aFmtErrors[$sLocation][$sKey] = $oErr->getOxMessage();
431  }
432  }
433  }
434 
435  return $aFmtErrors;
436  }
437 
445  protected function _render($oViewObject)
446  {
447  // get Smarty is important here as it sets template directory correct
448  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
449 
450  // render it
451  $sTemplateName = $oViewObject->render();
452 
453  // check if template dir exists
454  $sTemplateFile = $this->getConfig()->getTemplatePath($sTemplateName, $this->isAdmin());
455  if (!file_exists($sTemplateFile)) {
456 
457  $oEx = oxNew('oxSystemComponentException');
458  $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_TEMPLATENOTFOUND');
459  $oEx->setComponent($sTemplateName);
460 
461  $sTemplateName = "message/exception.tpl";
462 
463  if ($this->_isDebugMode()) {
464  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
465  }
466  $oEx->debugOut();
467  }
468 
469  // Output processing. This is useful for modules. As sometimes you may want to process output manually.
470  $oOutput = $this->_getOutputManager();
471  $aViewData = $oOutput->processViewArray($oViewObject->getViewData(), $oViewObject->getClassName());
472  $oViewObject->setViewData($aViewData);
473 
474  //add all exceptions to display
475  $aErrors = $this->_getErrors($oViewObject->getClassName());
476  if (is_array($aErrors) && count($aErrors)) {
477  oxRegistry::get("oxUtilsView")->passAllErrorsToView($aViewData, $aErrors);
478  }
479 
480  foreach (array_keys($aViewData) as $sViewName) {
481  $oSmarty->assign_by_ref($sViewName, $aViewData[$sViewName]);
482  }
483 
484  // passing current view object to smarty
485  $oSmarty->oxobject = $oViewObject;
486 
487 
488  $sOutput = $oSmarty->fetch($sTemplateName, $oViewObject->getViewId());
489 
490  //Output processing - useful for modules as sometimes you may want to process output manually.
491  $sOutput = $oOutput->process($sOutput, $oViewObject->getClassName());
492 
493  return $oOutput->addVersionTags($sOutput);
494  }
495 
501  protected function _getOutputManager()
502  {
503  if (!$this->_oOutput) {
504  $this->_oOutput = oxNew('oxOutput');
505  }
506 
507  return $this->_oOutput;
508  }
509 
517  protected function _getErrors($sCurrentControllerName)
518  {
519  if (null === $this->_aErrors) {
520  $this->_aErrors = oxRegistry::getSession()->getVariable('Errors');
521  $this->_aControllerErrors = oxRegistry::getSession()->getVariable('ErrorController');
522  if (null === $this->_aErrors) {
523  $this->_aErrors = array();
524  }
525  $this->_aAllErrors = $this->_aErrors;
526  }
527  // resetting errors of current controller or widget from session
528  if (is_array($this->_aControllerErrors) && !empty($this->_aControllerErrors)) {
529  foreach ($this->_aControllerErrors as $sErrorName => $sControllerName) {
530  if ($sControllerName == $sCurrentControllerName) {
531  unset($this->_aAllErrors[$sErrorName]);
532  unset($this->_aControllerErrors[$sErrorName]);
533  }
534  }
535  } else {
536  $this->_aAllErrors = array();
537  }
538  oxRegistry::getSession()->setVariable('ErrorController', $this->_aControllerErrors);
539  oxRegistry::getSession()->setVariable('Errors', $this->_aAllErrors);
540 
541  return $this->_aErrors;
542  }
543 
550  protected function _runOnce()
551  {
552  $oConfig = $this->getConfig();
553 
554  error_reporting($this->_getErrorReportingLevel());
555 
556 
557  $blRunOnceExecuted = oxRegistry::getSession()->getVariable('blRunOnceExecuted');
558  if (!$blRunOnceExecuted && !$this->isAdmin() && $oConfig->isProductiveMode()) {
559 
560  // check if setup is still there
561  if (file_exists($oConfig->getConfigParam('sShopDir') . '/setup/index.php')) {
562  $sTpl = 'message/err_setup.tpl';
563  $oActView = oxNew('oxubase');
564  $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty();
565  $oSmarty->assign('oView', $oActView);
566  $oSmarty->assign('oViewConf', $oActView->getViewConfig());
567  oxRegistry::getUtils()->showMessageAndExit($oSmarty->fetch($sTpl));
568  }
569 
570  oxRegistry::getSession()->setVariable('blRunOnceExecuted', true);
571  }
572  }
573 
580  protected function _getErrorReportingLevel()
581  {
582  $sOldReporting = error_reporting();
583 
584  $iErrorReporting = E_ALL ^ E_NOTICE;
585  // some 3rd party libraries still use deprecated functions
586  if (defined('E_DEPRECATED')) {
587  $iErrorReporting = $iErrorReporting ^ E_DEPRECATED;
588  }
589 
590  if ($this->getConfig()->isProductiveMode() && !ini_get('log_errors')) {
591  $iErrorReporting = 0;
592  }
593 
594 
595  return $iErrorReporting;
596  }
597 
603  protected function _isDebugMode()
604  {
605  if (OxRegistry::get("OxConfigFile")->getVar('iDebug')) {
606  return true;
607  }
608 
609  return false;
610  }
611 
612 
613 
620  protected function _handleSystemException($oEx)
621  {
622  $oEx->debugOut();
623 
624  if ($this->_isDebugMode()) {
625  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
626  $this->_process('exceptionError', 'displayExceptionError');
627  } else {
628  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
629  }
630  }
631 
637  protected function _handleCookieException($oEx)
638  {
639  if ($this->_isDebugMode()) {
640  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
641  }
642  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=start', true, 302);
643  }
644 
650  protected function _handleAccessRightsException($oEx)
651  {
652  oxRegistry::getUtils()->redirect($this->getConfig()->getShopHomeUrl() . 'cl=content&tpl=err_accessdenied.tpl', true, 302);
653  }
654 
660  protected function _handleDbConnectionException($oEx)
661  {
662  $oEx->debugOut();
663 
664  if ($this->_isDebugMode()) {
665  oxRegistry::getUtils()->showMessageAndExit($oEx->getString());
666  }
667  oxRegistry::getUtils()->redirectOffline();
668  }
669 
675  protected function _handleBaseException($oEx)
676  {
677  $oEx->debugOut();
678 
679  if ($this->_isDebugMode()) {
680  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
681  $this->_process('exceptionError', 'displayExceptionError');
682  }
683  }
684 
690  protected function _getStartController()
691  {
692  $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
693 
694  if (!$sClass) {
695  $sClass = $this->isAdmin() ? 'login' : $this->_getFrontendStartController();
696  oxRegistry::getSession()->setVariable('cl', $sClass);
697  }
698 
699  return $sClass;
700  }
701 
708  protected function _getFrontendStartController()
709  {
710  $sClass = 'start';
711  if ($this->getConfig()->isMall()) {
712  $sClass = $this->_getFrontendMallStartController();
713  }
714 
715  return $sClass;
716  }
717 
724  protected function _getFrontendMallStartController()
725  {
726  $iShopCount = oxDb::getDb()->getOne('select count(*) from oxshops where oxactive = 1');
727 
728  $sMallShopURL = $this->getConfig()->getConfigParam('sMallShopURL');
729 
730  $sClass = 'start';
731  if ($iShopCount && $iShopCount > 1 && $this->getConfig()->getConfigParam('iMallMode') != 0 && !$sMallShopURL) {
732  $sClass = 'mallstart';
733  }
734 
735  return $sClass;
736  }
737 }