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 function getAuth() // everyone allowed in here..
32 parent::getAuth(); // load company..
34 $ff = HTML_FlexyFramework::get();
36 $this->ip_management = (empty($ff->Pman['ip_management'])) ? false : true;
46 function get($v, $opts=array())
48 $this->initErrorHandling();
50 //DB_DataObject::DebugLevel(1);
51 if (!empty($_REQUEST['logout'])) {
52 return $this->logout();
56 if (!empty($_REQUEST['getAuthUser'])) {
57 //DB_Dataobject::debugLevel(5);
58 $this->sendAuthUserDetails();
62 // might be an idea to disable this?!?
63 if (!empty($_REQUEST['username'])) {
68 if (!empty($_REQUEST['switch'])) {
69 $this->switchUser($_REQUEST['switch']);
72 if (!empty($_REQUEST['loginPublic'])) {
73 $this->switchPublicUser($_REQUEST['loginPublic']);
76 $this->jerr("INVALID REQUEST");
83 $ff = class_exists('HTML_FlexyFramework2') ? HTML_FlexyFramework2::get() : HTML_FlexyFramework::get();
84 //DB_DAtaObject::debugLevel(1);
85 $u = $this->getAuthUser();
89 $this->addEvent('LOGOUT');
90 $e = DB_DataObject::factory('Events');
94 session_regenerate_id(true);
97 if(!empty($ff->Pman['local_autoauth']) && !empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'localhost') {
98 $this->jerr("you are using local autoauth!?");
100 //echo '<PRE>';print_R($_SESSION);
101 $this->jok("Logged out - user ");
105 //$_SESSION['Pman_I18N'] = array(); <<
106 session_regenerate_id(true);
109 $this->jok("Logged out - no user");
113 function sendAuthUserDetails()
115 // remove for normal use - it's a secuirty hole!
116 //DB_DataObject::debugLevel(1);
117 if (!empty($_REQUEST['_debug'])) {
118 // DB_DataObject::debugLevel(1);
121 $ff = HTML_FlexyFramework::get();
122 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
124 $u = DB_DataObject::factory($tbl);
127 $this->jok(array('id' => 0)); // not logged in..
131 $au = $u->getAuthUser();
133 // might occur on shared systems.
134 $ff= HTML_FlexyFramework::get();
136 if (!empty($ff->Pman['auth_comptype']) && $au->id > 0 &&
137 ($ff->Pman['auth_comptype'] != $au->company()->comptype)) {
139 $this->jerr("Login not permited to outside companies - please reload");
142 //$au = $u->getAuthUser();
144 $aur = $au ? $au->authUserArray() : array();
146 /** -- these need modulizing somehow! **/
150 // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
151 //echo '<PRE>'; print_r($this->modules());
152 // technically each module should only add properties to an array named after that module..
154 foreach($this->modules() as $m) {
158 if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
161 $cls = 'Pman_'.$m.'_Login';
162 require_once 'Pman/'.$m.'/Login.php';
165 $aur = $x->sendAuthUserDetails($aur);
170 // echo '<PRE>';print_r($aur);
178 function switchUser($id)
180 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
181 $u = DB_DataObject::factory($tbl);
183 $this->err("not logged in");
186 $au = $u->getAuthUser();
188 // first check they have perms to do this..
189 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
190 $this->jerr("User switching not permitted");
193 $u = DB_DataObject::factory($tbl);
196 $this->jerr('Account disabled');
199 // we might need this later..
200 $this->addEvent("SWITCH-USER", false, $au->name . ' TO ' . $u->name);
201 $this->jok("SWITCH");
205 function switchPublicUser($id)
207 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
209 $u = DB_DataObject::factory($tbl);
213 $this->jerr('Account disabled');
216 if(!$u->loginPublic()){
217 $this->jerr('Switch fail');
227 //DB_DataObject::debugLevel(1);
229 if (!empty($_REQUEST['getAuthUser'])) {
230 $this->sendAuthUserDetails();
234 if (!empty($_REQUEST['logout'])) {
235 return $this->logout();
238 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
240 return $this->passwordRequest($_REQUEST['passwordRequest']);
244 if (!empty($_REQUEST['changePassword'])) {
245 return $this->changePassword($_REQUEST);
250 $ff = HTML_FlexyFramework::get();
251 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
254 $u = DB_DataObject::factory($tbl);
256 $ip = $this->ip_lookup();
259 //DB_DataObject::DebugLevel(1);
260 $e = DB_DataObject::Factory('Events');
261 $e->action = 'LOGIN-BAD';
263 $e->whereAdd('event_when > NOW() - INTERVAL 10 MINUTE');
264 if ($e->count() > 5) {
265 $this->jerror('LOGIN-RATE', "Login failures are rate limited - please try later");
271 // empty username = not really a hacking attempt.
273 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
274 $this->jerror('LOGIN-EMPTY', 'You typed the wrong Username or Password (0)');
278 $u->authUserName($_REQUEST['username']);
280 if ($u->count() > 1 || !$u->find(true)) {
281 $this->jerror('LOGIN-BAD','You typed the wrong Username or Password (1)');
286 $this->jerror('LOGIN-BAD','Account disabled');
289 if(!empty($u->oath_key) && empty($_REQUEST['oath_password'])){
290 $this->jerror('LOGIN-BAD','Your account require Two-Factor Authentication');
293 // check if config allows non-owner passwords.
294 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
295 // perhaps it should support arrays..
296 $ff= HTML_FlexyFramework::get();
297 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
298 //print_r($u->company());
299 $this->jerror('LOGIN-BADUSER', "Login not permited to outside companies"); // serious failure
303 // note we trim \x10 -- line break - as it was injected the front end
304 // may have an old bug on safari/chrome that added that character in certian wierd scenarios..
305 if (!$u->checkPassword(trim($_REQUEST['password'],"\x10"))) {
306 $this->jerror('LOGIN-BAD', 'You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
310 if(!empty($u->oath_key) && !$u->checkTwoFactorAuthentication(trim($_REQUEST['oath_password'],"\x10"))){
311 $this->jerror('LOGIN-BAD', 'You typed the wrong Username or Password (3)');
315 $this->ip_checking();
318 // we might need this later..
319 $this->addEvent("LOGIN", false, session_id());
320 if (!empty($_REQUEST['lang'])) {
321 $u->lang($_REQUEST['lang']);
325 $this->sendAuthUserDetails();
331 function passwordRequest($n)
333 $u = DB_DataObject::factory('core_person');
334 //$u->company_id = $this->company->id;
336 $u->whereAdd('LENGTH(passwd) > 1');
339 if ($u->count() > 1 || !$u->find(true)) {
340 $this->jerr('invalid User (1)');
342 // got a avlid user..
343 if (!strlen($u->passwd)) {
344 $this->jerr('invalid User (2)');
346 // check to see if we have sent a request before..
348 if ($u->no_reset_sent > 3) {
349 $this->jerr('We have issued to many resets - please contact the Administrator');
356 $cm = DB_DataObject::factory('core_email');
357 if (!$cm->get('name', 'ADMIN_PASSWORD_RESET')) {
358 $this->jerr("no template ADMIN_PASSWORD_RESET exists - please run importer ");
363 $g = DB_DAtaObject::factory('Groups');
364 if (!$g->get('name', 'system-email-from')) {
365 $this->jerr("no group 'system-email-from' exists in the system");
367 $from_ar = $g->members();
368 if (count($from_ar) != 1) {
369 $this->jerr(count($from_ar) ? "To many members in the 'system-email-from' group " :
370 "'system-email-from' group does not have any members");
377 $g = DB_DAtaObject::factory('core_group');
378 if (!$g->get('name', 'bcc-email')) {
379 $this->jerr("no group 'bcc-email' exists in the system");
381 $bcc = $g->members('email');
383 $this->jerr( "'bcc-email' group does not have any members");
388 $this->authFrom = time();
389 $this->authKey = $u->genPassKey($this->authFrom);
390 //$this->authKey = md5($u->email . $this->authFrom . $u->passwd);
393 $this->rcpts = $u->getEmailFrom();
395 $ret = $cm->send($this);
396 //$this->jerr(print_r($r->toData(),true));
398 if (is_object($ret)) {
399 $this->addEvent('SYSERR',false, $ret->getMessage());
400 $this->jerr($ret->getMessage());
402 $this->addEvent('PASSREQ',$u, $u->email);
404 $uu->no_reset_sent++;
410 function changePassword($r)
412 $au = $this->getAuthUser();
415 $au->setPassword($r['passwd1']);
417 $this->addEvent("CHANGEPASS", $au);
420 // not logged in -> need to validate
421 if (empty($r['passwordReset'])) {
422 $this->jerr("invalid request");
424 // same code as reset pasword
426 $bits = explode('/', $r['passwordReset']);
429 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
431 if ($res !== false) {
434 // key is correct.. let's change password...
436 $u = DB_DataObject::factory('core_person');
438 //$u->company_id = $this->company->id;
439 $u->whereAdd('LENGTH(passwd) > 1');
441 if (!$u->get($bits[0])) {
442 $this->jerr("invalid id"); // should not happen!!!!
445 $u->setPassword($r['passwd1']);
448 $this->addEvent("CHANGEPASS", $u);
452 function ip_checking()
454 if(empty($this->ip_management)){
458 $ip = $this->ip_lookup();
461 $this->jerr('BAD-IP-ADDRESS', array('ip' => $ip));
464 $core_ip_access = DB_DataObject::factory('core_ip_access');
466 if(!DB_DataObject::factory('core_ip_access')->count()){ // first ip we always mark it as approved..
468 $core_ip_access = DB_DataObject::factory('core_ip_access');
470 $core_ip_access->setFrom(array(
472 'created_dt' => $core_ip_access->sqlValue("NOW()"),
473 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
475 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
476 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
479 $core_ip_access->insert();
484 $core_ip_access = DB_DataObject::factory('core_ip_access');
486 if(!$core_ip_access->get('ip', $ip)){ // new ip
488 $core_ip_access->setFrom(array(
490 'created_dt' => $core_ip_access->sqlValue("NOW()"),
491 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
493 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
494 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
497 $core_ip_access->insert();
499 $core_ip_access->sendXMPP();
501 $this->jerr('NEW-IP-ADDRESS', array('ip' => $ip));
506 if(empty($core_ip_access->status)){
507 $this->jerr('PENDING-IP-ADDRESS', array('ip' => $ip));
510 if($core_ip_access->status == -1){
511 $this->jerr('BLOCKED-IP-ADDRESS', array('ip' => $ip));
515 if($core_ip_access->status == -2 && strtotime($core_ip_access->expire_dt) < strtotime('NOW')){
516 $this->jerr('BLOCKED-IP-ADDRESS', array('ip' => $ip));
526 if (!empty($_SERVER['HTTP_CLIENT_IP'])){
527 return $_SERVER['HTTP_CLIENT_IP'];
530 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
531 return $_SERVER['HTTP_X_FORWARDED_FOR'];
534 return $_SERVER['REMOTE_ADDR'];