BUG#463 Strip first leading blank from preformatted text in output
[lhc/web/wiklou.git] / includes / User.php
1 <?php
2 /**
3 * See user.doc
4 *
5 * @package MediaWiki
6 */
7
8 /**
9 *
10 */
11 require_once( 'WatchedItem.php' );
12
13 /**
14 *
15 * @package MediaWiki
16 */
17 class User {
18 /**#@+
19 * @access private
20 */
21 var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
22 var $mRights, $mOptions;
23 var $mDataLoaded, $mNewpassword;
24 var $mSkin;
25 var $mBlockedby, $mBlockreason;
26 var $mTouched;
27 var $mCookiePassword;
28 var $mRealName;
29 var $mHash;
30 /**#@-*/
31
32 /** Construct using User:loadDefaults() */
33 function User() {
34 $this->loadDefaults();
35 }
36
37 /**
38 * Static factory method
39 * @static
40 * @param string $name Username, validated by Title:newFromText()
41 */
42 function newFromName( $name ) {
43 $u = new User();
44
45 # Clean up name according to title rules
46
47 $t = Title::newFromText( $name );
48 if( is_null( $t ) ) {
49 return NULL;
50 } else {
51 $u->setName( $t->getText() );
52 return $u;
53 }
54 }
55
56 /**
57 * Get username given an id.
58 * @param integer $id Database user id
59 * @return string Nickname of a user
60 * @static
61 */
62 function whoIs( $id ) {
63 $dbr =& wfGetDB( DB_SLAVE );
64 return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ) );
65 }
66
67 /**
68 * Get real username given an id.
69 * @param integer $id Database user id
70 * @return string Realname of a user
71 * @static
72 */
73 function whoIsReal( $id ) {
74 $dbr =& wfGetDB( DB_SLAVE );
75 return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ) );
76 }
77
78 /**
79 * Get database id given a user name
80 * @param string $name Nickname of a user
81 * @return integer|null Database user id (null: if non existent
82 * @static
83 */
84 function idFromName( $name ) {
85 $fname = "User::idFromName";
86
87 $nt = Title::newFromText( $name );
88 if( is_null( $nt ) ) {
89 # Illegal name
90 return null;
91 }
92 $dbr =& wfGetDB( DB_SLAVE );
93 $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), $fname );
94
95 if ( $s === false ) {
96 return 0;
97 } else {
98 return $s->user_id;
99 }
100 }
101
102 /**
103 * does the string match an anonymous user IP address?
104 * @param string $name Nickname of a user
105 * @static
106 */
107 function isIP( $name ) {
108 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
109 }
110
111 /**
112 * probably return a random password
113 * @return string probably a random password
114 * @static
115 * @todo Check what is doing really [AV]
116 */
117 function randomPassword() {
118 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
119 $l = strlen( $pwchars ) - 1;
120
121 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
122 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
123 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
124 $pwchars{mt_rand( 0, $l )};
125 return $np;
126 }
127
128 /**
129 * Set properties to default
130 * Used at construction. It will load per language default settings only
131 * if we have an available language object.
132 */
133 function loadDefaults() {
134 global $wgLang, $wgIP;
135 global $wgNamespacesToBeSearchedDefault;
136
137 $this->mId = $this->mNewtalk = 0;
138 $this->mName = $wgIP;
139 $this->mRealName = $this->mEmail = '';
140 $this->mPassword = $this->mNewpassword = '';
141 $this->mRights = array();
142 // Getting user defaults only if we have an available language
143 if(isset($wgLang)) { $this->loadDefaultFromLanguage(); }
144
145 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
146 $this->mOptions['searchNs'.$nsnum] = $val;
147 }
148 unset( $this->mSkin );
149 $this->mDataLoaded = false;
150 $this->mBlockedby = -1; # Unset
151 $this->mTouched = '0'; # Allow any pages to be cached
152 $this->cookiePassword = '';
153 $this->mHash = false;
154 }
155
156 /**
157 * Used to load user options from a language.
158 * This is not in loadDefault() cause we sometime create user before having
159 * a language object.
160 */
161 function loadDefaultFromLanguage(){
162 global $wgLang;
163 $defOpt = $wgLang->getDefaultUserOptions() ;
164 foreach ( $defOpt as $oname => $val ) {
165 $this->mOptions[$oname] = $val;
166 }
167 }
168
169 /**
170 * Get blocking information
171 * @access private
172 */
173 function getBlockedStatus() {
174 global $wgIP, $wgBlockCache, $wgProxyList;
175
176 if ( -1 != $this->mBlockedby ) { return; }
177
178 $this->mBlockedby = 0;
179
180 # User blocking
181 if ( $this->mId ) {
182 $block = new Block();
183 if ( $block->load( $wgIP , $this->mId ) ) {
184 $this->mBlockedby = $block->mBy;
185 $this->mBlockreason = $block->mReason;
186 }
187 }
188
189 # IP/range blocking
190 if ( !$this->mBlockedby ) {
191 $block = $wgBlockCache->get( $wgIP );
192 if ( $block !== false ) {
193 $this->mBlockedby = $block->mBy;
194 $this->mBlockreason = $block->mReason;
195 }
196 }
197
198 # Proxy blocking
199 if ( !$this->mBlockedby ) {
200 if ( array_key_exists( $wgIP, $wgProxyList ) ) {
201 $this->mBlockreason = wfMsg( 'proxyblockreason' );
202 $this->mBlockedby = "Proxy blocker";
203 }
204 }
205 }
206
207 /**
208 * Check if user is blocked
209 * @return bool True if blocked, false otherwise
210 */
211 function isBlocked() {
212 $this->getBlockedStatus();
213 if ( 0 === $this->mBlockedby ) { return false; }
214 return true;
215 }
216
217 /**
218 * Get name of blocker
219 * @return string name of blocker
220 */
221 function blockedBy() {
222 $this->getBlockedStatus();
223 return $this->mBlockedby;
224 }
225
226 /**
227 * Get blocking reason
228 * @return string Blocking reason
229 */
230 function blockedFor() {
231 $this->getBlockedStatus();
232 return $this->mBlockreason;
233 }
234
235 /**
236 * Initialise php session
237 */
238 function SetupSession() {
239 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
240 if( $wgSessionsInMemcached ) {
241 require_once( 'MemcachedSessions.php' );
242 } elseif( 'files' != ini_get( 'session.save_handler' ) ) {
243 # If it's left on 'user' or another setting from another
244 # application, it will end up failing. Try to recover.
245 ini_set ( 'session.save_handler', 'files' );
246 }
247 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
248 session_cache_limiter( 'private, must-revalidate' );
249 @session_start();
250 }
251
252 /**
253 * Read datas from session
254 * @static
255 */
256 function loadFromSession() {
257 global $wgMemc, $wgDBname;
258
259 if ( isset( $_SESSION['wsUserID'] ) ) {
260 if ( 0 != $_SESSION['wsUserID'] ) {
261 $sId = $_SESSION['wsUserID'];
262 } else {
263 return new User();
264 }
265 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
266 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
267 $_SESSION['wsUserID'] = $sId;
268 } else {
269 return new User();
270 }
271 if ( isset( $_SESSION['wsUserName'] ) ) {
272 $sName = $_SESSION['wsUserName'];
273 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
274 $sName = $_COOKIE["{$wgDBname}UserName"];
275 $_SESSION['wsUserName'] = $sName;
276 } else {
277 return new User();
278 }
279
280 $passwordCorrect = FALSE;
281 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
282 if($makenew = !$user) {
283 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
284 $user = new User();
285 $user->mId = $sId;
286 $user->loadFromDatabase();
287 } else {
288 wfDebug( "User::loadFromSession() got from cache!\n" );
289 }
290
291 if ( isset( $_SESSION['wsUserPassword'] ) ) {
292 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword;
293 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
294 $user->mCookiePassword = $_COOKIE["{$wgDBname}Password"];
295 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword );
296 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword;
297 } else {
298 return new User(); # Can't log in from session
299 }
300
301 if ( ( $sName == $user->mName ) && $passwordCorrect ) {
302 if($makenew) {
303 if($wgMemc->set( $key, $user ))
304 wfDebug( "User::loadFromSession() successfully saved user\n" );
305 else
306 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
307 }
308 $user->spreadBlock();
309 return $user;
310 }
311 return new User(); # Can't log in from session
312 }
313
314 /**
315 * Load a user from the database
316 */
317 function loadFromDatabase() {
318 global $wgCommandLineMode;
319 $fname = "User::loadFromDatabase";
320 if ( $this->mDataLoaded || $wgCommandLineMode ) {
321 return;
322 }
323
324 # Paranoia
325 $this->mId = IntVal( $this->mId );
326
327 # check in separate table if there are changes to the talk page
328 $this->mNewtalk=0; # reset talk page status
329 $dbr =& wfGetDB( DB_SLAVE );
330 if($this->mId) {
331 $res = $dbr->select( 'user_newtalk', 1, array( 'user_id' => $this->mId ), $fname );
332
333 if ( $dbr->numRows($res)>0 ) {
334 $this->mNewtalk= 1;
335 }
336 $dbr->freeResult( $res );
337 } else {
338 global $wgDBname, $wgMemc;
339 $key = "$wgDBname:newtalk:ip:{$this->mName}";
340 $newtalk = $wgMemc->get( $key );
341 if( ! is_integer( $newtalk ) ){
342 $res = $dbr->select( 'user_newtalk', 1, array( 'user_ip' => $this->mName ), $fname );
343
344 $this->mNewtalk = $dbr->numRows( $res ) > 0 ? 1 : 0;
345 $dbr->freeResult( $res );
346
347 $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
348 } else {
349 $this->mNewtalk = $newtalk ? 1 : 0;
350 }
351 }
352 if(!$this->mId) {
353 $this->mDataLoaded = true;
354 return;
355 } # the following stuff is for non-anonymous users only
356
357 $s = $dbr->selectRow( 'user', array( 'user_name','user_password','user_newpassword','user_email',
358 'user_real_name','user_options','user_touched' ),
359 array( 'user_id' => $this->mId ), $fname );
360
361 if ( $s !== false ) {
362 $this->mName = $s->user_name;
363 $this->mEmail = $s->user_email;
364 $this->mRealName = $s->user_real_name;
365 $this->mPassword = $s->user_password;
366 $this->mNewpassword = $s->user_newpassword;
367 $this->decodeOptions( $s->user_options );
368 $this->mTouched = wfTimestamp(TS_MW,$s->user_touched);
369 $this->mRights = explode( ",", strtolower(
370 $dbr->selectField( 'user_rights', 'user_rights', array( 'user_id' => $this->mId ) )
371 ) );
372 }
373
374 $this->mDataLoaded = true;
375 }
376
377 function getID() { return $this->mId; }
378 function setID( $v ) {
379 $this->mId = $v;
380 $this->mDataLoaded = false;
381 }
382
383 function getName() {
384 $this->loadFromDatabase();
385 return $this->mName;
386 }
387
388 function setName( $str ) {
389 $this->loadFromDatabase();
390 $this->mName = $str;
391 }
392
393 function getNewtalk() {
394 $this->loadFromDatabase();
395 return ( 0 != $this->mNewtalk );
396 }
397
398 function setNewtalk( $val ) {
399 $this->loadFromDatabase();
400 $this->mNewtalk = $val;
401 $this->invalidateCache();
402 }
403
404 function invalidateCache() {
405 $this->loadFromDatabase();
406 $this->mTouched = wfTimestampNow();
407 # Don't forget to save the options after this or
408 # it won't take effect!
409 }
410
411 function validateCache( $timestamp ) {
412 $this->loadFromDatabase();
413 return ($timestamp >= $this->mTouched);
414 }
415
416 /**
417 * Salt a password.
418 * Will only be salted if $wgPasswordSalt is true
419 * @param string Password.
420 * @return string Salted password or clear password.
421 */
422 function addSalt( $p ) {
423 global $wgPasswordSalt;
424 if($wgPasswordSalt)
425 return md5( "{$this->mId}-{$p}" );
426 else
427 return $p;
428 }
429
430 /**
431 * Encrypt a password.
432 * It can eventuall salt a password @see User::addSalt()
433 * @param string $p clear Password.
434 * @param string Encrypted password.
435 */
436 function encryptPassword( $p ) {
437 return $this->addSalt( md5( $p ) );
438 }
439
440 function setPassword( $str ) {
441 $this->loadFromDatabase();
442 $this->setCookiePassword( $str );
443 $this->mPassword = $this->encryptPassword( $str );
444 $this->mNewpassword = '';
445 }
446
447 function setCookiePassword( $str ) {
448 $this->loadFromDatabase();
449 $this->mCookiePassword = md5( $str );
450 }
451
452 function setNewpassword( $str ) {
453 $this->loadFromDatabase();
454 $this->mNewpassword = $this->encryptPassword( $str );
455 }
456
457 function getEmail() {
458 $this->loadFromDatabase();
459 return $this->mEmail;
460 }
461
462 function setEmail( $str ) {
463 $this->loadFromDatabase();
464 $this->mEmail = $str;
465 }
466
467 function getRealName() {
468 $this->loadFromDatabase();
469 return $this->mRealName;
470 }
471
472 function setRealName( $str ) {
473 $this->loadFromDatabase();
474 $this->mRealName = $str;
475 }
476
477 function getOption( $oname ) {
478 $this->loadFromDatabase();
479 if ( array_key_exists( $oname, $this->mOptions ) ) {
480 return $this->mOptions[$oname];
481 } else {
482 return '';
483 }
484 }
485
486 function setOption( $oname, $val ) {
487 $this->loadFromDatabase();
488 if ( $oname == 'skin' ) {
489 # Clear cached skin, so the new one displays immediately in Special:Preferences
490 unset( $this->mSkin );
491 }
492 $this->mOptions[$oname] = $val;
493 $this->invalidateCache();
494 }
495
496 function getRights() {
497 $this->loadFromDatabase();
498 return $this->mRights;
499 }
500
501 function addRight( $rname ) {
502 $this->loadFromDatabase();
503 array_push( $this->mRights, $rname );
504 $this->invalidateCache();
505 }
506
507 function isSysop() {
508 $this->loadFromDatabase();
509 if ( 0 == $this->mId ) { return false; }
510
511 return in_array( 'sysop', $this->mRights );
512 }
513
514 function isDeveloper() {
515 $this->loadFromDatabase();
516 if ( 0 == $this->mId ) { return false; }
517
518 return in_array( 'developer', $this->mRights );
519 }
520
521 function isBureaucrat() {
522 $this->loadFromDatabase();
523 if ( 0 == $this->mId ) { return false; }
524
525 return in_array( 'bureaucrat', $this->mRights );
526 }
527
528 /**
529 * Whether the user is a bot
530 */
531 function isBot() {
532 $this->loadFromDatabase();
533
534 # Why was this here? I need a UID=0 conversion script [TS]
535 # if ( 0 == $this->mId ) { return false; }
536
537 return in_array( 'bot', $this->mRights );
538 }
539
540 /**
541 * Load a skin if it doesn't exist or return it
542 * @todo FIXME : need to check the old failback system [AV]
543 */
544 function &getSkin() {
545 global $IP, $wgUsePHPTal;
546 if ( ! isset( $this->mSkin ) ) {
547 # get all skin names available
548 $skinNames = Skin::getSkinNames();
549 # get the user skin
550 $userSkin = $this->getOption( 'skin' );
551 if ( $userSkin == '' ) { $userSkin = 'standard'; }
552
553 if ( !isset( $skinNames[$userSkin] ) ) {
554 # in case the user skin could not be found find a replacement
555 $fallback = array(
556 0 => 'Standard',
557 1 => 'Nostalgia',
558 2 => 'CologneBlue');
559 # if phptal is enabled we should have monobook skin that
560 # superseed the good old SkinStandard.
561 if ( isset( $skinNames['monobook'] ) ) {
562 $fallback[0] = 'MonoBook';
563 }
564
565 if(is_numeric($userSkin) && isset( $fallback[$userSkin]) ){
566 $sn = $fallback[$userSkin];
567 } else {
568 $sn = 'Standard';
569 }
570 } else {
571 # The user skin is available
572 $sn = $skinNames[$userSkin];
573 }
574
575 # Grab the skin class and initialise it. Each skin checks for PHPTal
576 # and will not load if it's not enabled.
577 require_once( $IP.'/skins/'.$sn.'.php' );
578
579 # Check if we got if not failback to default skin
580 $sn = 'Skin'.$sn;
581 if(!class_exists($sn)) {
582 #FIXME : should we print an error message instead of loading
583 # standard skin ?
584 $sn = 'SkinStandard';
585 require_once( $IP.'/skins/Standard.php' );
586 }
587 $this->mSkin = new $sn;
588 }
589 return $this->mSkin;
590 }
591
592 /**#@+
593 * @param string $title Article title to look at
594 */
595
596 /**
597 * Check watched status of an article
598 * @return bool True if article is watched
599 */
600 function isWatched( $title ) {
601 $wl = WatchedItem::fromUserTitle( $this, $title );
602 return $wl->isWatched();
603 }
604
605 /**
606 * Watch an article
607 */
608 function addWatch( $title ) {
609 $wl = WatchedItem::fromUserTitle( $this, $title );
610 $wl->addWatch();
611 $this->invalidateCache();
612 }
613
614 /**
615 * Stop watching an article
616 */
617 function removeWatch( $title ) {
618 $wl = WatchedItem::fromUserTitle( $this, $title );
619 $wl->removeWatch();
620 $this->invalidateCache();
621 }
622 /**#@-*/
623
624 /**
625 * @access private
626 * @return string Encoding options
627 */
628 function encodeOptions() {
629 $a = array();
630 foreach ( $this->mOptions as $oname => $oval ) {
631 array_push( $a, $oname.'='.$oval );
632 }
633 $s = implode( "\n", $a );
634 return $s;
635 }
636
637 /**
638 * @access private
639 */
640 function decodeOptions( $str ) {
641 $a = explode( "\n", $str );
642 foreach ( $a as $s ) {
643 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
644 $this->mOptions[$m[1]] = $m[2];
645 }
646 }
647 }
648
649 function setCookies() {
650 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
651 if ( 0 == $this->mId ) return;
652 $this->loadFromDatabase();
653 $exp = time() + $wgCookieExpiration;
654
655 $_SESSION['wsUserID'] = $this->mId;
656 setcookie( $wgDBname.'UserID', $this->mId, $exp, $wgCookiePath, $wgCookieDomain );
657
658 $_SESSION['wsUserName'] = $this->mName;
659 setcookie( $wgDBname.'UserName', $this->mName, $exp, $wgCookiePath, $wgCookieDomain );
660
661 $_SESSION['wsUserPassword'] = $this->mPassword;
662 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
663 setcookie( $wgDBname.'Password', $this->mCookiePassword, $exp, $wgCookiePath, $wgCookieDomain );
664 } else {
665 setcookie( $wgDBname.'Password', '', time() - 3600 );
666 }
667 }
668
669 /**
670 * Logout user
671 * It will clean the session cookie
672 */
673 function logout() {
674 global $wgCookiePath, $wgCookieDomain, $wgDBname;
675 $this->mId = 0;
676
677 $_SESSION['wsUserID'] = 0;
678
679 setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
680 setcookie( $wgDBname.'Password', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
681 }
682
683 /**
684 * Save object settings into database
685 */
686 function saveSettings() {
687 global $wgMemc, $wgDBname;
688 $fname = 'User::saveSettings';
689
690 $dbw =& wfGetDB( DB_MASTER );
691 if ( ! $this->mNewtalk ) {
692 # Delete user_newtalk row
693 if( $this->mId ) {
694 $dbw->delete( 'user_newtalk', array( 'user_id' => $this->mId ), $fname );
695 } else {
696 $dbw->delete( 'user_newtalk', array( 'user_ip' => $this->mName ), $fname );
697 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
698 }
699 }
700 if ( 0 == $this->mId ) { return; }
701
702 $dbw->update( 'user',
703 array( /* SET */
704 'user_name' => $this->mName,
705 'user_password' => $this->mPassword,
706 'user_newpassword' => $this->mNewpassword,
707 'user_real_name' => $this->mRealName,
708 'user_email' => $this->mEmail,
709 'user_options' => $this->encodeOptions(),
710 'user_touched' => $dbw->timestamp($this->mTouched)
711 ), array( /* WHERE */
712 'user_id' => $this->mId
713 ), $fname
714 );
715 $dbw->set( 'user_rights', 'user_rights', implode( ",", $this->mRights ),
716 'user_id='. $this->mId, $fname );
717 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
718 }
719
720 /**
721 * Checks if a user with the given name exists, returns the ID
722 */
723 function idForName() {
724 $fname = 'User::idForName';
725
726 $gotid = 0;
727 $s = trim( $this->mName );
728 if ( 0 == strcmp( '', $s ) ) return 0;
729
730 $dbr =& wfGetDB( DB_SLAVE );
731 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), $fname );
732 if ( $id === false ) {
733 $id = 0;
734 }
735 return $id;
736 }
737
738 /**
739 * Add user object to the database
740 */
741 function addToDatabase() {
742 $fname = 'User::addToDatabase';
743 $dbw =& wfGetDB( DB_MASTER );
744 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
745 $dbw->insert( 'user',
746 array(
747 'user_id' => $seqVal,
748 'user_name' => $this->mName,
749 'user_password' => $this->mPassword,
750 'user_newpassword' => $this->mNewpassword,
751 'user_email' => $this->mEmail,
752 'user_real_name' => $this->mRealName,
753 'user_options' => $this->encodeOptions()
754 ), $fname
755 );
756 $this->mId = $dbw->insertId();
757 $dbw->insert( 'user_rights',
758 array(
759 'user_id' => $this->mId,
760 'user_rights' => implode( ',', $this->mRights )
761 ), $fname
762 );
763
764 }
765
766 function spreadBlock() {
767 global $wgIP;
768 # If the (non-anonymous) user is blocked, this function will block any IP address
769 # that they successfully log on from.
770 $fname = 'User::spreadBlock';
771
772 wfDebug( "User:spreadBlock()\n" );
773 if ( $this->mId == 0 ) {
774 return;
775 }
776
777 $userblock = Block::newFromDB( '', $this->mId );
778 if ( !$userblock->isValid() ) {
779 return;
780 }
781
782 # Check if this IP address is already blocked
783 $ipblock = Block::newFromDB( $wgIP );
784 if ( $ipblock->isValid() ) {
785 # Just update the timestamp
786 $ipblock->updateTimestamp();
787 return;
788 }
789
790 # Make a new block object with the desired properties
791 wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
792 $ipblock->mAddress = $wgIP;
793 $ipblock->mUser = 0;
794 $ipblock->mBy = $userblock->mBy;
795 $ipblock->mReason = wfMsg( 'autoblocker', $this->getName(), $userblock->mReason );
796 $ipblock->mTimestamp = wfTimestampNow();
797 $ipblock->mAuto = 1;
798 # If the user is already blocked with an expiry date, we don't
799 # want to pile on top of that!
800 if($userblock->mExpiry) {
801 $ipblock->mExpiry = min ( $userblock->mExpiry, Block::getAutoblockExpiry( $ipblock->mTimestamp ));
802 } else {
803 $ipblock->mExpiry = Block::getAutoblockExpiry( $ipblock->mTimestamp );
804 }
805
806 # Insert it
807 $ipblock->insert();
808
809 }
810
811 function getPageRenderingHash() {
812 if( $this->mHash ){
813 return $this->mHash;
814 }
815
816 // stubthreshold is only included below for completeness,
817 // it will always be 0 when this function is called by parsercache.
818
819 $confstr = $this->getOption( 'math' );
820 $confstr .= '!' . $this->getOption( 'highlightbroken' );
821 $confstr .= '!' . $this->getOption( 'stubthreshold' );
822 $confstr .= '!' . $this->getOption( 'editsection' );
823 $confstr .= '!' . $this->getOption( 'editsectiononrightclick' );
824 $confstr .= '!' . $this->getOption( 'showtoc' );
825 $confstr .= '!' . $this->getOption( 'date' );
826 $confstr .= '!' . $this->getOption( 'numberheadings' );
827
828 $this->mHash = $confstr;
829 return $confstr ;
830 }
831
832 function isAllowedToCreateAccount() {
833 global $wgWhitelistAccount;
834 $allowed = false;
835
836 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
837 foreach ($wgWhitelistAccount as $right => $ok) {
838 $userHasRight = (!strcmp($right, 'user') || in_array($right, $this->getRights()));
839 $allowed |= ($ok && $userHasRight);
840 }
841 return $allowed;
842 }
843
844 /**
845 * Set mDataLoaded, return previous value
846 * Use this to prevent DB access in command-line scripts or similar situations
847 */
848 function setLoaded( $loaded ) {
849 return wfSetVar( $this->mDataLoaded, $loaded );
850 }
851
852 function getUserPage() {
853 return Title::makeTitle( NS_USER, $this->mName );
854 }
855
856 /**
857 * @static
858 */
859 function getMaxID() {
860 $dbr =& wfGetDB( DB_SLAVE );
861 return $dbr->selectField( 'user', 'max(user_id)', false );
862 }
863
864 /**
865 * Determine whether the user is a newbie. Newbies are either
866 * anonymous IPs, or the 1% most recently created accounts.
867 * Bots and sysops are excluded.
868 * @return bool True if it is a newbie.
869 */
870 function isNewbie() {
871 return $this->mId > User::getMaxID() * 0.99 && !$this->isSysop() && !$this->isBot() || $this->getID() == 0;
872 }
873
874 /**
875 * Check to see if the given clear-text password is one of the accepted passwords
876 * @param string $password User password.
877 * @return bool True if the given password is correct otherwise False.
878 */
879 function checkPassword( $password ) {
880 $this->loadFromDatabase();
881 $ep = $this->encryptPassword( $password );
882 if ( 0 == strcmp( $ep, $this->mPassword ) ) {
883 return true;
884 } elseif ( 0 == strcmp( $ep, $this->mNewpassword ) ) {
885 return true;
886 } elseif ( function_exists( 'iconv' ) ) {
887 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
888 # Check for this with iconv
889 /* $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252', $password ) );
890 if ( 0 == strcmp( $cp1252hash, $this->mPassword ) ) {
891 return true;
892 }*/
893 }
894 return false;
895 }
896 }
897
898 ?>