Fixes to input validation and output escaping for user preferences.
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( -1 );
4 /**
5 * @package MediaWiki
6 */
7
8 if ( $wgUseTeX )
9 require_once 'Math.php';
10
11 /**
12 * @todo document
13 * @package MediaWiki
14 */
15 class OutputPage {
16 var $mHeaders, $mMetatags, $mKeywords;
17 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
18 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
19 var $mSubtitle, $mRedirect, $mStatusCode;
20 var $mLastModified, $mETag, $mCategoryLinks;
21 var $mScripts, $mLinkColours, $mPageLinkTitle;
22
23 var $mSuppressQuickbar;
24 var $mOnloadHandler;
25 var $mDoNothing;
26 var $mContainsOldMagic, $mContainsNewMagic;
27 var $mIsArticleRelated;
28 var $mParserOptions;
29 var $mShowFeedLinks = false;
30 var $mEnableClientCache = true;
31 var $mArticleBodyOnly = false;
32
33 var $mNewSectionLink = false;
34
35 /**
36 * Constructor
37 * Initialise private variables
38 */
39 function OutputPage() {
40 $this->mHeaders = $this->mMetatags =
41 $this->mKeywords = $this->mLinktags = array();
42 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
43 $this->mRedirect = $this->mLastModified =
44 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
45 $this->mOnloadHandler = $this->mPageLinkTitle = '';
46 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
47 $this->mSuppressQuickbar = $this->mPrintable = false;
48 $this->mLanguageLinks = array();
49 $this->mCategoryLinks = array();
50 $this->mDoNothing = false;
51 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
52 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
53 $this->mSquidMaxage = 0;
54 $this->mScripts = '';
55 $this->mETag = false;
56 $this->mRevisionId = null;
57 $this->mNewSectionLink = false;
58 }
59
60 function addHeader( $name, $val ) { array_push( $this->mHeaders, $name.': '.$val ); }
61 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
62 function setStatusCode( $statusCode ) { $this->mStatusCode = $statusCode; }
63
64 # To add an http-equiv meta tag, precede the name with "http:"
65 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
66 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
67 function addScript( $script ) { $this->mScripts .= $script; }
68 function getScript() { return $this->mScripts; }
69
70 function setETag($tag) { $this->mETag = $tag; }
71 function setArticleBodyOnly($only) { $this->mArticleBodyOnly = $only; }
72 function getArticleBodyOnly($only) { return $this->mArticleBodyOnly; }
73
74 function addLink( $linkarr ) {
75 # $linkarr should be an associative array of attributes. We'll escape on output.
76 array_push( $this->mLinktags, $linkarr );
77 }
78
79 function addMetadataLink( $linkarr ) {
80 # note: buggy CC software only reads first "meta" link
81 static $haveMeta = false;
82 $linkarr['rel'] = ($haveMeta) ? 'alternate meta' : 'meta';
83 $this->addLink( $linkarr );
84 $haveMeta = true;
85 }
86
87 /**
88 * checkLastModified tells the client to use the client-cached page if
89 * possible. If sucessful, the OutputPage is disabled so that
90 * any future call to OutputPage->output() have no effect. The method
91 * returns true iff cache-ok headers was sent.
92 */
93 function checkLastModified ( $timestamp ) {
94 global $wgCachePages, $wgCacheEpoch, $wgUser;
95 $fname = 'OutputPage::checkLastModified';
96
97 if ( !$timestamp || $timestamp == '19700101000000' ) {
98 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
99 return;
100 }
101 if( !$wgCachePages ) {
102 wfDebug( "$fname: CACHE DISABLED\n", false );
103 return;
104 }
105 if( $wgUser->getOption( 'nocache' ) ) {
106 wfDebug( "$fname: USER DISABLED CACHE\n", false );
107 return;
108 }
109
110 $timestamp=wfTimestamp(TS_MW,$timestamp);
111 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
112
113 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
114 # IE sends sizes after the date like this:
115 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
116 # this breaks strtotime().
117 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
118 $modsinceTime = strtotime( $modsince );
119 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
120 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
121 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
122 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
123 # Make sure you're in a place you can leave when you call us!
124 header( "HTTP/1.0 304 Not Modified" );
125 $this->mLastModified = $lastmod;
126 $this->sendCacheControl();
127 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
128 $this->disable();
129 @ob_end_clean(); // Don't output compressed blob
130 return true;
131 } else {
132 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
133 $this->mLastModified = $lastmod;
134 }
135 } else {
136 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
137 $this->mLastModified = $lastmod;
138 }
139 }
140
141 function getPageTitleActionText () {
142 global $action;
143 switch($action) {
144 case 'edit':
145 case 'delete':
146 case 'protect':
147 case 'unprotect':
148 case 'watch':
149 case 'unwatch':
150 // Display title is already customized
151 return '';
152 case 'history':
153 return wfMsg('history_short');
154 case 'submit':
155 // FIXME: bug 2735; not correct for special pages etc
156 return wfMsg('preview');
157 case 'info':
158 return wfMsg('info_short');
159 default:
160 return '';
161 }
162 }
163
164 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
165 function setHTMLTitle( $name ) {$this->mHTMLtitle = $name; }
166 function setPageTitle( $name ) {
167 global $action, $wgContLang;
168 $name = $wgContLang->convert($name, true);
169 $this->mPagetitle = $name;
170 if(!empty($action)) {
171 $taction = $this->getPageTitleActionText();
172 if( !empty( $taction ) ) {
173 $name .= ' - '.$taction;
174 }
175 }
176
177 $this->setHTMLTitle( wfMsg( 'pagetitle', $name ) );
178 }
179 function getHTMLTitle() { return $this->mHTMLtitle; }
180 function getPageTitle() { return $this->mPagetitle; }
181 function setSubtitle( $str ) { $this->mSubtitle = /*$this->parse(*/$str/*)*/; } // @bug 2514
182 function getSubtitle() { return $this->mSubtitle; }
183 function isArticle() { return $this->mIsarticle; }
184 function setPrintable() { $this->mPrintable = true; }
185 function isPrintable() { return $this->mPrintable; }
186 function setSyndicated( $show = true ) { $this->mShowFeedLinks = $show; }
187 function isSyndicated() { return $this->mShowFeedLinks; }
188 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
189 function getOnloadHandler() { return $this->mOnloadHandler; }
190 function disable() { $this->mDoNothing = true; }
191
192 function setArticleRelated( $v ) {
193 $this->mIsArticleRelated = $v;
194 if ( !$v ) {
195 $this->mIsarticle = false;
196 }
197 }
198 function setArticleFlag( $v ) {
199 $this->mIsarticle = $v;
200 if ( $v ) {
201 $this->mIsArticleRelated = $v;
202 }
203 }
204
205 function isArticleRelated() { return $this->mIsArticleRelated; }
206
207 function getLanguageLinks() { return $this->mLanguageLinks; }
208 function addLanguageLinks($newLinkArray) {
209 $this->mLanguageLinks += $newLinkArray;
210 }
211 function setLanguageLinks($newLinkArray) {
212 $this->mLanguageLinks = $newLinkArray;
213 }
214
215 function getCategoryLinks() {
216 return $this->mCategoryLinks;
217 }
218
219 /**
220 * Add an array of categories, with names in the keys
221 */
222 function addCategoryLinks($categories) {
223 global $wgUser, $wgContLang;
224
225 if ( !is_array( $categories ) ) {
226 return;
227 }
228 # Add the links to the link cache in a batch
229 $arr = array( NS_CATEGORY => $categories );
230 $lb = new LinkBatch;
231 $lb->setArray( $arr );
232 $lb->execute();
233
234 $sk =& $wgUser->getSkin();
235 foreach ( $categories as $category => $arbitrary ) {
236 $title = Title::makeTitleSafe( NS_CATEGORY, $category );
237 $text = $wgContLang->convertHtml( $title->getText() );
238 $this->mCategoryLinks[] = $sk->makeLinkObj( $title, $text );
239 }
240 }
241
242 function setCategoryLinks($categories) {
243 $this->mCategoryLinks = array();
244 $this->addCategoryLinks($categories);
245 }
246
247 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
248 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
249
250 function addHTML( $text ) { $this->mBodytext .= $text; }
251 function clearHTML() { $this->mBodytext = ''; }
252 function getHTML() { return $this->mBodytext; }
253 function debug( $text ) { $this->mDebugtext .= $text; }
254
255 /* @deprecated */
256 function setParserOptions( $options ) {
257 return $this->ParserOptions( $options );
258 }
259
260 function ParserOptions( $options = null ) {
261 return wfSetVar( $this->mParserOptions, $options );
262 }
263
264 /**
265 * Set the revision ID which will be seen by the wiki text parser
266 * for things such as embedded {{REVISIONID}} variable use.
267 * @param mixed $revid an integer, or NULL
268 * @return mixed previous value
269 */
270 function setRevisionId( $revid ) {
271 $val = is_null( $revid ) ? null : intval( $revid );
272 return wfSetVar( $this->mRevisionId, $val );
273 }
274
275 /**
276 * Convert wikitext to HTML and add it to the buffer
277 * Default assumes that the current page title will
278 * be used.
279 */
280 function addWikiText( $text, $linestart = true ) {
281 global $wgTitle;
282 $this->addWikiTextTitle($text, $wgTitle, $linestart);
283 }
284
285 function addWikiTextWithTitle($text, &$title, $linestart = true) {
286 $this->addWikiTextTitle($text, $title, $linestart);
287 }
288
289 function addWikiTextTitle($text, &$title, $linestart) {
290 global $wgParser;
291 $parserOutput = $wgParser->parse( $text, $title, $this->mParserOptions,
292 $linestart, true, $this->mRevisionId );
293 $this->addParserOutput( $parserOutput );
294 }
295
296 function addParserOutputNoText( &$parserOutput ) {
297 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
298 $this->addCategoryLinks( $parserOutput->getCategories() );
299 $this->mNewSectionLink = $parserOutput->getNewSection();
300 $this->addKeywords( $parserOutput );
301 if ( $parserOutput->getCacheTime() == -1 ) {
302 $this->enableClientCache( false );
303 }
304 if ( $parserOutput->mHTMLtitle != "" ) {
305 $this->mPagetitle = $parserOutput->mHTMLtitle ;
306 $this->mSubtitle .= $parserOutput->mSubtitle ;
307 }
308 }
309
310 function addParserOutput( &$parserOutput ) {
311 $this->addParserOutputNoText( $parserOutput );
312 $this->addHTML( $parserOutput->getText() );
313 }
314
315 /**
316 * Add wikitext to the buffer, assuming that this is the primary text for a page view
317 * Saves the text into the parser cache if possible
318 */
319 function addPrimaryWikiText( $text, $article, $cache = true ) {
320 global $wgParser, $wgUser;
321
322 $this->mParserOptions->setTidy(true);
323 $parserOutput = $wgParser->parse( $text, $article->mTitle,
324 $this->mParserOptions, true, true, $this->mRevisionId );
325 $this->mParserOptions->setTidy(false);
326 if ( $cache && $article && $parserOutput->getCacheTime() != -1 ) {
327 $parserCache =& ParserCache::singleton();
328 $parserCache->save( $parserOutput, $article, $wgUser );
329 }
330
331 $this->addParserOutputNoText( $parserOutput );
332 $text = $parserOutput->getText();
333 wfRunHooks( 'OutputPageBeforeHTML',array( &$this, &$text ) );
334 $parserOutput->setText( $text );
335 $this->addHTML( $parserOutput->getText() );
336 }
337
338 /**
339 * For anything that isn't primary text or interface message
340 */
341 function addSecondaryWikiText( $text, $linestart = true ) {
342 global $wgTitle;
343 $this->mParserOptions->setTidy(true);
344 $this->addWikiTextTitle($text, $wgTitle, $linestart);
345 $this->mParserOptions->setTidy(false);
346 }
347
348
349 /**
350 * Add the output of a QuickTemplate to the output buffer
351 * @param QuickTemplate $template
352 */
353 function addTemplate( &$template ) {
354 ob_start();
355 $template->execute();
356 $this->addHTML( ob_get_contents() );
357 ob_end_clean();
358 }
359
360 /**
361 * Parse wikitext and return the HTML.
362 */
363 function parse( $text, $linestart = true, $interface = false ) {
364 global $wgParser, $wgTitle;
365 if ( $interface) { $this->mParserOptions->setInterfaceMessage(true); }
366 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions,
367 $linestart, true, $this->mRevisionId );
368 if ( $interface) { $this->mParserOptions->setInterfaceMessage(false); }
369 return $parserOutput->getText();
370 }
371
372 /**
373 * @param $article
374 * @param $user
375 *
376 * @return bool
377 */
378 function tryParserCache( &$article, $user ) {
379 $parserCache =& ParserCache::singleton();
380 $parserOutput = $parserCache->get( $article, $user );
381 if ( $parserOutput !== false ) {
382 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
383 $this->addCategoryLinks( $parserOutput->getCategories() );
384 $this->addKeywords( $parserOutput );
385 $this->mNewSectionLink = $parserOutput->getNewSection();
386 $text = $parserOutput->getText();
387 wfRunHooks( 'OutputPageBeforeHTML', array( &$this, &$text ) );
388 $this->addHTML( $text );
389 $t = $parserOutput->getTitleText();
390 if( !empty( $t ) ) {
391 $this->setPageTitle( $t );
392 }
393 return true;
394 } else {
395 return false;
396 }
397 }
398
399 /**
400 * Set the maximum cache time on the Squid in seconds
401 * @param $maxage
402 */
403 function setSquidMaxage( $maxage ) {
404 $this->mSquidMaxage = $maxage;
405 }
406
407 /**
408 * Use enableClientCache(false) to force it to send nocache headers
409 * @param $state
410 */
411 function enableClientCache( $state ) {
412 return wfSetVar( $this->mEnableClientCache, $state );
413 }
414
415 function uncacheableBecauseRequestvars() {
416 global $wgRequest;
417 return $wgRequest->getText('useskin', false) === false
418 && $wgRequest->getText('uselang', false) === false;
419 }
420
421 function sendCacheControl() {
422 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
423 $fname = 'OutputPage::sendCacheControl';
424
425 if ($this->mETag)
426 header("ETag: $this->mETag");
427
428 # don't serve compressed data to clients who can't handle it
429 # maintain different caches for logged-in users and non-logged in ones
430 header( 'Vary: Accept-Encoding, Cookie' );
431 if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) {
432 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
433 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
434 {
435 if ( $wgUseESI ) {
436 # We'll purge the proxy cache explicitly, but require end user agents
437 # to revalidate against the proxy on each visit.
438 # Surrogate-Control controls our Squid, Cache-Control downstream caches
439 wfDebug( "$fname: proxy caching with ESI; {$this->mLastModified} **\n", false );
440 # start with a shorter timeout for initial testing
441 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
442 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
443 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
444 } else {
445 # We'll purge the proxy cache for anons explicitly, but require end user agents
446 # to revalidate against the proxy on each visit.
447 # IMPORTANT! The Squid needs to replace the Cache-Control header with
448 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
449 wfDebug( "$fname: local proxy caching; {$this->mLastModified} **\n", false );
450 # start with a shorter timeout for initial testing
451 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
452 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
453 }
454 } else {
455 # We do want clients to cache if they can, but they *must* check for updates
456 # on revisiting the page.
457 wfDebug( "$fname: private caching; {$this->mLastModified} **\n", false );
458 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
459 header( "Cache-Control: private, must-revalidate, max-age=0" );
460 }
461 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
462 } else {
463 wfDebug( "$fname: no caching **\n", false );
464
465 # In general, the absence of a last modified header should be enough to prevent
466 # the client from using its cache. We send a few other things just to make sure.
467 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
468 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
469 header( 'Pragma: no-cache' );
470 }
471 }
472
473 /**
474 * Finally, all the text has been munged and accumulated into
475 * the object, let's actually output it:
476 */
477 function output() {
478 global $wgUser, $wgOutputEncoding;
479 global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType;
480 global $wgJsMimeType, $wgStylePath, $wgUseAjax, $wgScriptPath, $wgServer;
481
482 if( $this->mDoNothing ){
483 return;
484 }
485 $fname = 'OutputPage::output';
486 wfProfileIn( $fname );
487 $sk = $wgUser->getSkin();
488
489 if ( $wgUseAjax ) {
490 $this->addScript( "<script type=\"{$wgJsMimeType}\">
491 var wgScriptPath=\"{$wgScriptPath}\";
492 var wgServer=\"{$wgServer}\";
493 </script>" );
494 $this->addScript( "<script type=\"{$wgJsMimeType}\" src=\"{$wgStylePath}/common/ajax.js\"></script>\n" );
495 }
496
497 if ( '' != $this->mRedirect ) {
498 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
499 # Standards require redirect URLs to be absolute
500 global $wgServer;
501 $this->mRedirect = $wgServer . $this->mRedirect;
502 }
503 if( $this->mRedirectCode == '301') {
504 if( !$wgDebugRedirects ) {
505 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
506 }
507 $this->mLastModified = wfTimestamp( TS_RFC2822 );
508 }
509
510 $this->sendCacheControl();
511
512 if( $wgDebugRedirects ) {
513 $url = htmlspecialchars( $this->mRedirect );
514 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
515 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
516 print "</body>\n</html>\n";
517 } else {
518 header( 'Location: '.$this->mRedirect );
519 }
520 wfProfileOut( $fname );
521 return;
522 }
523 elseif ( $this->mStatusCode )
524 {
525 $statusMessage = array(
526 100 => 'Continue',
527 101 => 'Switching Protocols',
528 102 => 'Processing',
529 200 => 'OK',
530 201 => 'Created',
531 202 => 'Accepted',
532 203 => 'Non-Authoritative Information',
533 204 => 'No Content',
534 205 => 'Reset Content',
535 206 => 'Partial Content',
536 207 => 'Multi-Status',
537 300 => 'Multiple Choices',
538 301 => 'Moved Permanently',
539 302 => 'Found',
540 303 => 'See Other',
541 304 => 'Not Modified',
542 305 => 'Use Proxy',
543 307 => 'Temporary Redirect',
544 400 => 'Bad Request',
545 401 => 'Unauthorized',
546 402 => 'Payment Required',
547 403 => 'Forbidden',
548 404 => 'Not Found',
549 405 => 'Method Not Allowed',
550 406 => 'Not Acceptable',
551 407 => 'Proxy Authentication Required',
552 408 => 'Request Timeout',
553 409 => 'Conflict',
554 410 => 'Gone',
555 411 => 'Length Required',
556 412 => 'Precondition Failed',
557 413 => 'Request Entity Too Large',
558 414 => 'Request-URI Too Large',
559 415 => 'Unsupported Media Type',
560 416 => 'Request Range Not Satisfiable',
561 417 => 'Expectation Failed',
562 422 => 'Unprocessable Entity',
563 423 => 'Locked',
564 424 => 'Failed Dependency',
565 500 => 'Internal Server Error',
566 501 => 'Not Implemented',
567 502 => 'Bad Gateway',
568 503 => 'Service Unavailable',
569 504 => 'Gateway Timeout',
570 505 => 'HTTP Version Not Supported',
571 507 => 'Insufficient Storage'
572 );
573
574 if ( $statusMessage[$this->mStatusCode] )
575 header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $statusMessage[$this->mStatusCode] );
576 }
577
578 # Buffer output; final headers may depend on later processing
579 ob_start();
580
581 # Disable temporary placeholders, so that the skin produces HTML
582 $sk->postParseLinkColour( false );
583
584 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
585 header( 'Content-language: '.$wgContLanguageCode );
586
587 if ($this->mArticleBodyOnly) {
588 $this->out($this->mBodytext);
589 } else {
590 wfProfileIn( 'Output-skin' );
591 $sk->outputPage( $this );
592 wfProfileOut( 'Output-skin' );
593 }
594
595 $this->sendCacheControl();
596 ob_end_flush();
597 wfProfileOut( $fname );
598 }
599
600 function out( $ins ) {
601 global $wgInputEncoding, $wgOutputEncoding, $wgContLang;
602 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
603 $outs = $ins;
604 } else {
605 $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
606 if ( false === $outs ) { $outs = $ins; }
607 }
608 print $outs;
609 }
610
611 function setEncodings() {
612 global $wgInputEncoding, $wgOutputEncoding;
613 global $wgUser, $wgContLang;
614
615 $wgInputEncoding = strtolower( $wgInputEncoding );
616
617 if( $wgUser->getOption( 'altencoding' ) ) {
618 $wgContLang->setAltEncoding();
619 return;
620 }
621
622 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
623 $wgOutputEncoding = strtolower( $wgOutputEncoding );
624 return;
625 }
626 $wgOutputEncoding = $wgInputEncoding;
627 }
628
629 /**
630 * Returns a HTML comment with the elapsed time since request.
631 * This method has no side effects.
632 * Use wfReportTime() instead.
633 * @return string
634 * @deprecated
635 */
636 function reportTime() {
637 $time = wfReportTime();
638 return $time;
639 }
640
641 /**
642 * Produce a "user is blocked" page
643 */
644 function blockedPage() {
645 global $wgUser, $wgContLang, $wgTitle;
646
647 $this->setPageTitle( wfMsg( 'blockedtitle' ) );
648 $this->setRobotpolicy( 'noindex,nofollow' );
649 $this->setArticleRelated( false );
650
651 $id = $wgUser->blockedBy();
652 $reason = $wgUser->blockedFor();
653 $ip = wfGetIP();
654
655 if ( is_numeric( $id ) ) {
656 $name = User::whoIs( $id );
657 } else {
658 $name = $id;
659 }
660 $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]";
661
662 $this->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
663
664 # Don't auto-return to special pages
665 $return = $wgTitle->getNamespace() > -1 ? $wgTitle->getPrefixedText() : NULL;
666 $this->returnToMain( false, $return );
667 }
668
669 /**
670 * Note: these arguments are keys into wfMsg(), not text!
671 */
672 function errorpage( $title, $msg ) {
673 global $wgTitle;
674
675 $this->mDebugtext .= 'Original title: ' .
676 $wgTitle->getPrefixedText() . "\n";
677 $this->setPageTitle( wfMsg( $title ) );
678 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
679 $this->setRobotpolicy( 'noindex,nofollow' );
680 $this->setArticleRelated( false );
681 $this->enableClientCache( false );
682 $this->mRedirect = '';
683
684 $this->mBodytext = '';
685 $this->addWikiText( wfMsg( $msg ) );
686 $this->returnToMain( false );
687
688 $this->output();
689 wfErrorExit();
690 }
691
692 /**
693 * Display an error page indicating that a given version of MediaWiki is
694 * required to use it
695 *
696 * @param mixed $version The version of MediaWiki needed to use the page
697 */
698 function versionRequired( $version ) {
699 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
700 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
701 $this->setRobotpolicy( 'noindex,nofollow' );
702 $this->setArticleRelated( false );
703 $this->mBodytext = '';
704
705 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
706 $this->returnToMain();
707 }
708
709 /**
710 * Display an error page noting that a given permission bit is required.
711 * This should generally replace the sysopRequired, developerRequired etc.
712 * @param string $permission key required
713 */
714 function permissionRequired( $permission ) {
715 global $wgUser;
716
717 $this->setPageTitle( wfMsg( 'badaccess' ) );
718 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
719 $this->setRobotpolicy( 'noindex,nofollow' );
720 $this->setArticleRelated( false );
721 $this->mBodytext = '';
722
723 $sk = $wgUser->getSkin();
724 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ) );
725 $this->addHTML( wfMsgHtml( 'badaccesstext', $ap, $permission ) );
726 $this->returnToMain();
727 }
728
729 /**
730 * @deprecated
731 */
732 function sysopRequired() {
733 global $wgUser;
734
735 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
736 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
737 $this->setRobotpolicy( 'noindex,nofollow' );
738 $this->setArticleRelated( false );
739 $this->mBodytext = '';
740
741 $sk = $wgUser->getSkin();
742 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
743 $this->addHTML( wfMsgHtml( 'sysoptext', $ap ) );
744 $this->returnToMain();
745 }
746
747 /**
748 * @deprecated
749 */
750 function developerRequired() {
751 global $wgUser;
752
753 $this->setPageTitle( wfMsg( 'developertitle' ) );
754 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
755 $this->setRobotpolicy( 'noindex,nofollow' );
756 $this->setArticleRelated( false );
757 $this->mBodytext = '';
758
759 $sk = $wgUser->getSkin();
760 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
761 $this->addHTML( wfMsgHtml( 'developertext', $ap ) );
762 $this->returnToMain();
763 }
764
765 /**
766 * Produce the stock "please login to use the wiki" page
767 */
768 function loginToUse() {
769 global $wgUser, $wgTitle, $wgContLang;
770 $skin = $wgUser->getSkin();
771
772 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
773 $this->setHtmlTitle( wfMsg( 'errorpagetitle' ) );
774 $this->setRobotPolicy( 'noindex,nofollow' );
775 $this->setArticleFlag( false );
776
777 $loginTitle = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
778 $loginLink = $skin->makeKnownLinkObj( $loginTitle, wfMsgHtml( 'loginreqlink' ), 'returnto=' . $wgTitle->getPrefixedUrl() );
779 $this->addHtml( wfMsgWikiHtml( 'loginreqpagetext', $loginLink ) );
780 $this->addHtml( "\n<!--" . $wgTitle->getPrefixedUrl() . "-->" );
781
782 $this->returnToMain();
783 }
784
785 function databaseError( $fname, $sql, $error, $errno ) {
786 global $wgUser, $wgCommandLineMode, $wgShowSQLErrors;
787
788 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
789 $this->setRobotpolicy( 'noindex,nofollow' );
790 $this->setArticleRelated( false );
791 $this->enableClientCache( false );
792 $this->mRedirect = '';
793
794 if( !$wgShowSQLErrors ) {
795 $sql = wfMsg( 'sqlhidden' );
796 }
797
798 if ( $wgCommandLineMode ) {
799 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
800 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
801 } else {
802 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
803 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
804 }
805
806 if ( $wgCommandLineMode || !is_object( $wgUser )) {
807 print $msg."\n";
808 wfErrorExit();
809 }
810 $this->mBodytext = $msg;
811 $this->output();
812 wfErrorExit();
813 }
814
815 function readOnlyPage( $source = null, $protected = false ) {
816 global $wgUser, $wgReadOnlyFile, $wgReadOnly, $wgTitle;
817
818 $this->setRobotpolicy( 'noindex,nofollow' );
819 $this->setArticleRelated( false );
820
821 if( $protected ) {
822 $skin = $wgUser->getSkin();
823 $this->setPageTitle( wfMsg( 'viewsource' ) );
824 $this->setSubtitle( wfMsg( 'viewsourcefor', $skin->makeKnownLinkObj( $wgTitle ) ) );
825
826 # Determine if protection is due to the page being a system message
827 # and show an appropriate explanation
828 if( $wgTitle->getNamespace() == NS_MEDIAWIKI && !$wgUser->isAllowed( 'editinterface' ) ) {
829 $this->addWikiText( wfMsg( 'protectedinterface' ) );
830 } else {
831 $this->addWikiText( wfMsg( 'protectedtext' ) );
832 }
833 } else {
834 $this->setPageTitle( wfMsg( 'readonly' ) );
835 if ( $wgReadOnly ) {
836 $reason = $wgReadOnly;
837 } else {
838 $reason = file_get_contents( $wgReadOnlyFile );
839 }
840 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
841 }
842
843 if( is_string( $source ) ) {
844 if( strcmp( $source, '' ) == 0 ) {
845 global $wgTitle;
846 if ( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
847 $source = wfMsgWeirdKey ( $wgTitle->getText() );
848 } else {
849 $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
850 }
851 }
852 $rows = $wgUser->getIntOption( 'rows' );
853 $cols = $wgUser->getIntOption( 'cols' );
854
855 $text = "\n<textarea name='wpTextbox1' id='wpTextbox1' cols='$cols' rows='$rows' readonly='readonly'>" .
856 htmlspecialchars( $source ) . "\n</textarea>";
857 $this->addHTML( $text );
858 }
859
860 $this->returnToMain( false );
861 }
862
863 function fatalError( $message ) {
864 $this->setPageTitle( wfMsg( "internalerror" ) );
865 $this->setRobotpolicy( "noindex,nofollow" );
866 $this->setArticleRelated( false );
867 $this->enableClientCache( false );
868 $this->mRedirect = '';
869
870 $this->mBodytext = $message;
871 $this->output();
872 wfErrorExit();
873 }
874
875 function unexpectedValueError( $name, $val ) {
876 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
877 }
878
879 function fileCopyError( $old, $new ) {
880 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
881 }
882
883 function fileRenameError( $old, $new ) {
884 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
885 }
886
887 function fileDeleteError( $name ) {
888 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
889 }
890
891 function fileNotFoundError( $name ) {
892 $this->fatalError( wfMsg( 'filenotfound', $name ) );
893 }
894
895 /**
896 * return from error messages or notes
897 * @param $auto automatically redirect the user after 10 seconds
898 * @param $returnto page title to return to. Default is Main Page.
899 */
900 function returnToMain( $auto = true, $returnto = NULL ) {
901 global $wgUser, $wgOut, $wgRequest;
902
903 if ( $returnto == NULL ) {
904 $returnto = $wgRequest->getText( 'returnto' );
905 }
906 $returnto = htmlspecialchars( $returnto );
907
908 $sk = $wgUser->getSkin();
909 if ( '' == $returnto ) {
910 $returnto = wfMsgForContent( 'mainpage' );
911 }
912 $link = $sk->makeLinkObj( Title::newFromText( $returnto ), '' );
913
914 $r = wfMsg( 'returnto', $link );
915 if ( $auto ) {
916 $titleObj = Title::newFromText( $returnto );
917 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
918 }
919 $wgOut->addHTML( "\n<p>$r</p>\n" );
920 }
921
922 /**
923 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
924 * and uses the first 10 of them for META keywords
925 */
926 function addKeywords( &$parserOutput ) {
927 global $wgTitle;
928 $this->addKeyword( $wgTitle->getPrefixedText() );
929 $count = 1;
930 $links2d =& $parserOutput->getLinks();
931 if ( !is_array( $links2d ) ) {
932 return;
933 }
934 foreach ( $links2d as $ns => $dbkeys ) {
935 foreach( $dbkeys as $dbkey => $id ) {
936 $this->addKeyword( $dbkey );
937 if ( ++$count > 10 ) {
938 break 2;
939 }
940 }
941 }
942 }
943
944 /**
945 * @access private
946 * @return string
947 */
948 function headElement() {
949 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
950 global $wgUser, $wgContLang, $wgUseTrackbacks, $wgTitle;
951
952 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
953 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
954 } else {
955 $ret = '';
956 }
957
958 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
959
960 if ( '' == $this->getHTMLTitle() ) {
961 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ));
962 }
963
964 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
965 $ret .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
966 $ret .= "<head>\n<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
967 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
968
969 $ret .= $this->getHeadLinks();
970 global $wgStylePath;
971 if( $this->isPrintable() ) {
972 $media = '';
973 } else {
974 $media = "media='print'";
975 }
976 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
977 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
978
979 $sk = $wgUser->getSkin();
980 $ret .= $sk->getHeadScripts();
981 $ret .= $this->mScripts;
982 $ret .= $sk->getUserStyles();
983
984 if ($wgUseTrackbacks && $this->isArticleRelated())
985 $ret .= $wgTitle->trackbackRDF();
986
987 $ret .= "</head>\n";
988 return $ret;
989 }
990
991 function getHeadLinks() {
992 global $wgRequest;
993 $ret = '';
994 foreach ( $this->mMetatags as $tag ) {
995 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
996 $a = 'http-equiv';
997 $tag[0] = substr( $tag[0], 5 );
998 } else {
999 $a = 'name';
1000 }
1001 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
1002 }
1003
1004 $p = $this->mRobotpolicy;
1005 if( $p !== '' && $p != 'index,follow' ) {
1006 // http://www.robotstxt.org/wc/meta-user.html
1007 // Only show if it's different from the default robots policy
1008 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
1009 }
1010
1011 if ( count( $this->mKeywords ) > 0 ) {
1012 $strip = array(
1013 "/<.*?>/" => '',
1014 "/_/" => ' '
1015 );
1016 $ret .= "<meta name=\"keywords\" content=\"" .
1017 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
1018 }
1019 foreach ( $this->mLinktags as $tag ) {
1020 $ret .= '<link';
1021 foreach( $tag as $attr => $val ) {
1022 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
1023 }
1024 $ret .= " />\n";
1025 }
1026 if( $this->isSyndicated() ) {
1027 # FIXME: centralize the mime-type and name information in Feed.php
1028 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
1029 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
1030 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
1031 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 0.3' href='$link' />\n";
1032 }
1033
1034 return $ret;
1035 }
1036
1037 /**
1038 * Turn off regular page output and return an error reponse
1039 * for when rate limiting has triggered.
1040 * @todo i18n
1041 * @access public
1042 */
1043 function rateLimited() {
1044 global $wgOut;
1045 $wgOut->disable();
1046 wfHttpError( 500, 'Internal Server Error',
1047 'Sorry, the server has encountered an internal error. ' .
1048 'Please wait a moment and hit "refresh" to submit the request again.' );
1049 }
1050
1051 /**
1052 * Show an "add new section" link?
1053 *
1054 * @return bool True if the parser output instructs us to add one
1055 */
1056 function showNewSectionLink() {
1057 return $this->mNewSectionLink;
1058 }
1059
1060 }
1061 ?>