This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | * |
||
5 | * Created on Sep 19, 2006 |
||
6 | * |
||
7 | * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com" |
||
8 | * |
||
9 | * This program is free software; you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU General Public License as published by |
||
11 | * the Free Software Foundation; either version 2 of the License, or |
||
12 | * (at your option) any later version. |
||
13 | * |
||
14 | * This program is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU General Public License along |
||
20 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
22 | * http://www.gnu.org/copyleft/gpl.html |
||
23 | * |
||
24 | * @file |
||
25 | */ |
||
26 | |||
27 | /** |
||
28 | * This is the abstract base class for API formatters. |
||
29 | * |
||
30 | * @ingroup API |
||
31 | */ |
||
32 | abstract class ApiFormatBase extends ApiBase { |
||
33 | private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp; |
||
0 ignored issues
–
show
|
|||
34 | private $mBuffer, $mDisabled = false; |
||
0 ignored issues
–
show
|
|||
35 | private $mIsWrappedHtml = false; |
||
36 | private $mHttpStatus = false; |
||
37 | protected $mForceDefaultParams = false; |
||
38 | |||
39 | /** |
||
40 | * If $format ends with 'fm', pretty-print the output in HTML. |
||
41 | * @param ApiMain $main |
||
42 | * @param string $format Format name |
||
43 | */ |
||
44 | public function __construct( ApiMain $main, $format ) { |
||
45 | parent::__construct( $main, $format ); |
||
46 | |||
47 | $this->mIsHtml = ( substr( $format, -2, 2 ) === 'fm' ); // ends with 'fm' |
||
48 | if ( $this->mIsHtml ) { |
||
49 | $this->mFormat = substr( $format, 0, -2 ); // remove ending 'fm' |
||
50 | $this->mIsWrappedHtml = $this->getMain()->getCheck( 'wrappedhtml' ); |
||
51 | } else { |
||
52 | $this->mFormat = $format; |
||
53 | } |
||
54 | $this->mFormat = strtoupper( $this->mFormat ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Overriding class returns the MIME type that should be sent to the client. |
||
59 | * |
||
60 | * When getIsHtml() returns true, the return value here is used for syntax |
||
61 | * highlighting but the client sees text/html. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | abstract public function getMimeType(); |
||
66 | |||
67 | /** |
||
68 | * Get the internal format name |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getFormat() { |
||
72 | return $this->mFormat; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Returns true when the HTML pretty-printer should be used. |
||
77 | * The default implementation assumes that formats ending with 'fm' |
||
78 | * should be formatted in HTML. |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function getIsHtml() { |
||
82 | return $this->mIsHtml; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Returns true when the special wrapped mode is enabled. |
||
87 | * @since 1.27 |
||
88 | * @return bool |
||
89 | */ |
||
90 | protected function getIsWrappedHtml() { |
||
91 | return $this->mIsWrappedHtml; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Disable the formatter. |
||
96 | * |
||
97 | * This causes calls to initPrinter() and closePrinter() to be ignored. |
||
98 | */ |
||
99 | public function disable() { |
||
100 | $this->mDisabled = true; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Whether the printer is disabled |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function isDisabled() { |
||
108 | return $this->mDisabled; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Whether this formatter can handle printing API errors. |
||
113 | * |
||
114 | * If this returns false, then on API errors the default printer will be |
||
115 | * instantiated. |
||
116 | * @since 1.23 |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function canPrintErrors() { |
||
120 | return true; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Ignore request parameters, force a default. |
||
125 | * |
||
126 | * Used as a fallback if errors are being thrown. |
||
127 | * @since 1.26 |
||
128 | */ |
||
129 | public function forceDefaultParams() { |
||
130 | $this->mForceDefaultParams = true; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Overridden to honor $this->forceDefaultParams(), if applicable |
||
135 | * @since 1.26 |
||
136 | */ |
||
137 | protected function getParameterFromSettings( $paramName, $paramSettings, $parseLimit ) { |
||
138 | if ( !$this->mForceDefaultParams ) { |
||
139 | return parent::getParameterFromSettings( $paramName, $paramSettings, $parseLimit ); |
||
140 | } |
||
141 | |||
142 | if ( !is_array( $paramSettings ) ) { |
||
143 | return $paramSettings; |
||
144 | } elseif ( isset( $paramSettings[self::PARAM_DFLT] ) ) { |
||
145 | return $paramSettings[self::PARAM_DFLT]; |
||
146 | } else { |
||
147 | return null; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Set the HTTP status code to be used for the response |
||
153 | * @since 1.29 |
||
154 | * @param int $code |
||
155 | */ |
||
156 | public function setHttpStatus( $code ) { |
||
157 | if ( $this->mDisabled ) { |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | if ( $this->getIsHtml() ) { |
||
162 | $this->mHttpStatus = $code; |
||
0 ignored issues
–
show
The property
$mHttpStatus was declared of type boolean , but $code is of type integer . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
![]() |
|||
163 | } else { |
||
164 | $this->getMain()->getRequest()->response()->statusHeader( $code ); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Initialize the printer function and prepare the output headers. |
||
170 | * @param bool $unused Always false since 1.25 |
||
171 | */ |
||
172 | public function initPrinter( $unused = false ) { |
||
173 | if ( $this->mDisabled ) { |
||
174 | return; |
||
175 | } |
||
176 | |||
177 | $mime = $this->getIsWrappedHtml() |
||
178 | ? 'text/mediawiki-api-prettyprint-wrapped' |
||
179 | : ( $this->getIsHtml() ? 'text/html' : $this->getMimeType() ); |
||
180 | |||
181 | // Some printers (ex. Feed) do their own header settings, |
||
182 | // in which case $mime will be set to null |
||
183 | if ( $mime === null ) { |
||
184 | return; // skip any initialization |
||
185 | } |
||
186 | |||
187 | $this->getMain()->getRequest()->response()->header( "Content-Type: $mime; charset=utf-8" ); |
||
188 | |||
189 | // Set X-Frame-Options API results (bug 39180) |
||
190 | $apiFrameOptions = $this->getConfig()->get( 'ApiFrameOptions' ); |
||
191 | if ( $apiFrameOptions ) { |
||
192 | $this->getMain()->getRequest()->response()->header( "X-Frame-Options: $apiFrameOptions" ); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Finish printing and output buffered data. |
||
198 | */ |
||
199 | public function closePrinter() { |
||
200 | if ( $this->mDisabled ) { |
||
201 | return; |
||
202 | } |
||
203 | |||
204 | $mime = $this->getMimeType(); |
||
205 | if ( $this->getIsHtml() && $mime !== null ) { |
||
206 | $format = $this->getFormat(); |
||
207 | $lcformat = strtolower( $format ); |
||
208 | $result = $this->getBuffer(); |
||
209 | |||
210 | $context = new DerivativeContext( $this->getMain() ); |
||
211 | $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) ); |
||
212 | $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) ); |
||
213 | $out = new OutputPage( $context ); |
||
214 | $context->setOutput( $out ); |
||
215 | |||
216 | $out->addModuleStyles( 'mediawiki.apipretty' ); |
||
217 | $out->setPageTitle( $context->msg( 'api-format-title' ) ); |
||
218 | |||
219 | if ( !$this->getIsWrappedHtml() ) { |
||
220 | // When the format without suffix 'fm' is defined, there is a non-html version |
||
221 | if ( $this->getMain()->getModuleManager()->isDefined( $lcformat, 'format' ) ) { |
||
222 | $msg = $context->msg( 'api-format-prettyprint-header' )->params( $format, $lcformat ); |
||
223 | } else { |
||
224 | $msg = $context->msg( 'api-format-prettyprint-header-only-html' )->params( $format ); |
||
225 | } |
||
226 | |||
227 | $header = $msg->parseAsBlock(); |
||
228 | $out->addHTML( |
||
229 | Html::rawElement( 'div', [ 'class' => 'api-pretty-header' ], |
||
230 | ApiHelp::fixHelpLinks( $header ) |
||
231 | ) |
||
232 | ); |
||
233 | |||
234 | if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) { |
||
235 | $out->addHTML( |
||
236 | Html::rawElement( 'div', [ 'class' => 'api-pretty-header api-pretty-status' ], |
||
237 | $this->msg( |
||
238 | 'api-format-prettyprint-status', |
||
239 | $this->mHttpStatus, |
||
240 | HttpStatus::getMessage( $this->mHttpStatus ) |
||
241 | )->parse() |
||
242 | ) |
||
243 | ); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | if ( Hooks::run( 'ApiFormatHighlight', [ $context, $result, $mime, $format ] ) ) { |
||
248 | $out->addHTML( |
||
249 | Html::element( 'pre', [ 'class' => 'api-pretty-content' ], $result ) |
||
250 | ); |
||
251 | } |
||
252 | |||
253 | if ( $this->getIsWrappedHtml() ) { |
||
254 | // This is a special output mode mainly intended for ApiSandbox use |
||
255 | $time = microtime( true ) - $this->getConfig()->get( 'RequestTime' ); |
||
256 | $json = FormatJson::encode( |
||
257 | [ |
||
258 | 'status' => (int)( $this->mHttpStatus ?: 200 ), |
||
259 | 'statustext' => HttpStatus::getMessage( $this->mHttpStatus ?: 200 ), |
||
0 ignored issues
–
show
It seems like
$this->mHttpStatus ?: 200 can also be of type boolean ; however, HttpStatus::getMessage() does only seem to accept integer , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
260 | 'html' => $out->getHTML(), |
||
261 | 'modules' => array_values( array_unique( array_merge( |
||
262 | $out->getModules(), |
||
263 | $out->getModuleScripts(), |
||
264 | $out->getModuleStyles() |
||
265 | ) ) ), |
||
266 | 'continue' => $this->getResult()->getResultData( 'continue' ), |
||
267 | 'time' => round( $time * 1000 ), |
||
268 | ], |
||
269 | false, FormatJson::ALL_OK |
||
270 | ); |
||
271 | |||
272 | // Bug 66776: wfMangleFlashPolicy() is needed to avoid a nasty bug in |
||
273 | // Flash, but what it does isn't friendly for the API, so we need to |
||
274 | // work around it. |
||
275 | if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $json ) ) { |
||
276 | $json = preg_replace( |
||
277 | '/\<(\s*cross-domain-policy\s*)\>/i', '\\u003C$1\\u003E', $json |
||
278 | ); |
||
279 | } |
||
280 | |||
281 | echo $json; |
||
282 | } else { |
||
283 | // API handles its own clickjacking protection. |
||
284 | // Note, that $wgBreakFrames will still override $wgApiFrameOptions for format mode. |
||
285 | $out->allowClickjacking(); |
||
286 | $out->output(); |
||
287 | } |
||
288 | } else { |
||
289 | // For non-HTML output, clear all errors that might have been |
||
290 | // displayed if display_errors=On |
||
291 | ob_clean(); |
||
292 | |||
293 | echo $this->getBuffer(); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Append text to the output buffer. |
||
299 | * @param string $text |
||
300 | */ |
||
301 | public function printText( $text ) { |
||
302 | $this->mBuffer .= $text; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Get the contents of the buffer. |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getBuffer() { |
||
310 | return $this->mBuffer; |
||
311 | } |
||
312 | |||
313 | public function getAllowedParams() { |
||
314 | $ret = []; |
||
315 | if ( $this->getIsHtml() ) { |
||
316 | $ret['wrappedhtml'] = [ |
||
317 | ApiBase::PARAM_DFLT => false, |
||
318 | ApiBase::PARAM_HELP_MSG => 'apihelp-format-param-wrappedhtml', |
||
319 | |||
320 | ]; |
||
321 | } |
||
322 | return $ret; |
||
323 | } |
||
324 | |||
325 | protected function getExamplesMessages() { |
||
326 | return [ |
||
327 | 'action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName() |
||
328 | => [ 'apihelp-format-example-generic', $this->getFormat() ] |
||
329 | ]; |
||
330 | } |
||
331 | |||
332 | public function getHelpUrls() { |
||
333 | return 'https://www.mediawiki.org/wiki/API:Data_formats'; |
||
334 | } |
||
335 | |||
336 | } |
||
337 | |||
338 | /** |
||
339 | * For really cool vim folding this needs to be at the end: |
||
340 | * vim: foldmarker=@{,@} foldmethod=marker |
||
341 | */ |
||
342 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.