restore section folding
[lhc/web/wiklou.git] / includes / SpecialPreferences.php
1 <?php
2
3 function wfSpecialPreferences()
4 {
5 global $wgRequest;
6
7 $form = new PreferencesForm( $wgRequest );
8 $form->execute();
9 }
10
11 class PreferencesForm {
12 var $mQuickbar, $mOldpass, $mNewpass, $mRetypePass, $mStubs;
13 var $mRows, $mCols, $mSkin, $mMath, $mDate, $mUserEmail, $mEmailFlag, $mNick;
14 var $mSearch, $mRecent, $mHourDiff, $mSearchLines, $mSearchChars, $mAction;
15 var $mReset, $mPosted, $mToggles, $mSearchNs, $mRealName;
16
17 function PreferencesForm( &$request ) {
18 global $wgLang;
19
20 $this->mQuickbar = $request->getVal( 'wpQuickbar' );
21 $this->mOldpass = $request->getVal( 'wpOldpass' );
22 $this->mNewpass = $request->getVal( 'wpNewpass' );
23 $this->mRetypePass =$request->getVal( 'wpRetypePass' );
24 $this->mStubs = $request->getVal( 'wpStubs' );
25 $this->mCollapse = $request->getVal( 'wpCollapse' );
26 $this->mRows = $request->getVal( 'wpRows' );
27 $this->mCols = $request->getVal( 'wpCols' );
28 $this->mSkin = $request->getVal( 'wpSkin' );
29 $this->mMath = $request->getVal( 'wpMath' );
30 $this->mDate = $request->getVal( 'wpDate' );
31 $this->mUserEmail = $request->getVal( 'wpUserEmail' );
32 $this->mRealName = $request->getVal( 'wpRealName' );
33 $this->mEmailFlag = $request->getCheck( 'wpEmailFlag' ) ? 1 : 0;
34 $this->mNick = $request->getVal( 'wpNick' );
35 $this->mSearch = $request->getVal( 'wpSearch' );
36 $this->mRecent = $request->getVal( 'wpRecent' );
37 $this->mHourDiff = $request->getVal( 'wpHourDiff' );
38 $this->mSearchLines = $request->getVal( 'wpSearchLines' );
39 $this->mSearchChars = $request->getVal( 'wpSearchChars' );
40 $this->mAction = $request->getVal( 'action' );
41 $this->mReset = $request->getCheck( 'wpReset' );
42 $this->mPosted = $request->wasPosted();
43 $this->mSaveprefs = $request->getCheck( 'wpSaveprefs' ) && $this->mPosted;
44
45 # User toggles (the big ugly unsorted list of checkboxes)
46 $this->mToggles = array();
47 if ( $this->mPosted ) {
48 $togs = $wgLang->getUserToggles();
49 foreach ( $togs as $tname => $ttext ) {
50 $this->mToggles[$tname] = $request->getCheck( "wpOp$tname" ) ? 1 : 0;
51 }
52 }
53
54 $this->mUsedToggles = array();
55
56 # Search namespace options
57 # Note: namespaces don't necessarily have consecutive keys
58 $this->mSearchNs = array();
59 if ( $this->mPosted ) {
60 $namespaces = $wgLang->getNamespaces();
61 foreach ( $namespaces as $i => $namespace ) {
62 if ( $i >= 0 ) {
63 $this->mSearchNs[$i] = $request->getCheck( "wpNs$i" ) ? 1 : 0;
64 }
65 }
66 }
67 }
68
69 function execute() {
70 global $wgUser, $wgOut, $wgUseDynamicDates;
71
72 if ( 0 == $wgUser->getID() ) {
73 $wgOut->errorpage( "prefsnologin", "prefsnologintext" );
74 return;
75 }
76 if ( wfReadOnly() ) {
77 $wgOut->readOnlyPage();
78 return;
79 }
80 if ( $this->mReset ) {
81 $this->resetPrefs();
82 $this->mainPrefsForm( wfMsg( "prefsreset" ) );
83 } else if ( $this->mSaveprefs ) {
84 $this->savePreferences();
85 } else {
86 $this->resetPrefs();
87 $this->mainPrefsForm( "" );
88 }
89 }
90
91 /* private */ function validateInt( &$val, $min=0, $max=0x7fffffff ) {
92 $val = intval($val);
93 $val = min($val, $max);
94 $val = max($val, $min);
95 return $val;
96 }
97
98 /* private */ function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
99 $val = trim($val);
100 if($val === "") {
101 return $val;
102 } else {
103 return $this->validateInt( $val, $min, $max );
104 }
105 }
106
107 /* private */ function validateTimeZone( $s )
108 {
109
110 if ( $s !== "" ) {
111 if ( strpos( $s, ":" ) ) {
112 # HH:MM
113 $array = explode( ":" , $s );
114 $hour = intval( $array[0] );
115 $minute = intval( $array[1] );
116 } else {
117 $minute = intval( $s * 60 );
118 $hour = intval( $minute / 60 );
119 $minute = abs( $minute ) % 60;
120 }
121 $hour = min( $hour, 15 );
122 $hour = max( $hour, -15 );
123 $minute = min( $minute, 59 );
124 $minute = max( $minute, 0 );
125 $s = sprintf( "%02d:%02d", $hour, $minute );
126 }
127 return $s;
128 }
129
130 /* private */ function savePreferences()
131 {
132 global $wgUser, $wgLang, $wgDeferredUpdateList, $wgOut;
133
134 if ( "" != $this->mNewpass ) {
135 if ( $this->mNewpass != $this->mRetypePass ) {
136 $this->mainPrefsForm( wfMsg( "badretype" ) );
137 return;
138 }
139 $ep = $wgUser->encryptPassword( $this->mOldpass );
140 if ( $ep != $wgUser->getPassword() ) {
141 if ( $ep != $wgUser->getNewpassword() ) {
142 $this->mainPrefsForm( wfMsg( "wrongpassword" ) );
143 return;
144 }
145 }
146 $wgUser->setPassword( $this->mNewpass );
147 }
148 $wgUser->setEmail( $this->mUserEmail );
149 $wgUser->setRealName( $this->mRealName );
150 $wgUser->setOption( "nickname", $this->mNick );
151 $wgUser->setOption( "quickbar", $this->mQuickbar );
152 $wgUser->setOption( "skin", $this->mSkin );
153 $wgUser->setOption( "math", $this->mMath );
154 $wgUser->setOption( "date", $this->mDate );
155 $wgUser->setOption( "searchlimit", $this->validateIntOrNull( $this->mSearch ) );
156 $wgUser->setOption( "contextlines", $this->validateIntOrNull( $this->mSearchLines ) );
157 $wgUser->setOption( "contextchars", $this->validateIntOrNull( $this->mSearchChars ) );
158 $wgUser->setOption( "rclimit", $this->validateIntOrNull( $this->mRecent ) );
159 $wgUser->setOption( "rows", $this->validateInt( $this->mRows, 4, 1000 ) );
160 $wgUser->setOption( "cols", $this->validateInt( $this->mCols, 4, 1000 ) );
161 $wgUser->setOption( "stubthreshold", $this->validateIntOrNull( $this->mStubs ) );
162 $wgUser->setOption( "collapsethreshold", $this->validateIntOrNull( $this->mCollapse ) );
163 $wgUser->setOption( "timecorrection", $this->validateTimeZone( $this->mHourDiff, -12, 14 ) );
164
165 # Set search namespace options
166 foreach( $this->mSearchNs as $i => $value ) {
167 $wgUser->setOption( "searchNs{$i}", $value );
168 }
169
170 $wgUser->setOption( "disablemail", $this->mEmailFlag );
171
172 # Set user toggles
173 foreach ( $this->mToggles as $tname => $tvalue ) {
174 $wgUser->setOption( $tname, $tvalue );
175 }
176 $wgUser->setCookies();
177 $up = new UserUpdate();
178 array_push( $wgDeferredUpdateList, $up );
179 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
180 $po = ParserOptions::newFromUser( $wgUser );
181 $this->mainPrefsForm( wfMsg( "savedprefs" ) );
182 }
183
184 /* private */ function resetPrefs()
185 {
186 global $wgUser, $wgLang;
187
188 $this->mOldpass = $this->mNewpass = $this->mRetypePass = "";
189 $this->mUserEmail = $wgUser->getEmail();
190 $this->mRealName = $wgUser->getRealName();
191 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $this->mEmailFlag = 1; }
192 else { $this->mEmailFlag = 0; }
193 $this->mNick = $wgUser->getOption( "nickname" );
194
195 $this->mQuickbar = $wgUser->getOption( "quickbar" );
196 $this->mSkin = $wgUser->getOption( "skin" );
197 $this->mMath = $wgUser->getOption( "math" );
198 $this->mDate = $wgUser->getOption( "date" );
199 $this->mRows = $wgUser->getOption( "rows" );
200 $this->mCols = $wgUser->getOption( "cols" );
201 $this->mStubs = $wgUser->getOption( "stubthreshold" );
202 $this->mCollapse = $wgUser->getOption( "collapsethreshold" );
203 $this->mHourDiff = $wgUser->getOption( "timecorrection" );
204 $this->mSearch = $wgUser->getOption( "searchlimit" );
205 $this->mSearchLines = $wgUser->getOption( "contextlines" );
206 $this->mSearchChars = $wgUser->getOption( "contextchars" );
207 $this->mRecent = $wgUser->getOption( "rclimit" );
208
209 $togs = $wgLang->getUserToggles();
210 foreach ( $togs as $tname => $ttext ) {
211 $this->mToggles[$tname] = $wgUser->getOption( $tname );
212 }
213
214 $namespaces = $wgLang->getNamespaces();
215 foreach ( $namespaces as $i => $namespace ) {
216 if ( $i >= 0 ) {
217 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" );
218 }
219 }
220 }
221
222 /* private */ function namespacesCheckboxes()
223 {
224 global $wgLang, $wgUser;
225
226 # Determine namespace checkboxes
227 $namespaces = $wgLang->getNamespaces();
228 $r1 = "";
229
230 foreach ( $namespaces as $i => $name ) {
231 # Skip special or anything similar
232 if ( $i >= 0 ) {
233 $checked = "";
234 if ( $this->mSearchNs[$i] ) {
235 $checked = ' checked="checked"';
236 }
237 $name = str_replace( "_", " ", $namespaces[$i] );
238 if ( "" == $name ) {
239 $name = wfMsg( "blanknamespace" );
240 }
241
242 if ( 0 != $i ) {
243 $r1 .= " ";
244 }
245 $r1 .= "<label><input type='checkbox' value=\"1\" name=\"" .
246 "wpNs$i\"{$checked} />{$name}</label>\n";
247 }
248 }
249
250 return $r1;
251 }
252
253
254 function getToggle( $tname ) {
255 global $wgUser, $wgLang;
256
257 $this->mUsedToggles[$tname] = true;
258 $ttext = $wgLang->getUserToggle( $tname );
259
260 if ( 1 == $wgUser->getOption( $tname ) ) {
261 $checked = ' checked="checked"';
262 } else {
263 $checked = "";
264 }
265 return "<div><input type='checkbox' value=\"1\" "
266 . "id=\"$tname\" name=\"wpOp$tname\"$checked /><label for=\"$tname\">$ttext</label></div>\n";
267 }
268
269 /* private */ function mainPrefsForm( $err )
270 {
271 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames;
272
273 $wgOut->setPageTitle( wfMsg( "preferences" ) );
274 $wgOut->setArticleRelated( false );
275 $wgOut->setRobotpolicy( "noindex,nofollow" );
276
277 if ( "" != $err ) {
278 $wgOut->addHTML( "<p class='error'>" . htmlspecialchars( $err ) . "</p>\n" );
279 }
280 $uname = $wgUser->getName();
281 $uid = $wgUser->getID();
282
283 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
284
285 $qbs = $wgLang->getQuickbarSettings();
286 $skinNames = $wgLang->getSkinNames();
287 $mathopts = $wgLang->getMathNames();
288 $dateopts = $wgLang->getDateFormats();
289 $togs = $wgLang->getUserToggles();
290
291 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
292 $action = $titleObj->escapeLocalURL();
293
294 $qb = wfMsg( "qbsettings" );
295 $cp = wfMsg( "changepassword" );
296 $sk = wfMsg( "skin" );
297 $math = wfMsg( "math" );
298 $dateFormat = wfMsg("dateformat");
299 $opw = wfMsg( "oldpassword" );
300 $npw = wfMsg( "newpassword" );
301 $rpw = wfMsg( "retypenew" );
302 $svp = wfMsg( "saveprefs" );
303 $rsp = wfMsg( "resetprefs" );
304 $tbs = wfMsg( "textboxsize" );
305 $tbr = wfMsg( "rows" );
306 $tbc = wfMsg( "columns" );
307 $ltz = wfMsg( "localtime" );
308 $tzt = wfMsg( "timezonetext" );
309 $tzo = wfMsg( "timezoneoffset" );
310 $tzGuess = wfMsg( "guesstimezone" );
311 $tzServerTime = wfMsg( "servertime" );
312 $yem = wfMsg( "youremail" );
313 $yrn = wfMsg( "yourrealname" );
314 $emf = wfMsg( "emailflag" );
315 $ynn = wfMsg( "yournick" );
316 $stt = wfMsg ( "stubthreshold" ) ;
317 $coltr = wfMsg("collapsethreshold");
318 $srh = wfMsg( "searchresultshead" );
319 $rpp = wfMsg( "resultsperpage" );
320 $scl = wfMsg( "contextlines" );
321 $scc = wfMsg( "contextchars" );
322 $rcc = wfMsg( "recentchangescount" );
323 $dsn = wfMsg( "defaultns" );
324
325 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
326 method=\"post\">" );
327
328 # First section: identity
329 # Email, etc.
330 #
331 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail );
332 $this->mRealName = wfEscapeHTML( $this->mRealName );
333 $this->mNick = wfEscapeHTML( $this->mNick );
334 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; }
335 else { $emfc = ""; }
336
337 $ps = $this->namespacesCheckboxes();
338
339 $wgOut->addHTML( "<fieldset>
340 <legend>Idento</legend>
341 <div><label>$yrn: <input type='text' name=\"wpRealName\" value=\"{$this->mRealName}\" size='20' /></label></div>
342 <div><label>$yem: <input type='text' name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size='20' /></label></div>
343 <div><label><input type='checkbox' $emfc value=\"1\" name=\"wpEmailFlag\" /> $emf</label></div>
344 <div><label>$ynn: <input type='text' name=\"wpNick\" value=\"{$this->mNick}\" size='12' /></label></div>\n" );
345
346 # Fields for changing password
347 #
348 $this->mOldpass = wfEscapeHTML( $this->mOldpass );
349 $this->mNewpass = wfEscapeHTML( $this->mNewpass );
350 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass );
351
352 $wgOut->addHTML( "<fieldset>
353 <legend>$cp:</legend>
354 <div><label>$opw: <input type='password' name=\"wpOldpass\" value=\"{$this->mOldpass}\" size='20' /></label></div>
355 <div><label>$npw: <input type='password' name=\"wpNewpass\" value=\"{$this->mNewpass}\" size='20' /></label></div>
356 <div><label>$rpw: <input type='password' name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size='20' /></label></div>
357 " . $this->getToggle( "rememberpassword" ) . "
358 </fieldset>\n</fieldset>\n" );
359
360
361 # Quickbar setting
362 #
363 $wgOut->addHtml( "<fieldset>\n<legend>$qb:</legend>\n" );
364 for ( $i = 0; $i < count( $qbs ); ++$i ) {
365 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
366 else { $checked = ""; }
367 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpQuickbar\"
368 value=\"$i\"$checked /> {$qbs[$i]}</label></div>\n" );
369 }
370 $wgOut->addHtml( "</fieldset>\n\n" );
371
372 # Skin setting
373 #
374 $wgOut->addHTML( "<fieldset>\n<legend>$sk:</legend>\n" );
375 # Only show members of $wgValidSkinNames rather than
376 # $skinNames (skins is all skin names from Language.php)
377 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
378 if ( $skinkey == $this->mSkin ) {
379 $checked = ' checked="checked"';
380 } else {
381 $checked = "";
382 }
383 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpSkin\"
384 value=\"$skinkey\"$checked /> {$skinNames[$skinkey]}</label></div>\n" );
385 }
386 $wgOut->addHTML( "</fieldset>\n\n" );
387
388 # Math setting
389 #
390 $wgOut->addHTML( "<fieldset>\n<legend>$math:</legend>\n" );
391 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
392 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; }
393 else { $checked = ""; }
394 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpMath\"
395 value=\"$i\"$checked /> {$mathopts[$i]}</label></div>\n" );
396 }
397 $wgOut->addHTML( "</fieldset>\n\n" );
398
399 # Date format
400 #
401 if ( $wgUseDynamicDates ) {
402 $wgOut->addHTML( "<fieldset>\n<legend>$dateFormat:</legend>\n" );
403 for ( $i = 0; $i < count( $dateopts ); ++$i) {
404 if ( $i == $this->mDate ) {
405 $checked = ' checked="checked"';
406 } else {
407 $checked = "";
408 }
409 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpDate\" ".
410 "value=\"$i\"$checked /> {$dateopts[$i]}</label></div>\n" );
411 }
412 $wgOut->addHTML( "</fieldset>\n\n");
413 }
414
415 # Textbox rows, cols
416 #
417 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
418 $nowserver = $wgLang->time( $now, false );
419 $wgOut->addHTML( "<fieldset>
420 <legend>$tbs:</legend>\n
421 <div>
422 <label>$tbr: <input type='text' name=\"wpRows\" value=\"{$this->mRows}\" size='6' /></label>
423 <label>$tbc: <input type='text' name=\"wpCols\" value=\"{$this->mCols}\" size='6' /></label>
424 </div> " .
425 $this->getToggle( "editwidth" ) .
426 $this->getToggle( "showtoolbar" ) .
427 $this->getToggle( "previewontop" ) .
428 $this->getToggle( "watchdefault" ) .
429 $this->getToggle( "minordefault" ) . "
430 </fieldset>
431
432 <fieldset>
433 <legend>$dateFormat:</legend>
434 <div><b>$tzServerTime:</b> $nowserver</div>
435 <div><b>$ltz:</b> $nowlocal</div>
436 <div><label>$tzo*: <input type='text' name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size='6' /></label></div>
437 <div><input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" /></div>
438 </fieldset>\n\n" );
439
440 $wgOut->addHTML( "
441 <fieldset>
442 <div><label>$rcc: <input type='text' name=\"wpRecent\" value=\"$this->mRecent\" size='6' /></label></div>
443 " . $this->getToggle( "hideminor" ) .
444 $this->getToggle( "usenewrc" ) . "
445 <div><label>$stt: <input type='text' name=\"wpStubs\" value=\"$this->mStubs\" size='6' /></label></div>
446 <div><label>$coltr: <input type='text' name=\"wpCollapse\" value=\"$this->mCollapse\" size='6' /></label></div>
447 </fieldset>
448
449 <fieldset>
450 <legend>$srh</legend>
451 <div><label>$rpp: <input type='text' name=\"wpSearch\" value=\"$this->mSearch\" size='6' /></label></div>
452 <div><label>$scl: <input type='text' name=\"wpSearchLines\" value=\"$this->mSearchLines\" size='6' /></label></div>
453 <div><label>$scc: <input type='text' name=\"wpSearchChars\" value=\"$this->mSearchChars\" size='6' /></label></div>
454
455 <fieldset>
456 <legend>$dsn</legend>
457 $ps
458 </fieldset>
459 </fieldset>
460 " );
461
462 # Various checkbox options
463 #
464 $wgOut->addHTML("<fieldset>");
465 foreach ( $togs as $tname => $ttext ) {
466 if( !array_key_exists( $tname, $this->mUsedToggles ) ) {
467 $wgOut->addHTML( $this->getToggle( $tname ) );
468 }
469 }
470 $wgOut->addHTML( "</fieldset>\n\n" );
471
472 $wgOut->addHTML( "
473 <div>
474 <input type='submit' name=\"wpSaveprefs\" value=\"$svp\" accesskey=\"".
475 wfMsg('accesskey-save')."\" title=\"[alt-".wfMsg('accesskey-save')."]\" />
476 <input type='submit' name=\"wpReset\" value=\"$rsp\" />
477 </div>
478
479 <div>* {$tzt}</div>
480
481 </form>\n" );
482 }
483 }
484 ?>