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