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