Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
50.00% |
17 / 34 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
HttpError | |
50.00% |
17 / 34 |
|
66.67% |
4 / 6 |
26.12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
isLoggable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStatusCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
report | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
doLog | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
getHTML | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\Logger\LoggerFactory; |
22 | |
23 | /** |
24 | * Show an error that looks like an HTTP server error. |
25 | * Replacement for wfHttpError(). |
26 | * |
27 | * @newable |
28 | * @stable to extend |
29 | * @since 1.19 |
30 | * @ingroup Exception |
31 | */ |
32 | class HttpError extends MWException { |
33 | /** @var int */ |
34 | private $httpCode; |
35 | /** @var string|Message|null */ |
36 | private $header; |
37 | /** @var string|Message */ |
38 | private $content; |
39 | |
40 | /** |
41 | * @stable to call |
42 | * @param int $httpCode HTTP status code to send to the client |
43 | * @param string|Message $content Content of the message |
44 | * @param string|Message|null $header Content of the header (\<title\> and \<h1\>) |
45 | * @param-taint $content tainted |
46 | * @param-taint $header tainted |
47 | */ |
48 | public function __construct( $httpCode, $content, $header = null ) { |
49 | parent::__construct( $content ); |
50 | $this->httpCode = (int)$httpCode; |
51 | $this->header = $header; |
52 | $this->content = $content; |
53 | } |
54 | |
55 | /** |
56 | * We don't want the default exception logging as we got our own logging set |
57 | * up in self::report. |
58 | * |
59 | * @see MWException::isLoggable |
60 | * |
61 | * @since 1.24 |
62 | * @return bool |
63 | */ |
64 | public function isLoggable() { |
65 | return false; |
66 | } |
67 | |
68 | /** |
69 | * Returns the HTTP status code supplied to the constructor. |
70 | * |
71 | * @return int |
72 | */ |
73 | public function getStatusCode() { |
74 | return $this->httpCode; |
75 | } |
76 | |
77 | /** |
78 | * Report and log the HTTP error. |
79 | * Sends the appropriate HTTP status code and outputs an |
80 | * HTML page with an error message. |
81 | */ |
82 | public function report() { |
83 | $this->doLog(); |
84 | |
85 | HttpStatus::header( $this->httpCode ); |
86 | header( 'Content-type: text/html; charset=utf-8' ); |
87 | |
88 | print $this->getHTML(); |
89 | } |
90 | |
91 | private function doLog() { |
92 | $logger = LoggerFactory::getInstance( 'HttpError' ); |
93 | $content = $this->content; |
94 | |
95 | if ( $content instanceof Message ) { |
96 | $content = $content->text(); |
97 | } |
98 | |
99 | $context = [ |
100 | 'file' => $this->getFile(), |
101 | 'line' => $this->getLine(), |
102 | 'http_code' => $this->httpCode, |
103 | ]; |
104 | |
105 | $logMsg = "$content ({http_code}) from {file}:{line}"; |
106 | |
107 | if ( $this->getStatusCode() < 500 ) { |
108 | $logger->info( $logMsg, $context ); |
109 | } else { |
110 | $logger->error( $logMsg, $context ); |
111 | } |
112 | } |
113 | |
114 | /** |
115 | * Returns HTML for reporting the HTTP error. |
116 | * This will be a minimal but complete HTML document. |
117 | * |
118 | * @return string HTML |
119 | */ |
120 | public function getHTML() { |
121 | if ( $this->header === null ) { |
122 | $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) ); |
123 | } elseif ( $this->header instanceof Message ) { |
124 | $titleHtml = $this->header->escaped(); |
125 | } else { |
126 | $titleHtml = htmlspecialchars( $this->header ); |
127 | } |
128 | |
129 | if ( $this->content instanceof Message ) { |
130 | $contentHtml = $this->content->escaped(); |
131 | } else { |
132 | $contentHtml = nl2br( htmlspecialchars( $this->content ) ); |
133 | } |
134 | |
135 | return "<!DOCTYPE html>\n" . |
136 | "<html><head><title>$titleHtml</title></head>\n" . |
137 | "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n"; |
138 | } |
139 | } |