ff805b507359645aa8c182ca4fce764648b58928
[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;
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 = $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;
184
185 $this->mOldpass = $this->mNewpass = $this->mRetypePass = "";
186 $this->mUserEmail = $wgUser->getEmail();
187 $this->mRealName = $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
269 $wgOut->setPageTitle( wfMsg( "preferences" ) );
270 $wgOut->setArticleRelated( false );
271 $wgOut->setRobotpolicy( "noindex,nofollow" );
272
273 if ( "" != $err ) {
274 $wgOut->addHTML( "<p class='error'>" . htmlspecialchars( $err ) . "</p>\n" );
275 }
276 $uname = $wgUser->getName();
277 $uid = $wgUser->getID();
278
279 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
280
281 $qbs = $wgLang->getQuickbarSettings();
282 $skinNames = $wgLang->getSkinNames();
283 $mathopts = $wgLang->getMathNames();
284 $dateopts = $wgLang->getDateFormats();
285 $togs = $wgLang->getUserToggles();
286
287 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
288 $action = $titleObj->escapeLocalURL();
289
290 $qb = wfMsg( "qbsettings" );
291 $cp = wfMsg( "changepassword" );
292 $sk = wfMsg( "skin" );
293 $math = wfMsg( "math" );
294 $dateFormat = wfMsg("dateformat");
295 $opw = wfMsg( "oldpassword" );
296 $npw = wfMsg( "newpassword" );
297 $rpw = wfMsg( "retypenew" );
298 $svp = wfMsg( "saveprefs" );
299 $rsp = wfMsg( "resetprefs" );
300 $tbs = wfMsg( "textboxsize" );
301 $tbr = wfMsg( "rows" );
302 $tbc = wfMsg( "columns" );
303 $ltz = wfMsg( "localtime" );
304 $tzt = wfMsg( "timezonetext" );
305 $tzo = wfMsg( "timezoneoffset" );
306 $tzGuess = wfMsg( "guesstimezone" );
307 $tzServerTime = wfMsg( "servertime" );
308 $yem = wfMsg( "youremail" );
309 $yrn = wfMsg( "yourrealname" );
310 $emf = wfMsg( "emailflag" );
311 $ynn = wfMsg( "yournick" );
312 $stt = wfMsg ( "stubthreshold" ) ;
313 $srh = wfMsg( "searchresultshead" );
314 $rpp = wfMsg( "resultsperpage" );
315 $scl = wfMsg( "contextlines" );
316 $scc = wfMsg( "contextchars" );
317 $rcc = wfMsg( "recentchangescount" );
318 $dsn = wfMsg( "defaultns" );
319
320 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
321 method=\"post\">" );
322
323 # First section: identity
324 # Email, etc.
325 #
326 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail );
327 $this->mRealName = wfEscapeHTML( $this->mRealName );
328 $this->mNick = wfEscapeHTML( $this->mNick );
329 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; }
330 else { $emfc = ""; }
331
332 $ps = $this->namespacesCheckboxes();
333
334 $wgOut->addHTML( "<fieldset>
335 <legend>".wfMsg('prefs-personal')."</legend>
336 <div><label>$yrn: <input type='text' name=\"wpRealName\" value=\"{$this->mRealName}\" size='20' /></label></div>
337 <div><label>$yem: <input type='text' name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size='20' /></label></div>
338 <div><label><input type='checkbox' $emfc value=\"1\" name=\"wpEmailFlag\" /> $emf</label></div>
339 <div><label>$ynn: <input type='text' name=\"wpNick\" value=\"{$this->mNick}\" size='12' /></label></div>\n" );
340
341 # Fields for changing password
342 #
343 $this->mOldpass = wfEscapeHTML( $this->mOldpass );
344 $this->mNewpass = wfEscapeHTML( $this->mNewpass );
345 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass );
346
347 $wgOut->addHTML( "<fieldset>
348 <legend>$cp</legend>
349 <div><label>$opw: <input type='password' name=\"wpOldpass\" value=\"{$this->mOldpass}\" size='20' /></label></div>
350 <div><label>$npw: <input type='password' name=\"wpNewpass\" value=\"{$this->mNewpass}\" size='20' /></label></div>
351 <div><label>$rpw: <input type='password' name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size='20' /></label></div>
352 " . $this->getToggle( "rememberpassword" ) . "
353 </fieldset>
354 <div class='prefsectiontip'>".wfMsg('prefs-help-userdata')."</div>\n</fieldset>\n" );
355
356
357 # Quickbar setting
358 #
359 $wgOut->addHtml( "<fieldset>\n<legend>$qb</legend>\n" );
360 for ( $i = 0; $i < count( $qbs ); ++$i ) {
361 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
362 else { $checked = ""; }
363 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpQuickbar\"
364 value=\"$i\"$checked /> {$qbs[$i]}</label></div>\n" );
365 }
366 $wgOut->addHtml( "</fieldset>\n\n" );
367
368 # Skin setting
369 #
370 $wgOut->addHTML( "<fieldset>\n<legend>$sk</legend>\n" );
371 # Only show members of $wgValidSkinNames rather than
372 # $skinNames (skins is all skin names from Language.php)
373 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
374 if ( $skinkey == $this->mSkin ) {
375 $checked = ' checked="checked"';
376 } else {
377 $checked = "";
378 }
379 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpSkin\"
380 value=\"$skinkey\"$checked /> {$skinNames[$skinkey]}</label></div>\n" );
381 }
382 $wgOut->addHTML( "</fieldset>\n\n" );
383
384 # Math setting
385 #
386 $wgOut->addHTML( "<fieldset>\n<legend>$math</legend>\n" );
387 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
388 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; }
389 else { $checked = ""; }
390 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpMath\"
391 value=\"$i\"$checked /> {$mathopts[$i]}</label></div>\n" );
392 }
393 $wgOut->addHTML( "</fieldset>\n\n" );
394
395 # Date format
396 #
397 if ( $wgUseDynamicDates ) {
398 $wgOut->addHTML( "<fieldset>\n<legend>$dateFormat</legend>\n" );
399 for ( $i = 0; $i < count( $dateopts ); ++$i) {
400 if ( $i == $this->mDate ) {
401 $checked = ' checked="checked"';
402 } else {
403 $checked = "";
404 }
405 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpDate\" ".
406 "value=\"$i\"$checked /> {$dateopts[$i]}</label></div>\n" );
407 }
408 $wgOut->addHTML( "</fieldset>\n\n");
409 }
410
411 # Textbox rows, cols
412 #
413 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
414 $nowserver = $wgLang->time( $now, false );
415 $wgOut->addHTML( "<fieldset>
416 <legend>$tbs</legend>\n
417 <div>
418 <label>$tbr: <input type='text' name=\"wpRows\" value=\"{$this->mRows}\" size='6' /></label>
419 <label>$tbc: <input type='text' name=\"wpCols\" value=\"{$this->mCols}\" size='6' /></label>
420 </div> " .
421 $this->getToggle( "editwidth" ) .
422 $this->getToggle( "showtoolbar" ) .
423 $this->getToggle( "previewontop" ) .
424 $this->getToggle( "watchdefault" ) .
425 $this->getToggle( "minordefault" ) . "
426 </fieldset>
427
428 <fieldset>
429 <legend>$dateFormat</legend>
430 <div><b>$tzServerTime:</b> $nowserver</div>
431 <div><b>$ltz:</b> $nowlocal</div>
432 <div><label>$tzo*: <input type='text' name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size='6' /></label></div>
433 <div><input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" /></div>
434 <div class='prefsectiontip'>* {$tzt}</div>
435 </fieldset>\n\n" );
436
437 $wgOut->addHTML( "
438 <fieldset><legend>".wfMsg('prefs-rc')."</legend>
439 <div><label>$rcc: <input type='text' name=\"wpRecent\" value=\"$this->mRecent\" size='6' /></label></div>
440 " . $this->getToggle( "hideminor" ) .
441 $this->getToggle( "usenewrc" ) . "
442 <div><label>$stt: <input type='text' name=\"wpStubs\" value=\"$this->mStubs\" size='6' /></label></div>
443 </fieldset>
444
445 <fieldset>
446 <legend>$srh</legend>
447 <div><label>$rpp: <input type='text' name=\"wpSearch\" value=\"$this->mSearch\" size='6' /></label></div>
448 <div><label>$scl: <input type='text' name=\"wpSearchLines\" value=\"$this->mSearchLines\" size='6' /></label></div>
449 <div><label>$scc: <input type='text' name=\"wpSearchChars\" value=\"$this->mSearchChars\" size='6' /></label></div>
450
451 <fieldset>
452 <legend>$dsn</legend>
453 $ps
454 </fieldset>
455 </fieldset>
456 " );
457
458 # Various checkbox options
459 #
460 $wgOut->addHTML("<fieldset><legend>".wfMsg('prefs-misc')."</legend>");
461 foreach ( $togs as $tname => $ttext ) {
462 if( !array_key_exists( $tname, $this->mUsedToggles ) ) {
463 $wgOut->addHTML( $this->getToggle( $tname ) );
464 }
465 }
466 $wgOut->addHTML( "</fieldset>\n\n" );
467
468 $wgOut->addHTML( "
469 <div id='prefsubmit'>
470 <div>
471 <input type='submit' name=\"wpSaveprefs\" value=\"$svp\" accesskey=\"".
472 wfMsg('accesskey-save')."\" title=\"[alt-".wfMsg('accesskey-save')."]\" />
473 <input type='submit' name=\"wpReset\" value=\"$rsp\" />
474 </div>
475
476 </div>
477
478 </form>\n" );
479 }
480 }
481 ?>