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