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