2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright (c)2004-2009 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 (c)2004 - 2009, Blue Static
34 class UserAPI
extends BSApi
40 protected $fields = array(
41 'userid' => array(TYPE_UINT
, REQ_AUTO
),
42 'email' => array(TYPE_STR
, REQ_YES
),
43 'displayname' => array(TYPE_STR
, REQ_YES
),
44 'usergroupid' => array(TYPE_UINT
, REQ_YES
),
45 'groupids' => array(TYPE_STR
, REQ_NO
),
46 'password' => array(TYPE_STR
, REQ_YES
),
47 'salt' => array(TYPE_STR
, REQ_SET
),
48 'authkey' => array(TYPE_STR
, REQ_SET
),
49 'showemail' => array(TYPE_BOOL
, REQ_NO
),
50 'showcolors' => array(TYPE_BOOL
, REQ_NO
),
51 'languageid' => array(TYPE_UINT
, REQ_NO
),
52 'timezone' => array(TYPE_FLOAT
,REQ_NO
),
53 'usedst' => array(TYPE_BOOL
, REQ_NO
),
54 'hidestatuses' => array(TYPE_STR
, REQ_NO
),
55 'defaultsortkey' => array(TYPE_STR
, REQ_NO
),
56 'defaultsortas' => array(TYPE_STR
, REQ_NO
),
57 'columnoptions' => array(TYPE_STR
, REQ_NO
),
58 'authid' => array(TYPE_STR
, REQ_NO
)
65 protected $table = 'user';
71 protected $prefix = TABLE_PREFIX
;
76 protected function set_salt()
78 $this->set('salt', BSFunctions
::random(rand(3, 15)));
84 protected function set_authkey()
86 $this->set('authkey', BSFunctions
::random());
92 protected function pre_insert()
94 $this->set('password', md5(md5($this->values
['password']) . md5($this->values
['salt'])));
100 protected function post_insert()
103 INSERT INTO " . TABLE_PREFIX
. "useremail
104 (userid, mask, relation)
106 (" . $this->insertid
. ", 32, 0),
107 (" . $this->insertid
. ", 320, 1),
108 (" . $this->insertid
. ", 1984, 2),
109 (" . $this->insertid
. ", 64, 4),
110 (" . $this->insertid
. ", 64, 8),
111 (" . $this->insertid
. ", 256, 16
120 protected function validate_email($field)
122 if (!$this->_verifyIsNotEmpty($field))
127 if (!BSFunctions
::is_valid_email($this->values
['email']))
129 $this->_error(new FieldException(T('The specified email is invalid.'), 'email'));
132 if (BSApp
::$db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "user WHERE email = '" . BSApp
::$input->escape($this->values
['email']) . "' AND userid <> " . BSApp
::$input->clean($this->values
['userid'], TYPE_UINT
)))
134 $this->_error(new FieldException(T('The specified email is already in use.'), 'email'));
141 * Validate: displayname
143 protected function validate_displayname($field)
145 if (!$this->_verifyIsNotEmpty($field))
150 if (BSApp
::$db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "user WHERE displayname = '" . BSApp
::$input->escape($this->values
['displayname']) . "' AND userid <> " . BSApp
::$input->clean($this->values
['userid'], TYPE_UINT
)))
152 $this->_error(new FieldException(T('That display name is already in use by another user.'), 'displayname'));
159 * Validate: usergroupid
161 protected function validate_usergroupid($field)
163 if (!isset(bugdar
::$datastore['usergroup'][ $this->values
['usergroupid'] ]))
165 $this->_error(new FieldException(L_INVALID_ID
, $field));
174 protected function validate_groupids($field)
176 $groups = $this->values
['groupids'];
177 if (!is_array($groups))
179 $groups = explode(',', $this->values
['groupids']);
181 $groups = BSFunctions
::array_strip_empty($groups);
183 foreach ($groups as $group)
185 if (!isset(bugdar
::$datastore['usergroup']["$group"]))
187 $this->_error(new FieldException(L_INVALID_ID, $field));
192 $this->values['groupids'] = implode(',', $groups);
200 protected function pre_update()
202 $this->setCondition();
205 if ($this->values['password'] == '')
207 $this->set('password', $this->record['password']);
211 BSApp::debug("updating password
= true");
212 $this->set('password', md5(md5($this->values['password']) . md5($this->record['salt'])));
219 protected function post_update()
221 if (isset($this->values['displayname']))
223 $username = BSApp::$input->escape($this->values['displayname']);
224 $id = $this->values['userid'];
226 BSApp::$db->query("UPDATE
" . TABLE_PREFIX . "bug SET username
= '$username' WHERE userid = $id");
227 BSApp::$db->query("UPDATE " . TABLE_PREFIX . "bug SET lastpostbyname = '$username' WHERE lastpostby
= $id");
228 BSApp
::$db->query("UPDATE " . TABLE_PREFIX
. "bug SET hiddenlastpostbyname = '$username' WHERE hiddenlastpostby = $id");
231 if (isset($this->values['displayname']) || isset($this->values['email']))
240 protected function pre_delete()
242 if ($this->values['userid'] == bugdar::$userinfo['userid'])
245 $this->error(T('You cannot delete your own account!'));
248 if ($this->values['usergroupid'] == 6)
250 $count = BSApp::$db->queryFirst("SELECT
COUNT(*) AS count FROM
" . TABLE_PREFIX . "user WHERE usergroupid
= 6 AND userid
<> " . $this->values['userid']);
251 if ($count['count'] < 1)
253 $this->error(T('At least one other administrator needs to be present before you can delete this user'));
261 protected function post_delete()
263 BSApp::$db->query("DELETE FROM
" . TABLE_PREFIX . "user WHERE userid
= " . $this->values['userid']);
264 BSApp::$db->query("DELETE FROM
" . TABLE_PREFIX . "favorite WHERE userid
= " . $this->values['userid']);
265 BSApp::$db->query("DELETE FROM
" . TABLE_PREFIX . "useractivation WHERE userid
= " . $this->values['userid']);
266 BSApp::$db->query("DELETE FROM
" . TABLE_PREFIX . "useremail WHERE userid
= " . $this->values['userid']);
267 BSApp::$db->query("DELETE FROM
" . TABLE_PREFIX . "search WHERE userid
= " . $this->values['userid']);
273 * Validate: hidestatuses
275 protected function validate_hidestatuses($field)
277 if (is_array($this->values['hidestatuses']))
279 $this->set('hidestatuses', implode(',', $this->values['hidestatuses']));
286 * Validate: defaultsortkey
288 protected function validate_defaultsortkey($field)
290 if (!ListSorter::fetch_by_text($this->values['defaultsortkey']))
292 $this->_error(new FieldException(L_INVALID_ID, $field));
300 * Validate: defaultsortas
302 protected function validate_defaultsortas($field)
304 if (!ListSorter::fetch_as_text($this->values['defaultsortas']))
306 $this->_error(new FieldException(L_INVALID_ID, $field));
314 * Validate: columnoptions
316 protected function validate_columnoptions($field)
318 if (is_array($this->values['columnoptions']))
320 $this->set('columnoptions', serialize($this->values['columnoptions']));