Refactored (object-orientified) to make them play nice with $wgRequest
[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;
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 $this->mainPrefsForm( wfMsg( "savedprefs" ) );
174 }
175
176 /* private */ function resetPrefs()
177 {
178 global $wgUser, $wgLang;
179
180 $this->mOldpass = $this->mNewpass = $this->mRetypePass = "";
181 $this->mUserEmail = $wgUser->getEmail();
182 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $this->mEmailFlag = 1; }
183 else { $this->mEmailFlag = 0; }
184 $this->mNick = $wgUser->getOption( "nickname" );
185
186 $this->mQuickbar = $wgUser->getOption( "quickbar" );
187 $this->mSkin = $wgUser->getOption( "skin" );
188 $this->mMath = $wgUser->getOption( "math" );
189 $this->mDate = $wgUser->getOption( "date" );
190 $this->mRows = $wgUser->getOption( "rows" );
191 $this->mCols = $wgUser->getOption( "cols" );
192 $this->mStubs = $wgUser->getOption( "stubthreshold" );
193 $this->mHourDiff = $wgUser->getOption( "timecorrection" );
194 $this->mSearch = $wgUser->getOption( "searchlimit" );
195 $this->mSearchLines = $wgUser->getOption( "contextlines" );
196 $this->mSearchChars = $wgUser->getOption( "contextchars" );
197 $this->mRecent = $wgUser->getOption( "rclimit" );
198
199 $togs = $wgLang->getUserToggles();
200 foreach ( $togs as $tname => $ttext ) {
201 $this->mToggles[$tname] = $wgUser->getOption( $tname );
202 }
203
204 $namespaces = $wgLang->getNamespaces();
205 foreach ( $namespaces as $i => $namespace ) {
206 if ( $i >= 0 ) {
207 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" );
208 }
209 }
210 }
211
212 /* private */ function namespacesCheckboxes()
213 {
214 global $wgLang, $wgUser;
215
216 # Determine namespace checkboxes
217 $namespaces = $wgLang->getNamespaces();
218 $r1 = "";
219
220 foreach ( $namespaces as $i => $name ) {
221 # Skip special or anything similar
222 if ( $i >= 0 ) {
223 $checked = "";
224 if ( $this->mSearchNs[$i] ) {
225 $checked = " checked";
226 }
227 $name = str_replace( "_", " ", $namespaces[$i] );
228 if ( "" == $name ) {
229 $name = wfMsg( "blanknamespace" );
230 }
231
232 if ( 0 != $i ) {
233 $r1 .= " ";
234 }
235 $r1 .= "<label><input type=checkbox value=\"1\" name=\"" .
236 "wpNs$i\"{$checked}>{$name}</label>\n";
237 }
238 }
239
240 return $r1;
241 }
242
243
244
245
246 /* private */ function mainPrefsForm( $err )
247 {
248 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames;
249
250 $wgOut->setPageTitle( wfMsg( "preferences" ) );
251 $wgOut->setArticleRelated( false );
252 $wgOut->setRobotpolicy( "noindex,nofollow" );
253
254 if ( "" != $err ) {
255 $wgOut->addHTML( "<font size='+1' color='red'>$err</font>\n<p>" );
256 }
257 $uname = $wgUser->getName();
258 $uid = $wgUser->getID();
259
260 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
261
262 $qbs = $wgLang->getQuickbarSettings();
263 $skinNames = $wgLang->getSkinNames();
264 $mathopts = $wgLang->getMathNames();
265 $dateopts = $wgLang->getDateFormats();
266 $togs = $wgLang->getUserToggles();
267
268 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
269 $action = $titleObj->escapeLocalURL();
270
271 $qb = wfMsg( "qbsettings" );
272 $cp = wfMsg( "changepassword" );
273 $sk = wfMsg( "skin" );
274 $math = wfMsg( "math" );
275 $dateFormat = wfMsg("dateformat");
276 $opw = wfMsg( "oldpassword" );
277 $npw = wfMsg( "newpassword" );
278 $rpw = wfMsg( "retypenew" );
279 $svp = wfMsg( "saveprefs" );
280 $rsp = wfMsg( "resetprefs" );
281 $tbs = wfMsg( "textboxsize" );
282 $tbr = wfMsg( "rows" );
283 $tbc = wfMsg( "columns" );
284 $ltz = wfMsg( "localtime" );
285 $tzt = wfMsg( "timezonetext" );
286 $tzo = wfMsg( "timezoneoffset" );
287 $tzGuess = wfMsg( "guesstimezone" );
288 $tzServerTime = wfMsg( "servertime" );
289 $yem = wfMsg( "youremail" );
290 $emf = wfMsg( "emailflag" );
291 $ynn = wfMsg( "yournick" );
292 $stt = wfMsg ( "stubthreshold" ) ;
293 $srh = wfMsg( "searchresultshead" );
294 $rpp = wfMsg( "resultsperpage" );
295 $scl = wfMsg( "contextlines" );
296 $scc = wfMsg( "contextchars" );
297 $rcc = wfMsg( "recentchangescount" );
298 $dsn = wfMsg( "defaultns" );
299
300 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
301 method=\"post\"><table border=\"1\"><tr><td valign=top nowrap><b>$qb:</b><br>\n" );
302
303 # Quickbar setting
304 #
305 for ( $i = 0; $i < count( $qbs ); ++$i ) {
306 if ( $i == $this->mQuickbar ) { $checked = " checked"; }
307 else { $checked = ""; }
308 $wgOut->addHTML( "<label><input type=radio name=\"wpQuickbar\"
309 value=\"$i\"$checked> {$qbs[$i]}</label><br>\n" );
310 }
311
312 # Fields for changing password
313 #
314 $this->mOldpass = wfEscapeHTML( $this->mOldpass );
315 $this->mNewpass = wfEscapeHTML( $this->mNewpass );
316 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass );
317
318 $wgOut->addHTML( "</td><td vaign=top nowrap><b>$cp:</b><br>
319 <label>$opw: <input type=password name=\"wpOldpass\" value=\"{$this->mOldpass}\" size=20></label><br>
320 <label>$npw: <input type=password name=\"wpNewpass\" value=\"{$this->mNewpass}\" size=20></label><br>
321 <label>$rpw: <input type=password name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size=20></label><br>
322 </td></tr>\n" );
323
324 # Skin setting
325 #
326 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$sk:</b><br>\n" );
327 # Only show members of $wgValidSkinNames rather than
328 # $skinNames (skins is all skin names from Language.php)
329 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
330 if ( $skinkey == $this->mSkin ) {
331 $checked = ' checked';
332 } else {
333 $checked = "";
334 }
335 $wgOut->addHTML( "<label><input type=radio name=\"wpSkin\"
336 value=\"$skinkey\"$checked> {$skinNames[$skinkey]}</label><br>\n" );
337 }
338
339 # Various checkbox options
340 #
341 if ( $wgUseDynamicDates ) {
342 $wgOut->addHTML( "</td><td rowspan=3 valign=top nowrap>\n" );
343 } else {
344 $wgOut->addHTML( "</td><td rowspan=2 valign=top nowrap>\n" );
345 }
346 $wgOut->addHTML("<table border=0>");
347 foreach ( $togs as $tname => $ttext ) {
348 if ( 1 == $wgUser->getOption( $tname ) ) {
349 $checked = " checked";
350 } else {
351 $checked = "";
352 }
353 $wgOut->addHTML( "<tr valign=\"top\"><td><input type=checkbox value=\"1\" "
354 . "id=\"$tname\" name=\"wpOp$tname\"$checked></td><td><label for=\"$tname\">$ttext</label></td></tr>\n" );
355 }
356 $wgOut->addHTML( "</table></td>" );
357
358 # Math setting
359 #
360 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$math:</b><br>\n" );
361 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
362 if ( $i == $this->mMath ) { $checked = " checked"; }
363 else { $checked = ""; }
364 $wgOut->addHTML( "<label><input type=radio name=\"wpMath\"
365 value=\"$i\"$checked> {$mathopts[$i]}</label><br>\n" );
366 }
367 $wgOut->addHTML( "</td></tr>" );
368
369 # Date format
370 #
371 if ( $wgUseDynamicDates ) {
372 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$dateFormat:</b><br>" );
373 for ( $i = 0; $i < count( $dateopts ); ++$i) {
374 if ( $i == $this->mDate ) {
375 $checked = " checked";
376 } else {
377 $checked = "";
378 }
379 $wgOut->addHTML( "<label><input type=radio name=\"wpDate\" ".
380 "value=\"$i\"$checked> {$dateopts[$i]}</label><br>\n" );
381 }
382 $wgOut->addHTML( "</td></tr>");
383 }
384 # Textbox rows, cols
385 #
386 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
387 $nowserver = $wgLang->time( $now, false );
388 $wgOut->addHTML( "<td valign=top nowrap><b>$tbs:</b><br>
389 <label>$tbr: <input type=text name=\"wpRows\" value=\"{$this->mRows}\" size=6></label><br>
390 <label>$tbc: <input type=text name=\"wpCols\" value=\"{$this->mCols}\" size=6></label><br><br>
391 <b>$tzServerTime:</b> $nowserver<br />
392 <b>$ltz:</b> $nowlocal<br />
393 <label>$tzo*: <input type=text name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size=6></label><br />
394 <input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" />
395 </td>" );
396
397 # Email, etc.
398 #
399 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail );
400 $this->mNick = wfEscapeHTML( $this->mNick );
401 if ( $this->mEmailFlag ) { $emfc = "checked"; }
402 else { $emfc = ""; }
403
404 $ps = $this->namespacesCheckboxes();
405
406 $wgOut->addHTML( "<td valign=top nowrap>
407 <label>$yem: <input type=text name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size=20></label><br>
408 <label><input type=checkbox $emfc value=\"1\" name=\"wpEmailFlag\"> $emf</label><br>
409 <label>$ynn: <input type=text name=\"wpNick\" value=\"{$this->mNick}\" size=12></label><br>
410 <label>$rcc: <input type=text name=\"wpRecent\" value=\"$this->mRecent\" size=6></label><br>
411 <label>$stt: <input type=text name=\"wpStubs\" value=\"$this->mStubs\" size=6></label><br>
412 <strong>{$srh}:</strong><br>
413 <label>$rpp: <input type=text name=\"wpSearch\" value=\"$this->mSearch\" size=6></label><br>
414 <label>$scl: <input type=text name=\"wpSearchLines\" value=\"$this->mSearchLines\" size=6></label><br>
415 <label>$scc: <input type=text name=\"wpSearchChars\" value=\"$this->mSearchChars\" size=6></label></td>
416 </tr><tr>
417 <td colspan=2>
418 <b>$dsn</b><br>
419 $ps
420 </td>
421 </tr><tr>
422 <td align=center><input type=submit name=\"wpSaveprefs\" value=\"$svp\"></td>
423 <td align=center><input type=submit name=\"wpReset\" value=\"$rsp\"></td>
424 </tr></table>* {$tzt} </form>\n" );
425 }
426 }
427 ?>