Fixed bug with magic word fallback to en
[lhc/web/wiklou.git] / includes / User.php
1 <?php
2 # See user.doc
3
4 require_once( "WatchedItem.php" );
5
6 class User {
7 /* private */ var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
8 /* private */ var $mRights, $mOptions;
9 /* private */ var $mDataLoaded, $mNewpassword;
10 /* private */ var $mSkin;
11 /* private */ var $mBlockedby, $mBlockreason;
12 /* private */ var $mTouched;
13 /* private */ var $mCookiePassword;
14 /* private */ var $mRealName;
15
16 function User()
17 {
18 $this->loadDefaults();
19 }
20
21 # Static factory method
22 #
23 function newFromName( $name )
24 {
25 $u = new User();
26
27 # Clean up name according to title rules
28
29 $t = Title::newFromText( $name );
30 $u->setName( $t->getText() );
31 return $u;
32 }
33
34 /* static */ function whoIs( $id )
35 {
36 return wfGetSQL( "user", "user_name", "user_id=$id" );
37 }
38
39 /* static */ function whoIsReal( $id )
40 {
41 return wfGetSQL( "user", "user_real_name", "user_id=$id" );
42 }
43
44 /* static */ function idFromName( $name )
45 {
46 $nt = Title::newFromText( $name );
47 if( is_null( $nt ) ) {
48 # Illegal name
49 return null;
50 }
51 $sql = "SELECT user_id FROM user WHERE user_name='" .
52 wfStrencode( $nt->getText() ) . "'";
53 $res = wfQuery( $sql, DB_READ, "User::idFromName" );
54
55 if ( 0 == wfNumRows( $res ) ) {
56 return 0;
57 } else {
58 $s = wfFetchObject( $res );
59 wfFreeResult( $res );
60 return $s->user_id;
61 }
62 }
63
64 # does the string match an anonymous user IP address?
65 /* static */ function isIP( $name ) {
66 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
67
68 }
69
70 /* static */ function randomPassword()
71 {
72 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
73 $l = strlen( $pwchars ) - 1;
74
75 wfSeedRandom();
76 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
77 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
78 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
79 $pwchars{mt_rand( 0, $l )};
80 return $np;
81 }
82
83 function loadDefaults()
84 {
85 global $wgLang, $wgIP;
86 global $wgNamespacesToBeSearchedDefault;
87
88 $this->mId = $this->mNewtalk = 0;
89 $this->mName = $wgIP;
90 $this->mEmail = "";
91 $this->mPassword = $this->mNewpassword = "";
92 $this->mRights = array();
93 $defOpt = $wgLang->getDefaultUserOptions() ;
94 foreach ( $defOpt as $oname => $val ) {
95 $this->mOptions[$oname] = $val;
96 }
97 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
98 $this->mOptions["searchNs".$nsnum] = $val;
99 }
100 unset( $this->mSkin );
101 $this->mDataLoaded = false;
102 $this->mBlockedby = -1; # Unset
103 $this->mTouched = '0'; # Allow any pages to be cached
104 $this->cookiePassword = "";
105 }
106
107 /* private */ function getBlockedStatus()
108 {
109 global $wgIP, $wgBlockCache;
110
111 if ( -1 != $this->mBlockedby ) { return; }
112
113 $this->mBlockedby = 0;
114
115 # User blocking
116 if ( $this->mId ) {
117 $block = new Block();
118 if ( $block->load( $wgIP , $this->mId ) ) {
119 $this->mBlockedby = $block->mBy;
120 $this->mBlockreason = $block->mReason;
121 }
122 }
123
124 # IP/range blocking
125 if ( !$this->mBlockedby ) {
126 $block = $wgBlockCache->get( $wgIP );
127 if ( $block !== false ) {
128 $this->mBlockedby = $block->mBy;
129 $this->mBlockreason = $block->mReason;
130 }
131 }
132 }
133
134 function isBlocked()
135 {
136 $this->getBlockedStatus();
137 if ( 0 == $this->mBlockedby ) { return false; }
138 return true;
139 }
140
141 function blockedBy() {
142 $this->getBlockedStatus();
143 return $this->mBlockedby;
144 }
145
146 function blockedFor() {
147 $this->getBlockedStatus();
148 return $this->mBlockreason;
149 }
150
151 function SetupSession() {
152 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
153 if( $wgSessionsInMemcached ) {
154 require_once( "MemcachedSessions.php" );
155 }
156 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
157 session_cache_limiter( "private, must-revalidate" );
158 @session_start();
159 }
160
161 /* static */ function loadFromSession()
162 {
163 global $wgMemc, $wgDBname;
164
165 if ( isset( $_SESSION['wsUserID'] ) ) {
166 if ( 0 != $_SESSION['wsUserID'] ) {
167 $sId = $_SESSION['wsUserID'];
168 } else {
169 return new User();
170 }
171 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
172 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
173 $_SESSION['wsUserID'] = $sId;
174 } else {
175 return new User();
176 }
177 if ( isset( $_SESSION['wsUserName'] ) ) {
178 $sName = $_SESSION['wsUserName'];
179 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
180 $sName = $_COOKIE["{$wgDBname}UserName"];
181 $_SESSION['wsUserName'] = $sName;
182 } else {
183 return new User();
184 }
185
186 $passwordCorrect = FALSE;
187 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
188 if($makenew = !$user) {
189 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
190 $user = new User();
191 $user->mId = $sId;
192 $user->loadFromDatabase();
193 } else {
194 wfDebug( "User::loadFromSession() got from cache!\n" );
195 }
196
197 if ( isset( $_SESSION['wsUserPassword'] ) ) {
198 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword;
199 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
200 $user->mCookiePassword = $_COOKIE["{$wgDBname}Password"];
201 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword );
202 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword;
203 } else {
204 return new User(); # Can't log in from session
205 }
206
207 if ( ( $sName == $user->mName ) && $passwordCorrect ) {
208 if($makenew) {
209 if($wgMemc->set( $key, $user ))
210 wfDebug( "User::loadFromSession() successfully saved user\n" );
211 else
212 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
213 }
214 $user->spreadBlock();
215 return $user;
216 }
217 return new User(); # Can't log in from session
218 }
219
220 function loadFromDatabase()
221 {
222 global $wgCommandLineMode;
223 if ( $this->mDataLoaded || $wgCommandLineMode ) {
224 return;
225 }
226
227 # Paranoia
228 $this->mId = IntVal( $this->mId );
229
230 # check in separate table if there are changes to the talk page
231 $this->mNewtalk=0; # reset talk page status
232 if($this->mId) {
233 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
234 $res = wfQuery ($sql, DB_READ, "User::loadFromDatabase" );
235
236 if (wfNumRows($res)>0) {
237 $this->mNewtalk= 1;
238 }
239 wfFreeResult( $res );
240 } else {
241 global $wgDBname, $wgMemc;
242 $key = "$wgDBname:newtalk:ip:{$this->mName}";
243 $newtalk = $wgMemc->get( $key );
244 if( ! is_integer( $newtalk ) ){
245 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
246 $res = wfQuery ($sql, DB_READ, "User::loadFromDatabase" );
247
248 $this->mNewtalk = (wfNumRows($res)>0) ? 1 : 0;
249 wfFreeResult( $res );
250
251 $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
252 } else {
253 $this->mNewtalk = $newtalk ? 1 : 0;
254 }
255 }
256 if(!$this->mId) {
257 $this->mDataLoaded = true;
258 return;
259 } # the following stuff is for non-anonymous users only
260
261 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
262 "user_real_name,user_options,user_rights,user_touched " .
263 " FROM user WHERE user_id=" . $this->mId;
264 $res = wfQuery( $sql, DB_READ, "User::loadFromDatabase" );
265
266 if ( wfNumRows( $res ) > 0 ) {
267 $s = wfFetchObject( $res );
268 $this->mName = $s->user_name;
269 $this->mEmail = $s->user_email;
270 $this->mRealName = $s->user_real_name;
271 $this->mPassword = $s->user_password;
272 $this->mNewpassword = $s->user_newpassword;
273 $this->decodeOptions( $s->user_options );
274 $this->mRights = explode( ",", strtolower( $s->user_rights ) );
275 $this->mTouched = $s->user_touched;
276 }
277
278 wfFreeResult( $res );
279 $this->mDataLoaded = true;
280 }
281
282 function getID() { return $this->mId; }
283 function setID( $v ) {
284 $this->mId = $v;
285 $this->mDataLoaded = false;
286 }
287
288 function getName() {
289 $this->loadFromDatabase();
290 return $this->mName;
291 }
292
293 function setName( $str )
294 {
295 $this->loadFromDatabase();
296 $this->mName = $str;
297 }
298
299 function getNewtalk()
300 {
301 $this->loadFromDatabase();
302 return ( 0 != $this->mNewtalk );
303 }
304
305 function setNewtalk( $val )
306 {
307 $this->loadFromDatabase();
308 $this->mNewtalk = $val;
309 $this->invalidateCache();
310 }
311
312 function invalidateCache() {
313 $this->loadFromDatabase();
314 $this->mTouched = wfTimestampNow();
315 # Don't forget to save the options after this or
316 # it won't take effect!
317 }
318
319 function validateCache( $timestamp ) {
320 $this->loadFromDatabase();
321 return ($timestamp >= $this->mTouched);
322 }
323
324 function getPassword()
325 {
326 $this->loadFromDatabase();
327 return $this->mPassword;
328 }
329
330 function getNewpassword()
331 {
332 $this->loadFromDatabase();
333 return $this->mNewpassword;
334 }
335
336 function addSalt( $p )
337 {
338 global $wgPasswordSalt;
339 if($wgPasswordSalt)
340 return md5( "{$this->mId}-{$p}" );
341 else
342 return $p;
343 }
344
345 function encryptPassword( $p )
346 {
347 return $this->addSalt( md5( $p ) );
348 }
349
350 function setPassword( $str )
351 {
352 $this->loadFromDatabase();
353 $this->setCookiePassword( $str );
354 $this->mPassword = $this->encryptPassword( $str );
355 $this->mNewpassword = "";
356 }
357
358 function setCookiePassword( $str )
359 {
360 $this->loadFromDatabase();
361 $this->mCookiePassword = md5( $str );
362 }
363
364 function setNewpassword( $str )
365 {
366 $this->loadFromDatabase();
367 $this->mNewpassword = $this->encryptPassword( $str );
368 }
369
370 function getEmail()
371 {
372 $this->loadFromDatabase();
373 return $this->mEmail;
374 }
375
376 function setEmail( $str )
377 {
378 $this->loadFromDatabase();
379 $this->mEmail = $str;
380 }
381
382 function getRealName()
383 {
384 $this->loadFromDatabase();
385 return $this->mRealName;
386 }
387
388 function setRealName( $str )
389 {
390 $this->loadFromDatabase();
391 $this->mRealName = $str;
392 }
393
394 function getOption( $oname )
395 {
396 $this->loadFromDatabase();
397 if ( array_key_exists( $oname, $this->mOptions ) ) {
398 return $this->mOptions[$oname];
399 } else {
400 return "";
401 }
402 }
403
404 function setOption( $oname, $val )
405 {
406 $this->loadFromDatabase();
407 if ( $oname == 'skin' ) {
408 # Clear cached skin, so the new one displays immediately in Special:Preferences
409 unset( $this->mSkin );
410 }
411 $this->mOptions[$oname] = $val;
412 $this->invalidateCache();
413 }
414
415 function getRights()
416 {
417 $this->loadFromDatabase();
418 return $this->mRights;
419 }
420
421 function addRight( $rname )
422 {
423 $this->loadFromDatabase();
424 array_push( $this->mRights, $rname );
425 $this->invalidateCache();
426 }
427
428 function isSysop()
429 {
430 $this->loadFromDatabase();
431 if ( 0 == $this->mId ) { return false; }
432
433 return in_array( "sysop", $this->mRights );
434 }
435
436 function isDeveloper()
437 {
438 $this->loadFromDatabase();
439 if ( 0 == $this->mId ) { return false; }
440
441 return in_array( "developer", $this->mRights );
442 }
443
444 function isBureaucrat()
445 {
446 $this->loadFromDatabase();
447 if ( 0 == $this->mId ) { return false; }
448
449 return in_array( "bureaucrat", $this->mRights );
450 }
451
452 function isBot()
453 {
454 $this->loadFromDatabase();
455
456 # Why was this here? I need a UID=0 conversion script [TS]
457 # if ( 0 == $this->mId ) { return false; }
458
459 return in_array( "bot", $this->mRights );
460 }
461
462 function &getSkin()
463 {
464 if ( ! isset( $this->mSkin ) ) {
465 $skinNames = Skin::getSkinNames();
466 $s = $this->getOption( "skin" );
467 if ( "" == $s ) { $s = 'standard'; }
468
469 if ( !isset( $skinNames[$s] ) ) {
470 $fallback = array(
471 0 => "SkinStandard",
472 1 => "SkinNostalgia",
473 2 => "SkinCologneBlue");
474 if ( isset( $skinNames['monobook'] ) ) {
475 $fallback[0] = "SkinMonoBook";
476 }
477
478 if(is_numeric($s) && isset( $fallback[$s]) ){
479 $sn = $fallback[$s];
480 } else {
481 $sn = "SkinStandard";
482 }
483 } else {
484 $sn = "Skin" . $skinNames[$s];
485 }
486 $this->mSkin = new $sn;
487 }
488 return $this->mSkin;
489 }
490
491 function isWatched( $title ) {
492 $wl = WatchedItem::fromUserTitle( $this, $title );
493 return $wl->isWatched();
494 }
495
496 function addWatch( $title ) {
497 $wl = WatchedItem::fromUserTitle( $this, $title );
498 $wl->addWatch();
499 $this->invalidateCache();
500 }
501
502 function removeWatch( $title ) {
503 $wl = WatchedItem::fromUserTitle( $this, $title );
504 $wl->removeWatch();
505 $this->invalidateCache();
506 }
507
508
509 /* private */ function encodeOptions()
510 {
511 $a = array();
512 foreach ( $this->mOptions as $oname => $oval ) {
513 array_push( $a, "{$oname}={$oval}" );
514 }
515 $s = implode( "\n", $a );
516 return wfStrencode( $s );
517 }
518
519 /* private */ function decodeOptions( $str )
520 {
521 $a = explode( "\n", $str );
522 foreach ( $a as $s ) {
523 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
524 $this->mOptions[$m[1]] = $m[2];
525 }
526 }
527 }
528
529 function setCookies()
530 {
531 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
532 if ( 0 == $this->mId ) return;
533 $this->loadFromDatabase();
534 $exp = time() + $wgCookieExpiration;
535
536 $_SESSION['wsUserID'] = $this->mId;
537 setcookie( "{$wgDBname}UserID", $this->mId, $exp, $wgCookiePath, $wgCookieDomain );
538
539 $_SESSION['wsUserName'] = $this->mName;
540 setcookie( "{$wgDBname}UserName", $this->mName, $exp, $wgCookiePath, $wgCookieDomain );
541
542 $_SESSION['wsUserPassword'] = $this->mPassword;
543 if ( 1 == $this->getOption( "rememberpassword" ) ) {
544 setcookie( "{$wgDBname}Password", $this->mCookiePassword, $exp, $wgCookiePath, $wgCookieDomain );
545 } else {
546 setcookie( "{$wgDBname}Password", "", time() - 3600 );
547 }
548 }
549
550 function logout()
551 {
552 global $wgCookiePath, $wgCookieDomain, $wgDBname;
553 $this->mId = 0;
554
555 $_SESSION['wsUserID'] = 0;
556
557 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
558 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
559 }
560
561 function saveSettings()
562 {
563 global $wgMemc, $wgDBname;
564
565 if ( ! $this->mNewtalk ) {
566 if( $this->mId ) {
567 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
568 wfQuery ($sql, DB_WRITE, "User::saveSettings");
569 } else {
570 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
571 wfQuery ($sql, DB_WRITE, "User::saveSettings");
572 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
573 }
574 }
575 if ( 0 == $this->mId ) { return; }
576
577 $sql = "UPDATE user SET " .
578 "user_name= '" . wfStrencode( $this->mName ) . "', " .
579 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
580 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
581 "user_real_name= '" . wfStrencode( $this->mRealName ) . "', " .
582 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
583 "user_options= '" . $this->encodeOptions() . "', " .
584 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " .
585 "user_touched= '" . wfStrencode( $this->mTouched ) .
586 "' WHERE user_id={$this->mId}";
587 wfQuery( $sql, DB_WRITE, "User::saveSettings" );
588 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
589 }
590
591 # Checks if a user with the given name exists
592 #
593 function idForName()
594 {
595 $gotid = 0;
596 $s = trim( $this->mName );
597 if ( 0 == strcmp( "", $s ) ) return 0;
598
599 $sql = "SELECT user_id FROM user WHERE user_name='" .
600 wfStrencode( $s ) . "'";
601 $res = wfQuery( $sql, DB_READ, "User::idForName" );
602 if ( 0 == wfNumRows( $res ) ) { return 0; }
603
604 $s = wfFetchObject( $res );
605 if ( "" == $s ) return 0;
606
607 $gotid = $s->user_id;
608 wfFreeResult( $res );
609 return $gotid;
610 }
611
612 function addToDatabase()
613 {
614 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
615 "user_email, user_real_name, user_rights, user_options) " .
616 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
617 wfStrencode( $this->mPassword ) . "', '" .
618 wfStrencode( $this->mNewpassword ) . "', '" .
619 wfStrencode( $this->mEmail ) . "', '" .
620 wfStrencode( $this->mRealName ) . "', '" .
621 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
622 $this->encodeOptions() . "')";
623 wfQuery( $sql, DB_WRITE, "User::addToDatabase" );
624 $this->mId = $this->idForName();
625 }
626
627 function spreadBlock()
628 {
629 global $wgIP;
630 # If the (non-anonymous) user is blocked, this function will block any IP address
631 # that they successfully log on from.
632 $fname = "User::spreadBlock";
633
634 wfDebug( "User:spreadBlock()\n" );
635 if ( $this->mId == 0 ) {
636 return;
637 }
638
639 $userblock = Block::newFromDB( "", $this->mId );
640 if ( !$userblock->isValid() ) {
641 return;
642 }
643
644 # Check if this IP address is already blocked
645 $ipblock = Block::newFromDB( $wgIP );
646 if ( $ipblock->isValid() ) {
647 # Just update the timestamp
648 $ipblock->updateTimestamp();
649 return;
650 }
651
652 # Make a new block object with the desired properties
653 wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
654 $ipblock->mAddress = $wgIP;
655 $ipblock->mUser = 0;
656 $ipblock->mBy = $userblock->mBy;
657 $ipblock->mReason = wfMsg( "autoblocker", $this->getName(), $userblock->mReason );
658 $ipblock->mTimestamp = wfTimestampNow();
659 $ipblock->mAuto = 1;
660 # If the user is already blocked with an expiry date, we don't
661 # want to pile on top of that!
662 if($userblock->mExpiry) {
663 $ipblock->mExpiry = min ( $userblock->mExpiry, Block::getAutoblockExpiry( $ipblock->mTimestamp ));
664 } else {
665 $ipblock->mExpiry = Block::getAutoblockExpiry( $ipblock->mTimestamp );
666 }
667
668 # Insert it
669 $ipblock->insert();
670
671 }
672
673 function getPageRenderingHash(){
674 static $hash = false;
675 if( $hash ){
676 return $hash;
677 }
678
679 // stubthreshold is only included below for completeness,
680 // it will always be 0 when this function is called by parsercache.
681
682 $confstr = $this->getOption( "quickbar" );
683 $confstr .= "!" . $this->getOption( "underline" );
684 $confstr .= "!" . $this->getOption( "hover" );
685 $confstr .= "!" . $this->getOption( "skin" );
686 $confstr .= "!" . $this->getOption( "math" );
687 $confstr .= "!" . $this->getOption( "highlightbroken" );
688 $confstr .= "!" . $this->getOption( "stubthreshold" );
689 $confstr .= "!" . $this->getOption( "editsection" );
690 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
691 $confstr .= "!" . $this->getOption( "showtoc" );
692 $confstr .= "!" . $this->getOption( "date" );
693
694 if(strlen($confstr) > 32)
695 $hash = md5($confstr);
696 else
697 $hash = $confstr;
698 return $hash;
699 }
700
701 function isAllowedToCreateAccount()
702 {
703 global $wgWhitelistAccount;
704 $allowed = false;
705
706 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
707 foreach ($wgWhitelistAccount as $right => $ok) {
708 $userHasRight = (!strcmp($right, "user") || in_array($right, $this->getRights()));
709 $allowed |= ($ok && $userHasRight);
710 }
711 return $allowed;
712 }
713
714 # Set mDataLoaded, return previous value
715 # Use this to prevent DB access in command-line scripts or similar situations
716 function setLoaded( $loaded )
717 {
718 wfSetVar( $this->mDataLoaded, $loaded );
719 }
720
721 function getUserPage() {
722 return Title::makeTitle( NS_USER, $this->mName );
723 }
724 }
725
726 ?>