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 = isset($_POST['username']) ? $_POST['username'] : '';
90: $password = isset($_POST['password']) ? $_POST['password'] : '';
91: $formtimestamp = isset($_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: 177: 178: 179: 180:
181: public function logSuccessfulAuth() {
182: global $client, $lang, $saveLoginTime;
183:
184: $perm = new cPermission();
185:
186:
187: $clientLangColl = new cApiClientLanguageCollection();
188: $clientLangColl->select();
189:
190: $bFound = false;
191: while ($bFound == false) {
192: if (($item = $clientLangColl->next()) === false) {
193: break;
194: }
195:
196: $iTmpClient = $item->get('idclient');
197: $iTmpLang = $item->get('idlang');
198:
199: if ($perm->have_perm_client_lang($iTmpClient, $iTmpLang)) {
200: $client = $iTmpClient;
201: $lang = $iTmpLang;
202: $bFound = true;
203: }
204: }
205:
206: if (!is_numeric($client) || !is_numeric($lang)) {
207: return;
208: }
209:
210: $idaction = $perm->getIDForAction('login');
211:
212: $authInfo = $this->getAuthInfo();
213: $uid = $authInfo['uid'];
214:
215:
216: $actionLogCol = new cApiActionlogCollection();
217: $actionLogCol->create($uid, $client, $lang, $idaction, 0);
218:
219: $sess = cRegistry::getSession();
220: $sess->register('saveLoginTime');
221: $saveLoginTime = true;
222: }
223:
224: 225: 226: 227: 228: 229:
230: public function isLoggedIn() {
231: $authInfo = $this->getAuthInfo();
232:
233: if(isset($authInfo['uid'])) {
234: $user = new cApiUser($authInfo['uid']);
235:
236: return $user->get('user_id') != '';
237: } else {
238: return false;
239: }
240: }
241:
242: }
243: