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 cAuthHandlerBackend extends cAuthHandlerAbstract {
24:
25: 26: 27: 28: 29: 30:
31: public function __construct() {
32: $cfg = cRegistry::getConfig();
33: $this->_lifetime = (int) $cfg['backend']['timeout'];
34: if ($this->_lifetime == 0) {
35: $this->_lifetime = 15;
36: }
37: }
38:
39: 40: 41: 42: 43: 44: 45: 46:
47: public function preAuthorize() {
48: return false;
49: }
50:
51: 52: 53: 54: 55: 56: 57: 58: 59: 60:
61: public function displayLoginForm() {
62:
63:
64:
65:
66: if (isset($_REQUEST['ajax']) && $_REQUEST['ajax'] != '') {
67: $oAjax = new cAjaxRequest();
68: $sReturn = $oAjax->handle('authentication_fail');
69: echo $sReturn;
70: } else {
71: include(cRegistry::getBackendPath() . 'main.loginform.php');
72: }
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:
88: public function validateCredentials() {
89: $username = !empty($_POST['username']) ? $_POST['username'] : '';
90: $password = !empty($_POST['password']) ? $_POST['password'] : '';
91: $formtimestamp = !empty($_POST['formtimestamp']) ? $_POST['formtimestamp'] : '';
92:
93:
94: if (cRegistry::getConfigValue('simulate_magic_quotes') !== true) {
95:
96: $password = addslashes($password);
97:
98: $username = addslashes($username);
99: }
100:
101: $groupPerm = array();
102:
103: if ($password == '') {
104: return false;
105: }
106:
107: if (($formtimestamp + (60 * 15)) < time()) {
108: return false;
109: }
110:
111: if (isset($username)) {
112: $this->auth['uname'] = $username;
113: } else if ($this->_defaultNobody == true) {
114: $uid = $this->auth['uname'] = $this->auth['uid'] = self::AUTH_UID_NOBODY;
115:
116: return $uid;
117: }
118:
119: $uid = false;
120: $perm = false;
121: $pass = false;
122: $salt = false;
123:
124: $userColl = new cApiUserCollection();
125: $where = "username = '" . $username . "'";
126: $where .= " AND (valid_from <= NOW() OR valid_from = '0000-00-00 00:00:00' OR valid_from is NULL)";
127: $where .= " AND (valid_to >= NOW() OR valid_to = '0000-00-00 00:00:00' OR valid_to is NULL)";
128:
129: $maintenanceMode = getSystemProperty('maintenance', 'mode');
130: if ($maintenanceMode == 'enabled') {
131: $where .= " AND perms = 'sysadmin'";
132: }
133:
134: $userColl->select($where);
135:
136: while (($item = $userColl->next()) !== false) {
137: $uid = $item->get('user_id');
138: $perm = $item->get('perms');
139:
140: $pass = $item->get('password');
141: $salt = $item->get("salt");
142: }
143:
144: if ($uid == false || hash("sha256", md5($password) . $salt) != $pass) {
145:
146: sleep(5);
147:
148: return false;
149: }
150:
151: if ($perm != '') {
152: $groupPerm[] = $perm;
153: }
154:
155: $groupColl = new cApiGroupCollection();
156: $groups = $groupColl->fetchByUserID($uid);
157: foreach ($groups as $group) {
158: $groupPerm[] = $group->get('perms');
159: }
160:
161: $perm = implode(',', $groupPerm);
162:
163: $this->auth['perm'] = $perm;
164:
165: return $uid;
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175:
176: public function logSuccessfulAuth() {
177: global $client, $lang, $saveLoginTime;
178:
179: $perm = new cPermission();
180:
181:
182: $clientLangColl = new cApiClientLanguageCollection();
183: $clientLangColl->select();
184:
185: $bFound = false;
186: while ($bFound == false) {
187: if (($item = $clientLangColl->next()) === false) {
188: break;
189: }
190:
191: $iTmpClient = $item->get('idclient');
192: $iTmpLang = $item->get('idlang');
193:
194: if ($perm->have_perm_client_lang($iTmpClient, $iTmpLang)) {
195: $client = $iTmpClient;
196: $lang = $iTmpLang;
197: $bFound = true;
198: }
199: }
200:
201: if (!is_numeric($client) || !is_numeric($lang)) {
202: return;
203: }
204:
205: $idaction = $perm->getIDForAction('login');
206:
207: $authInfo = $this->getAuthInfo();
208: $uid = $authInfo['uid'];
209:
210:
211: $actionLogCol = new cApiActionlogCollection();
212: $actionLogCol->create($uid, $client, $lang, $idaction, 0);
213:
214: $sess = cRegistry::getSession();
215: $sess->register('saveLoginTime');
216: $saveLoginTime = true;
217: }
218:
219: 220: 221: 222: 223: 224:
225: public function isLoggedIn() {
226: $authInfo = $this->getAuthInfo();
227:
228: if(isset($authInfo['uid'])) {
229: $user = new cApiUser($authInfo['uid']);
230:
231: return $user->get('user_id') != '';
232: } else {
233: return false;
234: }
235: }
236:
237: }
238: