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