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();
96 $aur = $au->authUserArray();
98 /** -- these need modulizing somehow! **/
100 if ($this->hasModule('Fax')) {
101 // should check fax module???
102 $f = DB_DataObject::factory('Fax_Queue');
103 $aur['faxMax'] = $f->getMaxId();
104 $aur['faxNumPending'] = $f->getNumPending();
107 if ($this->hasModule('Documents')) {
109 $d = DB_DataObject::factory('Documents_Tracking');
110 $d->person_id = $au->id;
111 //$d->status = 0; // unread
112 $d->whereAdd('date_read IS NULL');
113 $d->applyFilters(array('query'=> array('unread' => 1)), $au);
114 $aur['inbox_unread'] = $d->count();
117 //echo '<PRE>';print_r($aur);
126 function switchUser($id)
128 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
129 $u = DB_DataObject::factory($tbl);
131 $this->err("not logged in");
134 $au = $u->getAuthUser();
137 // first check they have perms to do this..
138 if (!$au|| ($au->company_id_comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
139 $this->jerr("User switching not permitted");
143 $u = DB_DataObject::factory($tbl);
146 $this->jerr('Account disabled');
149 // we might need this later..
150 $this->addEvent("SWITCH USER", false, $old->name . ' TO ' . $u->name);
151 $this->jok("SWITCH");
159 //DB_DataObject::debugLevel(1);
160 if (!empty($_REQUEST['getAuthUser'])) {
161 $this->sendAuthUserDetails();
166 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
168 return $this->passwordRequest($_REQUEST['passwordRequest']);
172 if (!empty($_REQUEST['changePassword'])) {
173 return $this->changePassword($_REQUEST);
178 $ff = HTML_FlexyFramework::get();
179 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
182 $u = DB_DataObject::factory($tbl);
186 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
187 $this->jerr('You typed the wrong Username or Password (0)');
191 $u->authUserName($_REQUEST['username']);
194 if ($u->count() > 1 || !$u->find(true)) {
195 $this->jerr('You typed the wrong Username or Password (1)');
200 $this->jerr('Account disabled');
203 if ($u->checkPassword($_REQUEST['password'])) {
205 // we might need this later..
206 $this->addEvent("LOGIN", false, session_id());
207 if (!empty($_REQUEST['lang'])) {
208 $u->lang($_REQUEST['lang']);
212 $this->sendAuthUserDetails();
219 $this->jerr('You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
223 function passwordRequest($n)
225 $u = DB_DataObject::factory('Person');
226 //$u->company_id = $this->company->id;
228 $u->whereAdd('LENGTH(passwd) > 1');
231 if ($u->count() > 1 || !$u->find(true)) {
232 $this->jerr('invalid User (1)');
234 // got a avlid user..
235 if (!strlen($u->passwd)) {
236 $this->jerr('invalid User (2)');
238 // check to see if we have sent a request before..
240 if ($u->no_reset_sent > 3) {
241 $this->jerr('We have issued to many resets - please contact the Administrator');
243 $this->authFrom = time();
244 $this->authKey = $u->genPassKey($this->authFrom);
245 $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
247 $ret = $u->sendTemplate('password_reset', $this);
248 if (is_object($ret)) {
249 $this->addEvent('SYSERR',false, $ret->getMessage());
250 $this->jerr($ret->getMessage());
252 $this->addEvent('PASSREQ',$u, $u->email);
254 $uu->no_reset_sent++;
260 function changePassword($r)
262 $au = $this->getAuthUser();
265 $au->setPassword($r['passwd1']);
269 // not logged in -> need to validate
270 if (empty($r['passwordReset'])) {
271 $this->jerr("invalid request");
273 // same code as reset pasword
275 $bits = explode('/', $r['passwordReset']);
278 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
280 if ($res !== false) {
283 // key is correct.. let's change password...
285 $u = DB_DataObject::factory('Person');
287 //$u->company_id = $this->company->id;
288 $u->whereAdd('LENGTH(passwd) > 1');
290 if (!$u->get($bits[0])) {
291 $this->jerr("invalid id"); // should not happen!!!!
294 $u->setPassword($r['passwd1']);