r781: Adding user API class
[bugdar.git] / includes / api_user.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 $GLOBALS['isso:callback']->load('api', null);
23
24 /**
25 * API: User
26 *
27 * @author Iris Studios, Inc.
28 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
29 * @version $Revision$
30 * @package Bugdar
31 *
32 */
33 class UserAPI extends API
34 {
35 /**
36 * Database fields
37 * @var array
38 * @access private
39 */
40 var $fields = array(
41 'userid' => array(TYPE_UINT, REQ_AUTO, 'verify_nozero'),
42 'email' => array(TYPE_STR, REQ_YES, ':self'),
43 'displayname' => array(TYPE_STR, REQ_YES, ':self'),
44 'usergroupid' => array(TYPE_UINT, REQ_YES, ':self'),
45 'password' => array(TYPE_STR, REQ_YES),
46 'salt' => array(TYPE_STR, REQ_SET),
47 'authkey' => array(TYPE_STR, REQ_SET),
48 'showemail' => array(TYPE_BOOL, REQ_NO),
49 'showcolours' => array(TYPE_BOOL, REQ_NO),
50 'languageid' => array(TYPE_UINT, REQ_NO),
51 'timezone' => array(TYPE_INT, REQ_NO)
52 );
53
54 /**
55 * Database table
56 * @var string
57 * @access private
58 */
59 var $table = 'user';
60
61 /**
62 * Table prefix
63 * @var string
64 * @access private
65 */
66 var $prefix = TABLE_PREFIX;
67
68 // ###################################################################
69 /**
70 * Set field: salt
71 *
72 * @access private
73 */
74 function set_salt()
75 {
76 $this->set('salt', $this->registry->funct->rand(array(1, 15)));
77 }
78
79 // ###################################################################
80 /**
81 * Set field: authkey
82 *
83 * @access private
84 */
85 function set_authkey()
86 {
87 $this->set('authkey', $this->registry->funct->rand());
88 }
89
90 // ###################################################################
91 /**
92 * Pre-insert
93 *
94 * @access private
95 */
96 function pre_insert()
97 {
98 $this->set('password', md5(md5($this->values['password']) . md5($this->values['salt'])));
99 }
100
101 // ###################################################################
102 /**
103 * Verify: email
104 *
105 * @access private
106 */
107 function verify_email()
108 {
109 $this->verify_noempty('displayname');
110
111 if ($this->registry->funct->is_valid_email($this->values['email']))
112 {
113 return $this->registry->lang->string('The specified email is invalid.');
114 }
115 if ($this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email = '" . $this->registry->db->escape_string($this->values['email']) . "'"))
116 {
117 return $this->registry->lang->string('The specified email is already in use.');
118 }
119 return true;
120 }
121
122 // ###################################################################
123 /**
124 * Verify: displayname
125 *
126 * @access private
127 */
128 function verify_displayname()
129 {
130 $this->verify_noempty('displayname');
131
132 if ($this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE displayname = '" . $this->registry->db->escape_string($this->values['displayname']) . "'"))
133 {
134 return $this->registry->lang->string('That display name is already in use by another user.');
135 }
136 return true;
137 }
138
139 // ###################################################################
140 /**
141 * Verify: usergroupid
142 *
143 * @access private
144 */
145 function verify_usergroupid()
146 {
147 if (!isset($this->registry->datastore['usergroup'][ $this->values['usergroupid'] ]))
148 {
149 return false;
150 }
151 return true;
152 }
153
154 // ###################################################################
155 /**
156 * Pre-update
157 *
158 * @access private
159 */
160 function pre_update()
161 {
162 $this->set_condition();
163 $this->fetch();
164
165 if ($this->values['password'] == '')
166 {
167 $this->set('password', $this->objdata['password']);
168 }
169 else
170 {
171 $this->registry->debug("updating password = true");
172 $this->set('password', md5(md5($this->values['password']) . md5($this->objdata['salt'])));
173 }
174 }
175 }
176
177 /*=====================================================================*\
178 || ###################################################################
179 || # $HeadURL$
180 || # $Id$
181 || ###################################################################
182 \*=====================================================================*/
183 ?>