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 // might occur on shared systems.
95 $ff= HTML_FlexyFramework::get();
96 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
97 $this->jerr("Login not permited to outside companies");
101 $au = $u->getAuthUser();
103 $aur = $au->authUserArray();
105 /** -- these need modulizing somehow! **/
107 if ($this->hasModule('Fax')) {
108 // should check fax module???
109 $f = DB_DataObject::factory('Fax_Queue');
110 $aur['faxMax'] = $f->getMaxId();
111 $aur['faxNumPending'] = $f->getNumPending();
114 if ($this->hasModule('Documents')) {
116 $d = DB_DataObject::factory('Documents_Tracking');
117 $d->person_id = $au->id;
118 //$d->status = 0; // unread
119 $d->whereAdd('date_read IS NULL');
120 $d->applyFilters(array('query'=> array('unread' => 1)), $au);
121 $aur['inbox_unread'] = $d->count();
124 //echo '<PRE>';print_r($aur);
133 function switchUser($id)
135 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
136 $u = DB_DataObject::factory($tbl);
138 $this->err("not logged in");
141 $au = $u->getAuthUser();
144 // first check they have perms to do this..
145 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
146 $this->jerr("User switching not permitted");
150 $u = DB_DataObject::factory($tbl);
153 $this->jerr('Account disabled');
156 // we might need this later..
157 $this->addEvent("SWITCH USER", false, $au->name . ' TO ' . $u->name);
158 $this->jok("SWITCH");
166 //DB_DataObject::debugLevel(1);
167 if (!empty($_REQUEST['getAuthUser'])) {
168 $this->sendAuthUserDetails();
173 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
175 return $this->passwordRequest($_REQUEST['passwordRequest']);
179 if (!empty($_REQUEST['changePassword'])) {
180 return $this->changePassword($_REQUEST);
185 $ff = HTML_FlexyFramework::get();
186 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
189 $u = DB_DataObject::factory($tbl);
193 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
194 $this->jerr('You typed the wrong Username or Password (0)');
198 $u->authUserName($_REQUEST['username']);
201 if ($u->count() > 1 || !$u->find(true)) {
202 $this->jerr('You typed the wrong Username or Password (1)');
207 $this->jerr('Account disabled');
210 // check if config allows non-owner passwords.
211 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
212 // perhaps it should support arrays..
213 $ff= HTML_FlexyFramework::get();
214 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
215 $this->jerr("Login not permited to outside companies");
220 if ($u->checkPassword($_REQUEST['password'])) {
222 // we might need this later..
223 $this->addEvent("LOGIN", false, session_id());
224 if (!empty($_REQUEST['lang'])) {
225 $u->lang($_REQUEST['lang']);
229 $this->sendAuthUserDetails();
236 $this->jerr('You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
240 function passwordRequest($n)
242 $u = DB_DataObject::factory('Person');
243 //$u->company_id = $this->company->id;
245 $u->whereAdd('LENGTH(passwd) > 1');
248 if ($u->count() > 1 || !$u->find(true)) {
249 $this->jerr('invalid User (1)');
251 // got a avlid user..
252 if (!strlen($u->passwd)) {
253 $this->jerr('invalid User (2)');
255 // check to see if we have sent a request before..
257 if ($u->no_reset_sent > 3) {
258 $this->jerr('We have issued to many resets - please contact the Administrator');
260 $this->authFrom = time();
261 $this->authKey = $u->genPassKey($this->authFrom);
262 $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
264 $ret = $u->sendTemplate('password_reset', $this);
265 if (is_object($ret)) {
266 $this->addEvent('SYSERR',false, $ret->getMessage());
267 $this->jerr($ret->getMessage());
269 $this->addEvent('PASSREQ',$u, $u->email);
271 $uu->no_reset_sent++;
277 function changePassword($r)
279 $au = $this->getAuthUser();
282 $au->setPassword($r['passwd1']);
286 // not logged in -> need to validate
287 if (empty($r['passwordReset'])) {
288 $this->jerr("invalid request");
290 // same code as reset pasword
292 $bits = explode('/', $r['passwordReset']);
295 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
297 if ($res !== false) {
300 // key is correct.. let's change password...
302 $u = DB_DataObject::factory('Person');
304 //$u->company_id = $this->company->id;
305 $u->whereAdd('LENGTH(passwd) > 1');
307 if (!$u->get($bits[0])) {
308 $this->jerr("invalid id"); // should not happen!!!!
311 $u->setPassword($r['passwd1']);