2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright ©2002-2007 Blue Static
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.
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
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 \*=====================================================================*/
22 require_once ISSO
. '/Api.php';
23 require_once('./includes/functions_datastore.php');
24 require_once('./includes/class_sort.php');
30 * @copyright Copyright ©2002 - 2007, Blue Static
35 class UserAPI
extends BSApi
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
)
75 var $prefix = TABLE_PREFIX
;
77 // ###################################################################
85 $this->set('salt', BSFunctions
::random(rand(3, 15)));
88 // ###################################################################
94 function set_authkey()
96 $this->set('authkey', BSFunctions
::random());
99 // ###################################################################
105 function pre_insert()
107 $this->set('password', md5(md5($this->values
['password']) . md5($this->values
['salt'])));
110 // ###################################################################
116 function post_insert()
119 INSERT INTO " . TABLE_PREFIX
. "useremail
120 (userid, mask, relation)
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
133 // ###################################################################
139 function verify_email()
141 if (!is_bool($ne = $this->verify_noempty('email')))
146 if (!BSFunctions
::is_valid_email($this->values
['email']))
148 return T('The specified email is invalid.');
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
)))
152 return T('The specified email is already in use.');
157 // ###################################################################
159 * Verify: displayname
163 function verify_displayname()
165 if (!is_bool($ne = $this->verify_noempty('displayname')))
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
)))
172 return T('That display name is already in use by another user.');
177 // ###################################################################
179 * Verify: usergroupid
183 function verify_usergroupid()
185 if (!isset(bugdar
::$datastore['usergroup'][ $this->values
['usergroupid'] ]))
192 // ###################################################################
198 function verify_groupids()
200 $groups = $this->values
['groupids'];
201 if (!is_array($groups))
203 $groups = explode(',', $this->values
['groupids']);
205 $groups = BSFunctions
::array_strip_empty($groups);
207 foreach ($groups AS $group)
209 if (!isset(bugdar
::$datastore['usergroup']["$group"]))
215 $this->values
['groupids'] = implode(',', $groups);
220 // ###################################################################
226 function pre_update()
228 $this->setCondition();
231 if ($this->values
['password'] == '')
233 $this->set('password', $this->record
['password']);
237 $this->registry
->debug("updating password = true");
238 $this->set('password', md5(md5($this->values
['password']) . md5($this->record
['salt'])));
242 // ###################################################################
248 function post_update()
250 if (isset($this->values
['displayname']))
252 $username = $this->registry
->escape($this->values
['displayname']);
253 $id = $this->values
['userid'];
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");
260 if (isset($this->values
['displayname']) OR isset($this->values
['email']))
266 // ###################################################################
272 function pre_delete()
274 if ($this->values
['userid'] == bugdar
::$userinfo['userid'])
276 $this->error(T('You cannot delete your own account!'));
279 if ($this->values
['usergroupid'] == 6)
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)
284 $this->error(T('At least one other administrator needs to be present before you can delete this user'));
289 // ###################################################################
295 function post_delete()
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']);
306 // ###################################################################
308 * Verify: hidestatuses
312 function verify_hidestatuses()
314 if (is_array($this->values
['hidestatuses']))
316 $this->set('hidestatuses', implode(',', $this->values
['hidestatuses']));
322 // ###################################################################
324 * Verify: defaultsortkey
328 function verify_defaultsortkey()
330 if (!ListSorter
::fetch_by_text($this->values
['defaultsortkey']))
338 // ###################################################################
340 * Verify: defaultsortas
344 function verify_defaultsortas()
346 if (!ListSorter
::fetch_as_text($this->values
['defaultsortas']))
354 // ###################################################################
356 * Verify: columnoptions
360 function verify_columnoptions()
362 if (is_array($this->values
['columnoptions']))
364 $this->set('columnoptions', serialize($this->values
['columnoptions']));
370 /*=====================================================================*\
371 || ###################################################################
374 || ###################################################################
375 \*=====================================================================*/