1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cApiFrontendUserCollection extends ItemCollection {
24:
25: 26: 27:
28: public function __construct() {
29: global $cfg;
30: parent::__construct($cfg['tab']['frontendusers'], 'idfrontenduser');
31: $this->_setItemClass('cApiFrontendUser');
32:
33:
34: $this->_setJoinPartner('cApiClientCollection');
35: }
36:
37: 38: 39: 40: 41: 42: 43:
44: public function userExists($sUsername) {
45: global $client;
46:
47: $feUsers = new cApiFrontendUserCollection();
48: $feUsers->setWhere('idclient', $client);
49: $feUsers->setWhere('username', strtolower($sUsername));
50: $feUsers->query();
51:
52: return ($feUsers->next()) ? true : false;
53: }
54:
55: 56: 57: 58: 59: 60: 61: 62: 63:
64: public function create($username, $password = '') {
65: global $client, $auth;
66:
67:
68: $this->select("idclient = " . (int) $client . " AND username = '" . $this->escape($username) . "'");
69:
70: if ($this->next()) {
71: return $this->create($username . '_' . substr(md5(rand()), 0, 10), $password);
72: }
73:
74: $item = $this->createNewItem();
75: $item->set('idclient', $client);
76: $item->set('username', $username);
77: $item->set('salt', md5($username . rand(1000, 9999) . rand(1000, 9999) . rand(1000, 9999)));
78: $item->set('password', $password);
79: $item->set('created', date('Y-m-d H:i:s'), false);
80: $item->set('author', $auth->auth['uid']);
81: $item->set('active', 0);
82:
83: $item->store();
84:
85:
86: $feGroups = new cApiFrontendGroupCollection();
87: $feGroups->select("idclient = " . (int) $client . " AND defaultgroup = 1");
88:
89: $feGroupMembers = new cApiFrontendGroupMemberCollection();
90:
91: $iduser = $item->get('idfrontenduser');
92:
93: while (($feGroup = $feGroups->next()) !== false) {
94: $idgroup = $feGroup->get('idfrontendgroup');
95: $feGroupMembers->create($idgroup, $iduser);
96: }
97:
98: return $item;
99: }
100:
101: 102: 103: 104: 105: 106: 107: 108:
109: public function delete($itemId) {
110:
111: $feGroupMembers = new cApiFrontendGroupMemberCollection();
112: $feGroupMembers->select('idfrontenduser = ' . (int) $itemId);
113: while (($item = $feGroupMembers->next()) !== false) {
114: $feGroupMembers->delete($item->get('idfrontendgroupmember'));
115: }
116:
117:
118: return parent::delete($itemId);
119: }
120:
121: }
122:
123: 124: 125: 126: 127: 128:
129: class cApiFrontendUser extends Item {
130:
131: 132: 133: 134: 135: 136:
137: public function __construct($mId = false) {
138: global $cfg;
139: parent::__construct($cfg['tab']['frontendusers'], 'idfrontenduser');
140: if ($mId !== false) {
141: $this->loadByPrimaryKey($mId);
142: }
143: }
144:
145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156:
157: public function setField($field, $value, $safe = true) {
158: if ($field == 'password') {
159: return parent::setField($field, hash('sha256', md5($value) . $this->get('salt')), $safe);
160: } else {
161: return parent::setField($field, $value, $safe);
162: }
163: }
164:
165: 166: 167: 168: 169: 170: 171:
172: public function setRawPassword($password) {
173: return $this->setField('password', $password);
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183:
184: public function checkPassword($password) {
185: if ($this->isLoaded() === false) {
186: return false;
187: }
188:
189: $pass = $this->get('password');
190: $salt = $this->get('salt');
191:
192: return hash('sha256', md5($password) . $salt) == $pass;
193: }
194:
195: 196: 197: 198: 199:
200: public function store() {
201: global $auth;
202:
203: $this->set('modified', date('Y-m-d H:i:s'), false);
204: $this->set('modifiedby', $auth->auth['uid']);
205: return parent::store();
206: }
207:
208: 209: 210: 211: 212: 213:
214: public function getGroupsForUser() {
215: $feGroupMembers = new cApiFrontendGroupMemberCollection();
216: $feGroupMembers->setWhere('idfrontenduser', $this->get('idfrontenduser'));
217: $feGroupMembers->query();
218:
219: $groups = array();
220: while (($feGroupMember = $feGroupMembers->next()) !== false) {
221: $groups[] = $feGroupMember->get('idfrontendgroup');
222: }
223: return $groups;
224: }
225:
226: }
227: