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