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