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