More unitialized variable cleanup && 'pure' register_globals cleanup...
[lhc/web/wiklou.git] / index.php
1 <?php
2 # Main wiki script; see design.doc
3 #
4 $wgRequestTime = microtime();
5
6 ## Enable this to debug total elimination of register_globals
7 #define( "DEBUG_GLOBALS", 1 );
8
9 if( defined('DEBUG_GLOBALS') ) error_reporting(E_ALL);
10
11 function &fix_magic_quotes( &$arr ) {
12 foreach( $arr as $key => $val ) {
13 if( is_array( $val ) ) {
14 fix_magic_quotes( $arr[$key] );
15 } else {
16 $arr[$key] = stripslashes( $val );
17 }
18 }
19 return $arr;
20 }
21
22 if ( get_magic_quotes_gpc() ) {
23 fix_magic_quotes( $_COOKIE );
24 fix_magic_quotes( $_ENV );
25 fix_magic_quotes( $_GET );
26 fix_magic_quotes( $_POST );
27 fix_magic_quotes( $_REQUEST );
28 fix_magic_quotes( $_SERVER );
29 } elseif( defined('DEBUG_GLOBALS') ) {
30 die("DEBUG_GLOBALS: turn on magic_quotes_gpc" );
31 }
32
33 if( defined('DEBUG_GLOBALS') ) {
34 if( ini_get( "register_globals" ) ) {
35 die( "DEBUG_GLOBALS: turn off register_globals" );
36 }
37 } elseif( !ini_get( "register_globals" ) ) {
38 # Insecure, but at least it'll run
39 import_request_variables( "GPC" );
40 }
41
42 unset( $IP );
43 ini_set( "allow_url_fopen", 0 ); # For security...
44 if(!file_exists("LocalSettings.php")) {
45 die( "You'll have to <a href='config/index.php'>set the wiki up</a> first!" );
46 }
47 include_once( "./LocalSettings.php" );
48
49 if( $wgSitename == "MediaWiki" ) {
50 die( "You must set the site name in \$wgSitename before installation.\n\n" );
51 }
52
53 # PATH_SEPARATOR avaialble only from 4.3.0
54 $sep = (DIRECTORY_SEPARATOR == "\\") ? ";" : ":";
55 ini_set( "include_path", $IP . $sep . ini_get( "include_path" ) );
56
57 include_once( "Setup.php" );
58
59 wfProfileIn( "main-misc-setup" );
60 OutputPage::setEncodings(); # Not really used yet
61
62 # Query string fields
63 if( empty( $_REQUEST['action'] ) ) {
64 $action = "view";
65 } else {
66 $action = $_REQUEST['action'];
67 }
68
69 if( isset( $_SERVER['PATH_INFO'] ) ) {
70 $title = substr( $_SERVER['PATH_INFO'], 1 );
71 } elseif( !empty( $_REQUEST['title'] ) ) {
72 $title = $_REQUEST['title'];
73 } else {
74 $title = "";
75 }
76
77 # Placeholders in case of DB error
78 $wgTitle = Title::newFromText( wfMsg( "badtitle" ) );
79 $wgArticle = new Article($wgTitle);
80
81 $action = strtolower( trim( $action ) );
82 if ( "" == $action ) { $action = "view"; }
83 if ( !empty( $_REQUEST['printable'] ) && $_REQUEST['printable'] == "yes") {
84 $wgOut->setPrintable();
85 }
86
87 if ( "" == $title && "delete" != $action ) {
88 $wgTitle = Title::newFromText( wfMsg( "mainpage" ) );
89 } elseif ( !empty( $_REQUEST['curid'] ) ) {
90 # URLs like this are generated by RC, because rc_title isn't always accurate
91 $wgTitle = Title::newFromID( $_REQUEST['curid'] );
92 } else {
93 $wgTitle = Title::newFromURL( $title );
94 }
95 wfProfileOut( "main-misc-setup" );
96
97 # If the user is not logged in, the Namespace:title of the article must be in the Read array in
98 # order for the user to see it.
99 if ( !$wgUser->getID() && is_array( $wgWhitelistRead ) && $wgTitle) {
100 if ( !in_array( $wgLang->getNsText( $wgTitle->getNamespace() ) . ":" . $wgTitle->getDBkey(), $wgWhitelistRead ) ) {
101 $wgOut->loginToUse();
102 $wgOut->output();
103 exit;
104 }
105 }
106
107 if ( !empty( $_REQUEST['search'] ) ) {
108 if( isset($_REQUEST['fulltext']) ) {
109 wfSearch( $_REQUEST['search'] );
110 } else {
111 wfGo( $_REQUEST['search'] );
112 }
113 } else if( !$wgTitle or $wgTitle->getInterwiki() != "" or $wgTitle->getDBkey() == "" ) {
114 $wgTitle = Title::newFromText( wfMsg( "badtitle" ) );
115 $wgOut->errorpage( "badtitle", "badtitletext" );
116 } else if ( ( $action == "view" ) && $wgTitle->getPrefixedDBKey() != $title ) {
117 /* redirect to canonical url, make it a 301 to allow caching */
118 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ), '301');
119 } else if ( Namespace::getSpecial() == $wgTitle->getNamespace() ) {
120 wfSpecialPage();
121 } else {
122 if ( Namespace::getMedia() == $wgTitle->getNamespace() ) {
123 $wgTitle = Title::makeTitle( Namespace::getImage(), $wgTitle->getDBkey() );
124 }
125
126 switch( $wgTitle->getNamespace() ) {
127 case 6:
128 include_once( "ImagePage.php" );
129 $wgArticle = new ImagePage( $wgTitle );
130 break;
131 default:
132 $wgArticle = new Article( $wgTitle );
133 }
134
135 wfQuery("BEGIN", DB_WRITE);
136 switch( $action ) {
137 case "view":
138 case "watch":
139 case "unwatch":
140 case "delete":
141 case "revert":
142 case "rollback":
143 case "protect":
144 case "unprotect":
145 $wgArticle->$action();
146 break;
147 case "print":
148 $wgArticle->view();
149 break;
150 case "edit":
151 case "submit":
152 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
153 User::SetupSession();
154 }
155 include_once( "EditPage.php" );
156 $editor = new EditPage( $wgArticle );
157 $editor->$action();
158 break;
159 case "history":
160 include_once( "PageHistory.php" );
161 $history = new PageHistory( $wgArticle );
162 $history->history();
163 break;
164 default:
165 $wgOut->errorpage( "nosuchaction", "nosuchactiontext" );
166 }
167 wfQuery("COMMIT", DB_WRITE);
168 }
169
170 $wgOut->output();
171 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }
172 logProfilingData();
173 wfDebug( "Request ended normally\n" );
174 ?>