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