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_prefix = '';
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']);
85 $this->jerr("INVALID REQUEST");
92 $ff = class_exists('HTML_FlexyFramework2') ? HTML_FlexyFramework2::get() : HTML_FlexyFramework::get();
94 //DB_DAtaObject::debugLevel(1);
95 $u = $this->getAuthUser();
99 $this->addEvent('LOGOUT');
100 $e = DB_DataObject::factory('Events');
104 session_regenerate_id(true);
107 if(!empty($ff->Pman['local_autoauth']) && !empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'localhost') {
108 $this->jerr("you are using local autoauth!?");
110 //echo '<PRE>';print_R($_SESSION);
111 $this->jok("Logged out - user ");
115 //$_SESSION['Pman_I18N'] = array(); <<
116 session_regenerate_id(true);
119 $this->jok("Logged out - no user");
123 function sendAuthUserDetails()
125 // remove for normal use - it's a secuirty hole!
126 //DB_DataObject::debugLevel(1);
127 if (!empty($_REQUEST['_debug'])) {
128 // DB_DataObject::debugLevel(1);
131 $ff = HTML_FlexyFramework::get();
132 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
134 $u = DB_DataObject::factory($tbl);
135 $s = DB_DataObject::factory('core_setting');
136 $require_oath_val = 1;
137 $require_oath = $s->lookup('core', 'two_factor_auth_required');
138 if(!empty($require_oath)) {
139 if($require_oath->val == 0) {
140 $require_oath_val = 0;
152 $au = $u->getAuthUser();
154 // might occur on shared systems.
155 $ff= HTML_FlexyFramework::get();
157 if (!empty($ff->Pman['auth_comptype']) && $au->id > 0 &&
158 ($ff->Pman['auth_comptype'] != $au->company()->comptype)) {
160 $this->jerr("Login not permited to outside companies - please reload");
163 //$au = $u->getAuthUser();
165 $aur = $au ? $au->authUserArray() : array();
167 /** -- these need modulizing somehow! **/
171 // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
172 //echo '<PRE>'; print_r($this->modules());
173 // technically each module should only add properties to an array named after that module..
175 foreach($this->modules() as $m) {
179 if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
182 $cls = 'Pman_'.$m.'_Login';
183 require_once 'Pman/'.$m.'/Login.php';
186 $aur = $x->sendAuthUserDetails($aur);
191 // echo '<PRE>';print_r($aur);
199 function switchUser($id)
201 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
202 $u = DB_DataObject::factory($tbl);
204 $this->err("not logged in");
207 $au = $u->getAuthUser();
209 // first check they have perms to do this..
210 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
211 $this->jerr("User switching not permitted");
214 $u = DB_DataObject::factory($tbl);
217 $this->jerr('Account disabled');
220 // we might need this later..
221 $this->addEvent("SWITCH-USER", false, $au->name . ' TO ' . $u->name);
222 $this->jok("SWITCH");
226 function switchPublicUser($id)
228 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
230 $u = DB_DataObject::factory($tbl);
234 $this->jerr('Account disabled');
237 if(!$u->loginPublic()){
238 $this->jerr('Switch fail');
248 //DB_DataObject::debugLevel(1);
250 if (!empty($_REQUEST['getAuthUser'])) {
251 $this->sendAuthUserDetails();
255 if (!empty($_REQUEST['logout'])) {
256 return $this->logout();
259 if(!empty($_REQUEST['check_owner_company'])) {
260 $core_company = DB_DataObject::factory('core_company');
261 $core_company->comptype = 'OWNER';
262 $this->jok($core_company->count());
265 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
266 return $this->passwordRequest($_REQUEST['passwordRequest']);
269 if (!empty($_REQUEST['ResetPassword'])) {
270 if (empty($_REQUEST['id']) ||
271 empty($_REQUEST['ts']) ||
272 empty($_REQUEST['key']) ||
273 empty($_REQUEST['password1']) ||
274 empty($_REQUEST['password2']) ||
275 ($_REQUEST['password1'] != $_REQUEST['password2'])
277 $this->jerr("Invalid request to reset password");
280 $this->resetPassword($_REQUEST['id'], $_REQUEST['ts'], $_REQUEST['key'], $_REQUEST['password1'] );
284 if (!empty($_REQUEST['_verifyCheckSum'])) {
285 if (empty($_REQUEST['id']) ||
286 empty($_REQUEST['ts']) ||
287 empty($_REQUEST['key'])
290 $this->jerr("Invalid request to reset password");
293 $this->verifyResetPassword($_REQUEST['id'], $_REQUEST['ts'], $_REQUEST['key']);
294 $this->jok("Checksum is ok");
297 // this is 'classic' change password...
298 if (!empty($_REQUEST['changePassword'])) {
299 return $this->changePassword($_REQUEST);
304 $ff = HTML_FlexyFramework::get();
305 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
308 $u = DB_DataObject::factory($tbl);
310 $ip = $this->ip_lookup();
313 //DB_DataObject::DebugLevel(1);
314 $e = DB_DataObject::Factory('Events');
315 $e->action = $this->event_prefix . 'LOGIN-BAD';
317 $e->whereAdd('event_when > NOW() - INTERVAL 10 MINUTE');
318 if ($e->count() > 5) {
319 $this->jerror($this->event_prefix . 'LOGIN-RATE', "Login failures are rate limited - please try later");
323 // this was removed before - not quite sure why.
324 // when a duplicate login account is created, this stops the old one from interfering..
327 // empty username = not really a hacking attempt.
329 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
330 $this->jerror($this->event_prefix . 'LOGIN-EMPTY', 'You typed the wrong Username or Password (0)');
334 $u->authUserName($_REQUEST['username']);
336 if ($u->count() > 1 || !$u->find(true)) {
337 $this->jerror($this->event_prefix . 'LOGIN-BAD','You typed the wrong Username or Password (1)');
342 $this->jerror($this->event_prefix . 'LOGIN-BAD','Account disabled');
345 if(!empty($u->oath_key) && empty($_REQUEST['oath_password'])){
346 $this->jerror($this->event_prefix . 'LOGIN-2FA','Your account requires Two-Factor Authentication');
349 // check if config allows non-owner passwords.
350 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
351 // perhaps it should support arrays..
352 $ff= HTML_FlexyFramework::get();
353 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
354 //print_r($u->company());
355 $this->jerror($this->event_prefix . 'LOGIN-BADUSER', "Login not permited to outside companies"); // serious failure
359 // note we trim \x10 -- line break - as it was injected the front end
360 // may have an old bug on safari/chrome that added that character in certian wierd scenarios..
361 if (!$u->checkPassword(trim($_REQUEST['password'],"\x10"))) {
362 $this->jerror($this->event_prefix . 'LOGIN-BAD', 'You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
367 !empty($u->oath_key) &&
369 empty($_REQUEST['oath_password']) ||
370 !$u->checkTwoFactorAuthentication($_REQUEST['oath_password'])
373 $this->jerror($this->event_prefix . 'LOGIN-BAD', 'You typed the wrong Username or Password (3)');
377 $this->ip_checking();
380 // we might need this later..
381 $this->addEvent($this->event_prefix . "LOGIN", false, session_id());
385 if (!empty($_REQUEST['lang'])) {
387 if (!empty($ff->languages['avail']) && !in_array($_REQUEST['lang'],$ff->languages['avail'])) {
391 $u->lang($_REQUEST['lang']);
396 $this->sendAuthUserDetails();
402 function passwordRequest($n)
404 $u = DB_DataObject::factory('core_person');
405 //$u->company_id = $this->company->id;
407 $u->whereAdd('LENGTH(passwd) > 1');
410 if ($u->count() > 1 || !$u->find(true)) {
411 $this->jerr('invalid User (1)');
413 // got a avlid user..
414 if (!strlen($u->passwd)) {
415 $this->jerr('invalid User (2)');
417 // check to see if we have sent a request before..
419 if ($u->no_reset_sent > 3) {
420 $this->jerr('We have issued to many resets - please contact the Administrator');
427 $cm = DB_DataObject::factory('core_email');
428 if (!$cm->get('name', 'ADMIN_PASSWORD_RESET')) {
429 $this->jerr("no template Admin password reset (ADMIN_PASSWORD_RESET) exists - please run importer ");
432 $this->jerr("template for Admin password reset has been disabled");
436 $g = DB_DAtaObject::factory('Groups');
437 if (!$g->get('name', 'system-email-from')) {
438 $this->jerr("no group 'system-email-from' exists in the system");
440 $from_ar = $g->members();
441 if (count($from_ar) != 1) {
442 $this->jerr(count($from_ar) ? "To many members in the 'system-email-from' group " :
443 "'system-email-from' group does not have any members");
450 $g = DB_DAtaObject::factory('core_group');
451 if (!$g->get('name', 'bcc-email')) {
452 $this->jerr("no group 'bcc-email' exists in the system");
454 $bcc = $g->members('email');
456 $this->jerr( "'bcc-email' group does not have any members");
461 $this->authFrom = time();
462 $this->authKey = $u->genPassKey($this->authFrom);
463 //$this->authKey = md5($u->email . $this->authFrom . $u->passwd);
466 $this->rcpts = $u->getEmailFrom();
469 $mailer = $cm->toMailer($this, false);
470 if (is_a($mailer,'PEAR_Error') ) {
471 $this->addEvent('SYSERR',false, $mailer->getMessage());
472 $this->jerr($mailer->getMessage());
474 $sent = $mailer->send();
475 if (is_a($sent,'PEAR_Error') ) {
476 $this->addEvent('SYSERR',false, $sent->getMessage());
477 $this->jerr($sent->getMessage());
480 $this->addEvent('PASSREQ',$u, $u->email);
482 $uu->no_reset_sent++;
488 function verifyResetPassword($id,$t, $key)
490 $au = $this->getAuthUser();
493 $this->jerr( "Already Logged in - no need to use Password Reset");
496 $u = DB_DataObject::factory('core_person');
497 //$u->company_id = $this->company->id;
499 if (!$u->get($id) || !strlen($u->passwd)) {
500 $this->jerr("Password reset link is not valid (id)");
504 if ($key != $u->genPassKey($t)) {
505 $this->jerr("Password reset link is not valid (key)");
508 if ($t < strtotime("NOW - 1 DAY")) {
509 $this->jerr("Password reset link has expired");
518 function resetPassword($id,$t, $key, $newpass )
521 $u = $this->verifyResetPassword($id,$t,$key);
525 $u->no_reset_sent = 0;
526 if ($newpass != false) {
527 $u->setPassword($newpass);
530 $this->addEvent("CHANGEPASS", $u);
532 $this->jok("Password has been Updated");
536 function changePassword($r)
538 $au = $this->getAuthUser();
540 $this->jerr("Password change attempted when not logged in");
543 $au->setPassword($r['passwd1']);
545 $this->addEvent("CHANGEPASS", $au);
550 function ip_checking()
552 if(empty($this->ip_management)){
556 $ip = $this->ip_lookup();
559 $this->jerr('BAD-IP-ADDRESS', array('ip' => $ip));
562 $core_ip_access = DB_DataObject::factory('core_ip_access');
564 if(!DB_DataObject::factory('core_ip_access')->count()){ // first ip we always mark it as approved..
566 $core_ip_access = DB_DataObject::factory('core_ip_access');
568 $core_ip_access->setFrom(array(
570 'created_dt' => $core_ip_access->sqlValue("NOW()"),
571 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
573 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
574 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
577 $core_ip_access->insert();
582 $core_ip_access = DB_DataObject::factory('core_ip_access');
584 if(!$core_ip_access->get('ip', $ip)){ // new ip
586 $core_ip_access->setFrom(array(
588 'created_dt' => $core_ip_access->sqlValue("NOW()"),
589 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
591 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
592 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
595 $core_ip_access->insert();
597 $core_ip_access->sendXMPP();
599 $this->jerror('NEW-IP-ADDRESS', "New IP Address = needs approving", array('ip' => $ip));
604 if(empty($core_ip_access->status)){
605 $this->jerror('PENDING-IP-ADDRESS', "IP is still pending approval", array('ip' => $ip));
608 if($core_ip_access->status == -1){
609 $this->jerror('BLOCKED-IP-ADDRESS', "Your IP is blocked", array('ip' => $ip));
613 if($core_ip_access->status == -2 && strtotime($core_ip_access->expire_dt) < strtotime('NOW')){
614 $this->jerrpr('BLOCKED-IP-ADDRESS', "Your IP is blocked", array('ip' => $ip));
624 if (!empty($_SERVER['HTTP_CLIENT_IP'])){
625 return $_SERVER['HTTP_CLIENT_IP'];
628 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
629 return $_SERVER['HTTP_X_FORWARDED_FOR'];
632 return $_SERVER['REMOTE_ADDR'];