Moved wgUserToggles to wgAllMessages to enable users to translate the option labels
[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 ) {
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 ) {
207 $ttext = wfMsg("tog-".$tname);
208 $this->mToggles[$tname] = $wgUser->getOption( $tname );
209 }
210
211 $namespaces = $wgLang->getNamespaces();
212 foreach ( $namespaces as $i => $namespace ) {
213 if ( $i >= 0 ) {
214 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" );
215 }
216 }
217 }
218
219 /* private */ function namespacesCheckboxes()
220 {
221 global $wgLang, $wgUser;
222
223 # Determine namespace checkboxes
224 $namespaces = $wgLang->getNamespaces();
225 $r1 = "";
226
227 foreach ( $namespaces as $i => $name ) {
228 # Skip special or anything similar
229 if ( $i >= 0 ) {
230 $checked = "";
231 if ( $this->mSearchNs[$i] ) {
232 $checked = ' checked="checked"';
233 }
234 $name = str_replace( "_", " ", $namespaces[$i] );
235 if ( "" == $name ) {
236 $name = wfMsg( "blanknamespace" );
237 }
238
239 if ( 0 != $i ) {
240 $r1 .= " ";
241 }
242 $r1 .= "<label><input type='checkbox' value=\"1\" name=\"" .
243 "wpNs$i\"{$checked} />{$name}</label>\n";
244 }
245 }
246
247 return $r1;
248 }
249
250
251 function getToggle( $tname ) {
252 global $wgUser, $wgLang;
253
254 $this->mUsedToggles[$tname] = true;
255 $ttext = $wgLang->getUserToggle( $tname );
256
257 if ( 1 == $wgUser->getOption( $tname ) ) {
258 $checked = ' checked="checked"';
259 } else {
260 $checked = "";
261 }
262 return "<div><input type='checkbox' value=\"1\" "
263 . "id=\"$tname\" name=\"wpOp$tname\"$checked /><label for=\"$tname\">$ttext</label></div>\n";
264 }
265
266 /* private */ function mainPrefsForm( $err )
267 {
268 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames;
269 global $wgAllowRealName;
270
271 $wgOut->setPageTitle( wfMsg( "preferences" ) );
272 $wgOut->setArticleRelated( false );
273 $wgOut->setRobotpolicy( "noindex,nofollow" );
274
275 if ( "" != $err ) {
276 $wgOut->addHTML( "<p class='error'>" . htmlspecialchars( $err ) . "</p>\n" );
277 }
278 $uname = $wgUser->getName();
279 $uid = $wgUser->getID();
280
281 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
282 $wgOut->addWikiText( wfMsg('clearyourcache'));
283
284 $qbs = $wgLang->getQuickbarSettings();
285 $skinNames = $wgLang->getSkinNames();
286 $mathopts = $wgLang->getMathNames();
287 $dateopts = $wgLang->getDateFormats();
288 $togs = $wgLang->getUserToggles();
289
290 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
291 $action = $titleObj->escapeLocalURL();
292
293 $qb = wfMsg( "qbsettings" );
294 $cp = wfMsg( "changepassword" );
295 $sk = wfMsg( "skin" );
296 $math = wfMsg( "math" );
297 $dateFormat = wfMsg("dateformat");
298 $opw = wfMsg( "oldpassword" );
299 $npw = wfMsg( "newpassword" );
300 $rpw = wfMsg( "retypenew" );
301 $svp = wfMsg( "saveprefs" );
302 $rsp = wfMsg( "resetprefs" );
303 $tbs = wfMsg( "textboxsize" );
304 $tbr = wfMsg( "rows" );
305 $tbc = wfMsg( "columns" );
306 $ltz = wfMsg( "localtime" );
307 $timezone = wfMsg( "timezonelegend" );
308 $tzt = wfMsg( "timezonetext" );
309 $tzo = wfMsg( "timezoneoffset" );
310 $tzGuess = wfMsg( "guesstimezone" );
311 $tzServerTime = wfMsg( "servertime" );
312 $yem = wfMsg( "youremail" );
313 $yrn = ($wgAllowRealName) ? wfMsg( "yourrealname" ) : '';
314 $emf = wfMsg( "emailflag" );
315 $ynn = wfMsg( "yournick" );
316 $stt = wfMsg ( "stubthreshold" ) ;
317 $srh = wfMsg( "searchresultshead" );
318 $rpp = wfMsg( "resultsperpage" );
319 $scl = wfMsg( "contextlines" );
320 $scc = wfMsg( "contextchars" );
321 $rcc = wfMsg( "recentchangescount" );
322 $dsn = wfMsg( "defaultns" );
323
324 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
325 method=\"post\">" );
326
327 # First section: identity
328 # Email, etc.
329 #
330 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail );
331 $this->mRealName = wfEscapeHTML( $this->mRealName );
332 $this->mNick = wfEscapeHTML( $this->mNick );
333 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; }
334 else { $emfc = ""; }
335
336 $ps = $this->namespacesCheckboxes();
337
338 $wgOut->addHTML( "<fieldset>
339 <legend>".wfMsg('prefs-personal')."</legend>");
340 if ($wgAllowRealName) {
341 $wgOut->addHTML("<div><label>$yrn: <input type='text' name=\"wpRealName\" value=\"{$this->mRealName}\" size='20' /></label></div>");
342 }
343 $wgOut->addHTML("
344 <div><label>$yem: <input type='text' name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size='20' /></label></div>
345 <div><label><input type='checkbox' $emfc value=\"1\" name=\"wpEmailFlag\" /> $emf</label></div>
346 <div><label>$ynn: <input type='text' name=\"wpNick\" value=\"{$this->mNick}\" size='12' /></label></div>\n" );
347
348 # Fields for changing password
349 #
350 $this->mOldpass = wfEscapeHTML( $this->mOldpass );
351 $this->mNewpass = wfEscapeHTML( $this->mNewpass );
352 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass );
353
354 $wgOut->addHTML( "<fieldset>
355 <legend>$cp</legend>
356 <div><label>$opw: <input type='password' name=\"wpOldpass\" value=\"{$this->mOldpass}\" size='20' /></label></div>
357 <div><label>$npw: <input type='password' name=\"wpNewpass\" value=\"{$this->mNewpass}\" size='20' /></label></div>
358 <div><label>$rpw: <input type='password' name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size='20' /></label></div>
359 " . $this->getToggle( "rememberpassword" ) . "
360 </fieldset>
361 <div class='prefsectiontip'>".wfMsg('prefs-help-userdata')."</div>\n</fieldset>\n" );
362
363
364 # Quickbar setting
365 #
366 $wgOut->addHtml( "<fieldset>\n<legend>$qb</legend>\n" );
367 for ( $i = 0; $i < count( $qbs ); ++$i ) {
368 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
369 else { $checked = ""; }
370 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpQuickbar\"
371 value=\"$i\"$checked /> {$qbs[$i]}</label></div>\n" );
372 }
373 $wgOut->addHtml('<div class="prefsectiontip">'.wfMsg('qbsettingsnote').'</div>');
374 $wgOut->addHtml( "</fieldset>\n\n" );
375
376 # Skin setting
377 #
378 $wgOut->addHTML( "<fieldset>\n<legend>$sk</legend>\n" );
379 # Only show members of $wgValidSkinNames rather than
380 # $skinNames (skins is all skin names from Language.php)
381 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
382 if ( $skinkey == $this->mSkin ) {
383 $checked = ' checked="checked"';
384 } else {
385 $checked = "";
386 }
387 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpSkin\"
388 value=\"$skinkey\"$checked /> {$skinNames[$skinkey]}</label></div>\n" );
389 }
390 $wgOut->addHTML( "</fieldset>\n\n" );
391
392 # Math setting
393 #
394 $wgOut->addHTML( "<fieldset>\n<legend>$math</legend>\n" );
395 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
396 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; }
397 else { $checked = ""; }
398 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpMath\"
399 value=\"$i\"$checked /> ".wfMsg($mathopts[$i])."</label></div>\n" );
400 }
401 $wgOut->addHTML( "</fieldset>\n\n" );
402
403 # Date format
404 #
405 if ( $wgUseDynamicDates ) {
406 $wgOut->addHTML( "<fieldset>\n<legend>$dateFormat</legend>\n" );
407 for ( $i = 0; $i < count( $dateopts ); ++$i) {
408 if ( $i == $this->mDate ) {
409 $checked = ' checked="checked"';
410 } else {
411 $checked = "";
412 }
413 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpDate\" ".
414 "value=\"$i\"$checked /> {$dateopts[$i]}</label></div>\n" );
415 }
416 $wgOut->addHTML( "</fieldset>\n\n");
417 }
418
419 # Textbox rows, cols
420 #
421 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
422 $nowserver = $wgLang->time( $now, false );
423 $wgOut->addHTML( "<fieldset>
424 <legend>$tbs</legend>\n
425 <div>
426 <label>$tbr: <input type='text' name=\"wpRows\" value=\"{$this->mRows}\" size='6' /></label>
427 <label>$tbc: <input type='text' name=\"wpCols\" value=\"{$this->mCols}\" size='6' /></label>
428 </div> " .
429 $this->getToggle( "editwidth" ) .
430 $this->getToggle( "showtoolbar" ) .
431 $this->getToggle( "previewontop" ) .
432 $this->getToggle( "watchdefault" ) .
433 $this->getToggle( "minordefault" ) . "
434 </fieldset>
435
436 <fieldset>
437 <legend>$timezone</legend>
438 <div><b>$tzServerTime:</b> $nowserver</div>
439 <div><b>$ltz:</b> $nowlocal</div>
440 <div><label>$tzo*: <input type='text' name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size='6' /></label></div>
441 <div><input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" id=\"guesstimezonebutton\" style=\"display:none\" /></div>
442 <div class='prefsectiontip'>* {$tzt}</div>
443 </fieldset>\n\n" );
444
445 $wgOut->addHTML( "
446 <fieldset><legend>".wfMsg('prefs-rc')."</legend>
447 <div><label>$rcc: <input type='text' name=\"wpRecent\" value=\"$this->mRecent\" size='6' /></label></div>
448 " . $this->getToggle( "hideminor" ) .
449 $this->getToggle( "usenewrc" ) . "
450 <div><label>$stt: <input type='text' name=\"wpStubs\" value=\"$this->mStubs\" size='6' /></label></div>
451 </fieldset>
452
453 <fieldset>
454 <legend>$srh</legend>
455 <div><label>$rpp: <input type='text' name=\"wpSearch\" value=\"$this->mSearch\" size='6' /></label></div>
456 <div><label>$scl: <input type='text' name=\"wpSearchLines\" value=\"$this->mSearchLines\" size='6' /></label></div>
457 <div><label>$scc: <input type='text' name=\"wpSearchChars\" value=\"$this->mSearchChars\" size='6' /></label></div>
458
459 <fieldset>
460 <legend>$dsn</legend>
461 $ps
462 </fieldset>
463 </fieldset>
464 " );
465
466 # Various checkbox options
467 #
468 $wgOut->addHTML("<fieldset><legend>".wfMsg('prefs-misc')."</legend>");
469 foreach ( $togs as $tname ) {
470 if( !array_key_exists( $tname, $this->mUsedToggles ) ) {
471 $wgOut->addHTML( $this->getToggle( $tname ) );
472 }
473 }
474 $wgOut->addHTML( "</fieldset>\n\n" );
475
476 $wgOut->addHTML( "
477 <div id='prefsubmit'>
478 <div>
479 <input type='submit' name=\"wpSaveprefs\" value=\"$svp\" accesskey=\"".
480 wfMsg('accesskey-save')."\" title=\"[alt-".wfMsg('accesskey-save')."]\" />
481 <input type='submit' name=\"wpReset\" value=\"$rsp\" />
482 </div>
483
484 </div>
485
486 </form>\n" );
487 }
488 }
489 ?>