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