typo
[lhc/web/wiklou.git] / includes / User.php
1 <?php
2 /**
3 * See user.txt
4 *
5 * @package MediaWiki
6 */
7
8 /**
9 *
10 */
11 require_once( 'WatchedItem.php' );
12
13 # Number of characters in user_token field
14 define( 'USER_TOKEN_LENGTH', 32 );
15
16 # Serialized record version
17 define( 'MW_USER_VERSION', 2 );
18
19 /**
20 *
21 * @package MediaWiki
22 */
23 class User {
24 /**#@+
25 * @access private
26 */
27 var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
28 var $mEmailAuthenticated;
29 var $mRights, $mOptions;
30 var $mDataLoaded, $mNewpassword;
31 var $mSkin;
32 var $mBlockedby, $mBlockreason;
33 var $mTouched;
34 var $mToken;
35 var $mRealName;
36 var $mHash;
37 var $mGroups;
38 var $mVersion; // serialized version
39
40 /** Construct using User:loadDefaults() */
41 function User() {
42 $this->loadDefaults();
43 $this->mVersion = MW_USER_VERSION;
44 }
45
46 /**
47 * Static factory method
48 * @param string $name Username, validated by Title:newFromText()
49 * @return User
50 * @static
51 */
52 function newFromName( $name ) {
53 $u = new User();
54
55 # Force usernames to capital
56 global $wgContLang;
57 $name = $wgContLang->ucfirst( $name );
58
59 # Clean up name according to title rules
60 $t = Title::newFromText( $name );
61 if( is_null( $t ) ) {
62 return null;
63 }
64
65 # Reject various classes of invalid names
66 $canonicalName = $t->getText();
67 global $wgAuth;
68 $canonicalName = $wgAuth->getCanonicalName( $t->getText() );
69
70 if( !User::isValidUserName( $canonicalName ) ) {
71 return null;
72 }
73
74 $u->setName( $canonicalName );
75 $u->setId( $u->idFromName( $canonicalName ) );
76 return $u;
77 }
78
79 /**
80 * Factory method to fetch whichever use has a given email confirmation code.
81 * This code is generated when an account is created or its e-mail address
82 * has changed.
83 *
84 * If the code is invalid or has expired, returns NULL.
85 *
86 * @param string $code
87 * @return User
88 * @static
89 */
90 function newFromConfirmationCode( $code ) {
91 $dbr =& wfGetDB( DB_SLAVE );
92 $name = $dbr->selectField( 'user', 'user_name', array(
93 'user_email_token' => md5( $code ),
94 'user_email_token_expires > ' . $dbr->addQuotes( $dbr->timestamp() ),
95 ) );
96 if( is_string( $name ) ) {
97 return User::newFromName( $name );
98 } else {
99 return null;
100 }
101 }
102
103 /**
104 * Serialze sleep function, for better cache efficiency and avoidance of
105 * silly "incomplete type" errors when skins are cached
106 */
107 function __sleep() {
108 return array( 'mId', 'mName', 'mPassword', 'mEmail', 'mNewtalk',
109 'mEmailAuthenticated', 'mRights', 'mOptions', 'mDataLoaded',
110 'mNewpassword', 'mBlockedby', 'mBlockreason', 'mTouched',
111 'mToken', 'mRealName', 'mHash', 'mGroups' );
112 }
113
114 /**
115 * Get username given an id.
116 * @param integer $id Database user id
117 * @return string Nickname of a user
118 * @static
119 */
120 function whoIs( $id ) {
121 $dbr =& wfGetDB( DB_SLAVE );
122 return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), 'User::whoIs' );
123 }
124
125 /**
126 * Get real username given an id.
127 * @param integer $id Database user id
128 * @return string Realname of a user
129 * @static
130 */
131 function whoIsReal( $id ) {
132 $dbr =& wfGetDB( DB_SLAVE );
133 return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ), 'User::whoIsReal' );
134 }
135
136 /**
137 * Get database id given a user name
138 * @param string $name Nickname of a user
139 * @return integer|null Database user id (null: if non existent
140 * @static
141 */
142 function idFromName( $name ) {
143 $fname = "User::idFromName";
144
145 $nt = Title::newFromText( $name );
146 if( is_null( $nt ) ) {
147 # Illegal name
148 return null;
149 }
150 $dbr =& wfGetDB( DB_SLAVE );
151 $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), $fname );
152
153 if ( $s === false ) {
154 return 0;
155 } else {
156 return $s->user_id;
157 }
158 }
159
160 /**
161 * does the string match an anonymous IPv4 address?
162 *
163 * @static
164 * @param string $name Nickname of a user
165 * @return bool
166 */
167 function isIP( $name ) {
168 return preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/",$name);
169 /*return preg_match("/^
170 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
171 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
172 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
173 (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))
174 $/x", $name);*/
175 }
176
177 /**
178 * Is the input a valid username?
179 *
180 * Checks if the input is a valid username, we don't want an empty string,
181 * an IP address, anything that containins slashes (would mess up subpages),
182 * is longer than the maximum allowed username size or doesn't begin with
183 * a capital letter.
184 *
185 * @param string $name
186 * @return bool
187 * @static
188 */
189 function isValidUserName( $name ) {
190 global $wgContLang, $wgMaxNameChars;
191
192 if ( $name == ''
193 || User::isIP( $name )
194 || strpos( $name, '/' ) !== false
195 || strlen( $name ) > $wgMaxNameChars
196 || $name != $wgContLang->ucfirst( $name ) )
197 return false;
198 else
199 return true;
200 }
201
202 /**
203 * Is the input a valid password?
204 *
205 * @param string $password
206 * @return bool
207 * @static
208 */
209 function isValidPassword( $password ) {
210 global $wgMinimalPasswordLength;
211 return strlen( $password ) >= $wgMinimalPasswordLength;
212 }
213
214 /**
215 * does the string match roughly an email address ?
216 *
217 * @todo Check for RFC 2822 compilance
218 * @bug 959
219 *
220 * @param string $addr email address
221 * @static
222 * @return bool
223 */
224 function isValidEmailAddr ( $addr ) {
225 # There used to be a regular expression here, it got removed because it
226 # rejected valid addresses.
227 return ( trim( $addr ) != '' ) &&
228 (false !== strpos( $addr, '@' ) );
229 }
230
231 /**
232 * Count the number of edits of a user
233 *
234 * @param int $uid The user ID to check
235 * @return int
236 */
237 function edits( $uid ) {
238 $fname = 'User::edits';
239
240 $dbr =& wfGetDB( DB_SLAVE );
241 return $dbr->selectField(
242 'revision', 'count(*)',
243 array( 'rev_user' => $uid ),
244 $fname
245 );
246 }
247
248 /**
249 * probably return a random password
250 * @return string probably a random password
251 * @static
252 * @todo Check what is doing really [AV]
253 */
254 function randomPassword() {
255 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
256 $l = strlen( $pwchars ) - 1;
257
258 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
259 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
260 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
261 $pwchars{mt_rand( 0, $l )};
262 return $np;
263 }
264
265 /**
266 * Set properties to default
267 * Used at construction. It will load per language default settings only
268 * if we have an available language object.
269 */
270 function loadDefaults() {
271 static $n=0;
272 $n++;
273 $fname = 'User::loadDefaults' . $n;
274 wfProfileIn( $fname );
275
276 global $wgContLang, $wgIP, $wgDBname;
277 global $wgNamespacesToBeSearchedDefault;
278
279 $this->mId = 0;
280 $this->mNewtalk = -1;
281 $this->mName = $wgIP;
282 $this->mRealName = $this->mEmail = '';
283 $this->mEmailAuthenticated = null;
284 $this->mPassword = $this->mNewpassword = '';
285 $this->mRights = array();
286 $this->mGroups = array();
287 $this->mOptions = User::getDefaultOptions();
288
289 foreach( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
290 $this->mOptions['searchNs'.$nsnum] = $val;
291 }
292 unset( $this->mSkin );
293 $this->mDataLoaded = false;
294 $this->mBlockedby = -1; # Unset
295 $this->setToken(); # Random
296 $this->mHash = false;
297
298 if ( isset( $_COOKIE[$wgDBname.'LoggedOut'] ) ) {
299 $this->mTouched = wfTimestamp( TS_MW, $_COOKIE[$wgDBname.'LoggedOut'] );
300 }
301 else {
302 $this->mTouched = '0'; # Allow any pages to be cached
303 }
304
305 wfProfileOut( $fname );
306 }
307
308 /**
309 * Combine the language default options with any site-specific options
310 * and add the default language variants.
311 *
312 * @return array
313 * @static
314 * @access private
315 */
316 function getDefaultOptions() {
317 /**
318 * Site defaults will override the global/language defaults
319 */
320 global $wgContLang, $wgDefaultUserOptions;
321 $defOpt = $wgDefaultUserOptions + $wgContLang->getDefaultUserOptions();
322
323 /**
324 * default language setting
325 */
326 $variant = $wgContLang->getPreferredVariant();
327 $defOpt['variant'] = $variant;
328 $defOpt['language'] = $variant;
329
330 return $defOpt;
331 }
332
333 /**
334 * Get a given default option value.
335 *
336 * @param string $opt
337 * @return string
338 * @static
339 * @access public
340 */
341 function getDefaultOption( $opt ) {
342 $defOpts = User::getDefaultOptions();
343 if( isset( $defOpts[$opt] ) ) {
344 return $defOpts[$opt];
345 } else {
346 return '';
347 }
348 }
349
350 /**
351 * Get blocking information
352 * @access private
353 * @param bool $bFromSlave Specify whether to check slave or master. To improve performance,
354 * non-critical checks are done against slaves. Check when actually saving should be done against
355 * master.
356 *
357 * Note that even if $bFromSlave is false, the check is done first against slave, then master.
358 * The logic is that if blocked on slave, we'll assume it's either blocked on master or
359 * just slightly outta sync and soon corrected - safer to block slightly more that less.
360 * And it's cheaper to check slave first, then master if needed, than master always.
361 */
362 function getBlockedStatus( $bFromSlave = true ) {
363 global $wgIP, $wgBlockCache, $wgProxyList, $wgEnableSorbs, $wgProxyWhitelist;
364
365 if ( -1 != $this->mBlockedby ) {
366 wfDebug( "User::getBlockedStatus: already loaded.\n" );
367 return;
368 }
369
370 $fname = 'User::getBlockedStatus';
371 wfProfileIn( $fname );
372 wfDebug( "$fname: checking...\n" );
373
374 $this->mBlockedby = 0;
375
376 # User/IP blocking
377 $block = new Block();
378 $block->forUpdate( $bFromSlave );
379 if ( $block->load( $wgIP , $this->mId ) ) {
380 wfDebug( "$fname: Found block.\n" );
381 $this->mBlockedby = $block->mBy;
382 $this->mBlockreason = $block->mReason;
383 if ( $this->isLoggedIn() ) {
384 $this->spreadBlock();
385 }
386 } else {
387 wfDebug( "$fname: No block.\n" );
388 }
389
390 # Range blocking
391 if ( !$this->mBlockedby ) {
392 # Check first against slave, and optionally from master.
393 wfDebug( "$fname: Checking range blocks\n" );
394 $block = $wgBlockCache->get( $wgIP, true );
395 if ( !$block && !$bFromSlave )
396 {
397 # Not blocked: check against master, to make sure.
398 $wgBlockCache->clearLocal( );
399 $block = $wgBlockCache->get( $wgIP, false );
400 }
401 if ( $block !== false ) {
402 $this->mBlockedby = $block->mBy;
403 $this->mBlockreason = $block->mReason;
404 }
405 }
406
407 # Proxy blocking
408 if ( !$this->isSysop() && !in_array( $wgIP, $wgProxyWhitelist ) ) {
409
410 # Local list
411 if ( array_key_exists( $wgIP, $wgProxyList ) ) {
412 $this->mBlockedby = wfMsg( 'proxyblocker' );
413 $this->mBlockreason = wfMsg( 'proxyblockreason' );
414 }
415
416 # DNSBL
417 if ( !$this->mBlockedby && $wgEnableSorbs && !$this->getID() ) {
418 if ( $this->inSorbsBlacklist( $wgIP ) ) {
419 $this->mBlockedby = wfMsg( 'sorbs' );
420 $this->mBlockreason = wfMsg( 'sorbsreason' );
421 }
422 }
423 }
424 wfProfileOut( $fname );
425 }
426
427 function inSorbsBlacklist( $ip ) {
428 global $wgEnableSorbs;
429 return $wgEnableSorbs &&
430 $this->inDnsBlacklist( $ip, 'http.dnsbl.sorbs.net.' );
431 }
432
433 function inOpmBlacklist( $ip ) {
434 global $wgEnableOpm;
435 return $wgEnableOpm &&
436 $this->inDnsBlacklist( $ip, 'opm.blitzed.org.' );
437 }
438
439 function inDnsBlacklist( $ip, $base ) {
440 $fname = 'User::inDnsBlacklist';
441 wfProfileIn( $fname );
442
443 $found = false;
444 $host = '';
445
446 if ( preg_match( '/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $m ) ) {
447 # Make hostname
448 for ( $i=4; $i>=1; $i-- ) {
449 $host .= $m[$i] . '.';
450 }
451 $host .= $base;
452
453 # Send query
454 $ipList = gethostbynamel( $host );
455
456 if ( $ipList ) {
457 wfDebug( "Hostname $host is {$ipList[0]}, it's a proxy says $base!\n" );
458 $found = true;
459 } else {
460 wfDebug( "Requested $host, not found in $base.\n" );
461 }
462 }
463
464 wfProfileOut( $fname );
465 return $found;
466 }
467
468 /**
469 * Primitive rate limits: enforce maximum actions per time period
470 * to put a brake on flooding.
471 *
472 * Note: when using a shared cache like memcached, IP-address
473 * last-hit counters will be shared across wikis.
474 *
475 * @return bool true if a rate limiter was tripped
476 * @access public
477 */
478 function pingLimiter( $action='edit' ) {
479 global $wgRateLimits;
480 if( !isset( $wgRateLimits[$action] ) ) {
481 return false;
482 }
483 if( $this->isAllowed( 'delete' ) ) {
484 // goddam cabal
485 return false;
486 }
487
488 global $wgMemc, $wgIP, $wgDBname, $wgRateLimitLog;
489 $fname = 'User::pingLimiter';
490 wfProfileIn( $fname );
491
492 $limits = $wgRateLimits[$action];
493 $keys = array();
494 $id = $this->getId();
495
496 if( isset( $limits['anon'] ) && $id == 0 ) {
497 $keys["$wgDBname:limiter:$action:anon"] = $limits['anon'];
498 }
499
500 if( isset( $limits['user'] ) && $id != 0 ) {
501 $keys["$wgDBname:limiter:$action:user:$id"] = $limits['user'];
502 }
503 if( $this->isNewbie() ) {
504 if( isset( $limits['newbie'] ) && $id != 0 ) {
505 $keys["$wgDBname:limiter:$action:user:$id"] = $limits['newbie'];
506 }
507 if( isset( $limits['ip'] ) ) {
508 $keys["mediawiki:limiter:$action:ip:$wgIP"] = $limits['ip'];
509 }
510 if( isset( $limits['subnet'] ) && preg_match( '/^(\d+\.\d+\.\d+)\.\d+$/', $wgIP, $matches ) ) {
511 $subnet = $matches[1];
512 $keys["mediawiki:limiter:$action:subnet:$subnet"] = $limits['subnet'];
513 }
514 }
515
516 $triggered = false;
517 foreach( $keys as $key => $limit ) {
518 list( $max, $period ) = $limit;
519 $summary = "(limit $max in {$period}s)";
520 $count = $wgMemc->get( $key );
521 if( $count ) {
522 if( $count > $max ) {
523 wfDebug( "$fname: tripped! $key at $count $summary\n" );
524 if( $wgRateLimitLog ) {
525 @error_log( wfTimestamp( TS_MW ) . ' ' . $wgDBname . ': ' . $this->getName() . " tripped $key at $count $summary\n", 3, $wgRateLimitLog );
526 }
527 $triggered = true;
528 } else {
529 wfDebug( "$fname: ok. $key at $count $summary\n" );
530 }
531 } else {
532 wfDebug( "$fname: adding record for $key $summary\n" );
533 $wgMemc->add( $key, 1, intval( $period ) );
534 }
535 $wgMemc->incr( $key );
536 }
537
538 wfProfileOut( $fname );
539 return $triggered;
540 }
541
542 /**
543 * Check if user is blocked
544 * @return bool True if blocked, false otherwise
545 */
546 function isBlocked( $bFromSlave = true ) { // hacked from false due to horrible probs on site
547 wfDebug( "User::isBlocked: enter\n" );
548 $this->getBlockedStatus( $bFromSlave );
549 return $this->mBlockedby !== 0;
550 }
551
552 /**
553 * Check if user is blocked from editing a particular article
554 */
555 function isBlockedFrom( $title, $bFromSlave = false ) {
556 global $wgBlockAllowsUTEdit;
557 $fname = 'User::isBlockedFrom';
558 wfProfileIn( $fname );
559 wfDebug( "$fname: enter\n" );
560
561 if ( $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
562 $title->getNamespace() == NS_USER_TALK )
563 {
564 $blocked = false;
565 wfDebug( "$fname: self-talk page, ignoring any blocks\n" );
566 } else {
567 wfDebug( "$fname: asking isBlocked()\n" );
568 $blocked = $this->isBlocked( $bFromSlave );
569 }
570 wfProfileOut( $fname );
571 return $blocked;
572 }
573
574 /**
575 * Get name of blocker
576 * @return string name of blocker
577 */
578 function blockedBy() {
579 $this->getBlockedStatus();
580 return $this->mBlockedby;
581 }
582
583 /**
584 * Get blocking reason
585 * @return string Blocking reason
586 */
587 function blockedFor() {
588 $this->getBlockedStatus();
589 return $this->mBlockreason;
590 }
591
592 /**
593 * Initialise php session
594 */
595 function SetupSession() {
596 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
597 if( $wgSessionsInMemcached ) {
598 require_once( 'MemcachedSessions.php' );
599 } elseif( 'files' != ini_get( 'session.save_handler' ) ) {
600 # If it's left on 'user' or another setting from another
601 # application, it will end up failing. Try to recover.
602 ini_set ( 'session.save_handler', 'files' );
603 }
604 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
605 session_cache_limiter( 'private, must-revalidate' );
606 @session_start();
607 }
608
609 /**
610 * Read datas from session
611 * @static
612 */
613 function loadFromSession() {
614 global $wgMemc, $wgDBname;
615
616 if ( isset( $_SESSION['wsUserID'] ) ) {
617 if ( 0 != $_SESSION['wsUserID'] ) {
618 $sId = $_SESSION['wsUserID'];
619 } else {
620 return new User();
621 }
622 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
623 $sId = intval( $_COOKIE["{$wgDBname}UserID"] );
624 $_SESSION['wsUserID'] = $sId;
625 } else {
626 return new User();
627 }
628 if ( isset( $_SESSION['wsUserName'] ) ) {
629 $sName = $_SESSION['wsUserName'];
630 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
631 $sName = $_COOKIE["{$wgDBname}UserName"];
632 $_SESSION['wsUserName'] = $sName;
633 } else {
634 return new User();
635 }
636
637 $passwordCorrect = FALSE;
638 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
639 if( !is_object( $user ) || $user->mVersion < MW_USER_VERSION ) {
640 # Expire old serialized objects; they may be corrupt.
641 $user = false;
642 }
643 if($makenew = !$user) {
644 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
645 $user = new User();
646 $user->mId = $sId;
647 $user->loadFromDatabase();
648 } else {
649 wfDebug( "User::loadFromSession() got from cache!\n" );
650 }
651
652 if ( isset( $_SESSION['wsToken'] ) ) {
653 $passwordCorrect = $_SESSION['wsToken'] == $user->mToken;
654 } else if ( isset( $_COOKIE["{$wgDBname}Token"] ) ) {
655 $passwordCorrect = $user->mToken == $_COOKIE["{$wgDBname}Token"];
656 } else {
657 return new User(); # Can't log in from session
658 }
659
660 if ( ( $sName == $user->mName ) && $passwordCorrect ) {
661 if($makenew) {
662 if($wgMemc->set( $key, $user ))
663 wfDebug( "User::loadFromSession() successfully saved user\n" );
664 else
665 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
666 }
667 return $user;
668 }
669 return new User(); # Can't log in from session
670 }
671
672 /**
673 * Load a user from the database
674 */
675 function loadFromDatabase() {
676 global $wgCommandLineMode;
677 $fname = "User::loadFromDatabase";
678
679 # Counter-intuitive, breaks various things, use User::setLoaded() if you want to suppress
680 # loading in a command line script, don't assume all command line scripts need it like this
681 #if ( $this->mDataLoaded || $wgCommandLineMode ) {
682 if ( $this->mDataLoaded ) {
683 return;
684 }
685
686 # Paranoia
687 $this->mId = intval( $this->mId );
688
689 /** Anonymous user */
690 if( !$this->mId ) {
691 /** Get rights */
692 $this->mRights = $this->getGroupPermissions( array( '*' ) );
693 $this->mDataLoaded = true;
694 return;
695 } # the following stuff is for non-anonymous users only
696
697 $dbr =& wfGetDB( DB_SLAVE );
698 $s = $dbr->selectRow( 'user', array( 'user_name','user_password','user_newpassword','user_email',
699 'user_email_authenticated',
700 'user_real_name','user_options','user_touched', 'user_token' ),
701 array( 'user_id' => $this->mId ), $fname );
702
703 if ( $s !== false ) {
704 $this->mName = $s->user_name;
705 $this->mEmail = $s->user_email;
706 $this->mEmailAuthenticated = wfTimestampOrNull( TS_MW, $s->user_email_authenticated );
707 $this->mRealName = $s->user_real_name;
708 $this->mPassword = $s->user_password;
709 $this->mNewpassword = $s->user_newpassword;
710 $this->decodeOptions( $s->user_options );
711 $this->mTouched = wfTimestamp(TS_MW,$s->user_touched);
712 $this->mToken = $s->user_token;
713
714 $res = $dbr->select( 'user_groups',
715 array( 'ug_group' ),
716 array( 'ug_user' => $this->mId ),
717 $fname );
718 $this->mGroups = array();
719 while( $row = $dbr->fetchObject( $res ) ) {
720 $this->mGroups[] = $row->ug_group;
721 }
722 $effectiveGroups = array_merge( array( '*', 'user' ), $this->mGroups );
723 $this->mRights = $this->getGroupPermissions( $effectiveGroups );
724 }
725
726 $this->mDataLoaded = true;
727 }
728
729 function getID() { return $this->mId; }
730 function setID( $v ) {
731 $this->mId = $v;
732 $this->mDataLoaded = false;
733 }
734
735 function getName() {
736 $this->loadFromDatabase();
737 return $this->mName;
738 }
739
740 function setName( $str ) {
741 $this->loadFromDatabase();
742 $this->mName = $str;
743 }
744
745
746 /**
747 * Return the title dbkey form of the name, for eg user pages.
748 * @return string
749 * @access public
750 */
751 function getTitleKey() {
752 return str_replace( ' ', '_', $this->getName() );
753 }
754
755 function getNewtalk() {
756 global $wgUseEnotif;
757 $fname = 'User::getNewtalk';
758 $this->loadFromDatabase();
759
760 # Load the newtalk status if it is unloaded (mNewtalk=-1)
761 if( $this->mNewtalk == -1 ) {
762 $this->mNewtalk = 0; # reset talk page status
763
764 # Check memcached separately for anons, who have no
765 # entire User object stored in there.
766 if( !$this->mId ) {
767 global $wgDBname, $wgMemc;
768 $key = "$wgDBname:newtalk:ip:{$this->mName}";
769 $newtalk = $wgMemc->get( $key );
770 if( is_integer( $newtalk ) ) {
771 $this->mNewtalk = $newtalk ? 1 : 0;
772 return (bool)$this->mNewtalk;
773 }
774 }
775
776 $dbr =& wfGetDB( DB_SLAVE );
777 if ( $wgUseEnotif ) {
778 $res = $dbr->select( 'watchlist',
779 array( 'wl_user' ),
780 array( 'wl_title' => $this->getTitleKey(),
781 'wl_namespace' => NS_USER_TALK,
782 'wl_user' => $this->mId,
783 'wl_notificationtimestamp ' . $dbr->notNullTimestamp() ),
784 'User::getNewtalk' );
785 if( $dbr->numRows($res) > 0 ) {
786 $this->mNewtalk = 1;
787 }
788 $dbr->freeResult( $res );
789 } elseif ( $this->mId ) {
790 $res = $dbr->select( 'user_newtalk', 1, array( 'user_id' => $this->mId ), $fname );
791
792 if ( $dbr->numRows($res)>0 ) {
793 $this->mNewtalk= 1;
794 }
795 $dbr->freeResult( $res );
796 } else {
797 $res = $dbr->select( 'user_newtalk', 1, array( 'user_ip' => $this->mName ), $fname );
798 $this->mNewtalk = $dbr->numRows( $res ) > 0 ? 1 : 0;
799 $dbr->freeResult( $res );
800 }
801
802 if( !$this->mId ) {
803 $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
804 }
805 }
806
807 return ( 0 != $this->mNewtalk );
808 }
809
810 function setNewtalk( $val ) {
811 $this->loadFromDatabase();
812 $this->mNewtalk = $val;
813 $this->invalidateCache();
814 }
815
816 function invalidateCache() {
817 global $wgClockSkewFudge;
818 $this->loadFromDatabase();
819 $this->mTouched = wfTimestamp(TS_MW, time() + $wgClockSkewFudge );
820 # Don't forget to save the options after this or
821 # it won't take effect!
822 }
823
824 function validateCache( $timestamp ) {
825 $this->loadFromDatabase();
826 return ($timestamp >= $this->mTouched);
827 }
828
829 /**
830 * Encrypt a password.
831 * It can eventuall salt a password @see User::addSalt()
832 * @param string $p clear Password.
833 * @return string Encrypted password.
834 */
835 function encryptPassword( $p ) {
836 return wfEncryptPassword( $this->mId, $p );
837 }
838
839 # Set the password and reset the random token
840 function setPassword( $str ) {
841 $this->loadFromDatabase();
842 $this->setToken();
843 $this->mPassword = $this->encryptPassword( $str );
844 $this->mNewpassword = '';
845 }
846
847 # Set the random token (used for persistent authentication)
848 function setToken( $token = false ) {
849 global $wgSecretKey, $wgProxyKey, $wgDBname;
850 if ( !$token ) {
851 if ( $wgSecretKey ) {
852 $key = $wgSecretKey;
853 } elseif ( $wgProxyKey ) {
854 $key = $wgProxyKey;
855 } else {
856 $key = microtime();
857 }
858 $this->mToken = md5( $key . mt_rand( 0, 0x7fffffff ) . $wgDBname . $this->mId );
859 } else {
860 $this->mToken = $token;
861 }
862 }
863
864
865 function setCookiePassword( $str ) {
866 $this->loadFromDatabase();
867 $this->mCookiePassword = md5( $str );
868 }
869
870 function setNewpassword( $str ) {
871 $this->loadFromDatabase();
872 $this->mNewpassword = $this->encryptPassword( $str );
873 }
874
875 function getEmail() {
876 $this->loadFromDatabase();
877 return $this->mEmail;
878 }
879
880 function getEmailAuthenticationTimestamp() {
881 $this->loadFromDatabase();
882 return $this->mEmailAuthenticated;
883 }
884
885 function setEmail( $str ) {
886 $this->loadFromDatabase();
887 $this->mEmail = $str;
888 }
889
890 function getRealName() {
891 $this->loadFromDatabase();
892 return $this->mRealName;
893 }
894
895 function setRealName( $str ) {
896 $this->loadFromDatabase();
897 $this->mRealName = $str;
898 }
899
900 function getOption( $oname ) {
901 $this->loadFromDatabase();
902 if ( array_key_exists( $oname, $this->mOptions ) ) {
903 return trim( $this->mOptions[$oname] );
904 } else {
905 return '';
906 }
907 }
908
909 function setOption( $oname, $val ) {
910 $this->loadFromDatabase();
911 if ( $oname == 'skin' ) {
912 # Clear cached skin, so the new one displays immediately in Special:Preferences
913 unset( $this->mSkin );
914 }
915 $this->mOptions[$oname] = $val;
916 $this->invalidateCache();
917 }
918
919 function getRights() {
920 $this->loadFromDatabase();
921 return $this->mRights;
922 }
923
924 /**
925 * Get the list of explicit group memberships this user has.
926 * The implicit * and user groups are not included.
927 * @return array of strings
928 */
929 function getGroups() {
930 $this->loadFromDatabase();
931 return $this->mGroups;
932 }
933
934 /**
935 * Get the list of implicit group memberships this user has.
936 * This includes all explicit groups, plus 'user' if logged in
937 * and '*' for all accounts.
938 * @return array of strings
939 */
940 function getEffectiveGroups() {
941 $base = array( '*' );
942 if( $this->isLoggedIn() ) {
943 $base[] = 'user';
944 }
945 return array_merge( $base, $this->getGroups() );
946 }
947
948 /**
949 * Remove the user from the given group.
950 * This takes immediate effect.
951 * @string $group
952 */
953 function addGroup( $group ) {
954 $dbw =& wfGetDB( DB_MASTER );
955 $dbw->insert( 'user_groups',
956 array(
957 'ug_user' => $this->getID(),
958 'ug_group' => $group,
959 ),
960 'User::addGroup',
961 array( 'IGNORE' ) );
962
963 $this->mGroups = array_merge( $this->mGroups, array( $group ) );
964 $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups() );
965
966 $this->invalidateCache();
967 $this->saveSettings();
968 }
969
970 /**
971 * Remove the user from the given group.
972 * This takes immediate effect.
973 * @string $group
974 */
975 function removeGroup( $group ) {
976 $dbw =& wfGetDB( DB_MASTER );
977 $dbw->delete( 'user_groups',
978 array(
979 'ug_user' => $this->getID(),
980 'ug_group' => $group,
981 ),
982 'User::removeGroup' );
983
984 $this->mGroups = array_diff( $this->mGroups, array( $group ) );
985 $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups() );
986
987 $this->invalidateCache();
988 $this->saveSettings();
989 }
990
991
992 /**
993 * A more legible check for non-anonymousness.
994 * Returns true if the user is not an anonymous visitor.
995 *
996 * @return bool
997 */
998 function isLoggedIn() {
999 return( $this->getID() != 0 );
1000 }
1001
1002 /**
1003 * A more legible check for anonymousness.
1004 * Returns true if the user is an anonymous visitor.
1005 *
1006 * @return bool
1007 */
1008 function isAnon() {
1009 return !$this->isLoggedIn();
1010 }
1011
1012 /**
1013 * Check if a user is sysop
1014 * Die with backtrace. Use User:isAllowed() instead.
1015 * @deprecated
1016 */
1017 function isSysop() {
1018 return $this->isAllowed( 'protect' );
1019 }
1020
1021 /** @deprecated */
1022 function isDeveloper() {
1023 return $this->isAllowed( 'siteadmin' );
1024 }
1025
1026 /** @deprecated */
1027 function isBureaucrat() {
1028 return $this->isAllowed( 'makesysop' );
1029 }
1030
1031 /**
1032 * Whether the user is a bot
1033 * @todo need to be migrated to the new user level management sytem
1034 */
1035 function isBot() {
1036 $this->loadFromDatabase();
1037 return in_array( 'bot', $this->mRights );
1038 }
1039
1040 /**
1041 * Check if user is allowed to access a feature / make an action
1042 * @param string $action Action to be checked (see $wgAvailableRights in Defines.php for possible actions).
1043 * @return boolean True: action is allowed, False: action should not be allowed
1044 */
1045 function isAllowed($action='') {
1046 $this->loadFromDatabase();
1047 return in_array( $action , $this->mRights );
1048 }
1049
1050 /**
1051 * Load a skin if it doesn't exist or return it
1052 * @todo FIXME : need to check the old failback system [AV]
1053 */
1054 function &getSkin() {
1055 global $IP, $wgRequest;
1056 if ( ! isset( $this->mSkin ) ) {
1057 $fname = 'User::getSkin';
1058 wfProfileIn( $fname );
1059
1060 # get all skin names available
1061 $skinNames = Skin::getSkinNames();
1062
1063 # get the user skin
1064 $userSkin = $this->getOption( 'skin' );
1065 $userSkin = $wgRequest->getText('useskin', $userSkin);
1066 if ( $userSkin == '' ) { $userSkin = 'standard'; }
1067
1068 if ( !isset( $skinNames[$userSkin] ) ) {
1069 # in case the user skin could not be found find a replacement
1070 $fallback = array(
1071 0 => 'Standard',
1072 1 => 'Nostalgia',
1073 2 => 'CologneBlue');
1074 # if phptal is enabled we should have monobook skin that
1075 # superseed the good old SkinStandard.
1076 if ( isset( $skinNames['monobook'] ) ) {
1077 $fallback[0] = 'MonoBook';
1078 }
1079
1080 if(is_numeric($userSkin) && isset( $fallback[$userSkin]) ){
1081 $sn = $fallback[$userSkin];
1082 } else {
1083 $sn = 'Standard';
1084 }
1085 } else {
1086 # The user skin is available
1087 $sn = $skinNames[$userSkin];
1088 }
1089
1090 # Grab the skin class and initialise it. Each skin checks for PHPTal
1091 # and will not load if it's not enabled.
1092 require_once( $IP.'/skins/'.$sn.'.php' );
1093
1094 # Check if we got if not failback to default skin
1095 $className = 'Skin'.$sn;
1096 if( !class_exists( $className ) ) {
1097 # DO NOT die if the class isn't found. This breaks maintenance
1098 # scripts and can cause a user account to be unrecoverable
1099 # except by SQL manipulation if a previously valid skin name
1100 # is no longer valid.
1101 $className = 'SkinStandard';
1102 require_once( $IP.'/skins/Standard.php' );
1103 }
1104 $this->mSkin =& new $className;
1105 wfProfileOut( $fname );
1106 }
1107 return $this->mSkin;
1108 }
1109
1110 /**#@+
1111 * @param string $title Article title to look at
1112 */
1113
1114 /**
1115 * Check watched status of an article
1116 * @return bool True if article is watched
1117 */
1118 function isWatched( $title ) {
1119 $wl = WatchedItem::fromUserTitle( $this, $title );
1120 return $wl->isWatched();
1121 }
1122
1123 /**
1124 * Watch an article
1125 */
1126 function addWatch( $title ) {
1127 $wl = WatchedItem::fromUserTitle( $this, $title );
1128 $wl->addWatch();
1129 $this->invalidateCache();
1130 }
1131
1132 /**
1133 * Stop watching an article
1134 */
1135 function removeWatch( $title ) {
1136 $wl = WatchedItem::fromUserTitle( $this, $title );
1137 $wl->removeWatch();
1138 $this->invalidateCache();
1139 }
1140
1141 /**
1142 * Clear the user's notification timestamp for the given title.
1143 * If e-notif e-mails are on, they will receive notification mails on
1144 * the next change of the page if it's watched etc.
1145 */
1146 function clearNotification( &$title ) {
1147 global $wgUser, $wgUseEnotif;
1148
1149 if ( !$wgUseEnotif ) {
1150 return;
1151 }
1152
1153 $userid = $this->getID();
1154 if ($userid==0) {
1155 return;
1156 }
1157
1158 // Only update the timestamp if the page is being watched.
1159 // The query to find out if it is watched is cached both in memcached and per-invocation,
1160 // and when it does have to be executed, it can be on a slave
1161 // If this is the user's newtalk page, we always update the timestamp
1162 if ($title->getNamespace() == NS_USER_TALK &&
1163 $title->getText() == $wgUser->getName())
1164 {
1165 $watched = true;
1166 } elseif ( $this->getID() == $wgUser->getID() ) {
1167 $watched = $title->userIsWatching();
1168 } else {
1169 $watched = true;
1170 }
1171
1172 // If the page is watched by the user (or may be watched), update the timestamp on any
1173 // any matching rows
1174 if ( $watched ) {
1175 $dbw =& wfGetDB( DB_MASTER );
1176 $success = $dbw->update( 'watchlist',
1177 array( /* SET */
1178 'wl_notificationtimestamp' => NULL
1179 ), array( /* WHERE */
1180 'wl_title' => $title->getDBkey(),
1181 'wl_namespace' => $title->getNamespace(),
1182 'wl_user' => $this->getID()
1183 ), 'User::clearLastVisited'
1184 );
1185 }
1186 }
1187
1188 /**#@-*/
1189
1190 /**
1191 * Resets all of the given user's page-change notification timestamps.
1192 * If e-notif e-mails are on, they will receive notification mails on
1193 * the next change of any watched page.
1194 *
1195 * @param int $currentUser user ID number
1196 * @access public
1197 */
1198 function clearAllNotifications( $currentUser ) {
1199 global $wgUseEnotif;
1200 if ( !$wgUseEnotif ) {
1201 return;
1202 }
1203 if( $currentUser != 0 ) {
1204
1205 $dbw =& wfGetDB( DB_MASTER );
1206 $success = $dbw->update( 'watchlist',
1207 array( /* SET */
1208 'wl_notificationtimestamp' => 0
1209 ), array( /* WHERE */
1210 'wl_user' => $currentUser
1211 ), 'UserMailer::clearAll'
1212 );
1213
1214 # we also need to clear here the "you have new message" notification for the own user_talk page
1215 # This is cleared one page view later in Article::viewUpdates();
1216 }
1217 }
1218
1219 /**
1220 * @access private
1221 * @return string Encoding options
1222 */
1223 function encodeOptions() {
1224 $a = array();
1225 foreach ( $this->mOptions as $oname => $oval ) {
1226 array_push( $a, $oname.'='.$oval );
1227 }
1228 $s = implode( "\n", $a );
1229 return $s;
1230 }
1231
1232 /**
1233 * @access private
1234 */
1235 function decodeOptions( $str ) {
1236 $a = explode( "\n", $str );
1237 foreach ( $a as $s ) {
1238 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
1239 $this->mOptions[$m[1]] = $m[2];
1240 }
1241 }
1242 }
1243
1244 function setCookies() {
1245 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
1246 if ( 0 == $this->mId ) return;
1247 $this->loadFromDatabase();
1248 $exp = time() + $wgCookieExpiration;
1249
1250 $_SESSION['wsUserID'] = $this->mId;
1251 setcookie( $wgDBname.'UserID', $this->mId, $exp, $wgCookiePath, $wgCookieDomain );
1252
1253 $_SESSION['wsUserName'] = $this->mName;
1254 setcookie( $wgDBname.'UserName', $this->mName, $exp, $wgCookiePath, $wgCookieDomain );
1255
1256 $_SESSION['wsToken'] = $this->mToken;
1257 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
1258 setcookie( $wgDBname.'Token', $this->mToken, $exp, $wgCookiePath, $wgCookieDomain );
1259 } else {
1260 setcookie( $wgDBname.'Token', '', time() - 3600 );
1261 }
1262 }
1263
1264 /**
1265 * Logout user
1266 * It will clean the session cookie
1267 */
1268 function logout() {
1269 global $wgCookiePath, $wgCookieDomain, $wgDBname, $wgIP;
1270 $this->loadDefaults();
1271 $this->setLoaded( true );
1272
1273 $_SESSION['wsUserID'] = 0;
1274
1275 setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
1276 setcookie( $wgDBname.'Token', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
1277
1278 # Remember when user logged out, to prevent seeing cached pages
1279 setcookie( $wgDBname.'LoggedOut', wfTimestampNow(), time() + 86400, $wgCookiePath, $wgCookieDomain );
1280 }
1281
1282 /**
1283 * Save object settings into database
1284 */
1285 function saveSettings() {
1286 global $wgMemc, $wgDBname, $wgUseEnotif;
1287 $fname = 'User::saveSettings';
1288
1289 if ( wfReadOnly() ) { return; }
1290 $this->saveNewtalk();
1291 if ( 0 == $this->mId ) { return; }
1292
1293 $dbw =& wfGetDB( DB_MASTER );
1294 $dbw->update( 'user',
1295 array( /* SET */
1296 'user_name' => $this->mName,
1297 'user_password' => $this->mPassword,
1298 'user_newpassword' => $this->mNewpassword,
1299 'user_real_name' => $this->mRealName,
1300 'user_email' => $this->mEmail,
1301 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated ),
1302 'user_options' => $this->encodeOptions(),
1303 'user_touched' => $dbw->timestamp($this->mTouched),
1304 'user_token' => $this->mToken
1305 ), array( /* WHERE */
1306 'user_id' => $this->mId
1307 ), $fname
1308 );
1309 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
1310 }
1311
1312 /**
1313 * Save value of new talk flag.
1314 */
1315 function saveNewtalk() {
1316 global $wgDBname, $wgMemc, $wgUseEnotif;
1317
1318 $fname = 'User::saveNewtalk';
1319
1320 $changed = false;
1321
1322 if ( wfReadOnly() ) { return ; }
1323 $dbr =& wfGetDB( DB_SLAVE );
1324 $dbw =& wfGetDB( DB_MASTER );
1325 $changed = false;
1326 if ( $wgUseEnotif ) {
1327 if ( ! $this->getNewtalk() ) {
1328 # Delete the watchlist entry for user_talk page X watched by user X
1329 $dbw->delete( 'watchlist',
1330 array( 'wl_user' => $this->mId,
1331 'wl_title' => $this->getTitleKey(),
1332 'wl_namespace' => NS_USER_TALK ),
1333 $fname );
1334 if ( $dbw->affectedRows() ) {
1335 $changed = true;
1336 }
1337 if( !$this->mId ) {
1338 # Anon users have a separate memcache space for newtalk
1339 # since they don't store their own info. Trim...
1340 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
1341 }
1342 }
1343 } else {
1344 if ($this->getID() != 0) {
1345 $field = 'user_id';
1346 $value = $this->getID();
1347 $key = false;
1348 } else {
1349 $field = 'user_ip';
1350 $value = $this->mName;
1351 $key = "$wgDBname:newtalk:ip:$this->mName";
1352 }
1353
1354 $dbr =& wfGetDB( DB_SLAVE );
1355 $dbw =& wfGetDB( DB_MASTER );
1356
1357 $res = $dbr->selectField('user_newtalk', $field,
1358 array($field => $value), $fname);
1359
1360 $changed = true;
1361 if ($res !== false && $this->mNewtalk == 0) {
1362 $dbw->delete('user_newtalk', array($field => $value), $fname);
1363 if ( $key ) {
1364 $wgMemc->set( $key, 0 );
1365 }
1366 } else if ($res === false && $this->mNewtalk == 1) {
1367 $dbw->insert('user_newtalk', array($field => $value), $fname);
1368 if ( $key ) {
1369 $wgMemc->set( $key, 1 );
1370 }
1371 } else {
1372 $changed = false;
1373 }
1374 }
1375
1376 # Update user_touched, so that newtalk notifications in the client cache are invalidated
1377 if ( $changed && $this->getID() ) {
1378 $dbw->update('user',
1379 /*SET*/ array( 'user_touched' => $this->mTouched ),
1380 /*WHERE*/ array( 'user_id' => $this->getID() ),
1381 $fname);
1382 $wgMemc->set( "$wgDBname:user:id:{$this->mId}", $this, 86400 );
1383 }
1384 }
1385
1386 /**
1387 * Checks if a user with the given name exists, returns the ID
1388 */
1389 function idForName() {
1390 $fname = 'User::idForName';
1391
1392 $gotid = 0;
1393 $s = trim( $this->mName );
1394 if ( 0 == strcmp( '', $s ) ) return 0;
1395
1396 $dbr =& wfGetDB( DB_SLAVE );
1397 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), $fname );
1398 if ( $id === false ) {
1399 $id = 0;
1400 }
1401 return $id;
1402 }
1403
1404 /**
1405 * Add user object to the database
1406 */
1407 function addToDatabase() {
1408 $fname = 'User::addToDatabase';
1409 $dbw =& wfGetDB( DB_MASTER );
1410 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
1411 $dbw->insert( 'user',
1412 array(
1413 'user_id' => $seqVal,
1414 'user_name' => $this->mName,
1415 'user_password' => $this->mPassword,
1416 'user_newpassword' => $this->mNewpassword,
1417 'user_email' => $this->mEmail,
1418 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated ),
1419 'user_real_name' => $this->mRealName,
1420 'user_options' => $this->encodeOptions(),
1421 'user_token' => $this->mToken
1422 ), $fname
1423 );
1424 $this->mId = $dbw->insertId();
1425 }
1426
1427 function spreadBlock() {
1428 global $wgIP;
1429 # If the (non-anonymous) user is blocked, this function will block any IP address
1430 # that they successfully log on from.
1431 $fname = 'User::spreadBlock';
1432
1433 wfDebug( "User:spreadBlock()\n" );
1434 if ( $this->mId == 0 ) {
1435 return;
1436 }
1437
1438 $userblock = Block::newFromDB( '', $this->mId );
1439 if ( !$userblock->isValid() ) {
1440 return;
1441 }
1442
1443 # Check if this IP address is already blocked
1444 $ipblock = Block::newFromDB( $wgIP );
1445 if ( $ipblock->isValid() ) {
1446 # Just update the timestamp
1447 $ipblock->updateTimestamp();
1448 return;
1449 }
1450
1451 # Make a new block object with the desired properties
1452 wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
1453 $ipblock->mAddress = $wgIP;
1454 $ipblock->mUser = 0;
1455 $ipblock->mBy = $userblock->mBy;
1456 $ipblock->mReason = wfMsg( 'autoblocker', $this->getName(), $userblock->mReason );
1457 $ipblock->mTimestamp = wfTimestampNow();
1458 $ipblock->mAuto = 1;
1459 # If the user is already blocked with an expiry date, we don't
1460 # want to pile on top of that!
1461 if($userblock->mExpiry) {
1462 $ipblock->mExpiry = min ( $userblock->mExpiry, Block::getAutoblockExpiry( $ipblock->mTimestamp ));
1463 } else {
1464 $ipblock->mExpiry = Block::getAutoblockExpiry( $ipblock->mTimestamp );
1465 }
1466
1467 # Insert it
1468 $ipblock->insert();
1469
1470 }
1471
1472 function getPageRenderingHash() {
1473 global $wgContLang;
1474 if( $this->mHash ){
1475 return $this->mHash;
1476 }
1477
1478 // stubthreshold is only included below for completeness,
1479 // it will always be 0 when this function is called by parsercache.
1480
1481 $confstr = $this->getOption( 'math' );
1482 $confstr .= '!' . $this->getOption( 'stubthreshold' );
1483 $confstr .= '!' . $this->getOption( 'date' );
1484 $confstr .= '!' . $this->getOption( 'numberheadings' );
1485 $confstr .= '!' . $this->getOption( 'language' );
1486 $confstr .= '!' . $this->getOption( 'thumbsize' );
1487 // add in language specific options, if any
1488 $extra = $wgContLang->getExtraHashOptions();
1489 $confstr .= $extra;
1490
1491 $this->mHash = $confstr;
1492 return $confstr ;
1493 }
1494
1495 function isAllowedToCreateAccount() {
1496 return $this->isAllowed( 'createaccount' );
1497 }
1498
1499 /**
1500 * Set mDataLoaded, return previous value
1501 * Use this to prevent DB access in command-line scripts or similar situations
1502 */
1503 function setLoaded( $loaded ) {
1504 return wfSetVar( $this->mDataLoaded, $loaded );
1505 }
1506
1507 /**
1508 * Get this user's personal page title.
1509 *
1510 * @return Title
1511 * @access public
1512 */
1513 function getUserPage() {
1514 return Title::makeTitle( NS_USER, $this->mName );
1515 }
1516
1517 /**
1518 * Get this user's talk page title.
1519 *
1520 * @return Title
1521 * @access public
1522 */
1523 function getTalkPage() {
1524 $title = $this->getUserPage();
1525 return $title->getTalkPage();
1526 }
1527
1528 /**
1529 * @static
1530 */
1531 function getMaxID() {
1532 $dbr =& wfGetDB( DB_SLAVE );
1533 return $dbr->selectField( 'user', 'max(user_id)', false, 'User::getMaxID' );
1534 }
1535
1536 /**
1537 * Determine whether the user is a newbie. Newbies are either
1538 * anonymous IPs, or the 1% most recently created accounts.
1539 * Bots and sysops are excluded.
1540 * @return bool True if it is a newbie.
1541 */
1542 function isNewbie() {
1543 return $this->isAnon() || $this->mId > User::getMaxID() * 0.99 && !$this->isAllowed( 'delete' ) && !$this->isBot();
1544 }
1545
1546 /**
1547 * Check to see if the given clear-text password is one of the accepted passwords
1548 * @param string $password User password.
1549 * @return bool True if the given password is correct otherwise False.
1550 */
1551 function checkPassword( $password ) {
1552 global $wgAuth, $wgMinimalPasswordLength;
1553 $this->loadFromDatabase();
1554
1555 // Even though we stop people from creating passwords that
1556 // are shorter than this, doesn't mean people wont be able
1557 // to. Certain authentication plugins do NOT want to save
1558 // domain passwords in a mysql database, so we should
1559 // check this (incase $wgAuth->strict() is false).
1560 if( strlen( $password ) < $wgMinimalPasswordLength ) {
1561 return false;
1562 }
1563
1564 if( $wgAuth->authenticate( $this->getName(), $password ) ) {
1565 return true;
1566 } elseif( $wgAuth->strict() ) {
1567 /* Auth plugin doesn't allow local authentication */
1568 return false;
1569 }
1570 $ep = $this->encryptPassword( $password );
1571 if ( 0 == strcmp( $ep, $this->mPassword ) ) {
1572 return true;
1573 } elseif ( ($this->mNewpassword != '') && (0 == strcmp( $ep, $this->mNewpassword )) ) {
1574 return true;
1575 } elseif ( function_exists( 'iconv' ) ) {
1576 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
1577 # Check for this with iconv
1578 $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252', $password ) );
1579 if ( 0 == strcmp( $cp1252hash, $this->mPassword ) ) {
1580 return true;
1581 }
1582 }
1583 return false;
1584 }
1585
1586 /**
1587 * Initialize (if necessary) and return a session token value
1588 * which can be used in edit forms to show that the user's
1589 * login credentials aren't being hijacked with a foreign form
1590 * submission.
1591 *
1592 * @param mixed $salt - Optional function-specific data for hash.
1593 * Use a string or an array of strings.
1594 * @return string
1595 * @access public
1596 */
1597 function editToken( $salt = '' ) {
1598 if( !isset( $_SESSION['wsEditToken'] ) ) {
1599 $token = $this->generateToken();
1600 $_SESSION['wsEditToken'] = $token;
1601 } else {
1602 $token = $_SESSION['wsEditToken'];
1603 }
1604 if( is_array( $salt ) ) {
1605 $salt = implode( '|', $salt );
1606 }
1607 return md5( $token . $salt );
1608 }
1609
1610 /**
1611 * Generate a hex-y looking random token for various uses.
1612 * Could be made more cryptographically sure if someone cares.
1613 * @return string
1614 */
1615 function generateToken( $salt = '' ) {
1616 $token = dechex( mt_rand() ) . dechex( mt_rand() );
1617 return md5( $token . $salt );
1618 }
1619
1620 /**
1621 * Check given value against the token value stored in the session.
1622 * A match should confirm that the form was submitted from the
1623 * user's own login session, not a form submission from a third-party
1624 * site.
1625 *
1626 * @param string $val - the input value to compare
1627 * @param string $salt - Optional function-specific data for hash
1628 * @return bool
1629 * @access public
1630 */
1631 function matchEditToken( $val, $salt = '' ) {
1632 global $wgMemc;
1633
1634 /*
1635 if ( !isset( $_SESSION['wsEditToken'] ) ) {
1636 $logfile = '/home/wikipedia/logs/session_debug/session.log';
1637 $mckey = memsess_key( session_id() );
1638 $uname = @posix_uname();
1639 $msg = "wsEditToken not set!\n" .
1640 'apache server=' . $uname['nodename'] . "\n" .
1641 'session_id = ' . session_id() . "\n" .
1642 '$_SESSION=' . var_export( $_SESSION, true ) . "\n" .
1643 '$_COOKIE=' . var_export( $_COOKIE, true ) . "\n" .
1644 "mc get($mckey) = " . var_export( $wgMemc->get( $mckey ), true ) . "\n\n\n";
1645
1646 @error_log( $msg, 3, $logfile );
1647 }
1648 */
1649 return ( $val == $this->editToken( $salt ) );
1650 }
1651
1652 /**
1653 * Generate a new e-mail confirmation token and send a confirmation
1654 * mail to the user's given address.
1655 *
1656 * @return mixed True on success, a WikiError object on failure.
1657 */
1658 function sendConfirmationMail() {
1659 global $wgIP, $wgContLang;
1660 $url = $this->confirmationTokenUrl( $expiration );
1661 return $this->sendMail( wfMsg( 'confirmemail_subject' ),
1662 wfMsg( 'confirmemail_body',
1663 $wgIP,
1664 $this->getName(),
1665 $url,
1666 $wgContLang->timeanddate( $expiration, false ) ) );
1667 }
1668
1669 /**
1670 * Send an e-mail to this user's account. Does not check for
1671 * confirmed status or validity.
1672 *
1673 * @param string $subject
1674 * @param string $body
1675 * @param strong $from Optional from address; default $wgPasswordSender will be used otherwise.
1676 * @return mixed True on success, a WikiError object on failure.
1677 */
1678 function sendMail( $subject, $body, $from = null ) {
1679 if( is_null( $from ) ) {
1680 global $wgPasswordSender;
1681 $from = $wgPasswordSender;
1682 }
1683
1684 require_once( 'UserMailer.php' );
1685 $error = userMailer( $this->getEmail(), $from, $subject, $body );
1686
1687 if( $error == '' ) {
1688 return true;
1689 } else {
1690 return new WikiError( $error );
1691 }
1692 }
1693
1694 /**
1695 * Generate, store, and return a new e-mail confirmation code.
1696 * A hash (unsalted since it's used as a key) is stored.
1697 * @param &$expiration mixed output: accepts the expiration time
1698 * @return string
1699 * @access private
1700 */
1701 function confirmationToken( &$expiration ) {
1702 $fname = 'User::confirmationToken';
1703
1704 $now = time();
1705 $expires = $now + 7 * 24 * 60 * 60;
1706 $expiration = wfTimestamp( TS_MW, $expires );
1707
1708 $token = $this->generateToken( $this->mId . $this->mEmail . $expires );
1709 $hash = md5( $token );
1710
1711 $dbw =& wfGetDB( DB_MASTER );
1712 $dbw->update( 'user',
1713 array( 'user_email_token' => $hash,
1714 'user_email_token_expires' => $dbw->timestamp( $expires ) ),
1715 array( 'user_id' => $this->mId ),
1716 $fname );
1717
1718 return $token;
1719 }
1720
1721 /**
1722 * Generate and store a new e-mail confirmation token, and return
1723 * the URL the user can use to confirm.
1724 * @param &$expiration mixed output: accepts the expiration time
1725 * @return string
1726 * @access private
1727 */
1728 function confirmationTokenUrl( &$expiration ) {
1729 $token = $this->confirmationToken( $expiration );
1730 $title = Title::makeTitle( NS_SPECIAL, 'Confirmemail/' . $token );
1731 return $title->getFullUrl();
1732 }
1733
1734 /**
1735 * Mark the e-mail address confirmed and save.
1736 */
1737 function confirmEmail() {
1738 $this->loadFromDatabase();
1739 $this->mEmailAuthenticated = wfTimestampNow();
1740 $this->saveSettings();
1741 return true;
1742 }
1743
1744 /**
1745 * Is this user allowed to send e-mails within limits of current
1746 * site configuration?
1747 * @return bool
1748 */
1749 function canSendEmail() {
1750 return $this->isEmailConfirmed();
1751 }
1752
1753 /**
1754 * Is this user allowed to receive e-mails within limits of current
1755 * site configuration?
1756 * @return bool
1757 */
1758 function canReceiveEmail() {
1759 return $this->canSendEmail() && !$this->getOption( 'disablemail' );
1760 }
1761
1762 /**
1763 * Is this user's e-mail address valid-looking and confirmed within
1764 * limits of the current site configuration?
1765 *
1766 * If $wgEmailAuthentication is on, this may require the user to have
1767 * confirmed their address by returning a code or using a password
1768 * sent to the address from the wiki.
1769 *
1770 * @return bool
1771 */
1772 function isEmailConfirmed() {
1773 global $wgEmailAuthentication;
1774 $this->loadFromDatabase();
1775 if( $this->isAnon() )
1776 return false;
1777 if( !$this->isValidEmailAddr( $this->mEmail ) )
1778 return false;
1779 if( $wgEmailAuthentication && !$this->getEmailAuthenticationTimestamp() )
1780 return false;
1781 return true;
1782 }
1783
1784 /**
1785 * @param array $groups list of groups
1786 * @return array list of permission key names for given groups combined
1787 * @static
1788 */
1789 function getGroupPermissions( $groups ) {
1790 global $wgGroupPermissions;
1791 $rights = array();
1792 foreach( $groups as $group ) {
1793 if( isset( $wgGroupPermissions[$group] ) ) {
1794 $rights = array_merge( $rights,
1795 array_keys( array_filter( $wgGroupPermissions[$group] ) ) );
1796 }
1797 }
1798 return $rights;
1799 }
1800
1801 /**
1802 * @param string $group key name
1803 * @return string localized descriptive name, if provided
1804 * @static
1805 */
1806 function getGroupName( $group ) {
1807 $key = "group-$group-name";
1808 $name = wfMsg( $key );
1809 if( $name == '' || $name == "&lt;$key&gt;" ) {
1810 return $group;
1811 } else {
1812 return $name;
1813 }
1814 }
1815
1816 /**
1817 * Return the set of defined explicit groups.
1818 * The * and 'user' groups are not included.
1819 * @return array
1820 * @static
1821 */
1822 function getAllGroups() {
1823 global $wgGroupPermissions;
1824 return array_diff(
1825 array_keys( $wgGroupPermissions ),
1826 array( '*', 'user' ) );
1827 }
1828
1829 }
1830
1831 ?>