valign=center changes to valign=middle (HTML4.0 Spec)
[lhc/web/wiklou.git] / includes / User.php
1 <?
2 # See user.doc
3
4 class User {
5 /* private */ var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
6 /* private */ var $mRights, $mOptions;
7 /* private */ var $mDataLoaded, $mNewpassword;
8 /* private */ var $mSkin;
9 /* private */ var $mBlockedby, $mBlockreason;
10 /* private */ var $mTouched;
11 /* private */ var $mCookiePassword;
12
13 function User()
14 {
15 $this->loadDefaults();
16 }
17
18 # Static factory method
19 #
20 function newFromName( $name )
21 {
22 $u = new User();
23
24 # Clean up name according to title rules
25
26 $t = Title::newFromText( $name );
27 $u->setName( $t->getText() );
28 return $u;
29 }
30
31 /* static */ function whoIs( $id )
32 {
33 return wfGetSQL( "user", "user_name", "user_id=$id" );
34 }
35
36 /* static */ function idFromName( $name )
37 {
38 $nt = Title::newFromText( $name );
39 $sql = "SELECT user_id FROM user WHERE user_name='" .
40 wfStrencode( $nt->getText() ) . "'";
41 $res = wfQuery( $sql, "User::idFromName" );
42
43 if ( 0 == wfNumRows( $res ) ) { return 0; }
44 else {
45 $s = wfFetchObject( $res );
46 return $s->user_id;
47 }
48 }
49
50 # does the string match an anonymous user IP address?
51 /* static */ function isIP( $name ) {
52 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
53
54 }
55
56 /* static */ function randomPassword()
57 {
58 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
59 $l = strlen( $pwchars ) - 1;
60
61 wfSeedRandom();
62 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
63 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
64 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
65 $pwchars{mt_rand( 0, $l )};
66 return $np;
67 }
68
69 function loadDefaults()
70 {
71 global $wgLang ;
72
73 $this->mId = $this->mNewtalk = 0;
74 $this->mName = getenv( "REMOTE_ADDR" );
75 $this->mEmail = "";
76 $this->mPassword = $this->mNewpassword = "";
77 $this->mRights = array();
78 $defOpt = $wgLang->getDefaultUserOptions() ;
79 foreach ( $defOpt as $oname => $val ) {
80 $this->mOptions[$oname] = $val;
81 }
82 unset( $this->mSkin );
83 $this->mDataLoaded = false;
84 $this->mBlockedby = -1; # Unset
85 $this->mTouched = '0'; # Allow any pages to be cached
86 $this->cookiePassword = "";
87 }
88
89 /* private */ function getBlockedStatus()
90 {
91 if ( -1 != $this->mBlockedby ) { return; }
92
93 $remaddr = getenv( "REMOTE_ADDR" );
94 if ( 0 == $this->mId ) {
95 $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
96 "ipb_address='$remaddr'";
97 } else {
98 $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
99 "(ipb_address='$remaddr' OR ipb_user={$this->mId})";
100 }
101 $res = wfQuery( $sql, "User::getBlockedStatus" );
102 if ( 0 == wfNumRows( $res ) ) {
103 $this->mBlockedby = 0;
104 return;
105 }
106 $s = wfFetchObject( $res );
107 $this->mBlockedby = $s->ipb_by;
108 $this->mBlockreason = $s->ipb_reason;
109 }
110
111 function isBlocked()
112 {
113 $this->getBlockedStatus();
114 if ( 0 == $this->mBlockedby ) { return false; }
115 return true;
116 }
117
118 function blockedBy() {
119 $this->getBlockedStatus();
120 return $this->mBlockedby;
121 }
122
123 function blockedFor() {
124 $this->getBlockedStatus();
125 return $this->mBlockreason;
126 }
127
128 function loadFromSession()
129 {
130 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
131
132 if ( isset( $wsUserID ) ) {
133 if ( 0 != $wsUserID ) {
134 $sId = $wsUserID;
135 } else {
136 $this->mId = 0;
137 return;
138 }
139 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
140 $sId = $HTTP_COOKIE_VARS["wcUserID"];
141 $wsUserID = $sId;
142 } else {
143 $this->mId = 0;
144 return;
145 }
146 if ( isset( $wsUserName ) ) {
147 $sName = $wsUserName;
148 } else if ( isset( $HTTP_COOKIE_VARS["wcUserName"] ) ) {
149 $sName = $HTTP_COOKIE_VARS["wcUserName"];
150 $wsUserName = $sName;
151 } else {
152 $this->mId = 0;
153 return;
154 }
155
156 $passwordCorrect = FALSE;
157 $this->mId = $sId;
158 $this->loadFromDatabase();
159
160 if ( isset( $wsUserPassword ) ) {
161 $passwordCorrect = $wsUserPassword == $this->mPassword;
162 } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
163 $this->mCookiePassword = $HTTP_COOKIE_VARS["wcUserPassword"];
164 $wsUserPassword = $this->addSalt( $this->mCookiePassword );
165 $passwordCorrect = $wsUserPassword == $this->mPassword;
166 } else {
167 $this->mId = 0;
168 $this->loadDefaults(); # Can't log in from session
169 return;
170 }
171
172 if ( ( $sName == $this->mName ) && $passwordCorrect ) {
173 return;
174 }
175 $this->loadDefaults(); # Can't log in from session
176 }
177
178 function loadFromDatabase()
179 {
180 if ( $this->mDataLoaded ) { return; }
181 # check in separate table if there are changes to the talk page
182 $this->mNewtalk=0; # reset talk page status
183 if($this->mId) {
184 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
185 $res = wfQuery ($sql, "User::loadFromDatabase" );
186
187 if (wfNumRows($res)>0) {
188 $this->mNewtalk= 1;
189 }
190 wfFreeResult( $res );
191 } else {
192 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
193 $res = wfQuery ($sql, "User::loadFromDatabase" );
194
195 if (wfNumRows($res)>0) {
196 $this->mNewtalk= 1;
197 }
198 wfFreeResult( $res );
199 }
200 if(!$this->mId) {
201 $this->mDataLoaded = true;
202 return;
203 } # the following stuff is for non-anonymous users only
204
205 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
206 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
207 "{$this->mId}";
208 $res = wfQuery( $sql, "User::loadFromDatabase" );
209
210 if ( wfNumRows( $res ) > 0 ) {
211 $s = wfFetchObject( $res );
212 $this->mName = $s->user_name;
213 $this->mEmail = $s->user_email;
214 $this->mPassword = $s->user_password;
215 $this->mNewpassword = $s->user_newpassword;
216 $this->decodeOptions( $s->user_options );
217 $this->mRights = explode( ",", strtolower( $s->user_rights ) );
218 $this->mTouched = $s->user_touched;
219 }
220
221 wfFreeResult( $res );
222 $this->mDataLoaded = true;
223 }
224
225 function getID() { return $this->mId; }
226 function setID( $v ) {
227 $this->mId = $v;
228 $this->mDataLoaded = false;
229 }
230
231 function getName() {
232 $this->loadFromDatabase();
233 return $this->mName;
234 }
235
236 function setName( $str )
237 {
238 $this->loadFromDatabase();
239 $this->mName = $str;
240 }
241
242 function getNewtalk()
243 {
244 $this->loadFromDatabase();
245 return ( 0 != $this->mNewtalk );
246 }
247
248 function setNewtalk( $val )
249 {
250 $this->loadFromDatabase();
251 $this->mNewtalk = $val;
252 $this->invalidateCache();
253 }
254
255 function invalidateCache() {
256 $this->loadFromDatabase();
257 $this->mTouched = wfTimestampNow();
258 # Don't forget to save the options after this or
259 # it won't take effect!
260 }
261
262 function validateCache( $timestamp ) {
263 $this->loadFromDatabase();
264 return ($timestamp >= $this->mTouched);
265 }
266
267 function getPassword()
268 {
269 $this->loadFromDatabase();
270 return $this->mPassword;
271 }
272
273 function getNewpassword()
274 {
275 $this->loadFromDatabase();
276 return $this->mNewpassword;
277 }
278
279 function addSalt( $p )
280 {
281 return md5( "{$this->mId}-{$p}" );
282 }
283
284 function encryptPassword( $p )
285 {
286 return $this->addSalt( md5( $p ) );
287 }
288
289 function setPassword( $str )
290 {
291 $this->loadFromDatabase();
292 $this->setCookiePassword( $str );
293 $this->mPassword = $this->encryptPassword( $str );
294 $this->mNewpassword = "";
295 }
296
297 function setCookiePassword( $str )
298 {
299 $this->loadFromDatabase();
300 $this->mCookiePassword = md5( $str );
301 }
302
303 function setNewpassword( $str )
304 {
305 $this->loadFromDatabase();
306 $this->mNewpassword = $this->encryptPassword( $str );
307 }
308
309 function getEmail()
310 {
311 $this->loadFromDatabase();
312 return $this->mEmail;
313 }
314
315 function setEmail( $str )
316 {
317 $this->loadFromDatabase();
318 $this->mEmail = $str;
319 }
320
321 function getOption( $oname )
322 {
323 $this->loadFromDatabase();
324 if ( array_key_exists( $oname, $this->mOptions ) ) {
325 return $this->mOptions[$oname];
326 } else {
327 return "";
328 }
329 }
330
331 function setOption( $oname, $val )
332 {
333 $this->loadFromDatabase();
334 $this->mOptions[$oname] = $val;
335 $this->invalidateCache();
336 }
337
338 function getRights()
339 {
340 $this->loadFromDatabase();
341 return $this->mRights;
342 }
343
344 function addRight( $rname )
345 {
346 $this->loadFromDatabase();
347 array_push( $this->mRights, $rname );
348 $this->invalidateCache();
349 }
350
351 function isSysop()
352 {
353 $this->loadFromDatabase();
354 if ( 0 == $this->mId ) { return false; }
355
356 return in_array( "sysop", $this->mRights );
357 }
358
359 function isDeveloper()
360 {
361 $this->loadFromDatabase();
362 if ( 0 == $this->mId ) { return false; }
363
364 return in_array( "developer", $this->mRights );
365 }
366
367 function isBot()
368 {
369 $this->loadFromDatabase();
370 if ( 0 == $this->mId ) { return false; }
371
372 return in_array( "bot", $this->mRights );
373 }
374
375 function &getSkin()
376 {
377 if ( ! isset( $this->mSkin ) ) {
378 $skinNames = Skin::getSkinNames();
379 $s = $this->getOption( "skin" );
380 if ( "" == $s ) { $s = 0; }
381
382 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
383 else $sn = "Skin" . $skinNames[$s];
384 $this->mSkin = new $sn;
385 }
386 return $this->mSkin;
387 }
388
389 function isWatched( $title )
390 {
391 # Note - $title should be a Title _object_
392 # Pages and their talk pages are considered equivalent for watching;
393 # remember that talk namespaces are numbered as page namespace+1.
394 if( $this->mId ) {
395 $sql = "SELECT 1 FROM watchlist
396 WHERE wl_user={$this->mId} AND
397 wl_namespace = " . ($title->getNamespace() & ~1) . " AND
398 wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
399 $res = wfQuery( $sql );
400 return (wfNumRows( $res ) > 0);
401 } else {
402 return false;
403 }
404 }
405
406 function addWatch( $title )
407 {
408 if( $this->mId ) {
409 # REPLACE instead of INSERT because occasionally someone
410 # accidentally reloads a watch-add operation.
411 $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
412 VALUES ({$this->mId}," . (($title->getNamespace() | 1) - 1) .
413 ",'" . wfStrencode( $title->getDBkey() ) . "')";
414 wfQuery( $sql );
415 $this->invalidateCache();
416 }
417 }
418
419 function removeWatch( $title )
420 {
421 if( $this->mId ) {
422 $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
423 wl_namespace=" . (($title->getNamespace() | 1) - 1) .
424 " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
425 wfQuery( $sql );
426 $this->invalidateCache();
427 }
428 }
429
430
431 /* private */ function encodeOptions()
432 {
433 $a = array();
434 foreach ( $this->mOptions as $oname => $oval ) {
435 array_push( $a, "{$oname}={$oval}" );
436 }
437 $s = implode( "\n", $a );
438 return wfStrencode( $s );
439 }
440
441 /* private */ function decodeOptions( $str )
442 {
443 $a = explode( "\n", $str );
444 foreach ( $a as $s ) {
445 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
446 $this->mOptions[$m[1]] = $m[2];
447 }
448 }
449 }
450
451 function setCookies()
452 {
453 global $wsUserID, $wsUserName, $wsUserPassword;
454 global $wgCookieExpiration;
455 if ( 0 == $this->mId ) return;
456 $this->loadFromDatabase();
457 $exp = time() + $wgCookieExpiration;
458
459 $wsUserID = $this->mId;
460 setcookie( "wcUserID", $this->mId, $exp, "/" );
461
462 $wsUserName = $this->mName;
463 setcookie( "wcUserName", $this->mName, $exp, "/" );
464
465 $wsUserPassword = $this->mPassword;
466 if ( 1 == $this->getOption( "rememberpassword" ) ) {
467 setcookie( "wcUserPassword", $this->mCookiePassword, $exp, "/" );
468 } else {
469 setcookie( "wcUserPassword", "", time() - 3600 );
470 }
471 }
472
473 function logout()
474 {
475 global $wsUserID;
476 $this->mId = 0;
477
478 $wsUserID = 0;
479
480 setcookie( "wcUserID", "", time() - 3600 );
481 setcookie( "wcUserPassword", "", time() - 3600 );
482 }
483
484 function saveSettings()
485 {
486 global $wgUser;
487
488 if ( ! $this->mNewtalk ) {
489 if( $this->mId ) {
490 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
491 wfQuery ($sql,"User::saveSettings");
492 } else {
493 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
494 wfQuery ($sql,"User::saveSettings");
495 }
496 }
497 if ( 0 == $this->mId ) { return; }
498
499 $sql = "UPDATE user SET " .
500 "user_name= '" . wfStrencode( $this->mName ) . "', " .
501 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
502 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
503 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
504 "user_options= '" . $this->encodeOptions() . "', " .
505 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', "
506 .
507 "user_touched= '" . wfStrencode( $this->mTouched ) .
508 "' WHERE user_id={$this->mId}";
509 wfQuery( $sql, "User::saveSettings" );
510 }
511
512 # Checks if a user with the given name exists
513 #
514 function idForName()
515 {
516 $gotid = 0;
517 $s = trim( $this->mName );
518 if ( 0 == strcmp( "", $s ) ) return 0;
519
520 $sql = "SELECT user_id FROM user WHERE user_name='" .
521 wfStrencode( $s ) . "'";
522 $res = wfQuery( $sql, "User::idForName" );
523 if ( 0 == wfNumRows( $res ) ) { return 0; }
524
525 $s = wfFetchObject( $res );
526 if ( "" == $s ) return 0;
527
528 $gotid = $s->user_id;
529 wfFreeResult( $res );
530 return $gotid;
531 }
532
533 function addToDatabase()
534 {
535 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
536 "user_email, user_rights, user_options) " .
537 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
538 wfStrencode( $this->mPassword ) . "', '" .
539 wfStrencode( $this->mNewpassword ) . "', '" .
540 wfStrencode( $this->mEmail ) . "', '" .
541 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
542 $this->encodeOptions() . "')";
543 wfQuery( $sql, "User::addToDatabase" );
544 $this->mId = $this->idForName();
545 }
546 }
547
548 ?>