From ec9b551ca9c9426e5a7e94eb575c42f809e8788a Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Fri, 16 Sep 2011 18:50:13 +0000 Subject: [PATCH] * Added HttpError exception as replacement for wfHttpError(); changed alls core calls to it except AjaxDispatcher.php * Changed FeedUtils' call to it to be similar than feeds are completely disabled * Use local context instead of global variables in Special:Userlogout --- includes/Exception.php | 49 ++++++++++++++++++++++++ includes/FeedUtils.php | 5 +-- includes/Metadata.php | 13 +++---- includes/WebRequest.php | 2 +- includes/Wiki.php | 2 +- includes/specials/SpecialUploadStash.php | 3 +- includes/specials/SpecialUserlogout.php | 19 +++++---- 7 files changed, 69 insertions(+), 24 deletions(-) diff --git a/includes/Exception.php b/includes/Exception.php index fb5f0ffa21..432b768af2 100644 --- a/includes/Exception.php +++ b/includes/Exception.php @@ -366,6 +366,55 @@ class UserBlockedError extends ErrorPageError { } } +/** + * Show an error that looks like an HTTP server error. + * Replacement for wfHttpError(). + * + * @ingroup Exception + */ +class HttpError extends MWException { + private $httpCode, $header, $content; + + /** + * Constructor + * + * @param $httpCode Integer: HTTP status code to send to the client + * @param $content String|Message: content of the message + * @param $header String|Message: content of the header (\ and \) + */ + public function __construct( $httpCode, $content, $header = null ){ + parent::__construct( $content ); + $this->httpCode = (int)$httpCode; + $this->header = $header; + $this->content = $content; + } + + public function reportHTML() { + $httpMessage = HttpStatus::getMessage( $this->httpCode ); + + header( "Status: {$this->httpCode} {$httpMessage}" ); + header( 'Content-type: text/html; charset=utf-8' ); + + if ( $this->header === null ) { + $header = $httpMessage; + } elseif ( $this->header instanceof Message ) { + $header = $this->header->escaped(); + } else { + $header = htmlspecialchars( $this->header ); + } + + if ( $this->content instanceof Message ) { + $content = $this->content->escaped(); + } else { + $content = htmlspecialchars( $this->content ); + } + + print "\n". + "$header\n" . + "

$header

$content

\n"; + } +} + /** * Handler class for MWExceptions * @ingroup Exception diff --git a/includes/FeedUtils.php b/includes/FeedUtils.php index e923a282cf..b6df0c641e 100644 --- a/includes/FeedUtils.php +++ b/includes/FeedUtils.php @@ -31,16 +31,15 @@ class FeedUtils { * @return Boolean */ public static function checkFeedOutput( $type ) { - global $wgFeed, $wgFeedClasses; + global $wgOut, $wgFeed, $wgFeedClasses; if ( !$wgFeed ) { - global $wgOut; $wgOut->addWikiMsg( 'feed-unavailable' ); return false; } if( !isset( $wgFeedClasses[$type] ) ) { - wfHttpError( 500, "Internal Server Error", "Unsupported feed type." ); + $wgOut->addWikiMsg( 'feed-invalid' ); return false; } diff --git a/includes/Metadata.php b/includes/Metadata.php index 2e4ab94cfc..e5e3296b3b 100644 --- a/includes/Metadata.php +++ b/includes/Metadata.php @@ -41,14 +41,13 @@ abstract class RdfMetaData { $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self::RDF_TYPE_PREFS ) ); if( !$rdftype ){ - wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) ); - return false; - } else { - $wgOut->disable(); - $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" ); - $wgOut->sendCacheControl(); - return true; + throw new HttpError( 406, wfMessage( 'notacceptable' ) ); } + + $wgOut->disable(); + $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" ); + $wgOut->sendCacheControl(); + return true; } protected function reallyFullUrl() { diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 98b5eb6a05..0c3a996020 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -874,7 +874,7 @@ class WebRequest { return false; } } - wfHttpError( 403, 'Forbidden', + throw new HttpError( 403, 'Invalid file extension found in the path info or query string.' ); return false; diff --git a/includes/Wiki.php b/includes/Wiki.php index 985d951556..a0d7751a5b 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -210,7 +210,7 @@ class MediaWiki { "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " . "to true."; } - wfHttpError( 500, "Internal error", $message ); + throw new HttpError( 500, $message ); } else { $output->setSquidMaxage( 1200 ); $output->redirect( $targetUrl, '301' ); diff --git a/includes/specials/SpecialUploadStash.php b/includes/specials/SpecialUploadStash.php index d62038543e..e72c81e9a8 100644 --- a/includes/specials/SpecialUploadStash.php +++ b/includes/specials/SpecialUploadStash.php @@ -95,8 +95,7 @@ class SpecialUploadStash extends UnlistedSpecialPage { $message = $e->getMessage(); } - wfHttpError( $code, HttpStatus::getMessage( $code ), $message ); - return false; + throw new HttpError( $code, $message ); } /** diff --git a/includes/specials/SpecialUserlogout.php b/includes/specials/SpecialUserlogout.php index 2e67eef3da..d747448f97 100644 --- a/includes/specials/SpecialUserlogout.php +++ b/includes/specials/SpecialUserlogout.php @@ -33,31 +33,30 @@ class SpecialUserlogout extends UnlistedSpecialPage { } function execute( $par ) { - global $wgUser, $wgOut; - /** * Some satellite ISPs use broken precaching schemes that log people out straight after * they're logged in (bug 17790). Luckily, there's a way to detect such requests. */ if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], '&' ) !== false ) { wfDebug( "Special:Userlogout request {$_SERVER['REQUEST_URI']} looks suspicious, denying.\n" ); - wfHttpError( 400, wfMsg( 'loginerror' ), wfMsg( 'suspicious-userlogout' ) ); - return; + throw new HttpError( 400, wfMessage( 'suspicious-userlogout' ), wfMessage( 'loginerror' ) ); } $this->setHeaders(); $this->outputHeader(); - $oldName = $wgUser->getName(); - $wgUser->logout(); + $user = $this->getUser(); + $oldName = $user->getName(); + $user->logout(); - $wgOut->addWikiMsg( 'logouttext' ); + $out = $this->getOutput(); + $out->addWikiMsg( 'logouttext' ); // Hook. $injected_html = ''; - wfRunHooks( 'UserLogoutComplete', array( &$wgUser, &$injected_html, $oldName ) ); - $wgOut->addHTML( $injected_html ); + wfRunHooks( 'UserLogoutComplete', array( &$user, &$injected_html, $oldName ) ); + $out->addHTML( $injected_html ); - $wgOut->returnToMain(); + $out->returnToMain(); } } -- 2.20.1