r899: Adding lots of pre and post processing methods for UserAPI to make all the...
[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 require_once('./includes/functions_datastore.php');
25
26 /**
27 * API: User
28 *
29 * @author Iris Studios, Inc.
30 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
31 * @version $Revision$
32 * @package Bugdar
33 *
34 */
35 class UserAPI extends API
36 {
37 /**
38 * Database fields
39 * @var array
40 * @access private
41 */
42 var $fields = array(
43 'userid' => array(TYPE_UINT, REQ_AUTO, 'verify_nozero'),
44 'email' => array(TYPE_STR, REQ_YES, ':self'),
45 'displayname' => array(TYPE_STR, REQ_YES, ':self'),
46 'usergroupid' => array(TYPE_UINT, REQ_YES, ':self'),
47 'password' => array(TYPE_STR, REQ_YES, 'verify_noempty'),
48 'salt' => array(TYPE_STR, REQ_SET),
49 'authkey' => array(TYPE_STR, REQ_SET),
50 'showemail' => array(TYPE_BOOL, REQ_NO),
51 'showcolours' => array(TYPE_BOOL, REQ_NO),
52 'languageid' => array(TYPE_UINT, REQ_NO),
53 'timezone' => array(TYPE_INT, REQ_NO)
54 );
55
56 /**
57 * Database table
58 * @var string
59 * @access private
60 */
61 var $table = 'user';
62
63 /**
64 * Table prefix
65 * @var string
66 * @access private
67 */
68 var $prefix = TABLE_PREFIX;
69
70 // ###################################################################
71 /**
72 * Set field: salt
73 *
74 * @access private
75 */
76 function set_salt()
77 {
78 $this->set('salt', $this->registry->funct->rand(array(1, 15)));
79 }
80
81 // ###################################################################
82 /**
83 * Set field: authkey
84 *
85 * @access private
86 */
87 function set_authkey()
88 {
89 $this->set('authkey', $this->registry->funct->rand());
90 }
91
92 // ###################################################################
93 /**
94 * Pre-insert
95 *
96 * @access private
97 */
98 function pre_insert()
99 {
100 $this->set('password', md5(md5($this->values['password']) . md5($this->values['salt'])));
101 }
102
103 // ###################################################################
104 /**
105 * Post-insert
106 *
107 * @access protected
108 */
109 function post_insert()
110 {
111 build_assignedto();
112 }
113
114 // ###################################################################
115 /**
116 * Verify: email
117 *
118 * @access private
119 */
120 function verify_email()
121 {
122 $this->verify_noempty('displayname');
123
124 if ($this->registry->funct->is_valid_email($this->values['email']))
125 {
126 return $this->registry->lang->string('The specified email is invalid.');
127 }
128 if ($this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email = '" . $this->registry->db->escape_string($this->values['email']) . "' AND userid <> " . $this->values['userid']))
129 {
130 return $this->registry->lang->string('The specified email is already in use.');
131 }
132 return true;
133 }
134
135 // ###################################################################
136 /**
137 * Verify: displayname
138 *
139 * @access private
140 */
141 function verify_displayname()
142 {
143 $this->verify_noempty('displayname');
144
145 if ($this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE displayname = '" . $this->registry->db->escape_string($this->values['displayname']) . "' AND userid <> " . $this->values['userid']))
146 {
147 return $this->registry->lang->string('That display name is already in use by another user.');
148 }
149 return true;
150 }
151
152 // ###################################################################
153 /**
154 * Verify: usergroupid
155 *
156 * @access private
157 */
158 function verify_usergroupid()
159 {
160 if (!isset($this->registry->datastore['usergroup'][ $this->values['usergroupid'] ]))
161 {
162 return false;
163 }
164 return true;
165 }
166
167 // ###################################################################
168 /**
169 * Pre-update
170 *
171 * @access private
172 */
173 function pre_update()
174 {
175 $this->set_condition();
176 $this->fetch();
177
178 if ($this->values['password'] == '')
179 {
180 $this->set('password', $this->objdata['password']);
181 }
182 else
183 {
184 $this->registry->debug("updating password = true");
185 $this->set('password', md5(md5($this->values['password']) . md5($this->objdata['salt'])));
186 }
187 }
188
189 // ###################################################################
190 /**
191 * Post-update
192 *
193 * @access protected
194 */
195 function post_update()
196 {
197 build_assignedto();
198 }
199
200 // ###################################################################
201 /**
202 * Pre-delete
203 *
204 * @access protected
205 */
206 function pre_delete()
207 {
208 if ($this->values['userid'] == $this->registry->userinfo['userid'])
209 {
210 $this->error($lang->string('You cannot delete your own account!'));
211 }
212
213 if ($this->values['usergroupid'] == 6)
214 {
215 $count = $this->registry->db->query_first("SELECT COUNT(*) AS count FROM " . TABLE_PREFIX . "user WHERE usergroupid = 6 AND userid <> " . $this->values['userid']);
216 if ($count['count'] < 1)
217 {
218 $this->error($lang->string('At least one other administrator needs to be present before you can delete this user'));
219 }
220 }
221 }
222
223 // ###################################################################
224 /**
225 * Post-delete
226 *
227 * @access protected
228 */
229 function post_delete()
230 {
231 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "user WHERE userid = " . $this->values['userid']);
232 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $this->values['userid']);
233 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "useractivation WHERE userid = " . $this->values['userid']);
234
235 build_assignedto();
236 }
237 }
238
239 /*=====================================================================*\
240 || ###################################################################
241 || # $HeadURL$
242 || # $Id$
243 || ###################################################################
244 \*=====================================================================*/
245 ?>