allowed changes in preferences, especially skin, to be immediately displayed in the...
[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 # Search namespace options
53 # Note: namespaces don't necessarily have consecutive keys
54 $this->mSearchNs = array();
55 if ( $this->mPosted ) {
56 $namespaces = $wgLang->getNamespaces();
57 foreach ( $namespaces as $i => $namespace ) {
58 if ( $i >= 0 ) {
59 $this->mSearchNs[$i] = $request->getCheck( "wpNs$i" ) ? 1 : 0;
60 }
61 }
62 }
63 }
64
65 function execute() {
66 global $wgUser, $wgOut, $wgUseDynamicDates;
67
68 if ( 0 == $wgUser->getID() ) {
69 $wgOut->errorpage( "prefsnologin", "prefsnologintext" );
70 return;
71 }
72 if ( wfReadOnly() ) {
73 $wgOut->readOnlyPage();
74 return;
75 }
76 if ( $this->mReset ) {
77 $this->resetPrefs();
78 $this->mainPrefsForm( wfMsg( "prefsreset" ) );
79 } else if ( $this->mSaveprefs ) {
80 $this->savePreferences();
81 } else {
82 $this->resetPrefs();
83 $this->mainPrefsForm( "" );
84 }
85 }
86
87 /* private */ function validateInt( &$val, $min=0, $max=0x7fffffff ) {
88 $val = intval($val);
89 $val = min($val, $max);
90 $val = max($val, $min);
91 return $val;
92 }
93
94 /* private */ function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
95 $val = trim($val);
96 if($val === "") {
97 return $val;
98 } else {
99 return $this->validateInt( $val, $min, $max );
100 }
101 }
102
103 /* private */ function validateTimeZone( $s )
104 {
105
106 if ( $s !== "" ) {
107 if ( strpos( $s, ":" ) ) {
108 # HH:MM
109 $array = explode( ":" , $s );
110 $hour = intval( $array[0] );
111 $minute = intval( $array[1] );
112 } else {
113 $minute = intval( $s * 60 );
114 $hour = intval( $minute / 60 );
115 $minute = abs( $minute ) % 60;
116 }
117 $hour = min( $hour, 15 );
118 $hour = max( $hour, -15 );
119 $minute = min( $minute, 59 );
120 $minute = max( $minute, 0 );
121 $s = sprintf( "%02d:%02d", $hour, $minute );
122 }
123 return $s;
124 }
125
126 /* private */ function savePreferences()
127 {
128 global $wgUser, $wgLang, $wgDeferredUpdateList, $wgOut;
129
130 if ( "" != $this->mNewpass ) {
131 if ( $this->mNewpass != $this->mRetypePass ) {
132 $this->mainPrefsForm( wfMsg( "badretype" ) );
133 return;
134 }
135 $ep = $wgUser->encryptPassword( $this->mOldpass );
136 if ( $ep != $wgUser->getPassword() ) {
137 if ( $ep != $wgUser->getNewpassword() ) {
138 $this->mainPrefsForm( wfMsg( "wrongpassword" ) );
139 return;
140 }
141 }
142 $wgUser->setPassword( $this->mNewpass );
143 }
144 $wgUser->setEmail( $this->mUserEmail );
145 $wgUser->setOption( "nickname", $this->mNick );
146 $wgUser->setOption( "quickbar", $this->mQuickbar );
147 $wgUser->setOption( "skin", $this->mSkin );
148 $wgUser->setOption( "math", $this->mMath );
149 $wgUser->setOption( "date", $this->mDate );
150 $wgUser->setOption( "searchlimit", $this->validateIntOrNull( $this->mSearch ) );
151 $wgUser->setOption( "contextlines", $this->validateIntOrNull( $this->mSearchLines ) );
152 $wgUser->setOption( "contextchars", $this->validateIntOrNull( $this->mSearchChars ) );
153 $wgUser->setOption( "rclimit", $this->validateIntOrNull( $this->mRecent ) );
154 $wgUser->setOption( "rows", $this->validateInt( $this->mRows, 4, 1000 ) );
155 $wgUser->setOption( "cols", $this->validateInt( $this->mCols, 4, 1000 ) );
156 $wgUser->setOption( "stubthreshold", $this->validateIntOrNull( $this->mStubs ) );
157 $wgUser->setOption( "timecorrection", $this->validateTimeZone( $this->mHourDiff, -12, 14 ) );
158
159 # Set search namespace options
160 foreach( $this->mSearchNs as $i => $value ) {
161 $wgUser->setOption( "searchNs{$i}", $value );
162 }
163
164 $wgUser->setOption( "disablemail", $this->mEmailFlag );
165
166 # Set user toggles
167 foreach ( $this->mToggles as $tname => $tvalue ) {
168 $wgUser->setOption( $tname, $tvalue );
169 }
170 $wgUser->setCookies();
171 $up = new UserUpdate();
172 array_push( $wgDeferredUpdateList, $up );
173 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
174 $po = ParserOptions::newFromUser( $wgUser );
175 print get_class( $wgUser->getSkin() ) . "<br>\n";
176 print get_class( $po->mSkin ) . "<br>\n";
177 print get_class( $wgOut->mParserOptions->mSkin ) . "<br>\n";
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 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $this->mEmailFlag = 1; }
188 else { $this->mEmailFlag = 0; }
189 $this->mNick = $wgUser->getOption( "nickname" );
190
191 $this->mQuickbar = $wgUser->getOption( "quickbar" );
192 $this->mSkin = $wgUser->getOption( "skin" );
193 $this->mMath = $wgUser->getOption( "math" );
194 $this->mDate = $wgUser->getOption( "date" );
195 $this->mRows = $wgUser->getOption( "rows" );
196 $this->mCols = $wgUser->getOption( "cols" );
197 $this->mStubs = $wgUser->getOption( "stubthreshold" );
198 $this->mHourDiff = $wgUser->getOption( "timecorrection" );
199 $this->mSearch = $wgUser->getOption( "searchlimit" );
200 $this->mSearchLines = $wgUser->getOption( "contextlines" );
201 $this->mSearchChars = $wgUser->getOption( "contextchars" );
202 $this->mRecent = $wgUser->getOption( "rclimit" );
203
204 $togs = $wgLang->getUserToggles();
205 foreach ( $togs as $tname => $ttext ) {
206 $this->mToggles[$tname] = $wgUser->getOption( $tname );
207 }
208
209 $namespaces = $wgLang->getNamespaces();
210 foreach ( $namespaces as $i => $namespace ) {
211 if ( $i >= 0 ) {
212 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" );
213 }
214 }
215 }
216
217 /* private */ function namespacesCheckboxes()
218 {
219 global $wgLang, $wgUser;
220
221 # Determine namespace checkboxes
222 $namespaces = $wgLang->getNamespaces();
223 $r1 = "";
224
225 foreach ( $namespaces as $i => $name ) {
226 # Skip special or anything similar
227 if ( $i >= 0 ) {
228 $checked = "";
229 if ( $this->mSearchNs[$i] ) {
230 $checked = ' checked="checked"';
231 }
232 $name = str_replace( "_", " ", $namespaces[$i] );
233 if ( "" == $name ) {
234 $name = wfMsg( "blanknamespace" );
235 }
236
237 if ( 0 != $i ) {
238 $r1 .= " ";
239 }
240 $r1 .= "<label><input type=checkbox value=\"1\" name=\"" .
241 "wpNs$i\"{$checked}>{$name}</label>\n";
242 }
243 }
244
245 return $r1;
246 }
247
248
249
250
251 /* private */ function mainPrefsForm( $err )
252 {
253 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames;
254
255 $wgOut->setPageTitle( wfMsg( "preferences" ) );
256 $wgOut->setArticleRelated( false );
257 $wgOut->setRobotpolicy( "noindex,nofollow" );
258
259 if ( "" != $err ) {
260 $wgOut->addHTML( "<font size='+1' color='red'>$err</font>\n<p>" );
261 }
262 $uname = $wgUser->getName();
263 $uid = $wgUser->getID();
264
265 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
266
267 $qbs = $wgLang->getQuickbarSettings();
268 $skinNames = $wgLang->getSkinNames();
269 $mathopts = $wgLang->getMathNames();
270 $dateopts = $wgLang->getDateFormats();
271 $togs = $wgLang->getUserToggles();
272
273 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
274 $action = $titleObj->escapeLocalURL();
275
276 $qb = wfMsg( "qbsettings" );
277 $cp = wfMsg( "changepassword" );
278 $sk = wfMsg( "skin" );
279 $math = wfMsg( "math" );
280 $dateFormat = wfMsg("dateformat");
281 $opw = wfMsg( "oldpassword" );
282 $npw = wfMsg( "newpassword" );
283 $rpw = wfMsg( "retypenew" );
284 $svp = wfMsg( "saveprefs" );
285 $rsp = wfMsg( "resetprefs" );
286 $tbs = wfMsg( "textboxsize" );
287 $tbr = wfMsg( "rows" );
288 $tbc = wfMsg( "columns" );
289 $ltz = wfMsg( "localtime" );
290 $tzt = wfMsg( "timezonetext" );
291 $tzo = wfMsg( "timezoneoffset" );
292 $tzGuess = wfMsg( "guesstimezone" );
293 $tzServerTime = wfMsg( "servertime" );
294 $yem = wfMsg( "youremail" );
295 $emf = wfMsg( "emailflag" );
296 $ynn = wfMsg( "yournick" );
297 $stt = wfMsg ( "stubthreshold" ) ;
298 $srh = wfMsg( "searchresultshead" );
299 $rpp = wfMsg( "resultsperpage" );
300 $scl = wfMsg( "contextlines" );
301 $scc = wfMsg( "contextchars" );
302 $rcc = wfMsg( "recentchangescount" );
303 $dsn = wfMsg( "defaultns" );
304
305 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
306 method=\"post\"><table border=\"1\"><tr><td valign=top nowrap><b>$qb:</b><br>\n" );
307
308 # Quickbar setting
309 #
310 for ( $i = 0; $i < count( $qbs ); ++$i ) {
311 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
312 else { $checked = ""; }
313 $wgOut->addHTML( "<label><input type=radio name=\"wpQuickbar\"
314 value=\"$i\"$checked> {$qbs[$i]}</label><br>\n" );
315 }
316
317 # Fields for changing password
318 #
319 $this->mOldpass = wfEscapeHTML( $this->mOldpass );
320 $this->mNewpass = wfEscapeHTML( $this->mNewpass );
321 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass );
322
323 $wgOut->addHTML( "</td><td vaign=top nowrap><b>$cp:</b><br>
324 <label>$opw: <input type=password name=\"wpOldpass\" value=\"{$this->mOldpass}\" size=20></label><br>
325 <label>$npw: <input type=password name=\"wpNewpass\" value=\"{$this->mNewpass}\" size=20></label><br>
326 <label>$rpw: <input type=password name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size=20></label><br>
327 </td></tr>\n" );
328
329 # Skin setting
330 #
331 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$sk:</b><br>\n" );
332 # Only show members of $wgValidSkinNames rather than
333 # $skinNames (skins is all skin names from Language.php)
334 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
335 if ( $skinkey == $this->mSkin ) {
336 $checked = ' checked="checked"';
337 } else {
338 $checked = "";
339 }
340 $wgOut->addHTML( "<label><input type=radio name=\"wpSkin\"
341 value=\"$skinkey\"$checked> {$skinNames[$skinkey]}</label><br>\n" );
342 }
343
344 # Various checkbox options
345 #
346 if ( $wgUseDynamicDates ) {
347 $wgOut->addHTML( "</td><td rowspan=3 valign=top nowrap>\n" );
348 } else {
349 $wgOut->addHTML( "</td><td rowspan=2 valign=top nowrap>\n" );
350 }
351 $wgOut->addHTML("<table border=0>");
352 foreach ( $togs as $tname => $ttext ) {
353 if ( 1 == $wgUser->getOption( $tname ) ) {
354 $checked = ' checked="checked"';
355 } else {
356 $checked = "";
357 }
358 $wgOut->addHTML( "<tr valign=\"top\"><td><input type=checkbox value=\"1\" "
359 . "id=\"$tname\" name=\"wpOp$tname\"$checked></td><td><label for=\"$tname\">$ttext</label></td></tr>\n" );
360 }
361 $wgOut->addHTML( "</table></td>" );
362
363 # Math setting
364 #
365 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$math:</b><br>\n" );
366 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
367 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; }
368 else { $checked = ""; }
369 $wgOut->addHTML( "<label><input type=radio name=\"wpMath\"
370 value=\"$i\"$checked> {$mathopts[$i]}</label><br>\n" );
371 }
372 $wgOut->addHTML( "</td></tr>" );
373
374 # Date format
375 #
376 if ( $wgUseDynamicDates ) {
377 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$dateFormat:</b><br>" );
378 for ( $i = 0; $i < count( $dateopts ); ++$i) {
379 if ( $i == $this->mDate ) {
380 $checked = ' checked="checked"';
381 } else {
382 $checked = "";
383 }
384 $wgOut->addHTML( "<label><input type=radio name=\"wpDate\" ".
385 "value=\"$i\"$checked> {$dateopts[$i]}</label><br>\n" );
386 }
387 $wgOut->addHTML( "</td></tr>");
388 }
389 # Textbox rows, cols
390 #
391 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
392 $nowserver = $wgLang->time( $now, false );
393 $wgOut->addHTML( "<td valign=top nowrap><b>$tbs:</b><br>
394 <label>$tbr: <input type=text name=\"wpRows\" value=\"{$this->mRows}\" size=6></label><br>
395 <label>$tbc: <input type=text name=\"wpCols\" value=\"{$this->mCols}\" size=6></label><br><br>
396 <b>$tzServerTime:</b> $nowserver<br />
397 <b>$ltz:</b> $nowlocal<br />
398 <label>$tzo*: <input type=text name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size=6></label><br />
399 <input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" />
400 </td>" );
401
402 # Email, etc.
403 #
404 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail );
405 $this->mNick = wfEscapeHTML( $this->mNick );
406 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; }
407 else { $emfc = ""; }
408
409 $ps = $this->namespacesCheckboxes();
410
411 $wgOut->addHTML( "<td valign=top nowrap>
412 <label>$yem: <input type=text name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size=20></label><br>
413 <label><input type=checkbox $emfc value=\"1\" name=\"wpEmailFlag\"> $emf</label><br>
414 <label>$ynn: <input type=text name=\"wpNick\" value=\"{$this->mNick}\" size=12></label><br>
415 <label>$rcc: <input type=text name=\"wpRecent\" value=\"$this->mRecent\" size=6></label><br>
416 <label>$stt: <input type=text name=\"wpStubs\" value=\"$this->mStubs\" size=6></label><br>
417 <strong>{$srh}:</strong><br>
418 <label>$rpp: <input type=text name=\"wpSearch\" value=\"$this->mSearch\" size=6></label><br>
419 <label>$scl: <input type=text name=\"wpSearchLines\" value=\"$this->mSearchLines\" size=6></label><br>
420 <label>$scc: <input type=text name=\"wpSearchChars\" value=\"$this->mSearchChars\" size=6></label></td>
421 </tr><tr>
422 <td colspan=2>
423 <b>$dsn</b><br>
424 $ps
425 </td>
426 </tr><tr>
427 <td align=center><input type=submit name=\"wpSaveprefs\" value=\"$svp\"></td>
428 <td align=center><input type=submit name=\"wpReset\" value=\"$rsp\"></td>
429 </tr></table>* {$tzt} </form>\n" );
430 }
431 }
432 ?>