3 require_once 'Pman.php';
9 * User class must provide the following features.
15 * active() -- is user active. // or set prior to checking..
16 * authUserName(n) - sets the value prior to a find(true)
17 * checkPassword($_REQUEST['password'])) {
19 * lang(val) - to set the language..
24 class Pman_Login extends Pman
26 var $masterTemplate = 'login.html';
28 var $ip_management = false;
30 var $event_suffix = '';
33 function getAuth() // everyone allowed in here..
35 parent::getAuth(); // load company..
37 $ff = HTML_FlexyFramework::get();
39 $this->ip_management = (empty($ff->Pman['ip_management'])) ? false : true;
49 function get($v, $opts=array())
51 $this->initErrorHandling();
53 // DB_DataObject::DebugLevel(5);
54 if (!empty($_REQUEST['logout'])) {
55 return $this->logout();
59 if (!empty($_REQUEST['getAuthUser'])) {
60 //DB_Dataobject::debugLevel(5);
61 $this->sendAuthUserDetails();
65 if(!empty($_REQUEST['check_owner_company'])) {
66 $core_company = DB_DataObject::factory('core_company');
67 $core_company->comptype = 'OWNER';
68 $this->jok($core_company->count());
71 // might be an idea to disable this?!?
72 if (!empty($_REQUEST['username'])) {
77 if (!empty($_REQUEST['switch'])) {
78 $this->switchUser($_REQUEST['switch']);
81 if (!empty($_REQUEST['loginPublic'])) {
82 $this->switchPublicUser($_REQUEST['loginPublic']);
84 if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('/^check_http/', $_SERVER['HTTP_USER_AGENT'])) {
85 die("server is alive = authFailure"); // should really use heartbeat now..
87 $this->jerror("NOTICE-INVALID", "INVALID REQUEST");
94 $ff = class_exists('HTML_FlexyFramework2') ? HTML_FlexyFramework2::get() : HTML_FlexyFramework::get();
96 //DB_DAtaObject::debugLevel(1);
97 $u = $this->getAuthUser();
101 $this->addEvent('LOGOUT'. $this->event_suffix);
102 $e = DB_DataObject::factory('Events');
106 session_regenerate_id(true);
109 if(!empty($ff->Pman['local_autoauth']) && !empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'localhost') {
110 $this->jerr("you are using local autoauth!?");
112 //echo '<PRE>';print_R($_SESSION);
113 $this->jok("Logged out - user ");
117 //$_SESSION['Pman_I18N'] = array(); <<
118 session_regenerate_id(true);
121 $this->jok("Logged out - no user");
125 function sendAuthUserDetails()
127 // remove for normal use - it's a secuirty hole!
128 //DB_DataObject::debugLevel(1);
129 if (!empty($_REQUEST['_debug'])) {
130 // DB_DataObject::debugLevel(1);
133 $ff = HTML_FlexyFramework::get();
134 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
136 $u = DB_DataObject::factory($tbl);
137 $s = DB_DataObject::factory('core_setting');
138 $require_oath_val = 1;
139 $require_oath = $s->lookup('core', 'two_factor_auth_required');
140 if(!empty($require_oath)) {
141 if($require_oath->val == 0) {
142 $require_oath_val = 0;
154 $au = $u->getAuthUser();
156 // might occur on shared systems.
157 $ff= HTML_FlexyFramework::get();
159 if (!empty($ff->Pman['auth_comptype']) && $au->id > 0 &&
160 ($ff->Pman['auth_comptype'] != $au->company()->comptype)) {
162 $this->jerr("Login not permited to outside companies - please reload");
165 //$au = $u->getAuthUser();
167 $aur = $au ? $au->authUserArray() : array();
169 /** -- these need modulizing somehow! **/
173 // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
174 //echo '<PRE>'; print_r($this->modules());
175 // technically each module should only add properties to an array named after that module..
177 foreach($this->modules() as $m) {
181 if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
184 $cls = 'Pman_'.$m.'_Login';
185 require_once 'Pman/'.$m.'/Login.php';
188 $aur = $x->sendAuthUserDetails($aur);
193 // echo '<PRE>';print_r($aur);
201 function switchUser($id)
203 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
204 $u = DB_DataObject::factory($tbl);
206 $this->err("not logged in");
209 $au = $u->getAuthUser();
211 // first check they have perms to do this..
212 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
213 $this->jerr("User switching not permitted");
216 $u = DB_DataObject::factory($tbl);
219 $this->jerr('Account disabled');
222 // we might need this later..
223 $this->addEvent("LOGIN-SWITCH-USER". $this->event_suffix, false, $au->name . ' TO ' . $u->name);
224 $this->jok("SWITCH");
228 function switchPublicUser($id)
230 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
232 $u = DB_DataObject::factory($tbl);
236 $this->jerr('Account disabled');
239 if(!$u->loginPublic()){
240 $this->jerr('Switch fail');
250 //DB_DataObject::debugLevel(1);
252 if (!empty($_REQUEST['getAuthUser'])) {
253 $this->sendAuthUserDetails();
257 if (!empty($_REQUEST['logout'])) {
258 return $this->logout();
261 if(!empty($_REQUEST['check_owner_company'])) {
262 $core_company = DB_DataObject::factory('core_company');
263 $core_company->comptype = 'OWNER';
264 $this->jok($core_company->count());
267 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
268 return $this->passwordRequest($_REQUEST['passwordRequest']);
271 if (!empty($_REQUEST['ResetPassword'])) {
272 if (empty($_REQUEST['id']) ||
273 empty($_REQUEST['ts']) ||
274 empty($_REQUEST['key']) ||
275 empty($_REQUEST['password1']) ||
276 empty($_REQUEST['password2']) ||
277 ($_REQUEST['password1'] != $_REQUEST['password2'])
279 $this->jerr("Invalid request to reset password");
282 $this->resetPassword($_REQUEST['id'], $_REQUEST['ts'], $_REQUEST['key'], $_REQUEST['password1'] );
286 if (!empty($_REQUEST['_verifyCheckSum'])) {
287 if (empty($_REQUEST['id']) ||
288 empty($_REQUEST['ts']) ||
289 empty($_REQUEST['key'])
292 $this->jerr("Invalid request to reset password");
295 $this->verifyResetPassword($_REQUEST['id'], $_REQUEST['ts'], $_REQUEST['key']);
296 $this->jok("Checksum is ok");
299 // this is 'classic' change password...
300 if (!empty($_REQUEST['changePassword'])) {
301 return $this->changePassword($_REQUEST);
306 $ff = HTML_FlexyFramework::get();
307 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
310 $u = DB_DataObject::factory($tbl);
312 $ip = $this->ip_lookup();
315 //DB_DataObject::DebugLevel(1);
316 $e = DB_DataObject::Factory('Events');
317 $e->action = 'LOGIN-BAD'. $this->event_suffix;
319 $e->whereAdd('event_when > NOW() - INTERVAL 10 MINUTE');
320 if ($e->count() > 5) {
321 $this->jerror('LOGIN-RATE'. $this->event_suffix, "Login failures are rate limited - please try later");
325 // this was removed before - not quite sure why.
326 // when a duplicate login account is created, this stops the old one from interfering..
329 // empty username = not really a hacking attempt.
331 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
332 $this->jerror('LOGIN-EMPTY'. $this->event_suffix, 'You typed the wrong Username or Password (0)');
336 $u->authUserName($_REQUEST['username']);
338 if ($u->count() > 1 || !$u->find(true)) {
339 $this->jerror('LOGIN-BAD'. $this->event_suffix,'You typed the wrong Username or Password (1)');
344 $this->jerror('LOGIN-BAD'. $this->event_suffix,'Account disabled');
347 if(!empty($u->oath_key) && empty($_REQUEST['oath_password'])){
348 $this->jerror('LOGIN-2FA'. $this->event_suffix,'Your account requires Two-Factor Authentication');
351 // check if config allows non-owner passwords.
352 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
353 // perhaps it should support arrays..
354 $ff= HTML_FlexyFramework::get();
355 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
356 //print_r($u->company());
357 $this->jerror('LOGIN-BADUSER'. $this->event_suffix, "Login not permited to outside companies"); // serious failure
361 // note we trim \x10 -- line break - as it was injected the front end
362 // may have an old bug on safari/chrome that added that character in certian wierd scenarios..
363 if (!$u->checkPassword(trim($_REQUEST['password'],"\x10"))) {
364 $this->jerror('LOGIN-BAD'. $this->event_suffix, 'You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
369 !empty($u->oath_key) &&
371 empty($_REQUEST['oath_password']) ||
372 !$u->checkTwoFactorAuthentication($_REQUEST['oath_password'])
375 $this->jerror('LOGIN-BAD'. $this->event_suffix, 'You typed the wrong Username or Password (3)');
379 $this->ip_checking();
382 // we might need this later..
383 $this->addEvent("LOGIN". $this->event_suffix, false, session_id());
387 if (!empty($_REQUEST['lang'])) {
389 if (!empty($ff->languages['avail']) && !in_array($_REQUEST['lang'],$ff->languages['avail'])) {
393 $u->lang($_REQUEST['lang']);
398 $this->sendAuthUserDetails();
404 function passwordRequest($n)
406 $u = DB_DataObject::factory('core_person');
407 //$u->company_id = $this->company->id;
409 $u->whereAdd('LENGTH(passwd) > 1');
412 if ($u->count() > 1 || !$u->find(true)) {
413 $this->jerr('invalid User (1)');
415 // got a avlid user..
416 if (!strlen($u->passwd)) {
417 $this->jerr('invalid User (2)');
419 // check to see if we have sent a request before..
421 if ($u->no_reset_sent > 3) {
422 $this->jerr('We have issued to many resets - please contact the Administrator');
429 $cm = DB_DataObject::factory('core_email');
430 if (!$cm->get('name', 'ADMIN_PASSWORD_RESET')) {
431 $this->jerr("no template Admin password reset (ADMIN_PASSWORD_RESET) exists - please run importer ");
434 $this->jerr("template for Admin password reset has been disabled");
438 $g = DB_DAtaObject::factory('Groups');
439 if (!$g->get('name', 'system-email-from')) {
440 $this->jerr("no group 'system-email-from' exists in the system");
442 $from_ar = $g->members();
443 if (count($from_ar) != 1) {
444 $this->jerr(count($from_ar) ? "To many members in the 'system-email-from' group " :
445 "'system-email-from' group does not have any members");
452 $g = DB_DAtaObject::factory('core_group');
453 if (!$g->get('name', 'bcc-email')) {
454 $this->jerr("no group 'bcc-email' exists in the system");
456 $bcc = $g->members('email');
458 $this->jerr( "'bcc-email' group does not have any members");
463 $this->authFrom = time();
464 $this->authKey = $u->genPassKey($this->authFrom);
465 //$this->authKey = md5($u->email . $this->authFrom . $u->passwd);
468 $this->rcpts = $u->getEmailFrom();
471 $mailer = $cm->toMailer($this, false);
472 if (is_a($mailer,'PEAR_Error') ) {
473 $this->addEvent('SYSERR',false, $mailer->getMessage());
474 $this->jerr($mailer->getMessage());
476 $sent = $mailer->send();
477 if (is_a($sent,'PEAR_Error') ) {
478 $this->addEvent('SYSERR',false, $sent->getMessage());
479 $this->jerr($sent->getMessage());
482 $this->addEvent('LOGIN-PASSREQ'. $this->event_suffix,$u, $u->email);
484 $uu->no_reset_sent++;
490 function verifyResetPassword($id,$t, $key)
492 $au = $this->getAuthUser();
495 $this->jerr( "Already Logged in - no need to use Password Reset");
498 $u = DB_DataObject::factory('core_person');
499 //$u->company_id = $this->company->id;
501 if (!$u->get($id) || !strlen($u->passwd)) {
502 $this->jerr("Password reset link is not valid (id)");
506 if ($key != $u->genPassKey($t)) {
507 $this->jerr("Password reset link is not valid (key)");
510 if ($t < strtotime("NOW - 1 DAY")) {
511 $this->jerr("Password reset link has expired");
520 function resetPassword($id,$t, $key, $newpass )
523 $u = $this->verifyResetPassword($id,$t,$key);
527 $u->no_reset_sent = 0;
528 if ($newpass != false) {
529 $u->setPassword($newpass);
532 $this->addEvent("LOGIN-CHANGEPASS". $this->event_suffix, $u);
534 $this->jok("Password has been Updated");
538 function changePassword($r)
540 $au = $this->getAuthUser();
542 $this->jerr("Password change attempted when not logged in");
545 $au->setPassword($r['passwd1']);
547 $this->addEvent("LOGIN-CHANGEPASS". $this->event_suffix, $au);
552 function ip_checking()
554 if(empty($this->ip_management)){
558 $ip = $this->ip_lookup();
561 $this->jerr('BAD-IP-ADDRESS', array('ip' => $ip));
564 $core_ip_access = DB_DataObject::factory('core_ip_access');
566 if(!DB_DataObject::factory('core_ip_access')->count()){ // first ip we always mark it as approved..
568 $core_ip_access = DB_DataObject::factory('core_ip_access');
570 $core_ip_access->setFrom(array(
572 'created_dt' => $core_ip_access->sqlValue("NOW()"),
573 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
575 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
576 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
579 $core_ip_access->insert();
584 $core_ip_access = DB_DataObject::factory('core_ip_access');
586 if(!$core_ip_access->get('ip', $ip)){ // new ip
588 $core_ip_access->setFrom(array(
590 'created_dt' => $core_ip_access->sqlValue("NOW()"),
591 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
593 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
594 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
597 $core_ip_access->insert();
599 $core_ip_access->sendXMPP();
601 $this->jerror('NEW-IP-ADDRESS', "New IP Address = needs approving", array('ip' => $ip));
606 if(empty($core_ip_access->status)){
607 $this->jerror('PENDING-IP-ADDRESS', "IP is still pending approval", array('ip' => $ip));
610 if($core_ip_access->status == -1){
611 $this->jerror('BLOCKED-IP-ADDRESS', "Your IP is blocked", array('ip' => $ip));
615 if($core_ip_access->status == -2 && strtotime($core_ip_access->expire_dt) < strtotime('NOW')){
616 $this->jerror('BLOCKED-IP-ADDRESS', "Your IP is blocked", array('ip' => $ip));
626 if (!empty($_SERVER['HTTP_CLIENT_IP'])){
627 return $_SERVER['HTTP_CLIENT_IP'];
630 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
631 return $_SERVER['HTTP_X_FORWARDED_FOR'];
634 return $_SERVER['REMOTE_ADDR'];