Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

SpecialPreferences.php

Go to the documentation of this file.
00001 <?php 00002 function wfSpecialPreferences() 00003 { 00004 global $wgRequest; 00005 00006 $form = new PreferencesForm( $wgRequest ); 00007 $form->execute(); 00008 } 00009 00010 class PreferencesForm { 00011 var $mQuickbar, $mOldpass, $mNewpass, $mRetypePass, $mStubs; 00012 var $mRows, $mCols, $mSkin, $mMath, $mDate, $mUserEmail, $mEmailFlag, $mNick; 00013 var $mSearch, $mRecent, $mHourDiff, $mSearchLines, $mSearchChars, $mAction; 00014 var $mReset, $mPosted, $mToggles, $mSearchNs, $mRealName; 00015 00016 function PreferencesForm( &$request ) { 00017 global $wgLang, $wgAllowRealName; 00018 00019 $this->mQuickbar = $request->getVal( 'wpQuickbar' ); 00020 $this->mOldpass = $request->getVal( 'wpOldpass' ); 00021 $this->mNewpass = $request->getVal( 'wpNewpass' ); 00022 $this->mRetypePass =$request->getVal( 'wpRetypePass' ); 00023 $this->mStubs = $request->getVal( 'wpStubs' ); 00024 $this->mRows = $request->getVal( 'wpRows' ); 00025 $this->mCols = $request->getVal( 'wpCols' ); 00026 $this->mSkin = $request->getVal( 'wpSkin' ); 00027 $this->mMath = $request->getVal( 'wpMath' ); 00028 $this->mDate = $request->getVal( 'wpDate' ); 00029 $this->mUserEmail = $request->getVal( 'wpUserEmail' ); 00030 $this->mRealName = ($wgAllowRealName) ? $request->getVal( 'wpRealName' ) : ''; 00031 $this->mEmailFlag = $request->getCheck( 'wpEmailFlag' ) ? 1 : 0; 00032 $this->mNick = $request->getVal( 'wpNick' ); 00033 $this->mSearch = $request->getVal( 'wpSearch' ); 00034 $this->mRecent = $request->getVal( 'wpRecent' ); 00035 $this->mHourDiff = $request->getVal( 'wpHourDiff' ); 00036 $this->mSearchLines = $request->getVal( 'wpSearchLines' ); 00037 $this->mSearchChars = $request->getVal( 'wpSearchChars' ); 00038 $this->mAction = $request->getVal( 'action' ); 00039 $this->mReset = $request->getCheck( 'wpReset' ); 00040 $this->mPosted = $request->wasPosted(); 00041 $this->mSaveprefs = $request->getCheck( 'wpSaveprefs' ) && $this->mPosted; 00042 00043 # User toggles (the big ugly unsorted list of checkboxes) 00044 $this->mToggles = array(); 00045 if ( $this->mPosted ) { 00046 $togs = $wgLang->getUserToggles(); 00047 foreach ( $togs as $tname => $ttext ) { 00048 $this->mToggles[$tname] = $request->getCheck( "wpOp$tname" ) ? 1 : 0; 00049 } 00050 } 00051 00052 $this->mUsedToggles = array(); 00053 00054 # Search namespace options 00055 # Note: namespaces don't necessarily have consecutive keys 00056 $this->mSearchNs = array(); 00057 if ( $this->mPosted ) { 00058 $namespaces = $wgLang->getNamespaces(); 00059 foreach ( $namespaces as $i => $namespace ) { 00060 if ( $i >= 0 ) { 00061 $this->mSearchNs[$i] = $request->getCheck( "wpNs$i" ) ? 1 : 0; 00062 } 00063 } 00064 } 00065 } 00066 00067 function execute() { 00068 global $wgUser, $wgOut, $wgUseDynamicDates; 00069 00070 if ( 0 == $wgUser->getID() ) { 00071 $wgOut->errorpage( "prefsnologin", "prefsnologintext" ); 00072 return; 00073 } 00074 if ( wfReadOnly() ) { 00075 $wgOut->readOnlyPage(); 00076 return; 00077 } 00078 if ( $this->mReset ) { 00079 $this->resetPrefs(); 00080 $this->mainPrefsForm( wfMsg( "prefsreset" ) ); 00081 } else if ( $this->mSaveprefs ) { 00082 $this->savePreferences(); 00083 } else { 00084 $this->resetPrefs(); 00085 $this->mainPrefsForm( "" ); 00086 } 00087 } 00088 00089 /* private */ function validateInt( &$val, $min=0, $max=0x7fffffff ) { 00090 $val = intval($val); 00091 $val = min($val, $max); 00092 $val = max($val, $min); 00093 return $val; 00094 } 00095 00096 /* private */ function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) { 00097 $val = trim($val); 00098 if($val === "") { 00099 return $val; 00100 } else { 00101 return $this->validateInt( $val, $min, $max ); 00102 } 00103 } 00104 00105 /* private */ function validateTimeZone( $s ) 00106 { 00107 00108 if ( $s !== "" ) { 00109 if ( strpos( $s, ":" ) ) { 00110 # HH:MM 00111 $array = explode( ":" , $s ); 00112 $hour = intval( $array[0] ); 00113 $minute = intval( $array[1] ); 00114 } else { 00115 $minute = intval( $s * 60 ); 00116 $hour = intval( $minute / 60 ); 00117 $minute = abs( $minute ) % 60; 00118 } 00119 $hour = min( $hour, 15 ); 00120 $hour = max( $hour, -15 ); 00121 $minute = min( $minute, 59 ); 00122 $minute = max( $minute, 0 ); 00123 $s = sprintf( "%02d:%02d", $hour, $minute ); 00124 } 00125 return $s; 00126 } 00127 00128 /* private */ function savePreferences() 00129 { 00130 global $wgUser, $wgLang, $wgDeferredUpdateList, $wgOut; 00131 00132 if ( "" != $this->mNewpass ) { 00133 if ( $this->mNewpass != $this->mRetypePass ) { 00134 $this->mainPrefsForm( wfMsg( "badretype" ) ); 00135 return; 00136 } 00137 $ep = $wgUser->encryptPassword( $this->mOldpass ); 00138 if ( $ep != $wgUser->getPassword() ) { 00139 if ( $ep != $wgUser->getNewpassword() ) { 00140 $this->mainPrefsForm( wfMsg( "wrongpassword" ) ); 00141 return; 00142 } 00143 } 00144 $wgUser->setPassword( $this->mNewpass ); 00145 } 00146 $wgUser->setEmail( $this->mUserEmail ); 00147 $wgUser->setRealName( $this->mRealName ); 00148 $wgUser->setOption( "nickname", $this->mNick ); 00149 $wgUser->setOption( "quickbar", $this->mQuickbar ); 00150 $wgUser->setOption( "skin", $this->mSkin ); 00151 $wgUser->setOption( "math", $this->mMath ); 00152 $wgUser->setOption( "date", $this->mDate ); 00153 $wgUser->setOption( "searchlimit", $this->validateIntOrNull( $this->mSearch ) ); 00154 $wgUser->setOption( "contextlines", $this->validateIntOrNull( $this->mSearchLines ) ); 00155 $wgUser->setOption( "contextchars", $this->validateIntOrNull( $this->mSearchChars ) ); 00156 $wgUser->setOption( "rclimit", $this->validateIntOrNull( $this->mRecent ) ); 00157 $wgUser->setOption( "rows", $this->validateInt( $this->mRows, 4, 1000 ) ); 00158 $wgUser->setOption( "cols", $this->validateInt( $this->mCols, 4, 1000 ) ); 00159 $wgUser->setOption( "stubthreshold", $this->validateIntOrNull( $this->mStubs ) ); 00160 $wgUser->setOption( "timecorrection", $this->validateTimeZone( $this->mHourDiff, -12, 14 ) ); 00161 00162 # Set search namespace options 00163 foreach( $this->mSearchNs as $i => $value ) { 00164 $wgUser->setOption( "searchNs{$i}", $value ); 00165 } 00166 00167 $wgUser->setOption( "disablemail", $this->mEmailFlag ); 00168 00169 # Set user toggles 00170 foreach ( $this->mToggles as $tname => $tvalue ) { 00171 $wgUser->setOption( $tname, $tvalue ); 00172 } 00173 $wgUser->setCookies(); 00174 $up = new UserUpdate(); 00175 array_push( $wgDeferredUpdateList, $up ); 00176 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) ); 00177 $po = ParserOptions::newFromUser( $wgUser ); 00178 $this->mainPrefsForm( wfMsg( "savedprefs" ) ); 00179 } 00180 00181 /* private */ function resetPrefs() 00182 { 00183 global $wgUser, $wgLang, $wgAllowRealName; 00184 00185 $this->mOldpass = $this->mNewpass = $this->mRetypePass = ""; 00186 $this->mUserEmail = $wgUser->getEmail(); 00187 $this->mRealName = ($wgAllowRealName) ? $wgUser->getRealName() : ''; 00188 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $this->mEmailFlag = 1; } 00189 else { $this->mEmailFlag = 0; } 00190 $this->mNick = $wgUser->getOption( "nickname" ); 00191 00192 $this->mQuickbar = $wgUser->getOption( "quickbar" ); 00193 $this->mSkin = $wgUser->getOption( "skin" ); 00194 $this->mMath = $wgUser->getOption( "math" ); 00195 $this->mDate = $wgUser->getOption( "date" ); 00196 $this->mRows = $wgUser->getOption( "rows" ); 00197 $this->mCols = $wgUser->getOption( "cols" ); 00198 $this->mStubs = $wgUser->getOption( "stubthreshold" ); 00199 $this->mHourDiff = $wgUser->getOption( "timecorrection" ); 00200 $this->mSearch = $wgUser->getOption( "searchlimit" ); 00201 $this->mSearchLines = $wgUser->getOption( "contextlines" ); 00202 $this->mSearchChars = $wgUser->getOption( "contextchars" ); 00203 $this->mRecent = $wgUser->getOption( "rclimit" ); 00204 00205 $togs = $wgLang->getUserToggles(); 00206 foreach ( $togs as $tname => $ttext ) { 00207 $this->mToggles[$tname] = $wgUser->getOption( $tname ); 00208 } 00209 00210 $namespaces = $wgLang->getNamespaces(); 00211 foreach ( $namespaces as $i => $namespace ) { 00212 if ( $i >= 0 ) { 00213 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" ); 00214 } 00215 } 00216 } 00217 00218 /* private */ function namespacesCheckboxes() 00219 { 00220 global $wgLang, $wgUser; 00221 00222 # Determine namespace checkboxes 00223 $namespaces = $wgLang->getNamespaces(); 00224 $r1 = ""; 00225 00226 foreach ( $namespaces as $i => $name ) { 00227 # Skip special or anything similar 00228 if ( $i >= 0 ) { 00229 $checked = ""; 00230 if ( $this->mSearchNs[$i] ) { 00231 $checked = ' checked="checked"'; 00232 } 00233 $name = str_replace( "_", " ", $namespaces[$i] ); 00234 if ( "" == $name ) { 00235 $name = wfMsg( "blanknamespace" ); 00236 } 00237 00238 if ( 0 != $i ) { 00239 $r1 .= " "; 00240 } 00241 $r1 .= "<label><input type='checkbox' value=\"1\" name=\"" . 00242 "wpNs$i\"{$checked} />{$name}</label>\n"; 00243 } 00244 } 00245 00246 return $r1; 00247 } 00248 00249 00250 function getToggle( $tname ) { 00251 global $wgUser, $wgLang; 00252 00253 $this->mUsedToggles[$tname] = true; 00254 $ttext = $wgLang->getUserToggle( $tname ); 00255 00256 if ( 1 == $wgUser->getOption( $tname ) ) { 00257 $checked = ' checked="checked"'; 00258 } else { 00259 $checked = ""; 00260 } 00261 return "<div><input type='checkbox' value=\"1\" " 00262 . "id=\"$tname\" name=\"wpOp$tname\"$checked /><label for=\"$tname\">$ttext</label></div>\n"; 00263 } 00264 00265 /* private */ function mainPrefsForm( $err ) 00266 { 00267 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames; 00268 global $wgAllowRealName; 00269 00270 $wgOut->setPageTitle( wfMsg( "preferences" ) ); 00271 $wgOut->setArticleRelated( false ); 00272 $wgOut->setRobotpolicy( "noindex,nofollow" ); 00273 00274 if ( "" != $err ) { 00275 $wgOut->addHTML( "<p class='error'>" . htmlspecialchars( $err ) . "</p>\n" ); 00276 } 00277 $uname = $wgUser->getName(); 00278 $uid = $wgUser->getID(); 00279 00280 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) ); 00281 $wgOut->addWikiText( wfMsg('clearyourcache')); 00282 00283 $qbs = $wgLang->getQuickbarSettings(); 00284 $skinNames = $wgLang->getSkinNames(); 00285 $mathopts = $wgLang->getMathNames(); 00286 $dateopts = $wgLang->getDateFormats(); 00287 $togs = $wgLang->getUserToggles(); 00288 00289 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" ); 00290 $action = $titleObj->escapeLocalURL(); 00291 00292 $qb = wfMsg( "qbsettings" ); 00293 $cp = wfMsg( "changepassword" ); 00294 $sk = wfMsg( "skin" ); 00295 $math = wfMsg( "math" ); 00296 $dateFormat = wfMsg("dateformat"); 00297 $opw = wfMsg( "oldpassword" ); 00298 $npw = wfMsg( "newpassword" ); 00299 $rpw = wfMsg( "retypenew" ); 00300 $svp = wfMsg( "saveprefs" ); 00301 $rsp = wfMsg( "resetprefs" ); 00302 $tbs = wfMsg( "textboxsize" ); 00303 $tbr = wfMsg( "rows" ); 00304 $tbc = wfMsg( "columns" ); 00305 $ltz = wfMsg( "localtime" ); 00306 $timezone = wfMsg( "timezonelegend" ); 00307 $tzt = wfMsg( "timezonetext" ); 00308 $tzo = wfMsg( "timezoneoffset" ); 00309 $tzGuess = wfMsg( "guesstimezone" ); 00310 $tzServerTime = wfMsg( "servertime" ); 00311 $yem = wfMsg( "youremail" ); 00312 $yrn = ($wgAllowRealName) ? wfMsg( "yourrealname" ) : ''; 00313 $emf = wfMsg( "emailflag" ); 00314 $ynn = wfMsg( "yournick" ); 00315 $stt = wfMsg ( "stubthreshold" ) ; 00316 $srh = wfMsg( "searchresultshead" ); 00317 $rpp = wfMsg( "resultsperpage" ); 00318 $scl = wfMsg( "contextlines" ); 00319 $scc = wfMsg( "contextchars" ); 00320 $rcc = wfMsg( "recentchangescount" ); 00321 $dsn = wfMsg( "defaultns" ); 00322 00323 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\" 00324 method=\"post\">" ); 00325 00326 # First section: identity 00327 # Email, etc. 00328 # 00329 $this->mUserEmail = wfEscapeHTML( $this->mUserEmail ); 00330 $this->mRealName = wfEscapeHTML( $this->mRealName ); 00331 $this->mNick = wfEscapeHTML( $this->mNick ); 00332 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; } 00333 else { $emfc = ""; } 00334 00335 $ps = $this->namespacesCheckboxes(); 00336 00337 $wgOut->addHTML( "<fieldset> 00338 <legend>".wfMsg('prefs-personal')."</legend>"); 00339 if ($wgAllowRealName) { 00340 $wgOut->addHTML("<div><label>$yrn: <input type='text' name=\"wpRealName\" value=\"{$this->mRealName}\" size='20' /></label></div>"); 00341 } 00342 $wgOut->addHTML(" 00343 <div><label>$yem: <input type='text' name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size='20' /></label></div> 00344 <div><label><input type='checkbox' $emfc value=\"1\" name=\"wpEmailFlag\" /> $emf</label></div> 00345 <div><label>$ynn: <input type='text' name=\"wpNick\" value=\"{$this->mNick}\" size='12' /></label></div>\n" ); 00346 00347 # Fields for changing password 00348 # 00349 $this->mOldpass = wfEscapeHTML( $this->mOldpass ); 00350 $this->mNewpass = wfEscapeHTML( $this->mNewpass ); 00351 $this->mRetypePass = wfEscapeHTML( $this->mRetypePass ); 00352 00353 $wgOut->addHTML( "<fieldset> 00354 <legend>$cp</legend> 00355 <div><label>$opw: <input type='password' name=\"wpOldpass\" value=\"{$this->mOldpass}\" size='20' /></label></div> 00356 <div><label>$npw: <input type='password' name=\"wpNewpass\" value=\"{$this->mNewpass}\" size='20' /></label></div> 00357 <div><label>$rpw: <input type='password' name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size='20' /></label></div> 00358 " . $this->getToggle( "rememberpassword" ) . " 00359 </fieldset> 00360 <div class='prefsectiontip'>".wfMsg('prefs-help-userdata')."</div>\n</fieldset>\n" ); 00361 00362 00363 # Quickbar setting 00364 # 00365 $wgOut->addHtml( "<fieldset>\n<legend>$qb</legend>\n" ); 00366 for ( $i = 0; $i < count( $qbs ); ++$i ) { 00367 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; } 00368 else { $checked = ""; } 00369 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpQuickbar\" 00370 value=\"$i\"$checked /> {$qbs[$i]}</label></div>\n" ); 00371 } 00372 $wgOut->addHtml('<div class="prefsectiontip">'.wfMsg('qbsettingsnote').'</div>'); 00373 $wgOut->addHtml( "</fieldset>\n\n" ); 00374 00375 # Skin setting 00376 # 00377 $wgOut->addHTML( "<fieldset>\n<legend>$sk</legend>\n" ); 00378 # Only show members of $wgValidSkinNames rather than 00379 # $skinNames (skins is all skin names from Language.php) 00380 foreach ($wgValidSkinNames as $skinkey => $skinname ) { 00381 if ( $skinkey == $this->mSkin ) { 00382 $checked = ' checked="checked"'; 00383 } else { 00384 $checked = ""; 00385 } 00386 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpSkin\" 00387 value=\"$skinkey\"$checked /> {$skinNames[$skinkey]}</label></div>\n" ); 00388 } 00389 $wgOut->addHTML( "</fieldset>\n\n" ); 00390 00391 # Math setting 00392 # 00393 $wgOut->addHTML( "<fieldset>\n<legend>$math</legend>\n" ); 00394 for ( $i = 0; $i < count( $mathopts ); ++$i ) { 00395 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; } 00396 else { $checked = ""; } 00397 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpMath\" 00398 value=\"$i\"$checked /> {$mathopts[$i]}</label></div>\n" ); 00399 } 00400 $wgOut->addHTML( "</fieldset>\n\n" ); 00401 00402 # Date format 00403 # 00404 if ( $wgUseDynamicDates ) { 00405 $wgOut->addHTML( "<fieldset>\n<legend>$dateFormat</legend>\n" ); 00406 for ( $i = 0; $i < count( $dateopts ); ++$i) { 00407 if ( $i == $this->mDate ) { 00408 $checked = ' checked="checked"'; 00409 } else { 00410 $checked = ""; 00411 } 00412 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpDate\" ". 00413 "value=\"$i\"$checked /> {$dateopts[$i]}</label></div>\n" ); 00414 } 00415 $wgOut->addHTML( "</fieldset>\n\n"); 00416 } 00417 00418 # Textbox rows, cols 00419 # 00420 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true ); 00421 $nowserver = $wgLang->time( $now, false ); 00422 $wgOut->addHTML( "<fieldset> 00423 <legend>$tbs</legend>\n 00424 <div> 00425 <label>$tbr: <input type='text' name=\"wpRows\" value=\"{$this->mRows}\" size='6' /></label> 00426 <label>$tbc: <input type='text' name=\"wpCols\" value=\"{$this->mCols}\" size='6' /></label> 00427 </div> " . 00428 $this->getToggle( "editwidth" ) . 00429 $this->getToggle( "showtoolbar" ) . 00430 $this->getToggle( "previewontop" ) . 00431 $this->getToggle( "watchdefault" ) . 00432 $this->getToggle( "minordefault" ) . " 00433 </fieldset> 00434 00435 <fieldset> 00436 <legend>$timezone</legend> 00437 <div><b>$tzServerTime:</b> $nowserver</div> 00438 <div><b>$ltz:</b> $nowlocal</div> 00439 <div><label>$tzo*: <input type='text' name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size='6' /></label></div> 00440 <div><input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" id=\"guesstimezonebutton\" style=\"display:none\" /></div> 00441 <div class='prefsectiontip'>* {$tzt}</div> 00442 </fieldset>\n\n" ); 00443 00444 $wgOut->addHTML( " 00445 <fieldset><legend>".wfMsg('prefs-rc')."</legend> 00446 <div><label>$rcc: <input type='text' name=\"wpRecent\" value=\"$this->mRecent\" size='6' /></label></div> 00447 " . $this->getToggle( "hideminor" ) . 00448 $this->getToggle( "usenewrc" ) . " 00449 <div><label>$stt: <input type='text' name=\"wpStubs\" value=\"$this->mStubs\" size='6' /></label></div> 00450 </fieldset> 00451 00452 <fieldset> 00453 <legend>$srh</legend> 00454 <div><label>$rpp: <input type='text' name=\"wpSearch\" value=\"$this->mSearch\" size='6' /></label></div> 00455 <div><label>$scl: <input type='text' name=\"wpSearchLines\" value=\"$this->mSearchLines\" size='6' /></label></div> 00456 <div><label>$scc: <input type='text' name=\"wpSearchChars\" value=\"$this->mSearchChars\" size='6' /></label></div> 00457 00458 <fieldset> 00459 <legend>$dsn</legend> 00460 $ps 00461 </fieldset> 00462 </fieldset> 00463 " ); 00464 00465 # Various checkbox options 00466 # 00467 $wgOut->addHTML("<fieldset><legend>".wfMsg('prefs-misc')."</legend>"); 00468 foreach ( $togs as $tname => $ttext ) { 00469 if( !array_key_exists( $tname, $this->mUsedToggles ) ) { 00470 $wgOut->addHTML( $this->getToggle( $tname ) ); 00471 } 00472 } 00473 $wgOut->addHTML( "</fieldset>\n\n" ); 00474 00475 $wgOut->addHTML( " 00476 <div id='prefsubmit'> 00477 <div> 00478 <input type='submit' name=\"wpSaveprefs\" value=\"$svp\" accesskey=\"". 00479 wfMsg('accesskey-save')."\" title=\"[alt-".wfMsg('accesskey-save')."]\" /> 00480 <input type='submit' name=\"wpReset\" value=\"$rsp\" /> 00481 </div> 00482 00483 </div> 00484 00485 </form>\n" ); 00486 } 00487 } 00488 ?>

Generated on Tue Jun 29 23:40:07 2004 for Mediawiki by doxygen 1.3.7