Too many changes to all list here, biggest ones:
[specialops2.git] / lib / class.authuser.php
blob0441a3748f7fe298ec8cfb3a980113a76e519611
1 <?php
2 // $Id$
4 /* Registered user, logged in (i.e. yourself) */
5 class authuser extends reguser
7 function __construct($prefetch)
9 $fields = array('`userid`', '`alias`', '`password`', '`level`', '`timezone`', '`theme`', '`date_format`');
10 if ( $prefetch )
11 $fields = array_merge($fields, $prefetch);
13 $this->attrcache = $GLOBALS['DB']->query('SELECT '.implode(', ', $fields).'
14 FROM `users` WHERE `userid` = @userid')->fetch_assoc();
15 $this->namecache[$this->attrcache['userid']] = $this->attrcache['alias'];
17 $this->userlinks = array(
18 $this->attrcache['alias'].' ('.( $this->attrcache['level'] == LVL_DEV ? '∞' : $this->attrcache['level'] ).')'
19 => 'user?id='.$this->attrcache['userid']
22 if ( $this->attrcache['level'] >= LVL_ADMIN )
23 $this->userlinks['Admin Stuff'] = 'admin';
24 if ( $this->attrcache['password'] === null )
25 $this->userlinks['Change Password'] = 'passwd';
27 // Update user info
28 $GLOBALS['DB']->query('UPDATE `users` SET
29 `useragent` = \''.$GLOBALS['DB']->escape_string(substr($_SERVER['HTTP_USER_AGENT'], 0, 130)).'\',
30 `last_active_date` = UNIX_TIMESTAMP(),
31 `last_ip` = INET_ATON(\''.$_SERVER['REMOTE_ADDR'].'\')
32 WHERE `userid` = @userid');
35 function __get($varname)
37 if ( defined('DEVELOPER') )
38 $this->gets++;
40 if ( !array_key_exists($varname, $this->attrcache) ) {
41 if ( defined('DEVELOPER') )
42 user_error('Uncached $user::get:<pre>'.print_r(debug_backtrace(), true).'</pre>', E_USER_WARNING);
44 switch ( $varname ) {
45 case 'reg_ip':
46 case 'last_ip':
47 case 'last_login_ip':
48 $fieldname = 'INET_NTOA(`'.$varname.'`)';
49 break;
50 case 'password':
51 $fieldname = 'AES_DECRYPT(`'.$varname.'`, `reg_ip`)';
52 break;
53 default:
54 $fieldname = '`'.$varname.'`';
55 break;
58 $q = $GLOBALS['DB']->query('SELECT '.$fieldname.' AS `'.$varname.'` FROM `users` WHERE `userid` = @userid');
59 if ( $GLOBALS['DB']->error )
60 throw new OutOfBoundsException('MySQL error in authuser::__get('.$varname.'): '.$GLOBALS['DB']->error);
61 else
62 $this->attrcache[$varname] = $q->fetch_object()->$varname;
64 if ( array_key_exists($varname, $this->attrcache) ) {
65 return $this->attrcache[$varname];
66 } else
67 throw new OutOfBoundsException('Invalid attribute name $user->'.$varname);
70 function __set($varname, $value)
72 if ( defined('DEVELOPER') )
73 $this->sets++;
75 $this->attrcache[$varname] = $value;
77 switch ( $varname ) {
78 case 'reg_ip':
79 case 'last_ip':
80 case 'last_login_ip':
81 if ( strpos($value, ':') !== false ) {
82 $value = 'NULL';
83 break;
85 $value = 'INET_ATON(\''.$value.'\')';
86 break;
87 case 'password':
88 $value = 'AES_ENCRYPT(\''.$value.'\', `reg_ip`)';
89 break;
90 case 'points':
91 if ( is_int(sqrt($value)) && sqrt($value) > $this->attrcache['invites']
92 && $GLOBALS['DB']->query('SELECT COUNT(*) AS `c` FROM `invites`
93 WHERE `userid` = @userid')->fetch_object()->c < 5
94 ) {
95 $GLOBALS['DB']->query('INSERT INTO `invites` VALUES (NULL, @userid, UUID())');
96 $GLOBALS['DB']->query('UPDATE `users` SET `invites` = (`invites` + 1) WHERE `userid` = @userid');
98 default:
99 if ( is_int($value) )
100 break;
101 $value = '\''.$GLOBALS['DB']->escape_string($value).'\'';
102 break;
105 $GLOBALS['DB']->query('UPDATE `users` SET `'.$varname.'` = '.$value.' WHERE `userid` = @userid');
106 if ( $GLOBALS['DB']->error )
107 throw new UnexpectedValueException('MySQL error in authuser::__set('.$varname.' = '.$value.'): '.$GLOBALS['DB']->error);
110 // getUsernameByID()
111 function namelink($id, $name = null)
113 if ( empty($this->namecache[$id]) ) {
114 if ( $name === null )
115 $this->namecache[$id] = $GLOBALS['DB']->query('SELECT `alias` FROM `users`
116 WHERE `userid` = '.$id)->fetch_object()->alias;
117 else
118 $this->namecache[$id] = $name;
120 return '<a href="user?id='.$id.'">'.$this->namecache[$id].'</a>';
123 // furdate v62.44 OMEGA
124 function fdate($timestamp)
126 return ( $timestamp == 0 ? 'N/A' : date($this->attrcache['date_format'], $timestamp) );