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
27 var $masterTemplate = 'login.html';
29 function getAuth() // everyone allowed in here..
31 parent::getAuth(); // load company..
41 function get($v, $opts=array())
43 $this->initErrorHandling();
45 //DB_DataObject::DebugLevel(1);
46 if (!empty($_REQUEST['logout'])) {
47 return $this->logout();
51 if (!empty($_REQUEST['getAuthUser'])) {
52 //DB_Dataobject::debugLevel(5);
53 $this->sendAuthUserDetails();
58 // might be an idea to disable this?!?
59 if (!empty($_REQUEST['username'])) {
64 if (!empty($_REQUEST['switch'])) {
65 $this->switchUser($_REQUEST['switch']);
68 if (!empty($_REQUEST['loginPublic'])) {
69 $this->switchPublicUser($_REQUEST['loginPublic']);
73 $this->jerr("INVALID REQUEST");
80 $ff = class_exists('HTML_FlexyFramework2') ? HTML_FlexyFramework2::get() : HTML_FlexyFramework::get();
81 //DB_DAtaObject::debugLevel(1);
82 $u = $this->getAuthUser();
86 $this->addEvent('LOGOUT');
87 $e = DB_DataObject::factory('Events');
91 session_regenerate_id(true);
94 if(!empty($ff->Pman['local_autoauth']) && !empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'localhost') {
95 $this->jerr("you are using local autoauth!?");
97 //echo '<PRE>';print_R($_SESSION);
98 $this->jok("Logged out - user ");
102 //$_SESSION['Pman_I18N'] = array(); <<
103 session_regenerate_id(true);
106 $this->jok("Logged out - no user");
110 function sendAuthUserDetails()
112 // remove for normal use - it's a secuirty hole!
113 //DB_DataObject::debugLevel(1);
114 if (!empty($_REQUEST['_debug'])) {
115 // DB_DataObject::debugLevel(1);
118 $ff = HTML_FlexyFramework::get();
119 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
121 $u = DB_DataObject::factory($tbl);
124 $this->jok(array('id' => 0)); // not logged in..
128 $au = $u->getAuthUser();
130 // might occur on shared systems.
131 $ff= HTML_FlexyFramework::get();
133 if (!empty($ff->Pman['auth_comptype']) && $au->id > 0 &&
134 ($ff->Pman['auth_comptype'] != $au->company()->comptype)) {
136 $this->jerr("Login not permited to outside companies - please reload");
139 //$au = $u->getAuthUser();
141 $aur = $au ? $au->authUserArray() : array();
143 /** -- these need modulizing somehow! **/
147 // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
148 //echo '<PRE>'; print_r($this->modules());
149 // technically each module should only add properties to an array named after that module..
151 foreach($this->modules() as $m) {
155 if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
158 $cls = 'Pman_'.$m.'_Login';
159 require_once 'Pman/'.$m.'/Login.php';
162 $aur = $x->sendAuthUserDetails($aur);
167 // echo '<PRE>';print_r($aur);
175 function switchUser($id)
177 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
178 $u = DB_DataObject::factory($tbl);
180 $this->err("not logged in");
183 $au = $u->getAuthUser();
185 // first check they have perms to do this..
186 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
187 $this->jerr("User switching not permitted");
190 $u = DB_DataObject::factory($tbl);
193 $this->jerr('Account disabled');
196 // we might need this later..
197 $this->addEvent("SWITCH-USER", false, $au->name . ' TO ' . $u->name);
198 $this->jok("SWITCH");
202 function switchPublicUser($id)
204 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
206 $u = DB_DataObject::factory($tbl);
210 $this->jerr('Account disabled');
213 if(!$u->loginPublic()){
214 $this->jerr('Switch fail');
225 //DB_DataObject::debugLevel(1);
226 if (!empty($_REQUEST['getAuthUser'])) {
227 $this->sendAuthUserDetails();
231 if (!empty($_REQUEST['logout'])) {
232 return $this->logout();
235 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
237 return $this->passwordRequest($_REQUEST['passwordRequest']);
241 if (!empty($_REQUEST['changePassword'])) {
242 return $this->changePassword($_REQUEST);
247 $ff = HTML_FlexyFramework::get();
248 $tbl = empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
251 $u = DB_DataObject::factory($tbl);
256 if (!empty($_SERVER['REMOTE_ADDR'])) {
257 //DB_DataObject::DebugLevel(1);
258 $e = DB_DataObject::Factory('Events');
259 $e->action = 'LOGIN-BAD';
260 $e->ipaddr = $_SERVER['REMOTE_ADDR'];
261 $e->whereAdd('event_when > NOW() - INTERVAL 10 MINUTE');
262 if ($e->count() > 5) {
263 $this->jerror('LOGIN-RATE', "Login failures are rate limited - please try later");
269 // empty username = not really a hacking attempt.
271 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
272 $this->jerror('LOGIN-EMPTY', 'You typed the wrong Username or Password (0)');
276 $u->authUserName($_REQUEST['username']);
278 if ($u->count() > 1 || !$u->find(true)) {
279 $this->jerror('LOGIN-BAD','You typed the wrong Username or Password (1)');
284 $this->jerror('LOGIN-BAD','Account disabled');
287 if(!empty($u->oath_key) && empty($_REQUEST['oath_password'])){
288 $this->jerror('LOGIN-BAD','Your account require Two-Factor Authentication');
291 // check if config allows non-owner passwords.
292 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
293 // perhaps it should support arrays..
294 $ff= HTML_FlexyFramework::get();
295 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
296 //print_r($u->company());
297 $this->jerror('LOGIN-BADUSER', "Login not permited to outside companies"); // serious failure
301 // note we trim \x10 -- line break - as it was injected the front end
302 // may have an old bug on safari/chrome that added that character in certian wierd scenarios..
303 if (!$u->checkPassword(trim($_REQUEST['password'],"\x10"))) {
304 $this->jerror('LOGIN-BAD', 'You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
308 if(!empty($u->oath_key) && !$u->checkTwoFactorAuthentication(trim($_REQUEST['oath_password'],"\x10"))){
309 $this->jerror('LOGIN-BAD', 'You typed the wrong Username or Password (3)');
314 // we might need this later..
315 $this->addEvent("LOGIN", false, session_id());
316 if (!empty($_REQUEST['lang'])) {
317 $u->lang($_REQUEST['lang']);
321 $this->sendAuthUserDetails();
327 function passwordRequest($n)
329 $u = DB_DataObject::factory('core_person');
330 //$u->company_id = $this->company->id;
332 $u->whereAdd('LENGTH(passwd) > 1');
335 if ($u->count() > 1 || !$u->find(true)) {
336 $this->jerr('invalid User (1)');
338 // got a avlid user..
339 if (!strlen($u->passwd)) {
340 $this->jerr('invalid User (2)');
342 // check to see if we have sent a request before..
344 if ($u->no_reset_sent > 3) {
345 $this->jerr('We have issued to many resets - please contact the Administrator');
352 $cm = DB_DataObject::factory('core_email');
353 if (!$cm->get('name', 'ADMIN_PASSWORD_RESET')) {
354 $this->jerr("no template ADMIN_PASSWORD_RESET exists - please run importer ");
359 $g = DB_DAtaObject::factory('Groups');
360 if (!$g->get('name', 'system-email-from')) {
361 $this->jerr("no group 'system-email-from' exists in the system");
363 $from_ar = $g->members();
364 if (count($from_ar) != 1) {
365 $this->jerr(count($from_ar) ? "To many members in the 'system-email-from' group " :
366 "'system-email-from' group does not have any members");
373 $g = DB_DAtaObject::factory('core_group');
374 if (!$g->get('name', 'bcc-email')) {
375 $this->jerr("no group 'bcc-email' exists in the system");
377 $bcc = $g->members('email');
379 $this->jerr( "'bcc-email' group does not have any members");
384 $this->authFrom = time();
385 $this->authKey = $u->genPassKey($this->authFrom);
386 //$this->authKey = md5($u->email . $this->authFrom . $u->passwd);
389 $this->rcpts = $u->getEmailFrom();
391 $ret = $cm->send($this);
392 //$this->jerr(print_r($r->toData(),true));
394 if (is_object($ret)) {
395 $this->addEvent('SYSERR',false, $ret->getMessage());
396 $this->jerr($ret->getMessage());
398 $this->addEvent('PASSREQ',$u, $u->email);
400 $uu->no_reset_sent++;
406 function changePassword($r)
409 $au = $this->getAuthUser();
412 $au->setPassword($r['passwd1']);
414 $this->addEvent("CHANGEPASS", $au);
417 // not logged in -> need to validate
418 if (empty($r['passwordReset'])) {
419 $this->jerr("invalid request");
421 // same code as reset pasword
423 $bits = explode('/', $r['passwordReset']);
426 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
428 if ($res !== false) {
431 // key is correct.. let's change password...
433 $u = DB_DataObject::factory('core_person');
435 //$u->company_id = $this->company->id;
436 $u->whereAdd('LENGTH(passwd) > 1');
438 if (!$u->get($bits[0])) {
439 $this->jerr("invalid id"); // should not happen!!!!
442 $u->setPassword($r['passwd1']);
445 $this->addEvent("CHANGEPASS", $u);