r935: - Adding user.hidestatuses
[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),
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 'usedst' => array(TYPE_BOOL, REQ_NO),
55 'hidestatuses' => array(TYPE_STR, REQ_NO, ':self')
56 );
57
58 /**
59 * Database table
60 * @var string
61 * @access private
62 */
63 var $table = 'user';
64
65 /**
66 * Table prefix
67 * @var string
68 * @access private
69 */
70 var $prefix = TABLE_PREFIX;
71
72 // ###################################################################
73 /**
74 * Set field: salt
75 *
76 * @access private
77 */
78 function set_salt()
79 {
80 $this->set('salt', $this->registry->funct->rand(array(1, 15)));
81 }
82
83 // ###################################################################
84 /**
85 * Set field: authkey
86 *
87 * @access private
88 */
89 function set_authkey()
90 {
91 $this->set('authkey', $this->registry->funct->rand());
92 }
93
94 // ###################################################################
95 /**
96 * Pre-insert
97 *
98 * @access private
99 */
100 function pre_insert()
101 {
102 $this->set('password', md5(md5($this->values['password']) . md5($this->values['salt'])));
103 }
104
105 // ###################################################################
106 /**
107 * Post-insert
108 *
109 * @access protected
110 */
111 function post_insert()
112 {
113 build_assignedto();
114 }
115
116 // ###################################################################
117 /**
118 * Verify: email
119 *
120 * @access private
121 */
122 function verify_email()
123 {
124 if (!is_bool($ne = $this->verify_noempty('email')))
125 {
126 return $ne;
127 }
128
129 if (!$this->registry->funct->is_valid_email($this->values['email']))
130 {
131 return $this->registry->lang->string('The specified email is invalid.');
132 }
133 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']))
134 {
135 return $this->registry->lang->string('The specified email is already in use.');
136 }
137 return true;
138 }
139
140 // ###################################################################
141 /**
142 * Verify: displayname
143 *
144 * @access private
145 */
146 function verify_displayname()
147 {
148 if (!is_bool($ne = $this->verify_noempty('displayname')))
149 {
150 return $ne;
151 }
152
153 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']))
154 {
155 return $this->registry->lang->string('That display name is already in use by another user.');
156 }
157 return true;
158 }
159
160 // ###################################################################
161 /**
162 * Verify: usergroupid
163 *
164 * @access private
165 */
166 function verify_usergroupid()
167 {
168 if (!isset($this->registry->datastore['usergroup'][ $this->values['usergroupid'] ]))
169 {
170 return false;
171 }
172 return true;
173 }
174
175 // ###################################################################
176 /**
177 * Pre-update
178 *
179 * @access private
180 */
181 function pre_update()
182 {
183 $this->set_condition();
184 $this->fetch();
185
186 if ($this->values['password'] == '')
187 {
188 $this->set('password', $this->objdata['password']);
189 }
190 else
191 {
192 $this->registry->debug("updating password = true");
193 $this->set('password', md5(md5($this->values['password']) . md5($this->objdata['salt'])));
194 }
195 }
196
197 // ###################################################################
198 /**
199 * Post-update
200 *
201 * @access protected
202 */
203 function post_update()
204 {
205 build_assignedto();
206 }
207
208 // ###################################################################
209 /**
210 * Pre-delete
211 *
212 * @access protected
213 */
214 function pre_delete()
215 {
216 if ($this->values['userid'] == $this->registry->userinfo['userid'])
217 {
218 $this->error($lang->string('You cannot delete your own account!'));
219 }
220
221 if ($this->values['usergroupid'] == 6)
222 {
223 $count = $this->registry->db->query_first("SELECT COUNT(*) AS count FROM " . TABLE_PREFIX . "user WHERE usergroupid = 6 AND userid <> " . $this->values['userid']);
224 if ($count['count'] < 1)
225 {
226 $this->error($lang->string('At least one other administrator needs to be present before you can delete this user'));
227 }
228 }
229 }
230
231 // ###################################################################
232 /**
233 * Post-delete
234 *
235 * @todo Finish post-delete user data cleanup
236 *
237 * @access protected
238 */
239 function post_delete()
240 {
241 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "user WHERE userid = " . $this->values['userid']);
242 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $this->values['userid']);
243 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "useractivation WHERE userid = " . $this->values['userid']);
244
245 build_assignedto();
246 }
247
248 // ###################################################################
249 /**
250 * Verify: hidestatuses
251 *
252 * @access public
253 */
254 function verify_hidestatuses()
255 {
256 if (is_array($this->values['hidestatuses']))
257 {
258 $this->set('hidestatuses', implode(',', $this->values['hidestatuses']));
259 }
260
261 return true;
262 }
263 }
264
265 /*=====================================================================*\
266 || ###################################################################
267 || # $HeadURL$
268 || # $Id$
269 || ###################################################################
270 \*=====================================================================*/
271 ?>