Implemented the "default namespaces for search" feature. This includes
[lhc/web/wiklou.git] / includes / SpecialPreferences.php
1 <?
2 function wfSpecialPreferences()
3 {
4 global $wgUser, $wgOut, $action;
5 global $wpSaveprefs, $wpReset;
6
7 $fields = array( "wpOldpass", "wpNewpass", "wpRetype",
8 "wpEmail", "wpNick" );
9 wfCleanFormFields( $fields );
10
11 if ( 0 == $wgUser->getID() ) {
12 $wgOut->errorpage( "prefsnologin", "prefsnologintext" );
13 return;
14 }
15 if ( wfReadOnly() ) {
16 $wgOut->readOnlyPage();
17 return;
18 }
19 if ( isset( $wpReset ) ) {
20 resetPrefs();
21 mainPrefsForm( WfMsg( "prefsreset" ) );
22 } else if ( "submit" == $action || isset( $wpSaveprefs ) ) {
23 savePreferences();
24 } else {
25 resetPrefs();
26 mainPrefsForm( "" );
27 }
28 }
29
30 /* private */ function validateInt( &$val, $min=0, $max=0x7fffffff ) {
31 $val = intval($val);
32 $val = min($val, $max);
33 $val = max($val, $min);
34 return $val;
35 }
36
37 /* private */ function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
38 $val = trim($val);
39 if($val === "") {
40 return $val;
41 } else {
42 return validateInt( $val, $min, $max );
43 }
44 }
45
46
47 /* private */ function validateCheckbox( $cb )
48 {
49 if ( $cb )
50 {
51 return 1;
52 }
53 else
54 {
55 return 0;
56 }
57 }
58
59
60
61 /* private */ function savePreferences()
62 {
63 global $wgUser, $wgLang, $wgDeferredUpdateList;
64 global $wpQuickbar, $wpOldpass, $wpNewpass, $wpRetype;
65 global $wpSkin, $wpMath, $wpEmail, $wpEmailFlag, $wpNick, $wpSearch, $wpRecent;
66 global $wpSearchLines, $wpSearchChars, $wpStubs;
67 global $wpRows, $wpCols, $wpHourDiff, $HTTP_POST_VARS;
68 global $wpNs0, $wpNs1, $wpNs2, $wpNs3, $wpNs4, $wpNs5, $wpNs6, $wpNs7;
69
70 if ( "" != $wpNewpass ) {
71 if ( $wpNewpass != $wpRetype ) {
72 mainPrefsForm( wfMsg( "badretype" ) );
73 return;
74 }
75 $ep = $wgUser->encryptPassword( $wpOldpass );
76 if ( $ep != $wgUser->getPassword() ) {
77 if ( $ep != $wgUser->getNewpassword() ) {
78 mainPrefsForm( wfMsg( "wrongpassword" ) );
79 return;
80 }
81 }
82 $wgUser->setPassword( $wpNewpass );
83 }
84 $wgUser->setEmail( $wpEmail );
85 $wgUser->setOption( "nickname", $wpNick );
86 $wgUser->setOption( "quickbar", $wpQuickbar );
87 $wgUser->setOption( "skin", $wpSkin );
88 $wgUser->setOption( "math", $wpMath );
89 $wgUser->setOption( "searchlimit", validateIntOrNull( $wpSearch ) );
90 $wgUser->setOption( "contextlines", validateIntOrNull( $wpSearchLines ) );
91 $wgUser->setOption( "contextchars", validateIntOrNull( $wpSearchChars ) );
92 $wgUser->setOption( "rclimit", validateIntOrNull( $wpRecent ) );
93 $wgUser->setOption( "rows", validateInt( $wpRows, 4, 1000 ) );
94 $wgUser->setOption( "cols", validateInt( $wpCols, 4, 1000 ) );
95 $wgUser->setOption( "stubthreshold", validateIntOrNull( $wpStubs ) );
96 $wgUser->setOption( "timecorrection", validateIntOrNull( $wpHourDiff, -12, 14 ) );
97
98 $wgUser->setOption( "searchNs0", validateCheckbox( $wpNs0 ) );
99 $wgUser->setOption( "searchNs1", validateCheckbox( $wpNs1 ) );
100 $wgUser->setOption( "searchNs2", validateCheckbox( $wpNs2 ) );
101 $wgUser->setOption( "searchNs3", validateCheckbox( $wpNs3 ) );
102 $wgUser->setOption( "searchNs4", validateCheckbox( $wpNs4 ) );
103 $wgUser->setOption( "searchNs5", validateCheckbox( $wpNs5 ) );
104 $wgUser->setOption( "searchNs6", validateCheckbox( $wpNs6 ) );
105 $wgUser->setOption( "searchNs7", validateCheckbox( $wpNs7 ) );
106
107
108 $wgUser->setOption( "disablemail", validateCheckbox( $wpEmailFlag ) );
109
110 $togs = $wgLang->getUserToggles();
111 foreach ( $togs as $tname => $ttext ) {
112 if ( array_key_exists( "wpOp$tname", $HTTP_POST_VARS ) ) {
113 $wgUser->setOption( $tname, 1 );
114 } else {
115 $wgUser->setOption( $tname, 0 );
116 }
117 }
118 $wgUser->setCookies();
119 $up = new UserUpdate();
120 array_push( $wgDeferredUpdateList, $up );
121 mainPrefsForm( wfMsg( "savedprefs" ) );
122 }
123
124 /* private */ function resetPrefs()
125 {
126 global $wgUser, $wgLang;
127 global $wpQuickbar, $wpOldpass, $wpNewpass, $wpRetype, $wpStubs;
128 global $wpRows, $wpCols, $wpSkin, $wpMath, $wpEmail, $wpEmailFlag, $wpNick;
129 global $wpSearch, $wpRecent, $HTTP_POST_VARS;
130 global $wpHourDiff, $wpSearchLines, $wpSearchChars;
131
132 $wpOldpass = $wpNewpass = $wpRetype = "";
133 $wpEmail = $wgUser->getEmail();
134 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $wpEmailFlag = 1; }
135 else { $wpEmailFlag = 0; }
136 $wpNick = $wgUser->getOption( "nickname" );
137
138 $wpQuickbar = $wgUser->getOption( "quickbar" );
139 $wpSkin = $wgUser->getOption( "skin" );
140 $wpMath = $wgUser->getOption( "math" );
141 $wpRows = $wgUser->getOption( "rows" );
142 $wpCols = $wgUser->getOption( "cols" );
143 $wpStubs = $wgUser->getOption( "stubthreshold" );
144 $wpHourDiff = $wgUser->getOption( "timecorrection" );
145 $wpSearch = $wgUser->getOption( "searchlimit" );
146 $wpSearchLines = $wgUser->getOption( "contextlines" );
147 $wpSearchChars = $wgUser->getOption( "contextchars" );
148 $wpRecent = $wgUser->getOption( "rclimit" );
149
150 $togs = $wgLang->getUserToggles();
151 foreach ( $togs as $tname => $ttext ) {
152 $HTTP_POST_VARS["wpOp$tname"] = $wgUser->getOption( $tname );
153 }
154 }
155
156
157
158
159 /* private */ function namespacesCheckboxes()
160 {
161 global $wgLang, $wgUser;
162 $nscb = array();
163
164
165 for ($i = 0; ($i < 8); $i++)
166 {
167 $nscb[$i] = $wgUser->getOption( "searchNs".$i );
168 }
169
170 # Determine namespace checkboxes
171
172 $ns = $wgLang->getNamespaces();
173 array_shift( $ns ); /* Skip "Special" */
174
175 $r1 = "";
176 for ( $i = 0; $i < count( $ns ); ++$i ) {
177 $checked = "";
178 if ( $nscb[$i] == 1 ) {
179 $checked = " checked";
180 }
181 $name = str_replace( "_", " ", $ns[$i] );
182 if ( "" == $name ) { $name = "(Main)"; }
183
184 if ( 0 != $i ) { $r1 .= " "; }
185 $r1 .= "<input type=checkbox value=\"1\" name=\"" .
186 "wpNs{$i}\"{$checked}>{$name}\n";
187 }
188
189 return $r1;
190 }
191
192
193
194
195 /* private */ function mainPrefsForm( $err )
196 {
197 global $wgUser, $wgOut, $wgLang;
198 global $wpQuickbar, $wpOldpass, $wpNewpass, $wpRetype;
199 global $wpSkin, $wpMath, $wpEmail, $wpEmailFlag, $wpNick, $wpSearch, $wpRecent;
200 global $wpRows, $wpCols, $wpSaveprefs, $wpReset, $wpHourDiff;
201 global $wpSearchLines, $wpSearchChars, $wpStubs;
202
203 $wgOut->setPageTitle( wfMsg( "preferences" ) );
204 $wgOut->setArticleFlag( false );
205 $wgOut->setRobotpolicy( "noindex,nofollow" );
206
207 if ( "" != $err ) {
208 $wgOut->addHTML( "<font size='+1' color='red'>$err</font>\n<p>" );
209 }
210 $uname = $wgUser->getName();
211 $uid = $wgUser->getID();
212
213 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
214
215 $qbs = $wgLang->getQuickbarSettings();
216 $skins = $wgLang->getSkinNames();
217 $mathopts = $wgLang->getMathNames();
218 $togs = $wgLang->getUserToggles();
219
220 $action = wfLocalUrlE( $wgLang->specialPage( "Preferences" ),
221 "action=submit" );
222 $qb = wfMsg( "qbsettings" );
223 $cp = wfMsg( "changepassword" );
224 $sk = wfMsg( "skin" );
225 $math = wfMsg( "math" );
226 $opw = wfMsg( "oldpassword" );
227 $npw = wfMsg( "newpassword" );
228 $rpw = wfMsg( "retypenew" );
229 $svp = wfMsg( "saveprefs" );
230 $rsp = wfMsg( "resetprefs" );
231 $tbs = wfMsg( "textboxsize" );
232 $tbr = wfMsg( "rows" );
233 $tbc = wfMsg( "columns" );
234 $ltz = wfMsg( "localtime" );
235 $tzt = wfMsg( "timezonetext" );
236 $tzo = wfMsg( "timezoneoffset" );
237 $tzGuess = wfMsg( "guesstimezone" );
238 $tzServerTime = wfMsg( "servertime" );
239 $yem = wfMsg( "youremail" );
240 $emf = wfMsg( "emailflag" );
241 $ynn = wfMsg( "yournick" );
242 $stt = wfMsg ( "stubthreshold" ) ;
243 $srh = wfMsg( "searchresultshead" );
244 $rpp = wfMsg( "resultsperpage" );
245 $scl = wfMsg( "contextlines" );
246 $scc = wfMsg( "contextchars" );
247 $rcc = wfMsg( "recentchangescount" );
248 $dsn = wfMsg( "defaultns" );
249
250 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
251 method=\"post\"><table border=\"1\"><tr><td valign=top nowrap><b>$qb:</b><br>\n" );
252
253 # Quickbar setting
254 #
255 for ( $i = 0; $i < count( $qbs ); ++$i ) {
256 if ( $i == $wpQuickbar ) { $checked = " checked"; }
257 else { $checked = ""; }
258 $wgOut->addHTML( "<label><input type=radio name=\"wpQuickbar\"
259 value=\"$i\"$checked> {$qbs[$i]}</label><br>\n" );
260 }
261
262 # Fields for changing password
263 #
264 $wpOldpass = wfEscapeHTML( $wpOldpass );
265 $wpNewpass = wfEscapeHTML( $wpNewpass );
266 $wpRetype = wfEscapeHTML( $wpRetype );
267
268 $wgOut->addHTML( "</td><td vaign=top nowrap><b>$cp:</b><br>
269 <label>$opw: <input type=password name=\"wpOldpass\" value=\"$wpOldpass\" size=20></label><br>
270 <label>$npw: <input type=password name=\"wpNewpass\" value=\"$wpNewpass\" size=20></label><br>
271 <label>$rpw: <input type=password name=\"wpRetype\" value=\"$wpRetype\" size=20></label><br>
272 </td></tr>\n" );
273
274 # Skin setting
275 #
276 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$sk:</b><br>\n" );
277 for ( $i = 0; $i < count( $skins ); ++$i ) {
278 if ( $i == $wpSkin ) { $checked = " checked"; }
279 else { $checked = ""; }
280 $wgOut->addHTML( "<label><input type=radio name=\"wpSkin\"
281 value=\"$i\"$checked> {$skins[$i]}</label><br>\n" );
282 }
283
284 # Various checkbox options
285 #
286 $wgOut->addHTML( "</td><td rowspan=2 valign=top nowrap>\n" );
287 foreach ( $togs as $tname => $ttext ) {
288 if ( 1 == $wgUser->getOption( $tname ) ) {
289 $checked = " checked";
290 } else {
291 $checked = "";
292 }
293 $wgOut->addHTML( "<label><input type=checkbox value=\"1\" "
294 . "name=\"wpOp$tname\"$checked>$ttext</label><br>\n" );
295 }
296 $wgOut->addHTML( "</td>" );
297
298 # Math setting
299 #
300 $wgOut->addHTML( "<tr><td valign=top nowrap><b>$math:</b><br>\n" );
301 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
302 if ( $i == $wpMath ) { $checked = " checked"; }
303 else { $checked = ""; }
304 $wgOut->addHTML( "<label><input type=radio name=\"wpMath\"
305 value=\"$i\"$checked> {$mathopts[$i]}</label><br>\n" );
306 }
307
308 $wgOut->addHTML( "</td></tr><tr>" );
309
310 # Textbox rows, cols
311 #
312 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
313 $nowserver = $wgLang->time( $now, false );
314 $wgOut->addHTML( "<td valign=top nowrap><b>$tbs:</b><br>
315 <label>$tbr: <input type=text name=\"wpRows\" value=\"{$wpRows}\" size=6></label><br>
316 <label>$tbc: <input type=text name=\"wpCols\" value=\"{$wpCols}\" size=6></label><br><br>
317 <b>$tzServerTime:</b> $nowserver<br />
318 <b>$ltz:</b> $nowlocal<br />
319 <label>$tzo*: <input type=text name=\"wpHourDiff\" value=\"{$wpHourDiff}\" size=6></label><br />
320 <input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" />
321 </td>" );
322
323 # Email, etc.
324 #
325 $wpEmail = wfEscapeHTML( $wpEmail );
326 $wpNick = wfEscapeHTML( $wpNick );
327 if ( $wpEmailFlag ) { $emfc = "checked"; }
328 else { $emfc = ""; }
329
330 $ps = namespacesCheckboxes();
331
332 $wgOut->addHTML( "<td valign=top nowrap>
333 <label>$yem: <input type=text name=\"wpEmail\" value=\"{$wpEmail}\" size=20></label><br>
334 <label><input type=checkbox $emfc value=\"1\" name=\"wpEmailFlag\"> $emf</label><br>
335 <label>$ynn: <input type=text name=\"wpNick\" value=\"{$wpNick}\" size=12></label><br>
336 <label>$rcc: <input type=text name=\"wpRecent\" value=\"$wpRecent\" size=6></label><br>
337 <label>$stt: <input type=text name=\"wpStubs\" value=\"$wpStubs\" size=6></label><br>
338 <strong>{$srh}:</strong><br>
339 <label>$rpp: <input type=text name=\"wpSearch\" value=\"$wpSearch\" size=6></label><br>
340 <label>$scl: <input type=text name=\"wpSearchLines\" value=\"$wpSearchLines\" size=6></label><br>
341 <label>$scc: <input type=text name=\"wpSearchChars\" value=\"$wpSearchChars\" size=6></label></td>
342 </tr><tr>
343 <td colspan=2>
344 <b>$dsn</b><br>
345 $ps
346 </td>
347 </tr><tr>
348 <td align=center><input type=submit name=\"wpSaveprefs\" value=\"$svp\"></td>
349 <td align=center><input type=submit name=\"wpReset\" value=\"$rsp\"></td>
350 </tr></table>* {$tzt} </form>\n" );
351 }
352
353 ?>