* Fixed unclosed <p> tag
[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
22 function RawPage( $article ) {
23 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
24 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
25 $this->mArticle =& $article;
26 $this->mTitle =& $article->mTitle;
27
28 $ctype = $wgRequest->getText( 'ctype' );
29 $smaxage = $wgRequest->getInt( 'smaxage', $wgSquidMaxage );
30 $maxage = $wgRequest->getInt( 'maxage', $wgSquidMaxage );
31 $this->mOldId = $wgRequest->getInt( 'oldid' );
32 # special case for 'generated' raw things: user css/js
33 $gen = $wgRequest->getText( 'gen' );
34 if($gen == 'css') {
35 $this->mGen = $gen;
36 if($smaxage == '') $smaxage = $wgSquidMaxage;
37 if($ctype == '') $ctype = 'text/css';
38 } else if ($gen == 'js') {
39 $this->mGen = $gen;
40 if($smaxage == '') $smaxage = $wgSquidMaxage;
41 if($ctype == '') $ctype = $wgJsMimeType;
42 } else {
43 $this->mGen = false;
44 }
45 $this->mCharset = $wgInputEncoding;
46 $this->mSmaxage = $smaxage;
47 $this->mMaxage = $maxage;
48 if(empty($ctype) or !in_array($ctype, $allowedCTypes)) {
49 $this->mContentType = 'text/x-wiki';
50 } else {
51 $this->mContentType = $ctype;
52 }
53 }
54
55 function view() {
56 global $wgUser, $wgOut, $wgScript;
57
58 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
59 # Normally we use PHP_SELF to get the URL to the script
60 # as it was called, minus the query string.
61 #
62 # Some sites use Apache rewrite rules to handle subdomains,
63 # and have PHP set up in a weird way that causes PHP_SELF
64 # to contain the rewritten URL instead of the one that the
65 # outside world sees.
66 #
67 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
68 # provides containing the "before" URL.
69 $url = $_SERVER['SCRIPT_URL'];
70 } else {
71 $url = $_SERVER['PHP_SELF'];
72 }
73 if( strcmp( $wgScript, $url ) ) {
74 # Internet Explorer will ignore the Content-Type header if it
75 # thinks it sees a file extension it recognizes. Make sure that
76 # all raw requests are done through the script node, which will
77 # have eg '.php' and should remain safe.
78 #
79 # We used to redirect to a canonical-form URL as a general
80 # backwards-compatibility / good-citizen nice thing. However
81 # a lot of servers are set up in buggy ways, resulting in
82 # redirect loops which hang the browser until the CSS load
83 # times out.
84 #
85 # Just return a 403 Forbidden and get it over with.
86 wfHttpError( 403, 'Forbidden',
87 'Raw pages must be accessed through the primary script entry point.' );
88 return;
89 }
90
91 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
92 # allow the client to cache this for 24 hours
93 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
94 if($this->mGen) {
95 $sk = $wgUser->getSkin();
96 $sk->initPage($wgOut);
97 if($this->mGen == 'css') {
98 echo $sk->getUserStylesheet();
99 } else if($this->mGen == 'js') {
100 echo $sk->getUserJs();
101 }
102 } else {
103 echo $this->getrawtext();
104 }
105 $wgOut->disable();
106 }
107
108 function getrawtext () {
109 global $wgInputEncoding, $wgContLang;
110 $fname = 'RawPage::getrawtext';
111
112 if( $this->mTitle ) {
113 # Special case for MediaWiki: messages; we can hit the message cache.
114 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI) {
115 $rawtext = wfMsgForContent( $this->mTitle->getDbkey() );
116 return $rawtext;
117 }
118
119 # else get it from the DB
120 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
121 if( $rev ) {
122 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
123 header( 'Last-modified: ' . $lastmod );
124 return $rev->getText();
125 }
126 }
127
128 # Bad title or page does not exist
129 if( $this->mContentType == 'text/x-wiki' ) {
130 # Don't return a 404 response for CSS or JavaScript;
131 # 404s aren't generally cached and it would create
132 # extra hits when user CSS/JS are on and the user doesn't
133 # have the pages.
134 header( "HTTP/1.0 404 Not Found" );
135 }
136 return '';
137 }
138 }
139 ?>