- Get the email system to work for register.php
[bugdar.git] / includes / api_user.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 Blue Static
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 2 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 require_once ISSO . '/Api.php';
23 require_once('./includes/functions_datastore.php');
24 require_once('./includes/class_sort.php');
25
26 /**
27 * API: User
28 *
29 * @author Blue Static
30 * @copyright Copyright ©2002 - 2007, Blue Static
31 * @version $Revision$
32 * @package Bugdar
33 *
34 */
35 class UserAPI extends BSApi
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 'groupids' => array(TYPE_STR, REQ_NO, ':self'),
48 'password' => array(TYPE_STR, REQ_YES),
49 'salt' => array(TYPE_STR, REQ_SET),
50 'authkey' => array(TYPE_STR, REQ_SET),
51 'showemail' => array(TYPE_BOOL, REQ_NO),
52 'showcolors' => array(TYPE_BOOL, REQ_NO),
53 'languageid' => array(TYPE_UINT, REQ_NO),
54 'timezone' => array(TYPE_FLOAT,REQ_NO),
55 'usedst' => array(TYPE_BOOL, REQ_NO),
56 'hidestatuses' => array(TYPE_STR, REQ_NO, ':self'),
57 'defaultsortkey' => array(TYPE_STR, REQ_NO, ':self'),
58 'defaultsortas' => array(TYPE_STR, REQ_NO, ':self'),
59 'columnoptions' => array(TYPE_STR, REQ_NO, ':self'),
60 'authid' => array(TYPE_STR, REQ_NO)
61 );
62
63 /**
64 * Database table
65 * @var string
66 * @access private
67 */
68 var $table = 'user';
69
70 /**
71 * Table prefix
72 * @var string
73 * @access private
74 */
75 var $prefix = TABLE_PREFIX;
76
77 // ###################################################################
78 /**
79 * Set field: salt
80 *
81 * @access private
82 */
83 function set_salt()
84 {
85 $this->set('salt', BSFunctions::random(rand(3, 15)));
86 }
87
88 // ###################################################################
89 /**
90 * Set field: authkey
91 *
92 * @access private
93 */
94 function set_authkey()
95 {
96 $this->set('authkey', BSFunctions::random());
97 }
98
99 // ###################################################################
100 /**
101 * Pre-insert
102 *
103 * @access private
104 */
105 function pre_insert()
106 {
107 $this->set('password', md5(md5($this->values['password']) . md5($this->values['salt'])));
108 }
109
110 // ###################################################################
111 /**
112 * Post-insert
113 *
114 * @access protected
115 */
116 function post_insert()
117 {
118 BSApp::$db->query("
119 INSERT INTO " . TABLE_PREFIX . "useremail
120 (userid, mask, relation)
121 VALUES
122 (" . $this->insertid . ", 32, 0),
123 (" . $this->insertid . ", 320, 1),
124 (" . $this->insertid . ", 1984, 2),
125 (" . $this->insertid . ", 64, 4),
126 (" . $this->insertid . ", 64, 8),
127 (" . $this->insertid . ", 256, 16
128 )
129 ");
130 build_assignedto();
131 }
132
133 // ###################################################################
134 /**
135 * Verify: email
136 *
137 * @access private
138 */
139 function verify_email()
140 {
141 if (!is_bool($ne = $this->verify_noempty('email')))
142 {
143 return $ne;
144 }
145
146 if (!BSFunctions::is_valid_email($this->values['email']))
147 {
148 return T('The specified email is invalid.');
149 }
150 if ($this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email = '" . $this->registry->db->escape_string($this->values['email']) . "' AND userid <> " . $this->registry->clean($this->values['userid'], TYPE_UINT)))
151 {
152 return T('The specified email is already in use.');
153 }
154 return true;
155 }
156
157 // ###################################################################
158 /**
159 * Verify: displayname
160 *
161 * @access private
162 */
163 function verify_displayname()
164 {
165 if (!is_bool($ne = $this->verify_noempty('displayname')))
166 {
167 return $ne;
168 }
169
170 if ($this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE displayname = '" . $this->registry->db->escape_string($this->values['displayname']) . "' AND userid <> " . $this->registry->clean($this->values['userid'], TYPE_UINT)))
171 {
172 return T('That display name is already in use by another user.');
173 }
174 return true;
175 }
176
177 // ###################################################################
178 /**
179 * Verify: usergroupid
180 *
181 * @access private
182 */
183 function verify_usergroupid()
184 {
185 if (!isset(bugdar::$datastore['usergroup'][ $this->values['usergroupid'] ]))
186 {
187 return false;
188 }
189 return true;
190 }
191
192 // ###################################################################
193 /**
194 * Verify: groupids
195 *
196 * @access private
197 */
198 function verify_groupids()
199 {
200 $groups = $this->values['groupids'];
201 if (!is_array($groups))
202 {
203 $groups = explode(',', $this->values['groupids']);
204 }
205 $groups = BSFunctions::array_strip_empty($groups);
206
207 foreach ($groups AS $group)
208 {
209 if (!isset(bugdar::$datastore['usergroup']["$group"]))
210 {
211 return false;
212 }
213 }
214
215 $this->values['groupids'] = implode(',', $groups);
216
217 return true;
218 }
219
220 // ###################################################################
221 /**
222 * Pre-update
223 *
224 * @access private
225 */
226 function pre_update()
227 {
228 $this->set_condition();
229 $this->fetch();
230
231 if ($this->values['password'] == '')
232 {
233 $this->set('password', $this->record['password']);
234 }
235 else
236 {
237 $this->registry->debug("updating password = true");
238 $this->set('password', md5(md5($this->values['password']) . md5($this->record['salt'])));
239 }
240 }
241
242 // ###################################################################
243 /**
244 * Post-update
245 *
246 * @access protected
247 */
248 function post_update()
249 {
250 if (isset($this->values['displayname']))
251 {
252 $username = $this->registry->escape($this->values['displayname']);
253 $id = $this->values['userid'];
254
255 $this->registry->db->query("UPDATE " . TABLE_PREFIX . "bug SET username = '$username' WHERE userid = $id");
256 $this->registry->db->query("UPDATE " . TABLE_PREFIX . "bug SET lastpostbyname = '$username' WHERE lastpostby = $id");
257 $this->registry->db->query("UPDATE " . TABLE_PREFIX . "bug SET hiddenlastpostbyname = '$username' WHERE hiddenlastpostby = $id");
258 }
259
260 if (isset($this->values['displayname']) OR isset($this->values['email']))
261 {
262 build_assignedto();
263 }
264 }
265
266 // ###################################################################
267 /**
268 * Pre-delete
269 *
270 * @access protected
271 */
272 function pre_delete()
273 {
274 if ($this->values['userid'] == bugdar::$userinfo['userid'])
275 {
276 $this->error(T('You cannot delete your own account!'));
277 }
278
279 if ($this->values['usergroupid'] == 6)
280 {
281 $count = $this->registry->db->query_first("SELECT COUNT(*) AS count FROM " . TABLE_PREFIX . "user WHERE usergroupid = 6 AND userid <> " . $this->values['userid']);
282 if ($count['count'] < 1)
283 {
284 $this->error(T('At least one other administrator needs to be present before you can delete this user'));
285 }
286 }
287 }
288
289 // ###################################################################
290 /**
291 * Post-delete
292 *
293 * @access protected
294 */
295 function post_delete()
296 {
297 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "user WHERE userid = " . $this->values['userid']);
298 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "favorite WHERE userid = " . $this->values['userid']);
299 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "useractivation WHERE userid = " . $this->values['userid']);
300 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "useremail WHERE userid = " . $this->values['userid']);
301 $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = " . $this->values['userid']);
302
303 build_assignedto();
304 }
305
306 // ###################################################################
307 /**
308 * Verify: hidestatuses
309 *
310 * @access private
311 */
312 function verify_hidestatuses()
313 {
314 if (is_array($this->values['hidestatuses']))
315 {
316 $this->set('hidestatuses', implode(',', $this->values['hidestatuses']));
317 }
318
319 return true;
320 }
321
322 // ###################################################################
323 /**
324 * Verify: defaultsortkey
325 *
326 * @access private
327 */
328 function verify_defaultsortkey()
329 {
330 if (!ListSorter::fetch_by_text($this->values['defaultsortkey']))
331 {
332 return false;
333 }
334
335 return true;
336 }
337
338 // ###################################################################
339 /**
340 * Verify: defaultsortas
341 *
342 * @access private
343 */
344 function verify_defaultsortas()
345 {
346 if (!ListSorter::fetch_as_text($this->values['defaultsortas']))
347 {
348 return false;
349 }
350
351 return true;
352 }
353
354 // ###################################################################
355 /**
356 * Verify: columnoptions
357 *
358 * @access private
359 */
360 function verify_columnoptions()
361 {
362 if (is_array($this->values['columnoptions']))
363 {
364 $this->set('columnoptions', serialize($this->values['columnoptions']));
365 }
366 return true;
367 }
368 }
369
370 /*=====================================================================*\
371 || ###################################################################
372 || # $HeadURL$
373 || # $Id$
374 || ###################################################################
375 \*=====================================================================*/
376 ?>