[BUGFIX] Throw HttpStatusExceptions in tslib_fe
authorSteffen Gebert <steffen.gebert@typo3.org>
Fri, 1 Apr 2011 10:25:14 +0000 (12:25 +0200)
committerSteffen Gebert <steffen.gebert@typo3.org>
Thu, 16 Jun 2011 05:31:19 +0000 (07:31 +0200)
Change #25697 added an Exception Hierarchy for HTTP statuses. This
change makes use of them in tslib_fe.

Change-Id: If3d30be37c97c9125d69dd3d98c41ea67deeb5c8
Resolves: #25698
Reviewed-on: http://review.typo3.org/1397
Reviewed-by: Andreas Wolf
Tested-by: Andreas Wolf
Reviewed-by: Georg Ringer
Tested-by: Georg Ringer
Reviewed-by: Steffen Gebert
Tested-by: Steffen Gebert
typo3/sysext/cms/tslib/class.tslib_fe.php

index 5735554..89e06a1 100644 (file)
                $link = $GLOBALS['TYPO3_DB']->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password);
                if ($link !== FALSE) {
                                // Connection to DB server ok, now select the database
-                       if (!$GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db))     {
-                               if ($this->checkPageUnavailableHandler())       {
-                                       $this->pageUnavailableAndExit('Cannot connect to the configured database "'.TYPO3_db.'"');
+                       if (!$GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db)) {
+                               $message = 'Cannot connect to the configured database "'.TYPO3_db.'"';
+                               if ($this->checkPageUnavailableHandler()) {
+                                       $this->pageUnavailableAndExit($message);
                                } else {
-                                       $message = 'Cannot connect to the configured database "'.TYPO3_db.'"';
                                        t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                       header('HTTP/1.0 503 Service Temporarily Unavailable');
-                                       throw new RuntimeException('Database Error: ' . $message, 1293617736);
+                                       throw new t3lib_error_http_ServiceUnavailableException($message, 1301648782);
                                }
                        }
                } else {
-                       if ($this->checkPageUnavailableHandler())       {
-                               $this->pageUnavailableAndExit('The current username, password or host was not accepted when the connection to the database was attempted to be established!');
+                       $message = 'The current username, password or host was not accepted when the connection to the database was attempted to be established!';
+                       if ($this->checkPageUnavailableHandler()) {
+                               $this->pageUnavailableAndExit($message);
                        } else {
-                               $message = 'The current username, password or host was not accepted when the connection to the database was attempted to be established!';
                                t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                               header('HTTP/1.0 503 Service Temporarily Unavailable');
-                               throw new RuntimeException('Database Error: ' . $message, 1293617741);
+                               throw new t3lib_error_http_ServiceUnavailableException('Database Error: ' . $message, 1301648945);
                        }
                }
 
                                if ($theFirstPage)      {
                                        $this->id = $theFirstPage['uid'];
                                } else {
-                                       if ($this->checkPageUnavailableHandler())       {
-                                               $this->pageUnavailableAndExit('No pages are found on the rootlevel!');
+                                       $message = 'No pages are found on the rootlevel!';
+                                       if ($this->checkPageUnavailableHandler()) {
+                                               $this->pageUnavailableAndExit($message);
                                        } else {
-                                               $message = 'No pages are found on the rootlevel!';
                                                t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                               header('HTTP/1.0 503 Service Temporarily Unavailable');
-                                               throw new RuntimeException($message, 1294587207);
+                                               throw new t3lib_error_http_ServiceUnavailableException($message, 1301648975);
                                        }
                                }
                        }
                                }
                        }
                                // If still no page...
-                       if (!count($this->page))        {
-                               if ($this->TYPO3_CONF_VARS['FE']['pageNotFound_handling'])      {
-                                       $this->pageNotFoundAndExit('The requested page does not exist!');
+                       if (!count($this->page)) {
+                               $message = 'The requested page does not exist!';
+                               if ($this->TYPO3_CONF_VARS['FE']['pageNotFound_handling']) {
+                                       $this->pageNotFoundAndExit($message);
                                } else {
-                                       $title = 'Page Not Found';
-                                       $message = 'The requested page does not exist!';
-                                       header(t3lib_utility_Http::HTTP_STATUS_404);
                                        t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                       $messagePage = t3lib_div::makeInstance('t3lib_message_ErrorpageMessage', $message, $title);
-                                       $messagePage->output();
-                                       exit;
+                                       throw new t3lib_error_http_PageNotFoundException($message, 1301648780);
                                }
                        }
                }
 
                        // Spacer is not accessible in frontend
                if ($this->page['doktype'] == t3lib_pageSelect::DOKTYPE_SPACER) {
-                       if ($this->TYPO3_CONF_VARS['FE']['pageNotFound_handling'])      {
-                               $this->pageNotFoundAndExit('The requested page does not exist!');
+                       $message = 'The requested page does not exist!';
+                       if ($this->TYPO3_CONF_VARS['FE']['pageNotFound_handling']) {
+                               $this->pageNotFoundAndExit($message);
                        } else {
-                               $title = 'Page Not Found';
-                               $message = 'The requested page does not exist!';
-                               header(t3lib_utility_Http::HTTP_STATUS_404);
                                t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                               $messagePage = t3lib_div::makeInstance('t3lib_message_ErrorpageMessage', $message, $title);
-                               $messagePage->output();
-                               exit;
+                               throw new t3lib_error_http_PageNotFoundException($message, 1301648781);
                        }
                }
 
                                $this->versioningWorkspaceId = $ws;
                                $this->rootLine = $this->sys_page->getRootLine($this->id,$this->MP);
                        }
-                       if (!count($this->rootLine))    {
-                               if ($this->checkPageUnavailableHandler())       {
-                                       $this->pageUnavailableAndExit('The requested page didn\'t have a proper connection to the tree-root!');
+                       if (!count($this->rootLine)) {
+                               $message = 'The requested page didn\'t have a proper connection to the tree-root!';
+                               if ($this->checkPageUnavailableHandler()) {
+                                       $this->pageUnavailableAndExit($message);
                                } else {
-                                       $message = 'The requested page didn\'t have a proper connection to the tree-root! <br /><br />('.$this->sys_page->error_getRootLine.')';
-                                       header('HTTP/1.0 503 Service Temporarily Unavailable');
-                                       t3lib_div::sysLog(str_replace('<br /><br />','',$message), 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                       throw new RuntimeException($message, 1294587210);
+                                       $rootline = '('.$this->sys_page->error_getRootLine.')';
+                                       t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
+                                       throw new t3lib_error_http_ServiceUnavailableException($message . '<br /><br />' . $rootline, 1301648167);
                                }
                        }
                        $this->fePreview = 1;
 
                        // Checking for include section regarding the hidden/starttime/endtime/fe_user (that is access control of a whole subbranch!)
                if ($this->checkRootlineForIncludeSection())    {
-                       if (!count($this->rootLine))    {
-                               if ($this->checkPageUnavailableHandler())       {
-                                       $this->pageUnavailableAndExit('The requested page was not accessible!');
+                       if (!count($this->rootLine)) {
+                               $message = 'The requested page was not accessible!';
+                               if ($this->checkPageUnavailableHandler()) {
+                                       $this->pageUnavailableAndExit($message);
                                } else {
-                                       $message = 'The requested page was not accessible!';
-                                       header('HTTP/1.0 503 Service Temporarily Unavailable');
                                        t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                       throw new RuntimeException($message, 1294587211);
+                                       throw new t3lib_error_http_ServiceUnavailableException($message, 1301648234);
                                }
                        } else {
                                $el = reset($this->rootLine);
                                        $c++;
                                }
                                if (count($page) == 0) {
-                                       header(t3lib_utility_Http::HTTP_STATUS_404);
-                                       $title = 'Page Not Found';
                                        $message = 'This page (ID ' . $thisUid . ') is of type "Shortcut" and configured to redirect to a subpage. '
                                                . 'However, this page has no accessible subpages.';
-                                       $messagePage = t3lib_div::makeInstance('t3lib_message_ErrorpageMessage', $message, $title);
-                                       $messagePage->output();
-                                       exit;
+                                       throw new t3lib_error_http_PageNotFoundException($message, 1301648328);
                                }
                        break;
                        case t3lib_pageSelect::SHORTCUT_MODE_PARENT_PAGE:
                                $parent = $this->sys_page->getPage($thisUid);
                                $page = $this->sys_page->getPage($parent['pid']);
                                if (count($page) == 0) {
-                                       header(t3lib_utility_Http::HTTP_STATUS_404);
-                                       $title = 'Page Not Found';
                                        $message = 'This page (ID ' . $thisUid . ') is of type "Shortcut" and configured to redirect to its parent page. '
                                                . 'However, the parent page is not accessible.';
-                                       $messagePage = t3lib_div::makeInstance('t3lib_message_ErrorpageMessage', $message, $title);
-                                       $messagePage->output();
-                                       exit;
+                                       throw new t3lib_error_http_PageNotFoundException($message, 1301648358);
                                }
                        break;
                        default:
                                $page = $this->sys_page->getPage($idArray[0]);
                                if (count($page) == 0) {
-                                       header(t3lib_utility_Http::HTTP_STATUS_404);
-                                       $title = 'Page Not Found';
                                        $message = 'This page (ID ' . $thisUid . ') is of type "Shortcut" and configured to redirect to a page, which is not accessible (ID ' . $idArray[0] . ').';
-                                       $messagePage = t3lib_div::makeInstance('t3lib_message_ErrorpageMessage', $message, $title);
-                                       $messagePage->output();
-                                       exit;
+                                       throw new t3lib_error_http_PageNotFoundException($message, 1301648404);
                                }
                        break;
                }
                        } else {
                                $pageLog[] = $page['uid'];
                                $message = 'Page shortcuts were looping in uids '.implode(',',$pageLog).'...!';
-                               header('HTTP/1.0 500 Internal Server Error');
                                t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
                                throw new RuntimeException($message, 1294587212);
                        }
                                $this->pSetup = $this->tmpl->setup[$this->sPre.'.'];
 
                                if (!is_array($this->pSetup))   {
-                                       if ($this->checkPageUnavailableHandler())       {
-                                               $this->pageUnavailableAndExit('The page is not configured! [type= '.$this->type.']['.$this->sPre.']');
+                                       $message = 'The page is not configured! [type='.$this->type.']['.$this->sPre.'].';
+                                       if ($this->checkPageUnavailableHandler()) {
+                                               $this->pageUnavailableAndExit($message);
                                        } else {
-                                               $message = 'The page is not configured! [type= '.$this->type.']['.$this->sPre.']';
-                                               header('HTTP/1.0 503 Service Temporarily Unavailable');
+                                               $explanation = 'This means that there is no TypoScript object of type PAGE with typeNum=' .
+                                                              $this->type . ' configured.';
                                                t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                               throw new RuntimeException($message, 1294587217);
+                                               throw new t3lib_error_http_ServiceUnavailableException($message . ' ' . $explanation, 1294587217);
                                        }
                                } else {
                                        $this->config['config'] = array();
                                        $this->pageUnavailableAndExit('No TypoScript template found!');
                                } else {
                                        $message = 'No TypoScript template found!';
-                                       header('HTTP/1.0 503 Service Temporarily Unavailable');
                                        t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                       throw new RuntimeException($message, 1294587218);
+                                       throw new t3lib_error_http_ServiceUnavailableException($message, 1294587218);
                                }
                        }
                }