Fix timestamp error in redirects (fails in particularly ugly fashion on Windows server)
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 # See design.doc
3
4 if($wgUseTeX) include_once( "Math.php" );
5
6 class OutputPage {
7 var $mHeaders, $mCookies, $mMetatags, $mKeywords;
8 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
9 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
10 var $mSubtitle, $mRedirect, $mHeadtext;
11 var $mLastModified, $mCategoryLinks;
12
13 var $mSuppressQuickbar;
14 var $mOnloadHandler;
15 var $mDoNothing;
16 var $mContainsOldMagic, $mContainsNewMagic;
17 var $mIsArticleRelated;
18 var $mParserOptions;
19
20 function OutputPage()
21 {
22 $this->mHeaders = $this->mCookies = $this->mMetatags =
23 $this->mKeywords = $this->mLinktags = array();
24 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
25 $this->mRedirect = $this->mLastModified =
26 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
27 $this->mOnloadHandler = "";
28 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
29 $this->mSuppressQuickbar = $this->mPrintable = false;
30 $this->mLanguageLinks = array();
31 $this->mCategoryLinks = array() ;
32 $this->mDoNothing = false;
33 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
34 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
35 }
36
37 function addHeader( $name, $val ) { array_push( $this->mHeaders, "$name: $val" ) ; }
38 function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
39 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
40
41 # To add an http-equiv meta tag, precede the name with "http:"
42 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
43 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
44 function addLink( $rel, $rev, $target ) { array_push( $this->mLinktags, array( $rel, $rev, $target ) ); }
45
46 # checkLastModified tells the client to use the client-cached page if
47 # possible. If sucessful, the OutputPage is disabled so that
48 # any future call to OutputPage->output() have no effect. The method
49 # returns true iff cache-ok headers was sent.
50 function checkLastModified ( $timestamp )
51 {
52 global $wgLang, $wgCachePages, $wgUser;
53 if( !$wgCachePages ) {
54 wfDebug( "CACHE DISABLED\n", false );
55 return;
56 }
57 if( preg_match( '/MSIE ([1-4]|5\.0)/', $_SERVER["HTTP_USER_AGENT"] ) ) {
58 # IE 5.0 has probs with our caching
59 wfDebug( "-- bad client, not caching\n", false );
60 return;
61 }
62 if( $wgUser->getOption( "nocache" ) ) {
63 wfDebug( "USER DISABLED CACHE\n", false );
64 return;
65 }
66
67 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
68 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
69
70 if( !empty( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ) {
71 # IE sends sizes after the date like this:
72 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
73 # this breaks strtotime().
74 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
75 $ismodsince = wfUnix2Timestamp( strtotime( $modsince ) );
76 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
77 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
78
79 if( ($ismodsince >= $timestamp ) and $wgUser->validateCache( $ismodsince ) ) {
80 # Make sure you're in a place you can leave when you call us!
81 header( "HTTP/1.0 304 Not Modified" );
82 $this->mLastModified = $lastmod;
83 $this->sendCacheControl();
84 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
85 $this->disable();
86 return true;
87 } else {
88 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
89 $this->mLastModified = $lastmod;
90 }
91 } else {
92 wfDebug( "We're confused.\n", false );
93 $this->mLastModified = $lastmod;
94 }
95 }
96
97 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
98 function setHTMLtitle( $name ) { $this->mHTMLtitle = $name; }
99 function setPageTitle( $name ) { $this->mPagetitle = $name; }
100 function getPageTitle() { return $this->mPagetitle; }
101 function setSubtitle( $str ) { $this->mSubtitle = $str; }
102 function getSubtitle() { return $this->mSubtitle; }
103 function isArticle() { return $this->mIsarticle; }
104 function setPrintable() { $this->mPrintable = true; }
105 function isPrintable() { return $this->mPrintable; }
106 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
107 function getOnloadHandler() { return $this->mOnloadHandler; }
108 function disable() { $this->mDoNothing = true; }
109
110 function setArticleRelated( $v )
111 {
112 $this->mIsArticleRelated = $v;
113 if ( !$v ) {
114 $this->mIsarticle = false;
115 }
116 }
117 function setArticleFlag( $v ) {
118 $this->mIsarticle = $v;
119 if ( $v ) {
120 $this->mIsArticleRelated = $v;
121 }
122 }
123
124 function isArticleRelated()
125 {
126 return $this->mIsArticleRelated;
127 }
128
129 function getLanguageLinks() {
130 global $wgTitle, $wgLanguageCode;
131 global $wgDBconnection, $wgDBname;
132 return $this->mLanguageLinks;
133 }
134 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
135 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
136
137 function addHTML( $text ) { $this->mBodytext .= $text; }
138 function addHeadtext( $text ) { $this->mHeadtext .= $text; }
139 function debug( $text ) { $this->mDebugtext .= $text; }
140
141 function setParserOptions( $options )
142 {
143 return wfSetVar( $this->mParserOptions, $options );
144 }
145
146 # First pass--just handle <nowiki> sections, pass the rest off
147 # to doWikiPass2() which does all the real work.
148 #
149 # $cacheArticle - assume this text is the main text for the given article
150 #
151 function addWikiText( $text, $linestart = true, $cacheArticle = NULL )
152 {
153 global $wgParser, $wgParserCache, $wgUser, $wgTitle;
154
155 $parserOutput = false;
156 if ( $cacheArticle ) {
157 $parserOutput = $wgParserCache->get( $cacheArticle, $wgUser );
158 }
159
160 if ( $parserOutput === false ) {
161 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, $linestart );
162 if ( $cacheArticle ) {
163 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
164 }
165 }
166
167 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
168 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
169
170 $this->addHTML( $parserOutput->getText() );
171
172 }
173
174 # Set the maximum cache time on the Squid in seconds
175 function setSquidMaxage( $maxage ) {
176 global $wgSquidMaxage;
177 $wgSquidMaxage = $maxage;
178 }
179
180 function sendCacheControl() {
181 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
182 # FIXME: This header may cause trouble with some versions of Internet Explorer
183 header( "Vary: Accept-Encoding, Cookie" );
184 if( $this->mLastModified != "" ) {
185 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( "session.name") ] ) &&
186 ! $this->isPrintable() )
187 {
188 if ( $wgUseESI ) {
189 # We'll purge the proxy cache explicitly, but require end user agents
190 # to revalidate against the proxy on each visit.
191 # Surrogate-Control controls our Squid, Cache-Control downstream caches
192 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
193 # start with a shorter timeout for initial testing
194 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
195 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$wgSquidMaxage.', content="ESI/1.0"');
196 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
197 } else {
198 # We'll purge the proxy cache for anons explicitly, but require end user agents
199 # to revalidate against the proxy on each visit.
200 # IMPORTANT! The Squid needs to replace the Cache-Control header with
201 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
202 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
203 # start with a shorter timeout for initial testing
204 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
205 header( 'Cache-Control: s-maxage='.$wgSquidMaxage.', must-revalidate, max-age=0' );
206 }
207 } else {
208 # We do want clients to cache if they can, but they *must* check for updates
209 # on revisiting the page.
210 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
211 header( "Expires: -1" );
212 header( "Cache-Control: private, must-revalidate, max-age=0" );
213 }
214 header( "Last-modified: {$this->mLastModified}" );
215 } else {
216 wfDebug( "** no caching **\n", false );
217 header( "Expires: -1" );
218 header( "Cache-Control: no-cache" );
219 header( "Pragma: no-cache" );
220 header( "Last-modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
221 }
222 }
223
224 # Finally, all the text has been munged and accumulated into
225 # the object, let's actually output it:
226 #
227 function output()
228 {
229 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
230 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
231 global $wgDebugRedirects;
232 if( $this->mDoNothing ){
233 return;
234 }
235 $fname = "OutputPage::output";
236 wfProfileIn( $fname );
237
238 $sk = $wgUser->getSkin();
239
240 if ( "" != $this->mRedirect ) {
241 if( substr( $this->mRedirect, 0, 4 ) != "http" ) {
242 # Standards require redirect URLs to be absolute
243 global $wgServer;
244 $this->mRedirect = $wgServer . $this->mRedirect;
245 }
246 if( $this->mRedirectCode == '301') {
247 if( !$wgDebugRedirects ) {
248 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
249 }
250 $this->mLastModified = gmdate( "D, j M Y H:i:s" ) . " GMT";
251 }
252
253 $this->sendCacheControl();
254
255 if( $wgDebugRedirects ) {
256 $url = htmlspecialchars( $this->mRedirect );
257 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
258 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
259 print "</body>\n</html>\n";
260 } else {
261 header( "Location: {$this->mRedirect}" );
262 }
263 return;
264 }
265
266
267 $this->sendCacheControl();
268
269 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
270 header( "Content-language: {$wgLanguageCode}" );
271
272 $exp = time() + $wgCookieExpiration;
273 foreach( $this->mCookies as $name => $val ) {
274 setcookie( $name, $val, $exp, "/" );
275 }
276
277 $sk->outputPage( $this );
278 # flush();
279 }
280
281 function out( $ins )
282 {
283 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
284 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
285 $outs = $ins;
286 } else {
287 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
288 if ( false === $outs ) { $outs = $ins; }
289 }
290 print $outs;
291 }
292
293 function setEncodings()
294 {
295 global $wgInputEncoding, $wgOutputEncoding;
296 global $wgUser, $wgLang;
297
298 $wgInputEncoding = strtolower( $wgInputEncoding );
299
300 if( $wgUser->getOption( 'altencoding' ) ) {
301 $wgLang->setAltEncoding();
302 return;
303 }
304
305 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
306 $wgOutputEncoding = strtolower( $wgOutputEncoding );
307 return;
308 }
309
310 /*
311 # This code is unused anyway!
312 # Commenting out. --bv 2003-11-15
313
314 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
315 $best = 0.0;
316 $bestset = "*";
317
318 foreach ( $a as $s ) {
319 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
320 $set = $m[1];
321 $q = (float)($m[2]);
322 } else {
323 $set = $s;
324 $q = 1.0;
325 }
326 if ( $q > $best ) {
327 $bestset = $set;
328 $best = $q;
329 }
330 }
331 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
332 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
333 $wgOutputEncoding = strtolower( $bestset );
334
335 # Disable for now
336 #
337 */
338 $wgOutputEncoding = $wgInputEncoding;
339 }
340
341 # Returns a HTML comment with the elapsed time since request.
342 # This method has no side effects.
343 function reportTime()
344 {
345 global $wgRequestTime;
346
347 $now = wfTime();
348 list( $usec, $sec ) = explode( " ", $wgRequestTime );
349 $start = (float)$sec + (float)$usec;
350 $elapsed = $now - $start;
351 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
352 $elapsed );
353 return $com;
354 }
355
356 # Note: these arguments are keys into wfMsg(), not text!
357 #
358 function errorpage( $title, $msg )
359 {
360 global $wgTitle;
361
362 $this->mDebugtext .= "Original title: " .
363 $wgTitle->getPrefixedText() . "\n";
364 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
365 $this->setPageTitle( wfMsg( $title ) );
366 $this->setRobotpolicy( "noindex,nofollow" );
367 $this->setArticleRelated( false );
368
369 $this->mBodytext = "";
370 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
371 $this->returnToMain( false );
372
373 $this->output();
374 wfAbruptExit();
375 }
376
377 function sysopRequired()
378 {
379 global $wgUser;
380
381 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
382 $this->setPageTitle( wfMsg( "sysoptitle" ) );
383 $this->setRobotpolicy( "noindex,nofollow" );
384 $this->setArticleRelated( false );
385 $this->mBodytext = "";
386
387 $sk = $wgUser->getSkin();
388 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
389 $this->addHTML( wfMsg( "sysoptext", $ap ) );
390 $this->returnToMain();
391 }
392
393 function developerRequired()
394 {
395 global $wgUser;
396
397 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
398 $this->setPageTitle( wfMsg( "developertitle" ) );
399 $this->setRobotpolicy( "noindex,nofollow" );
400 $this->setArticleRelated( false );
401 $this->mBodytext = "";
402
403 $sk = $wgUser->getSkin();
404 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
405 $this->addHTML( wfMsg( "developertext", $ap ) );
406 $this->returnToMain();
407 }
408
409 function loginToUse()
410 {
411 global $wgUser, $wgTitle, $wgLang;
412
413 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
414 $this->setPageTitle( wfMsg( "loginreqtitle" ) );
415 $this->setRobotpolicy( "noindex,nofollow" );
416 $this->setArticleFlag( false );
417 $this->mBodytext = "";
418 $this->addWikiText( wfMsg( "loginreqtext" ) );
419
420 # We put a comment in the .html file so a Sysop can diagnose the page the
421 # user can't see.
422 $this->addHTML( "\n<!--" .
423 $wgLang->getNsText( $wgTitle->getNamespace() ) .
424 ":" .
425 $wgTitle->getDBkey() . "-->" );
426 $this->returnToMain(); # Flip back to the main page after 10 seconds.
427 }
428
429 function databaseError( $fname, &$conn )
430 {
431 global $wgUser, $wgCommandLineMode;
432
433 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
434 $this->setRobotpolicy( "noindex,nofollow" );
435 $this->setArticleRelated( false );
436
437 if ( $wgCommandLineMode ) {
438 $msg = wfMsgNoDB( "dberrortextcl" );
439 } else {
440 $msg = wfMsgNoDB( "dberrortext" );
441 }
442
443 $msg = str_replace( "$1", htmlspecialchars( $conn->lastQuery() ), $msg );
444 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
445 $msg = str_replace( "$3", $conn->lastErrno(), $msg );
446 $msg = str_replace( "$4", htmlspecialchars( $conn->lastError() ), $msg );
447
448 if ( $wgCommandLineMode || !is_object( $wgUser )) {
449 print "$msg\n";
450 wfAbruptExit();
451 }
452 $sk = $wgUser->getSkin();
453 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
454 wfMsgNoDB( "searchingwikipedia" ) );
455 $msg = str_replace( "$5", $shlink, $msg );
456 $this->mBodytext = $msg;
457 $this->output();
458 wfAbruptExit();
459 }
460
461 function readOnlyPage( $source = "", $protected = false )
462 {
463 global $wgUser, $wgReadOnlyFile;
464
465 $this->setRobotpolicy( "noindex,nofollow" );
466 $this->setArticleRelated( false );
467
468 if( $protected ) {
469 $this->setPageTitle( wfMsg( "viewsource" ) );
470 $this->addWikiText( wfMsg( "protectedtext" ) );
471 } else {
472 $this->setPageTitle( wfMsg( "readonly" ) );
473 $reason = file_get_contents( $wgReadOnlyFile );
474 $this->addHTML( wfMsg( "readonlytext", $reason ) );
475 }
476
477 if($source) {
478 $rows = $wgUser->getOption( "rows" );
479 $cols = $wgUser->getOption( "cols" );
480 $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
481 htmlspecialchars( $source ) . "\n</textarea>";
482 $this->addHTML( $text );
483 }
484
485 $this->returnToMain( false );
486 }
487
488 function fatalError( $message )
489 {
490 $this->setPageTitle( wfMsg( "internalerror" ) );
491 $this->setRobotpolicy( "noindex,nofollow" );
492 $this->setArticleRelated( false );
493
494 $this->mBodytext = $message;
495 $this->output();
496 wfAbruptExit();
497 }
498
499 function unexpectedValueError( $name, $val )
500 {
501 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
502 }
503
504 function fileCopyError( $old, $new )
505 {
506 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
507 }
508
509 function fileRenameError( $old, $new )
510 {
511 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
512 }
513
514 function fileDeleteError( $name )
515 {
516 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
517 }
518
519 function fileNotFoundError( $name )
520 {
521 $this->fatalError( wfMsg( "filenotfound", $name ) );
522 }
523
524 function returnToMain( $auto = true )
525 {
526 global $wgUser, $wgOut, $returnto;
527
528 $sk = $wgUser->getSkin();
529 if ( "" == $returnto ) {
530 $returnto = wfMsg( "mainpage" );
531 }
532 $link = $sk->makeKnownLink( $returnto, "" );
533
534 $r = wfMsg( "returnto", $link );
535 if ( $auto ) {
536 $titleObj = Title::newFromText( $returnto );
537 $wgOut->addMeta( "http:Refresh", "10;url=" . $titleObj->getUrl( "", true, true ));
538 }
539 $wgOut->addHTML( "\n<p>$r\n" );
540 }
541
542 /* private */ function headElement()
543 {
544 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
545
546 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
547
548 if ( "" == $this->mHTMLtitle ) {
549 $this->mHTMLtitle = $this->mPagetitle;
550 }
551 $rtl = $wgLang->isRTL() ? " dir='RTL'" : "";
552 $ret .= "<html lang=\"$wgLanguageCode\"$rtl><head><title>{$this->mHTMLtitle}</title>\n";
553 array_push( $this->mMetatags, array( "http:Content-type", "text/html; charset={$wgOutputEncoding}" ) );
554 foreach ( $this->mMetatags as $tag ) {
555 if ( 0 == strcasecmp( "http:", substr( $tag[0], 0, 5 ) ) ) {
556 $a = "http-equiv";
557 $tag[0] = substr( $tag[0], 5 );
558 } else {
559 $a = "name";
560 }
561 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
562 }
563 $p = $this->mRobotpolicy;
564 if ( "" == $p ) { $p = "index,follow"; }
565 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
566
567 if ( count( $this->mKeywords ) > 0 ) {
568 $ret .= "<meta name=\"keywords\" content=\"" .
569 implode( ",", $this->mKeywords ) . "\">\n";
570 }
571 foreach ( $this->mLinktags as $tag ) {
572 $ret .= "<link ";
573 if ( "" != $tag[0] ) { $ret .= "rel=\"{$tag[0]}\" "; }
574 if ( "" != $tag[1] ) { $ret .= "rev=\"{$tag[1]}\" "; }
575 $ret .= "href=\"{$tag[2]}\">\n";
576 }
577 $sk = $wgUser->getSkin();
578 $ret .= $sk->getHeadScripts();
579 $ret .= $sk->getUserStyles();
580
581 $ret .= "</head>\n";
582 return $ret;
583 }
584 }
585 ?>