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']);
77 $this->jerr("INVALID REQUEST");
84 $ff = class_exists('HTML_FlexyFramework2') ? HTML_FlexyFramework2::get() : HTML_FlexyFramework::get();
85 //DB_DAtaObject::debugLevel(1);
86 $u = $this->getAuthUser();
90 $this->addEvent('LOGOUT');
91 $e = DB_DataObject::factory('Events');
95 session_regenerate_id(true);
98 if(!empty($ff->Pman['local_autoauth']) && !empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'localhost') {
99 $this->jerr("you are using local autoauth!?");
101 //echo '<PRE>';print_R($_SESSION);
102 $this->jok("Logged out - user ");
106 //$_SESSION['Pman_I18N'] = array(); <<
107 session_regenerate_id(true);
110 $this->jok("Logged out - no user");
114 function sendAuthUserDetails()
116 // remove for normal use - it's a secuirty hole!
117 //DB_DataObject::debugLevel(1);
118 if (!empty($_REQUEST['_debug'])) {
119 // DB_DataObject::debugLevel(1);
122 $ff = HTML_FlexyFramework::get();
123 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
125 $u = DB_DataObject::factory($tbl);
128 $this->jok(array('id' => 0)); // not logged in..
132 $au = $u->getAuthUser();
134 // might occur on shared systems.
135 $ff= HTML_FlexyFramework::get();
137 if (!empty($ff->Pman['auth_comptype']) && $au->id > 0 &&
138 ($ff->Pman['auth_comptype'] != $au->company()->comptype)) {
140 $this->jerr("Login not permited to outside companies - please reload");
143 //$au = $u->getAuthUser();
145 $aur = $au ? $au->authUserArray() : array();
147 /** -- these need modulizing somehow! **/
151 // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
152 //echo '<PRE>'; print_r($this->modules());
153 // technically each module should only add properties to an array named after that module..
155 foreach($this->modules() as $m) {
159 if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
162 $cls = 'Pman_'.$m.'_Login';
163 require_once 'Pman/'.$m.'/Login.php';
166 $aur = $x->sendAuthUserDetails($aur);
171 // echo '<PRE>';print_r($aur);
179 function switchUser($id)
181 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
182 $u = DB_DataObject::factory($tbl);
184 $this->err("not logged in");
187 $au = $u->getAuthUser();
189 // first check they have perms to do this..
190 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
191 $this->jerr("User switching not permitted");
194 $u = DB_DataObject::factory($tbl);
197 $this->jerr('Account disabled');
200 // we might need this later..
201 $this->addEvent("SWITCH-USER", false, $au->name . ' TO ' . $u->name);
202 $this->jok("SWITCH");
206 function switchPublicUser($id)
208 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
210 $u = DB_DataObject::factory($tbl);
214 $this->jerr('Account disabled');
217 if(!$u->loginPublic()){
218 $this->jerr('Switch fail');
228 //DB_DataObject::debugLevel(1);
230 if (!empty($_REQUEST['getAuthUser'])) {
231 $this->sendAuthUserDetails();
235 if (!empty($_REQUEST['logout'])) {
236 return $this->logout();
239 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
241 return $this->passwordRequest($_REQUEST['passwordRequest']);
245 if (!empty($_REQUEST['changePassword'])) {
246 return $this->changePassword($_REQUEST);
251 $ff = HTML_FlexyFramework::get();
252 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
255 $u = DB_DataObject::factory($tbl);
257 $ip = $this->ip_lookup();
260 //DB_DataObject::DebugLevel(1);
261 $e = DB_DataObject::Factory('Events');
262 $e->action = 'LOGIN-BAD';
264 $e->whereAdd('event_when > NOW() - INTERVAL 10 MINUTE');
265 if ($e->count() > 5) {
266 $this->jerror('LOGIN-RATE', "Login failures are rate limited - please try later");
272 // empty username = not really a hacking attempt.
274 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
275 $this->jerror('LOGIN-EMPTY', 'You typed the wrong Username or Password (0)');
279 $u->authUserName($_REQUEST['username']);
281 if ($u->count() > 1 || !$u->find(true)) {
282 $this->jerror('LOGIN-BAD','You typed the wrong Username or Password (1)');
287 $this->jerror('LOGIN-BAD','Account disabled');
290 if(!empty($u->oath_key) && empty($_REQUEST['oath_password'])){
291 $this->jerror('LOGIN-BAD','Your account require Two-Factor Authentication');
294 // check if config allows non-owner passwords.
295 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
296 // perhaps it should support arrays..
297 $ff= HTML_FlexyFramework::get();
298 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
299 //print_r($u->company());
300 $this->jerror('LOGIN-BADUSER', "Login not permited to outside companies"); // serious failure
304 // note we trim \x10 -- line break - as it was injected the front end
305 // may have an old bug on safari/chrome that added that character in certian wierd scenarios..
306 if (!$u->checkPassword(trim($_REQUEST['password'],"\x10"))) {
307 $this->jerror('LOGIN-BAD', 'You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
311 if(!empty($u->oath_key) && !$u->checkTwoFactorAuthentication(trim($_REQUEST['oath_password'],"\x10"))){
312 $this->jerror('LOGIN-BAD', 'You typed the wrong Username or Password (3)');
316 $this->ip_checking();
319 // we might need this later..
320 $this->addEvent("LOGIN", false, session_id());
321 if (!empty($_REQUEST['lang'])) {
322 $u->lang($_REQUEST['lang']);
326 $this->sendAuthUserDetails();
332 function passwordRequest($n)
334 $u = DB_DataObject::factory('core_person');
335 //$u->company_id = $this->company->id;
337 $u->whereAdd('LENGTH(passwd) > 1');
340 if ($u->count() > 1 || !$u->find(true)) {
341 $this->jerr('invalid User (1)');
343 // got a avlid user..
344 if (!strlen($u->passwd)) {
345 $this->jerr('invalid User (2)');
347 // check to see if we have sent a request before..
349 if ($u->no_reset_sent > 3) {
350 $this->jerr('We have issued to many resets - please contact the Administrator');
357 $cm = DB_DataObject::factory('core_email');
358 if (!$cm->get('name', 'ADMIN_PASSWORD_RESET')) {
359 $this->jerr("no template ADMIN_PASSWORD_RESET exists - please run importer ");
364 $g = DB_DAtaObject::factory('Groups');
365 if (!$g->get('name', 'system-email-from')) {
366 $this->jerr("no group 'system-email-from' exists in the system");
368 $from_ar = $g->members();
369 if (count($from_ar) != 1) {
370 $this->jerr(count($from_ar) ? "To many members in the 'system-email-from' group " :
371 "'system-email-from' group does not have any members");
378 $g = DB_DAtaObject::factory('core_group');
379 if (!$g->get('name', 'bcc-email')) {
380 $this->jerr("no group 'bcc-email' exists in the system");
382 $bcc = $g->members('email');
384 $this->jerr( "'bcc-email' group does not have any members");
389 $this->authFrom = time();
390 $this->authKey = $u->genPassKey($this->authFrom);
391 //$this->authKey = md5($u->email . $this->authFrom . $u->passwd);
394 $this->rcpts = $u->getEmailFrom();
396 $ret = $cm->send($this);
397 //$this->jerr(print_r($r->toData(),true));
399 if (is_object($ret)) {
400 $this->addEvent('SYSERR',false, $ret->getMessage());
401 $this->jerr($ret->getMessage());
403 $this->addEvent('PASSREQ',$u, $u->email);
405 $uu->no_reset_sent++;
411 function changePassword($r)
413 $au = $this->getAuthUser();
416 $au->setPassword($r['passwd1']);
418 $this->addEvent("CHANGEPASS", $au);
421 // not logged in -> need to validate
422 if (empty($r['passwordReset'])) {
423 $this->jerr("invalid request");
425 // same code as reset pasword
427 $bits = explode('/', $r['passwordReset']);
430 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
432 if ($res !== false) {
435 // key is correct.. let's change password...
437 $u = DB_DataObject::factory('core_person');
439 //$u->company_id = $this->company->id;
440 $u->whereAdd('LENGTH(passwd) > 1');
442 if (!$u->get($bits[0])) {
443 $this->jerr("invalid id"); // should not happen!!!!
446 $u->setPassword($r['passwd1']);
449 $this->addEvent("CHANGEPASS", $u);
453 function ip_checking()
455 if(empty($this->ip_management)){
459 $ip = $this->ip_lookup();
462 $this->jerr('BAD-IP-ADDRESS', array('ip' => $ip));
465 $core_ip_access = DB_DataObject::factory('core_ip_access');
467 if(!DB_DataObject::factory('core_ip_access')->count()){ // first ip we always mark it as approved..
469 $core_ip_access = DB_DataObject::factory('core_ip_access');
471 $core_ip_access->setFrom(array(
473 'created_dt' => $core_ip_access->sqlValue("NOW()"),
474 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
476 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
477 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
480 $core_ip_access->insert();
485 $core_ip_access = DB_DataObject::factory('core_ip_access');
487 if(!$core_ip_access->get('ip', $ip)){ // new ip
489 $core_ip_access->setFrom(array(
491 'created_dt' => $core_ip_access->sqlValue("NOW()"),
492 'authorized_key' => md5(openssl_random_pseudo_bytes(16)),
494 'email' => (empty($_REQUEST['username'])) ? '' : $_REQUEST['username'],
495 'user_agent' => (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']
498 $core_ip_access->insert();
500 $core_ip_access->sendXMPP();
502 $this->jerr('NEW-IP-ADDRESS', array('ip' => $ip));
507 if(empty($core_ip_access->status)){
508 $this->jerr('PENDING-IP-ADDRESS', array('ip' => $ip));
511 if($core_ip_access->status == -1){
512 $this->jerr('BLOCKED-IP-ADDRESS', array('ip' => $ip));
516 if($core_ip_access->status == -2 && strtotime($core_ip_access->expire_dt) < strtotime('NOW')){
517 $this->jerr('BLOCKED-IP-ADDRESS', array('ip' => $ip));
527 if (!empty($_SERVER['HTTP_CLIENT_IP'])){
528 return $_SERVER['HTTP_CLIENT_IP'];
531 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
532 return $_SERVER['HTTP_X_FORWARDED_FOR'];
535 return $_SERVER['REMOTE_ADDR'];