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