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..
47 if (!empty($_REQUEST['logout'])) {
48 $u = $this->getAuthUser();
52 $this->addEvent('LOGOUT');
53 $e = DB_DataObject::factory('Events');
55 session_regenerate_id(true);
60 $_SESSION['Pman_I18N'] = array();
65 if (!empty($_REQUEST['getAuthUser'])) {
66 //DB_Dataobject::debugLevel(5);
67 $this->sendAuthUserDetails();
71 if (!empty($_REQUEST['username'])) {
74 if (!empty($_REQUEST['switch'])) {
75 $this->switchUser($_REQUEST['switch']);
79 $this->jerr("INVALID REQUEST");
83 function sendAuthUserDetails()
85 // DB_DataObject::debugLevel(1);
86 $ff = HTML_FlexyFramework::get();
87 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
89 $u = DB_DataObject::factory($tbl);
91 $this->jok(array('id' => 0)); // not logged in..
94 $au = $u->getAuthUser();
95 // might occur on shared systems.
96 $ff= HTML_FlexyFramework::get();
97 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $au->company()->comptype) {
99 $this->jerr("Login not permited to outside companies - please reload");
103 $au = $u->getAuthUser();
105 $aur = $au->authUserArray();
107 /** -- these need modulizing somehow! **/
111 // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
112 foreach($this->modules as $m) {
114 if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
117 $cls = 'Pman_'.$m.'_Login';
121 if ($this->hasModule('Fax')) {
122 // should check fax module???
123 $f = DB_DataObject::factory('Fax_Queue');
124 $aur['faxMax'] = $f->getMaxId();
125 $aur['faxNumPending'] = $f->getNumPending();
128 if ($this->hasModule('Documents')) {
130 $d = DB_DataObject::factory('Documents_Tracking');
131 $d->person_id = $au->id;
132 //$d->status = 0; // unread
133 $d->whereAdd('date_read IS NULL');
134 $d->applyFilters(array('query'=> array('unread' => 1)), $au);
135 $aur['inbox_unread'] = $d->count();
138 //echo '<PRE>';print_r($aur);
147 function switchUser($id)
149 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
150 $u = DB_DataObject::factory($tbl);
152 $this->err("not logged in");
155 $au = $u->getAuthUser();
158 // first check they have perms to do this..
159 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
160 $this->jerr("User switching not permitted");
164 $u = DB_DataObject::factory($tbl);
167 $this->jerr('Account disabled');
170 // we might need this later..
171 $this->addEvent("SWITCH USER", false, $au->name . ' TO ' . $u->name);
172 $this->jok("SWITCH");
180 //DB_DataObject::debugLevel(1);
181 if (!empty($_REQUEST['getAuthUser'])) {
182 $this->sendAuthUserDetails();
187 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
189 return $this->passwordRequest($_REQUEST['passwordRequest']);
193 if (!empty($_REQUEST['changePassword'])) {
194 return $this->changePassword($_REQUEST);
199 $ff = HTML_FlexyFramework::get();
200 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
203 $u = DB_DataObject::factory($tbl);
207 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
208 $this->jerr('You typed the wrong Username or Password (0)');
212 $u->authUserName($_REQUEST['username']);
215 if ($u->count() > 1 || !$u->find(true)) {
216 $this->jerr('You typed the wrong Username or Password (1)');
221 $this->jerr('Account disabled');
224 // check if config allows non-owner passwords.
225 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
226 // perhaps it should support arrays..
227 $ff= HTML_FlexyFramework::get();
228 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
229 $this->jerr("Login not permited to outside companies");
234 if ($u->checkPassword($_REQUEST['password'])) {
236 // we might need this later..
237 $this->addEvent("LOGIN", false, session_id());
238 if (!empty($_REQUEST['lang'])) {
239 $u->lang($_REQUEST['lang']);
243 $this->sendAuthUserDetails();
250 $this->jerr('You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
254 function passwordRequest($n)
256 $u = DB_DataObject::factory('Person');
257 //$u->company_id = $this->company->id;
259 $u->whereAdd('LENGTH(passwd) > 1');
262 if ($u->count() > 1 || !$u->find(true)) {
263 $this->jerr('invalid User (1)');
265 // got a avlid user..
266 if (!strlen($u->passwd)) {
267 $this->jerr('invalid User (2)');
269 // check to see if we have sent a request before..
271 if ($u->no_reset_sent > 3) {
272 $this->jerr('We have issued to many resets - please contact the Administrator');
274 $this->authFrom = time();
275 $this->authKey = $u->genPassKey($this->authFrom);
276 $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
278 $ret = $u->sendTemplate('password_reset', $this);
279 if (is_object($ret)) {
280 $this->addEvent('SYSERR',false, $ret->getMessage());
281 $this->jerr($ret->getMessage());
283 $this->addEvent('PASSREQ',$u, $u->email);
285 $uu->no_reset_sent++;
291 function changePassword($r)
293 $au = $this->getAuthUser();
296 $au->setPassword($r['passwd1']);
300 // not logged in -> need to validate
301 if (empty($r['passwordReset'])) {
302 $this->jerr("invalid request");
304 // same code as reset pasword
306 $bits = explode('/', $r['passwordReset']);
309 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
311 if ($res !== false) {
314 // key is correct.. let's change password...
316 $u = DB_DataObject::factory('Person');
318 //$u->company_id = $this->company->id;
319 $u->whereAdd('LENGTH(passwd) > 1');
321 if (!$u->get($bits[0])) {
322 $this->jerr("invalid id"); // should not happen!!!!
325 $u->setPassword($r['passwd1']);