* Fix explicit s-maxage=0 on raw pages; should help with proxy issues in
[lhc/web/wiklou.git] / includes / RawPage.php
1 <?php
2 /**
3 * Copyright (C) 2004 Gabriel Wicke <gw@wikidev.net>
4 * http://www.aulinx.de/
5 * Based on PageHistory and SpecialExport
6 *
7 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
8 *
9 * @author Gabriel Wicke <gw@wikidev.net>
10 * @package MediaWiki
11 */
12
13 /** */
14 require_once( 'Revision.php' );
15
16 /**
17 * @todo document
18 * @package MediaWiki
19 */
20 class RawPage {
21 var $mArticle, $mTitle, $mRequest;
22 var $mOldId, $mGen, $mCharset;
23 var $mSmaxage, $mMaxage;
24 var $mContentType, $mExpandTemplates;
25
26 function RawPage( &$article, $request = false ) {
27 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
28
29 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
30 $this->mArticle =& $article;
31 $this->mTitle =& $article->mTitle;
32
33 if ( $request === false ) {
34 $this->mRequest =& $wgRequest;
35 } else {
36 $this->mRequest = $request;
37 }
38
39 $ctype = $this->mRequest->getVal( 'ctype' );
40 $smaxage = $this->mRequest->getIntOrNull( 'smaxage', $wgSquidMaxage );
41 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
42 $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
43 $this->mOldId = $this->mRequest->getInt( 'oldid' );
44 # special case for 'generated' raw things: user css/js
45 $gen = $this->mRequest->getVal( 'gen' );
46
47 if($gen == 'css') {
48 $this->mGen = $gen;
49 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
50 if($ctype == '') $ctype = 'text/css';
51 } elseif ($gen == 'js') {
52 $this->mGen = $gen;
53 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
54 if($ctype == '') $ctype = $wgJsMimeType;
55 } else {
56 $this->mGen = false;
57 }
58 $this->mCharset = $wgInputEncoding;
59 $this->mSmaxage = intval( $smaxage );
60 $this->mMaxage = $maxage;
61 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
62 $this->mContentType = 'text/x-wiki';
63 } else {
64 $this->mContentType = $ctype;
65 }
66 }
67
68 function view() {
69 global $wgOut, $wgScript;
70
71 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
72 # Normally we use PHP_SELF to get the URL to the script
73 # as it was called, minus the query string.
74 #
75 # Some sites use Apache rewrite rules to handle subdomains,
76 # and have PHP set up in a weird way that causes PHP_SELF
77 # to contain the rewritten URL instead of the one that the
78 # outside world sees.
79 #
80 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
81 # provides containing the "before" URL.
82 $url = $_SERVER['SCRIPT_URL'];
83 } else {
84 $url = $_SERVER['PHP_SELF'];
85 }
86
87 $ua = @$_SERVER['HTTP_USER_AGENT'];
88 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
89 # Internet Explorer will ignore the Content-Type header if it
90 # thinks it sees a file extension it recognizes. Make sure that
91 # all raw requests are done through the script node, which will
92 # have eg '.php' and should remain safe.
93 #
94 # We used to redirect to a canonical-form URL as a general
95 # backwards-compatibility / good-citizen nice thing. However
96 # a lot of servers are set up in buggy ways, resulting in
97 # redirect loops which hang the browser until the CSS load
98 # times out.
99 #
100 # Just return a 403 Forbidden and get it over with.
101 wfHttpError( 403, 'Forbidden',
102 'Raw pages must be accessed through the primary script entry point.' );
103 return;
104 }
105
106 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
107 # allow the client to cache this for 24 hours
108 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
109 echo $this->getRawText();
110 $wgOut->disable();
111 }
112
113 function getRawText() {
114 global $wgUser, $wgOut;
115 if($this->mGen) {
116 $sk = $wgUser->getSkin();
117 $sk->initPage($wgOut);
118 if($this->mGen == 'css') {
119 return $sk->getUserStylesheet();
120 } else if($this->mGen == 'js') {
121 return $sk->getUserJs();
122 }
123 } else {
124 return $this->getArticleText();
125 }
126 }
127
128 function getArticleText() {
129 global $wgParser;
130
131 if( $this->mTitle ) {
132 $text = '';
133
134 // If it's a MediaWiki message we can just hit the message cache
135 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
136 $text = wfMsgForContentNoTrans( $this->mTitle->getDbkey() );
137 } else {
138 // Get it from the DB
139 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
140 if ( $rev ) {
141 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
142 header( "Last-modified: $lastmod" );
143 $text = $rev->isDeleted() ? '' : $rev->getText();
144 } else
145 $text = '';
146 }
147
148 return $this->parseArticleText( $text );
149 }
150
151 # Bad title or page does not exist
152 if( $this->mContentType == 'text/x-wiki' ) {
153 # Don't return a 404 response for CSS or JavaScript;
154 # 404s aren't generally cached and it would create
155 # extra hits when user CSS/JS are on and the user doesn't
156 # have the pages.
157 header( "HTTP/1.0 404 Not Found" );
158 }
159 return '';
160 }
161
162 function parseArticleText( $text ) {
163 if ( $text === '' )
164 return '';
165 else
166 if ( $this->mExpandTemplates ) {
167 global $wgTitle;
168
169 $parser = new Parser();
170 $parser->Options( new ParserOptions() ); // We don't want this to be user-specific
171 $parser->Title( $wgTitle );
172 $parser->OutputType( OT_HTML );
173
174 return $parser->replaceVariables( $text );
175 } else
176 return $text;
177 }
178 }
179 ?>