Fixed notice
[lhc/web/wiklou.git] / includes / SpecialPreferences.php
1 <?php
2
3 function wfSpecialPreferences()
4 {
5 global $wgRequest;
6
7 $form = new PreferencesForm( $wgRequest );
8 $form->execute();
9 }
10
11 class PreferencesForm {
12 var $mQuickbar, $mOldpass, $mNewpass, $mRetypePass, $mStubs;
13 var $mRows, $mCols, $mSkin, $mMath, $mDate, $mUserEmail, $mEmailFlag, $mNick;
14 var $mSearch, $mRecent, $mHourDiff, $mSearchLines, $mSearchChars, $mAction;
15 var $mReset, $mPosted, $mToggles, $mSearchNs;
16
17 function PreferencesForm( &$request ) {
18 global $wgLang;
19
20 $this->mQuickbar = $request->getVal( 'wpQuickbar' );
21 $this->mOldpass = $request->getVal( 'wpOldpass' );
22 $this->mNewpass = $request->getVal( 'wpNewpass' );
23 $this->mRetypePass =$request->getVal( 'wpRetypePass' );
24 $this->mStubs = $request->getVal( 'wpStubs' );
25 $this->mRows = $request->getVal( 'wpRows' );
26 $this->mCols = $request->getVal( 'wpCols' );
27 $this->mSkin = $request->getVal( 'wpSkin' );
28 $this->mMath = $request->getVal( 'wpMath' );
29 $this->mDate = $request->getVal( 'wpDate' );
30 $this->mUserEmail = $request->getVal( 'wpUserEmail' );
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->setOption( "nickname", $this->mNick );
148 $wgUser->setOption( "quickbar", $this->mQuickbar );
149 $wgUser->setOption( "skin", $this->mSkin );
150 $wgUser->setOption( "math", $this->mMath );
151 $wgUser->setOption( "date", $this->mDate );
152 $wgUser->setOption( "searchlimit", $this->validateIntOrNull( $this->mSearch ) );
153 $wgUser->setOption( "contextlines", $this->validateIntOrNull( $this->mSearchLines ) );
154 $wgUser->setOption( "contextchars", $this->validateIntOrNull( $this->mSearchChars ) );
155 $wgUser->setOption( "rclimit", $this->validateIntOrNull( $this->mRecent ) );
156 $wgUser->setOption( "rows", $this->validateInt( $this->mRows, 4, 1000 ) );
157 $wgUser->setOption( "cols", $this->validateInt( $this->mCols, 4, 1000 ) );
158 $wgUser->setOption( "stubthreshold", $this->validateIntOrNull( $this->mStubs ) );
159 $wgUser->setOption( "timecorrection", $this->validateTimeZone( $this->mHourDiff, -12, 14 ) );
160
161 # Set search namespace options
162 foreach( $this->mSearchNs as $i => $value ) {
163 $wgUser->setOption( "searchNs{$i}", $value );
164 }
165
166 $wgUser->setOption( "disablemail", $this->mEmailFlag );
167
168 # Set user toggles
169 foreach ( $this->mToggles as $tname => $tvalue ) {
170 $wgUser->setOption( $tname, $tvalue );
171 }
172 $wgUser->setCookies();
173 $up = new UserUpdate();
174 array_push( $wgDeferredUpdateList, $up );
175 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
176 $po = ParserOptions::newFromUser( $wgUser );
177 $this->mainPrefsForm( wfMsg( "savedprefs" ) );
178 }
179
180 /* private */ function resetPrefs()
181 {
182 global $wgUser, $wgLang;
183
184 $this->mOldpass = $this->mNewpass = $this->mRetypePass = "";
185 $this->mUserEmail = $wgUser->getEmail();
186 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $this->mEmailFlag = 1; }
187 else { $this->mEmailFlag = 0; }
188 $this->mNick = $wgUser->getOption( "nickname" );
189
190 $this->mQuickbar = $wgUser->getOption( "quickbar" );
191 $this->mSkin = $wgUser->getOption( "skin" );
192 $this->mMath = $wgUser->getOption( "math" );
193 $this->mDate = $wgUser->getOption( "date" );
194 $this->mRows = $wgUser->getOption( "rows" );
195 $this->mCols = $wgUser->getOption( "cols" );
196 $this->mStubs = $wgUser->getOption( "stubthreshold" );
197 $this->mHourDiff = $wgUser->getOption( "timecorrection" );
198 $this->mSearch = $wgUser->getOption( "searchlimit" );
199 $this->mSearchLines = $wgUser->getOption( "contextlines" );
200 $this->mSearchChars = $wgUser->getOption( "contextchars" );
201 $this->mRecent = $wgUser->getOption( "rclimit" );
202
203 $togs = $wgLang->getUserToggles();
204 foreach ( $togs as $tname => $ttext ) {
205 $this->mToggles[$tname] = $wgUser->getOption( $tname );
206 }
207
208 $namespaces = $wgLang->getNamespaces();
209 foreach ( $namespaces as $i => $namespace ) {
210 if ( $i >= 0 ) {
211 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" );
212 }
213 }
214 }
215
216 /* private */ function namespacesCheckboxes()
217 {
218 global $wgLang, $wgUser;
219
220 # Determine namespace checkboxes
221 $namespaces = $wgLang->getNamespaces();
222 $r1 = "";
223
224 foreach ( $namespaces as $i => $name ) {
225 # Skip special or anything similar
226 if ( $i >= 0 ) {
227 $checked = "";
228 if ( $this->mSearchNs[$i] ) {
229 $checked = ' checked="checked"';
230 }
231 $name = str_replace( "_", " ", $namespaces[$i] );
232 if ( "" == $name ) {
233 $name = wfMsg( "blanknamespace" );
234 }
235
236 if ( 0 != $i ) {
237 $r1 .= " ";
238 }
239 $r1 .= "<label><input type='checkbox' value=\"1\" name=\"" .
240 "wpNs$i\"{$checked} />{$name}</label>\n";
241 }
242 }
243
244 return $r1;
245 }
246
247
248 function getToggle( $tname ) {
249 global $wgUser, $wgLang;
250
251 $this->mUsedToggles[$tname] = true;
252 $ttext = $wgLang->getUserToggle( $tname );
253
254 if ( 1 == $wgUser->getOption( $tname ) ) {
255 $checked = ' checked="checked"';
256 } else {
257 $checked = "";
258 }
259 return "<div><input type='checkbox' value=\"1\" "
260 . "id=\"$tname\" name=\"wpOp$tname\"$checked /><label for=\"$tname\">$ttext</label></div>\n";
261 }
262
263 /* private */ function mainPrefsForm( $err )
264 {
265 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames;
266
267 $wgOut->setPageTitle( wfMsg( "preferences" ) );
268 $wgOut->setArticleRelated( false );
269 $wgOut->setRobotpolicy( "noindex,nofollow" );
270
271 if ( "" != $err ) {
272 $wgOut->addHTML( "<font size='+1' color='red'>$err</font>\n<p>" );
273 }
274 $uname = $wgUser->getName();
275 $uid = $wgUser->getID();
276
277 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
278
279 $qbs = $wgLang->getQuickbarSettings();
280 $skinNames = $wgLang->getSkinNames();
281 $mathopts = $wgLang->getMathNames();
282 $dateopts = $wgLang->getDateFormats();
283 $togs = $wgLang->getUserToggles();
284
285 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
286 $action = $titleObj->escapeLocalURL();
287
288 $qb = wfMsg( "qbsettings" );
289 $cp = wfMsg( "changepassword" );
290 $sk = wfMsg( "skin" );
291 $math = wfMsg( "math" );
292 $dateFormat = wfMsg("dateformat");
293 $opw = wfMsg( "oldpassword" );
294 $npw = wfMsg( "newpassword" );
295 $rpw = wfMsg( "retypenew" );
296 $svp = wfMsg( "saveprefs" );
297 $rsp = wfMsg( "resetprefs" );
298 $tbs = wfMsg( "textboxsize" );
299 $tbr = wfMsg( "rows" );
300 $tbc = wfMsg( "columns" );
301 $ltz = wfMsg( "localtime" );
302 $tzt = wfMsg( "timezonetext" );
303 $tzo = wfMsg( "timezoneoffset" );
304 $tzGuess = wfMsg( "guesstimezone" );
305 $tzServerTime = wfMsg( "servertime" );
306 $yem = wfMsg( "youremail" );
307 $emf = wfMsg( "emailflag" );
308 $ynn = wfMsg( "yournick" );
309 $stt = wfMsg ( "stubthreshold" ) ;
310 $srh = wfMsg( "searchresultshead" );
311 $rpp = wfMsg( "resultsperpage" );
312 $scl = wfMsg( "contextlines" );
313 $scc = wfMsg( "contextchars" );
314 $rcc = wfMsg( "recentchangescount" );
315 $dsn = wfMsg( "defaultns" );
316
317 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
318 method=\"post\">" );
319
320 # Quickbar setting
321 #
322 $wgOut->addHtml( "<fieldset>\n<legend>$qb:</legend>\n" );
323 for ( $i = 0; $i < count( $qbs ); ++$i ) {
324 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
325 else { $checked = ""; }
326 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpQuickbar\"
327 value=\"$i\"$checked /> {$qbs[$i]}</label></div>\n" );
328 }
329 $wgOut->addHtml( "</fieldset>\n\n" );
330
331 # Fields for changing password
332 #
333 $this->mOldpass = wfEscapeHTML( $this->mOldpass );
334 $this->mNewpass = wfEscapeHTML( $this->mNewpass );
335 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass );
336
337 $wgOut->addHTML( "<fieldset>
338 <legend>$cp:</legend>
339 <div><label>$opw: <input type='password' name=\"wpOldpass\" value=\"{$this->mOldpass}\" size='20' /></label></div>
340 <div><label>$npw: <input type='password' name=\"wpNewpass\" value=\"{$this->mNewpass}\" size='20' /></label></div>
341 <div><label>$rpw: <input type='password' name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size='20' /></label></div>
342 " . $this->getToggle( "rememberpassword" ) . "
343 </fieldset>\n\n" );
344
345 # Skin setting
346 #
347 $wgOut->addHTML( "<fieldset>\n<legend>$sk:</legend>\n" );
348 # Only show members of $wgValidSkinNames rather than
349 # $skinNames (skins is all skin names from Language.php)
350 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
351 if ( $skinkey == $this->mSkin ) {
352 $checked = ' checked="checked"';
353 } else {
354 $checked = "";
355 }
356 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpSkin\"
357 value=\"$skinkey\"$checked /> {$skinNames[$skinkey]}</label></div>\n" );
358 }
359 $wgOut->addHTML( "</fieldset>\n\n" );
360
361 # Math setting
362 #
363 $wgOut->addHTML( "<fieldset>\n<legend>$math:</legend>\n" );
364 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
365 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; }
366 else { $checked = ""; }
367 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpMath\"
368 value=\"$i\"$checked /> {$mathopts[$i]}</label></div>\n" );
369 }
370 $wgOut->addHTML( "</fieldset>\n\n" );
371
372 # Date format
373 #
374 if ( $wgUseDynamicDates ) {
375 $wgOut->addHTML( "<fieldset>\n<legend>$dateFormat:</legend>\n" );
376 for ( $i = 0; $i < count( $dateopts ); ++$i) {
377 if ( $i == $this->mDate ) {
378 $checked = ' checked="checked"';
379 } else {
380 $checked = "";
381 }
382 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpDate\" ".
383 "value=\"$i\"$checked /> {$dateopts[$i]}</label></div>\n" );
384 }
385 $wgOut->addHTML( "</fieldset>\n\n");
386 }
387
388 # Textbox rows, cols
389 #
390 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
391 $nowserver = $wgLang->time( $now, false );
392 $wgOut->addHTML( "<fieldset>
393 <legend>$tbs:</legend>\n
394 <div>
395 <label>$tbr: <input type='text' name=\"wpRows\" value=\"{$this->mRows}\" size='6' /></label>
396 <label>$tbc: <input type='text' name=\"wpCols\" value=\"{$this->mCols}\" size='6' /></label>
397 </div> " .
398 $this->getToggle( "editwidth" ) .
399 $this->getToggle( "showtoolbar" ) .
400 $this->getToggle( "previewontop" ) .
401 $this->getToggle( "watchdefault" ) .
402 $this->getToggle( "minordefault" ) . "
403 </fieldset>
404
405 <fieldset>
406 <legend>$dateFormat:</legend>
407 <div><b>$tzServerTime:</b> $nowserver</div>
408 <div><b>$ltz:</b> $nowlocal</div>
409 <div><label>$tzo*: <input type='text' name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size='6' /></label></div>
410 <div><input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" /></div>
411 </fieldset>\n\n" );
412
413 # Email, etc.
414 #
415 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail );
416 $this->mNick = wfEscapeHTML( $this->mNick );
417 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; }
418 else { $emfc = ""; }
419
420 $ps = $this->namespacesCheckboxes();
421
422 $wgOut->addHTML( "<fieldset>
423 <div><label>$yem: <input type='text' name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size='20' /></label></div>
424 <div><label><input type='checkbox' $emfc value=\"1\" name=\"wpEmailFlag\" /> $emf</label></div>
425 <div><label>$ynn: <input type='text' name=\"wpNick\" value=\"{$this->mNick}\" size='12' /></label></div>
426 </fieldset>
427
428 <fieldset>
429 <div><label>$rcc: <input type='text' name=\"wpRecent\" value=\"$this->mRecent\" size='6' /></label></div>
430 " . $this->getToggle( "hideminor" ) .
431 $this->getToggle( "usenewrc" ) . "
432 <div><label>$stt: <input type='text' name=\"wpStubs\" value=\"$this->mStubs\" size='6' /></label></div>
433 </fieldset>
434
435 <fieldset>
436 <legend>$srh</legend>
437 <div><label>$rpp: <input type='text' name=\"wpSearch\" value=\"$this->mSearch\" size='6' /></label></div>
438 <div><label>$scl: <input type='text' name=\"wpSearchLines\" value=\"$this->mSearchLines\" size='6' /></label></div>
439 <div><label>$scc: <input type='text' name=\"wpSearchChars\" value=\"$this->mSearchChars\" size='6' /></label></div>
440
441 <fieldset>
442 <legend>$dsn</legend>
443 $ps
444 </fieldset>
445 </fieldset>
446 " );
447
448 # Various checkbox options
449 #
450 $wgOut->addHTML("<fieldset>");
451 foreach ( $togs as $tname => $ttext ) {
452 if( !array_key_exists( $tname, $this->mUsedToggles ) ) {
453 $wgOut->addHTML( $this->getToggle( $tname ) );
454 }
455 }
456 $wgOut->addHTML( "</fieldset>\n\n" );
457
458 $wgOut->addHTML( "
459 <div>
460 <input type='submit' name=\"wpSaveprefs\" value=\"$svp\" />
461 <input type='submit' name=\"wpReset\" value=\"$rsp\" />
462 </div>
463
464 <div>* {$tzt}</div>
465
466 </form>\n" );
467 }
468 }
469 ?>