* Delete `users`.`admin`, use `things` for it instead
[specialops2.git] / lib / class.User_Registered.php
blobcb7ba080a6b01ed95bd10455874b1c30b0fbc817
1 <?php
2 /**
3 * Class for a registered user, but not the one viewing the page.
5 * @author Anthony Parsons (xmpp:ant@specialops.ath.cx)
6 * @license file://../COPYING
7 * @version $Id$
8 */
9 class User_Registered extends User_Anonymous
11 function __construct($userid, $prefetch = null)
13 global $DB;
15 $this->attrcache = array();
17 $fields = array('`userid`', '`alias`', '`timezone`', '`theme`');
18 if ( $prefetch ) {
19 $fields = array_merge($fields, $prefetch);
22 $this->attrcache = $DB->query('SELECT '.implode(', ', $fields).' FROM `users` WHERE `userid` = '.$userid)->fetch_assoc();
23 $this->namecache[$userid] = $this->alias;
26 function __get($varname)
28 global $DB;
30 if ( !array_key_exists($varname, $this->attrcache) ) {
31 switch ( $varname ) {
32 case 'reg_ip':
33 case 'last_ip':
34 case 'last_login_ip':
35 $column = "INET_NTOA(`$varname`)"; break;
36 case 'password':
37 $column = "AES_DECRYPT(`$varname`, `reg_ip`)"; break;
38 case 'posts':
39 list($this->attrcache['posts']) =
40 $DB->query('SELECT COUNT(*) FROM `messages` WHERE `userid` = '.$this->attrcache['userid'])->fetch_row();
41 return $this->attrcache['posts'];
42 case 'admin':
43 list($this->attrcache['admin']) =
44 $DB->query('SELECT COUNT(*) FROM `things`
45 WHERE `what` = "admin" AND `userid` = '.$this->attrcache['userid'])->fetch_row();
46 return $this->attrcache['admin'];
47 default:
48 $column = "`$varname`";
51 $tmp = $DB->query('SELECT '.$column.' AS `'.$varname.'` FROM `users` WHERE `userid` = '.$this->attrcache['userid']);
53 if ( $DB->error ) {
54 throw new OutOfBoundsException('MySQL error in reguser::_get('.$varname.'): '.$DB->error);
55 } else {
56 list($this->attrcache[$varname]) = $tmp->fetch_row();
60 if ( array_key_exists($varname, $this->attrcache) ) {
61 return $this->attrcache[$varname];
62 } else {
63 throw new OutOfBoundsException('Invalid attribute name '.$varname);
67 function __set($varname, $value)
69 global $DB;
71 $this->attrcache[$varname] = $value;
73 switch ( $varname ) {
74 case 'reg_ip':
75 case 'last_ip':
76 case 'last_login_ip':
77 if ( strpos($value, ':') !== false ) { // IPv6
78 $value = 'NULL';
79 break;
81 $value = 'INET_ATON("'.$DB->string($value).'")';
82 break;
83 case 'password':
84 $value = 'AES_ENCRYPT("'.$DB->string($value).'", `reg_ip`)';
85 break;
86 case 'points':
87 if ( is_int(sqrt($value)) && sqrt($value) > $this->attrcache['invites'] &&
88 $DB->query('SELECT COUNT(*) AS `c` FROM `things`
89 WHERE `what` = "invite"
90 AND `userid` = '.$this->userid)->fetch_object()->c < 5 ) {
91 $DB->query('INSERT INTO `things` VALUES (NULL, '.$this->attrcache['userid'].', "invite", UUID())');
92 $DB->query('UPDATE `users` SET `invites` = (`invites` + 1) WHERE `userid` = '.$this->attrcache['userid']);
94 break;
95 case 'options':
96 if ( is_array($value) ) {
97 $value = implode(',', $value);
99 $value = $DB->string($value);
100 break;
101 default:
102 if ( is_string($value) ) {
103 $value = $DB->string($value);
107 $DB->query('UPDATE `users` SET `'.$varname.'` = '.$value.' WHERE `userid` = '.$this->userid);
108 if ( $DB->error ) {
109 throw new UnexpectedValueException('MySQL error in reguser::_set('.$varname.' = '.$value.'): '.$DB->error);
113 function __toString()
115 return $this->alias;