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