Small changes to index.php, moved the page time to the footer (all pages), and hopefu...
[specialops2.git] / lib / class.User_Registered.php
blobe746632f0171c7599940d14358d8a6db3ac1755a
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 // Only add an invite if the points are going up, sqrt(points) > total given invites and user has < 5 already
88 if ( $value > $this->points && sqrt($value) > $this->invites &&
89 $DB->query('SELECT COUNT(*) AS `c` FROM `things`
90 WHERE `what` = "invite"
91 AND `userid` = '.$this->userid)->fetch_object()->c < 5 ) {
92 $DB->query('INSERT INTO `things` VALUES (NULL, '.$this->attrcache['userid'].', "invite", UUID())');
93 $DB->query('UPDATE `users` SET `invites` = (`invites` + 1) WHERE `userid` = '.$this->attrcache['userid']);
96 $DB->query('UPDATE `users` SET `points` = '.intval($value).' WHERE `userid` = '.$this->attrcache['userid']);
97 break;
98 case 'options':
99 if ( is_array($value) ) {
100 $value = implode(',', $value);
102 $value = $DB->string($value);
103 break;
104 default:
105 if ( is_string($value) ) {
106 $value = $DB->string($value);
110 $DB->query('UPDATE `users` SET `'.$varname.'` = '.$value.' WHERE `userid` = '.$this->userid);
111 if ( $DB->error ) {
112 throw new UnexpectedValueException('MySQL error in reguser::_set('.$varname.' = '.$value.'): '.$DB->error);
116 function __toString()
118 return $this->alias;