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