Commit JeLuF's register_globals fixes, first phase
[lhc/web/wiklou.git] / includes / SpecialPreferences.php
1 <?
2 function wfSpecialPreferences()
3 {
4 global $wgUser, $wgOut, $wgUseDynamicDates, $action;
5 global $wpSaveprefs, $wpReset;
6
7 $fields = array( "wpOldpass", "wpNewpass", "wpRetypePass",
8 "wpUserEmail", "wpNick" );
9 wfCleanFormFields( $fields );
10
11 if ( 0 == $wgUser->getID() ) {
12 $wgOut->errorpage( "prefsnologin", "prefsnologintext" );
13 return;
14 }
15 if ( wfReadOnly() ) {
16 $wgOut->readOnlyPage();
17 return;
18 }
19 if ( isset( $wpReset ) ) {
20 resetPrefs();
21 mainPrefsForm( WfMsg( "prefsreset" ) );
22 } else if ( "submit" == $action || isset( $wpSaveprefs ) ) {
23 savePreferences();
24 } else {
25 resetPrefs();
26 mainPrefsForm( "" );
27 }
28 }
29
30 /* private */ function validateInt( &$val, $min=0, $max=0x7fffffff ) {
31 $val = intval($val);
32 $val = min($val, $max);
33 $val = max($val, $min);
34 return $val;
35 }
36
37 /* private */ function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
38 $val = trim($val);
39 if($val === "") {
40 return $val;
41 } else {
42 return validateInt( $val, $min, $max );
43 }
44 }
45
46
47 /* private */ function validateCheckbox( $cb )
48 {
49 if ( $cb )
50 {
51 return 1;
52 }
53 else
54 {
55 return 0;
56 }
57 }
58
59
60
61 /* private */ function savePreferences()
62 {
63 global $wgUser, $wgLang, $wgDeferredUpdateList;
64 global $wpQuickbar, $wpOldpass, $wpNewpass, $wpRetypePass;
65 global $wpSkin, $wpMath, $wpDate, $wpUserEmail, $wpEmailFlag, $wpNick, $wpSearch, $wpRecent;
66 global $wpSearchLines, $wpSearchChars, $wpStubs;
67 global $wpRows, $wpCols, $wpHourDiff, $HTTP_POST_VARS;
68 $wpQuickbar = $_REQUEST["wpQuickbar"];
69 $wpOldpass = $_REQUEST["wpOldpass"];
70 $wpNewpass = $_REQUEST["wpNewpass"];
71 $wpRetypePass = $_REQUEST["wpRetypePass"];
72 $wpSkin = $_REQUEST["wpSkin"];
73 $wpMath = $_REQUEST["wpMath"];
74 $wpDate = $_REQUEST["wpDate"];
75 $wpUserEmail = $_REQUEST["wpUserEmail"];
76 $wpEmailFlag = $_REQUEST["wpEmailFlag"];
77 $wpNick = $_REQUEST["wpNick"];
78 $wpSearch = $_REQUEST["wpSearch"];
79 $wpRecent = $_REQUEST["wpRecent"];
80 $wpSearchLines = $_REQUEST["wpSearchLines"];
81 $wpSearchChars = $_REQUEST["wpSearchChars"];
82 $wpStubs = $_REQUEST["wpStubs"];
83 $wpRows = $_REQUEST["wpRows"];
84 $wpCols = $_REQUEST["wpCols"];
85 $wpHourDiff = $_REQUEST["wpHourDiff"];
86
87
88 if ( "" != $wpNewpass ) {
89 if ( $wpNewpass != $wpRetypePass ) {
90 mainPrefsForm( wfMsg( "badretype" ) );
91 return;
92 }
93 $ep = $wgUser->encryptPassword( $wpOldpass );
94 if ( $ep != $wgUser->getPassword() ) {
95 if ( $ep != $wgUser->getNewpassword() ) {
96 mainPrefsForm( wfMsg( "wrongpassword" ) );
97 return;
98 }
99 }
100 $wgUser->setPassword( $wpNewpass );
101 }
102 $wgUser->setEmail( $wpUserEmail );
103 $wgUser->setOption( "nickname", $wpNick );
104 $wgUser->setOption( "quickbar", $wpQuickbar );
105 $wgUser->setOption( "skin", $wpSkin );
106 $wgUser->setOption( "math", $wpMath );
107 $wgUser->setOption( "date", $wpDate );
108 $wgUser->setOption( "searchlimit", validateIntOrNull( $wpSearch ) );
109 $wgUser->setOption( "contextlines", validateIntOrNull( $wpSearchLines ) );
110 $wgUser->setOption( "contextchars", validateIntOrNull( $wpSearchChars ) );
111 $wgUser->setOption( "rclimit", validateIntOrNull( $wpRecent ) );
112 $wgUser->setOption( "rows", validateInt( $wpRows, 4, 1000 ) );
113 $wgUser->setOption( "cols", validateInt( $wpCols, 4, 1000 ) );
114 $wgUser->setOption( "stubthreshold", validateIntOrNull( $wpStubs ) );
115 $wgUser->setOption( "timecorrection", validateIntOrNull( $wpHourDiff, -12, 14 ) );
116
117 $namespaces = $wgLang->getNamespaces();
118 # Set search namespace options
119 # Note: namespaces don't necessarily have consecutive keys
120 foreach ( $namespaces as $i => $namespaces ) {
121 if ( $i >= 0 ) {
122 $nsvar = "wpNs$i";
123 $wgUser->setOption( "searchNs{$i}", validateCheckbox( $_REQUEST[$nsvar] ) );
124 }
125 }
126
127 $wgUser->setOption( "disablemail", validateCheckbox( $wpEmailFlag ) );
128
129 $togs = $wgLang->getUserToggles();
130 foreach ( $togs as $tname => $ttext ) {
131 if ( array_key_exists( "wpOp$tname", $HTTP_POST_VARS ) ) {
132 $wgUser->setOption( $tname, 1 );
133 } else {
134 $wgUser->setOption( $tname, 0 );
135 }
136 }
137 $wgUser->setCookies();
138 $up = new UserUpdate();
139 array_push( $wgDeferredUpdateList, $up );
140 mainPrefsForm( wfMsg( "savedprefs" ) );
141 }
142
143 /* private */ function resetPrefs()
144 {
145 global $wgUser, $wgLang;
146 global $wpQuickbar, $wpOldpass, $wpNewpass, $wpRetypePass, $wpStubs;
147 global $wpRows, $wpCols, $wpSkin, $wpMath, $wpDate, $wpUserEmail, $wpEmailFlag, $wpNick;
148 global $wpSearch, $wpRecent, $HTTP_POST_VARS;
149 global $wpHourDiff, $wpSearchLines, $wpSearchChars;
150
151 $wpOldpass = $wpNewpass = $wpRetypePass = "";
152 $wpUserEmail = $wgUser->getEmail();
153 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $wpEmailFlag = 1; }
154 else { $wpEmailFlag = 0; }
155 $wpNick = $wgUser->getOption( "nickname" );
156
157 $wpQuickbar = $wgUser->getOption( "quickbar" );
158 $wpSkin = $wgUser->getOption( "skin" );
159 $wpMath = $wgUser->getOption( "math" );
160 $wpDate = $wgUser->getOption( "date" );
161 $wpRows = $wgUser->getOption( "rows" );
162 $wpCols = $wgUser->getOption( "cols" );
163 $wpStubs = $wgUser->getOption( "stubthreshold" );
164 $wpHourDiff = $wgUser->getOption( "timecorrection" );
165 $wpSearch = $wgUser->getOption( "searchlimit" );
166 $wpSearchLines = $wgUser->getOption( "contextlines" );
167 $wpSearchChars = $wgUser->getOption( "contextchars" );
168 $wpRecent = $wgUser->getOption( "rclimit" );
169
170 $togs = $wgLang->getUserToggles();
171 foreach ( $togs as $tname => $ttext ) {
172 $HTTP_POST_VARS["wpOp$tname"] = $wgUser->getOption( $tname );
173 }
174 }
175
176 /* private */ function namespacesCheckboxes()
177 {
178 global $wgLang, $wgUser;
179
180 # Determine namespace checkboxes
181 $namespaces = $wgLang->getNamespaces();
182 $r1 = "";
183
184 foreach ( $namespaces as $i => $name ) {
185 # Skip special or anything similar
186 if ( $i >= 0 ) {
187 $checked = "";
188 if ( $wgUser->getOption( "searchNs$i" ) ) {
189 $checked = " checked";
190 }
191 $name = str_replace( "_", " ", $namespaces[$i] );
192 if ( "" == $name ) {
193 $name = wfMsg( "blanknamespace" );
194 }
195
196 if ( 0 != $i ) {
197 $r1 .= " ";
198 }
199 $r1 .= "<label><input type=checkbox value=\"1\" name=\"" .
200 "wpNs$i\"{$checked}>{$name}</label>\n";
201 }
202 }
203
204 return $r1;
205 }
206
207
208
209
210 /* private */ function mainPrefsForm( $err )
211 {
212 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates;
213 global $wpQuickbar, $wpOldpass, $wpNewpass, $wpRetypePass;
214 global $wpSkin, $wpMath, $wpDate, $wpUserEmail, $wpEmailFlag, $wpNick, $wpSearch, $wpRecent;
215 global $wpRows, $wpCols, $wpSaveprefs, $wpReset, $wpHourDiff;
216 global $wpSearchLines, $wpSearchChars, $wpStubs, $wgValidSkinNames;
217
218 $wgOut->setPageTitle( wfMsg( "preferences" ) );
219 $wgOut->setArticleFlag( false );
220 $wgOut->setRobotpolicy( "noindex,nofollow" );
221
222 if ( "" != $err ) {
223 $wgOut->addHTML( "<font size='+1' color='red'>$err</font>\n<p>" );
224 }
225 $uname = $wgUser->getName();
226 $uid = $wgUser->getID();
227
228 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
229
230 $qbs = $wgLang->getQuickbarSettings();
231 $skins = $wgLang->getSkinNames();
232 $mathopts = $wgLang->getMathNames();
233 $dateopts = $wgLang->getDateFormats();
234 $togs = $wgLang->getUserToggles();
235
236 $action = wfLocalUrlE( $wgLang->specialPage( "Preferences" ),
237 "action=submit" );
238 $qb = wfMsg( "qbsettings" );
239 $cp = wfMsg( "changepassword" );
240 $sk = wfMsg( "skin" );
241 $math = wfMsg( "math" );
242 $dateFormat = wfMsg("dateformat");
243 $opw = wfMsg( "oldpassword" );
244 $npw = wfMsg( "newpassword" );
245 $rpw = wfMsg( "retypenew" );
246 $svp = wfMsg( "saveprefs" );
247 $rsp = wfMsg( "resetprefs" );
248 $tbs = wfMsg( "textboxsize" );
249 $tbr = wfMsg( "rows" );
250 $tbc = wfMsg( "columns" );
251 $ltz = wfMsg( "localtime" );
252 $tzt = wfMsg( "timezonetext" );
253 $tzo = wfMsg( "timezoneoffset" );
254 $tzGuess = wfMsg( "guesstimezone" );
255 $tzServerTime = wfMsg( "servertime" );
256 $yem = wfMsg( "youremail" );
257 $emf = wfMsg( "emailflag" );
258 $ynn = wfMsg( "yournick" );
259 $stt = wfMsg ( "stubthreshold" ) ;
260 $srh = wfMsg( "searchresultshead" );
261 $rpp = wfMsg( "resultsperpage" );
262 $scl = wfMsg( "contextlines" );
263 $scc = wfMsg( "contextchars" );
264 $rcc = wfMsg( "recentchangescount" );
265 $dsn = wfMsg( "defaultns" );
266
267 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
268 method=\"post\"><table border=\"1\"><tr><td valign=top nowrap><b>$qb:</b><br>\n" );
269
270 # Quickbar setting
271 #
272 for ( $i = 0; $i < count( $qbs ); ++$i ) {
273 if ( $i == $wpQuickbar ) { $checked = " checked"; }
274 else { $checked = ""; }
275 $wgOut->addHTML( "<label><input type=radio name=\"wpQuickbar\"
276 value=\"$i\"$checked> {$qbs[$i]}</label><br>\n" );
277 }
278
279 # Fields for changing password
280 #
281 $wpOldpass = wfEscapeHTML( $wpOldpass );
282 $wpNewpass = wfEscapeHTML( $wpNewpass );
283 $wpRetypePass = wfEscapeHTML( $wpRetypePass );
284
285 $wgOut->addHTML( "</td><td vaign=top nowrap><b>$cp:</b><br>
286 <label>$opw: <input type=password name=\"wpOldpass\" value=\"$wpOldpass\" size=20></label><br>
287 <label>$npw: <input type=password name=\"wpNewpass\" value=\"$wpNewpass\" size=20></label><br>
288 <label>$rpw: <input type=password name=\"wpRetypePass\" value=\"$wpRetypePass\" size=20></label><br>
289 </td></tr>\n" );
290
291 # Skin setting
292 #
293 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$sk:</b><br>\n" );
294 # Only count up to count($wgValidSkinNames) rather than
295 # count($skins), to allow global disabling of experimental
296 # skins.
297 for ( $i = 0; $i < count( $wgValidSkinNames ); ++$i ) {
298 if ( $i == $wpSkin ) {
299 $checked = " checked";
300 } else {
301 $checked = "";
302 }
303 $wgOut->addHTML( "<label><input type=radio name=\"wpSkin\"
304 value=\"$i\"$checked> {$skins[$i]}</label><br>\n" );
305 }
306
307 # Various checkbox options
308 #
309 if ( $wgUseDynamicDates ) {
310 $wgOut->addHTML( "</td><td rowspan=3 valign=top nowrap>\n" );
311 } else {
312 $wgOut->addHTML( "</td><td rowspan=2 valign=top nowrap>\n" );
313 }
314 $wgOut->addHTML("<table border=0>");
315 foreach ( $togs as $tname => $ttext ) {
316 if ( 1 == $wgUser->getOption( $tname ) ) {
317 $checked = " checked";
318 } else {
319 $checked = "";
320 }
321 $wgOut->addHTML( "<tr valign=\"top\"><td><input type=checkbox value=\"1\" "
322 . "id=\"$tname\" name=\"wpOp$tname\"$checked></td><td><label for=\"$tname\">$ttext</label></td></tr>\n" );
323 }
324 $wgOut->addHTML( "</table></td>" );
325
326 # Math setting
327 #
328 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$math:</b><br>\n" );
329 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
330 if ( $i == $wpMath ) { $checked = " checked"; }
331 else { $checked = ""; }
332 $wgOut->addHTML( "<label><input type=radio name=\"wpMath\"
333 value=\"$i\"$checked> {$mathopts[$i]}</label><br>\n" );
334 }
335 $wgOut->addHTML( "</td></tr>" );
336
337 # Date format
338 #
339 if ( $wgUseDynamicDates ) {
340 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$dateFormat:</b><br>" );
341 for ( $i = 0; $i < count( $dateopts ); ++$i) {
342 if ( $i == $wpDate ) {
343 $checked = " checked";
344 } else {
345 $checked = "";
346 }
347 $wgOut->addHTML( "<label><input type=radio name=\"wpDate\" ".
348 "value=\"$i\"$checked> {$dateopts[$i]}</label><br>\n" );
349 }
350 $wgOut->addHTML( "</td></tr>");
351 }
352 # Textbox rows, cols
353 #
354 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
355 $nowserver = $wgLang->time( $now, false );
356 $wgOut->addHTML( "<td valign=top nowrap><b>$tbs:</b><br>
357 <label>$tbr: <input type=text name=\"wpRows\" value=\"{$wpRows}\" size=6></label><br>
358 <label>$tbc: <input type=text name=\"wpCols\" value=\"{$wpCols}\" size=6></label><br><br>
359 <b>$tzServerTime:</b> $nowserver<br />
360 <b>$ltz:</b> $nowlocal<br />
361 <label>$tzo*: <input type=text name=\"wpHourDiff\" value=\"{$wpHourDiff}\" size=6></label><br />
362 <input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" />
363 </td>" );
364
365 # Email, etc.
366 #
367 $wpUserEmail = wfEscapeHTML( $wpUserEmail );
368 $wpNick = wfEscapeHTML( $wpNick );
369 if ( $wpEmailFlag ) { $emfc = "checked"; }
370 else { $emfc = ""; }
371
372 $ps = namespacesCheckboxes();
373
374 $wgOut->addHTML( "<td valign=top nowrap>
375 <label>$yem: <input type=text name=\"wpUserEmail\" value=\"{$wpUserEmail}\" size=20></label><br>
376 <label><input type=checkbox $emfc value=\"1\" name=\"wpEmailFlag\"> $emf</label><br>
377 <label>$ynn: <input type=text name=\"wpNick\" value=\"{$wpNick}\" size=12></label><br>
378 <label>$rcc: <input type=text name=\"wpRecent\" value=\"$wpRecent\" size=6></label><br>
379 <label>$stt: <input type=text name=\"wpStubs\" value=\"$wpStubs\" size=6></label><br>
380 <strong>{$srh}:</strong><br>
381 <label>$rpp: <input type=text name=\"wpSearch\" value=\"$wpSearch\" size=6></label><br>
382 <label>$scl: <input type=text name=\"wpSearchLines\" value=\"$wpSearchLines\" size=6></label><br>
383 <label>$scc: <input type=text name=\"wpSearchChars\" value=\"$wpSearchChars\" size=6></label></td>
384 </tr><tr>
385 <td colspan=2>
386 <b>$dsn</b><br>
387 $ps
388 </td>
389 </tr><tr>
390 <td align=center><input type=submit name=\"wpSaveprefs\" value=\"$svp\"></td>
391 <td align=center><input type=submit name=\"wpReset\" value=\"$rsp\"></td>
392 </tr></table>* {$tzt} </form>\n" );
393 }
394
395 ?>