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