More paranoia checks for Internet Explorer
[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 * @todo document
15 * @package MediaWiki
16 */
17 class RawPage {
18
19 function RawPage( $article ) {
20 global $wgRequest, $wgInputEncoding, $wgSquidMaxage;
21 $allowedCTypes = array('text/x-wiki', 'text/javascript', 'text/css', 'application/x-zope-edit');
22 $this->mArticle =& $article;
23 $this->mTitle =& $article->mTitle;
24
25 $ctype = $wgRequest->getText( 'ctype' );
26 $smaxage = $wgRequest->getInt( 'smaxage', $wgSquidMaxage );
27 $maxage = $wgRequest->getInt( 'maxage', $wgSquidMaxage );
28 $this->mOldId = $wgRequest->getInt( 'oldid' );
29 # special case for 'generated' raw things: user css/js
30 $gen = $wgRequest->getText( 'gen' );
31 if($gen == 'css') {
32 $this->mGen = $gen;
33 if($smaxage == '') $smaxage = $wgSquidMaxage;
34 if($ctype == '') $ctype = 'text/css';
35 } else if ($gen == 'js') {
36 $this->mGen = $gen;
37 if($smaxage == '') $smaxage = $wgSquidMaxage;
38 if($ctype == '') $ctype = 'text/javascript';
39 } else {
40 $this->mGen = false;
41 }
42 $this->mCharset = $wgInputEncoding;
43 $this->mSmaxage = $smaxage;
44 $this->mMaxage = $maxage;
45 if(empty($ctype) or !in_array($ctype, $allowedCTypes)) {
46 $this->mContentType = 'text/x-wiki';
47 } else {
48 $this->mContentType = $ctype;
49 }
50 }
51
52 function view() {
53 global $wgUser, $wgOut, $wgScript;
54
55 if( strncmp( $wgScript . '?', $_SERVER['REQUEST_URI'], strlen( $wgScript ) + 1 ) ) {
56 # Internet Explorer will ignore the Content-Type header if it
57 # thinks it sees a file extension it recognizes. Make sure that
58 # all raw requests are done through the script node, which will
59 # have eg '.php' and should remain safe.
60
61 $destUrl = $this->mTitle->getFullUrl(
62 'action=raw' .
63 '&ctype=' . urlencode( $this->mContentType ) .
64 '&smaxage=' . urlencode( $this->mSmaxage ) .
65 '&maxage=' . urlencode( $this->mMaxage ) .
66 '&oldid=' . urlencode( $this->mOldId ) );
67 header( 'Location: ' . $destUrl );
68 $wgOut->disable();
69 return;
70 }
71
72 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
73 # allow the client to cache this for 24 hours
74 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
75 if($this->mGen) {
76 $sk = $wgUser->getSkin();
77 $sk->initPage($wgOut);
78 if($this->mGen == 'css') {
79 echo $sk->getUserStylesheet();
80 } else if($this->mGen == 'js') {
81 echo $sk->getUserJs();
82 }
83 } else {
84 echo $this->getrawtext();
85 }
86 $wgOut->disable();
87 }
88
89 function getrawtext () {
90 global $wgInputEncoding, $wgContLang;
91 $fname = 'RawPage::getrawtext';
92
93 if( !$this->mTitle ) return '';
94 $dbr =& wfGetDB( DB_SLAVE );
95 extract( $dbr->tableNames( 'cur', 'old' ) );
96
97 $t = $dbr->strencode( $this->mTitle->getDBKey() );
98 $ns = $this->mTitle->getNamespace();
99 # special case
100 if($ns == NS_MEDIAWIKI) {
101 $rawtext = wfMsg($t);
102 return $rawtext;
103 }
104 # else get it from the DB
105 if(!empty($this->mOldId)) {
106 $sql = "SELECT old_text AS text,old_timestamp AS timestamp,".
107 "old_user AS user,old_flags AS flags FROM $old " .
108 "WHERE old_id={$this->mOldId}";
109 } else {
110 $sql = "SELECT cur_id as id,cur_timestamp as timestamp,cur_user as user,cur_user_text as user_text," .
111 "cur_restrictions as restrictions,cur_comment as comment,cur_text as text FROM $cur " .
112 "WHERE cur_namespace=$ns AND cur_title='$t'";
113 }
114 $res = $dbr->query( $sql, $fname );
115 if( $s = $dbr->fetchObject( $res ) ) {
116 $rawtext = Article::getRevisionText( $s, "" );
117 header( 'Last-modified: '.gmdate( "D, j M Y H:i:s", wfTimestamp2Unix( $s->timestamp )).' GMT' );
118 return $rawtext;
119 } else {
120 return '';
121 }
122 }
123 }
124 ?>