3 require_once 'Pman.php';
5 class Pman_Login extends Pman
8 var $masterTemplate = 'login.html';
10 function getAuth() // everyone allowed in here..
12 parent::getAuth(); // load company..
28 if (!empty($_REQUEST['logout'])) {
29 $u = $this->getAuthUser();
32 $this->addEvent('LOGOUT');
37 $_SESSION['Pman_I18N'] = array();
42 if (!empty($_REQUEST['getAuthUser'])) {
43 $this->sendAuthUserDetails();
48 $this->jerr("INVALID REQUEST");
52 function sendAuthUserDetails()
54 $u = DB_DataObject::factory('Person');
56 $this->jok(array('id' => 0)); // not logged in..
59 $au = $u->getAuthUser();
60 $aur = $au->toArray();
61 //DB_DataObject::debugLevel(1);
62 $c = DB_Dataobject::factory('Companies');
63 $im = DB_Dataobject::factory('Images');
64 $c->joinAdd($im, 'LEFT');
66 $c->selectAs($c, 'company_id_%s');
67 $c->selectAs($im, 'company_id_logo_id_%s');
68 $c->id = $au->company_id;
72 $aur = array_merge( $c->toArray(),$aur);
74 if (empty($c->company_id_logo_id_id)) {
76 $im = DB_Dataobject::factory('Images');
77 $im->ontable = 'Companies';
79 $im->imgtype = 'LOGO';
81 $im->selectAs($im, 'company_id_logo_id_%s');
82 if ($im->find(true)) {
84 foreach($im->toArray() as $k=>$v) {
90 // i18n language and coutry lists.
93 $lang = empty($au->lang) ? 'en' : $au->lang;
94 if (empty($_SESSION['Pman_I18N'][$lang])) {
95 require_once 'Pman/I18N.php';
101 $aur['i18n'] =$_SESSION['Pman_I18N'][$lang];
104 $aur['perms'] = $au->getPerms();
105 $g = DB_DataObject::Factory('Group_Members');
106 $aur['groups'] = $g->listGroupMembership($au, 'name');
109 $aur['dailykey'] = '';
111 /** -- these need modulizing somehow! **/
113 if ($this->hasModule('Fax')) {
114 // should check fax module???
115 $f = DB_DataObject::factory('Fax_Queue');
116 $aur['faxMax'] = $f->getMaxId();
117 $aur['faxNumPending'] = $f->getNumPending();
120 if ($this->hasModule('Documents')) {
122 $d = DB_DataObject::factory('Documents_Tracking');
123 $d->person_id = $au->id;
124 //$d->status = 0; // unread
125 $d->whereAdd('date_read IS NULL');
126 $d->applyFilters(array('query'=> array('unread' => 1)), $au);
127 $aur['inbox_unread'] = $d->count();
130 //echo '<PRE>';print_r($aur);
143 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
145 return $this->passwordRequest($_REQUEST['passwordRequest']);
149 if (!empty($_REQUEST['changePassword'])) {
150 return $this->changePassword($_REQUEST);
154 $u = DB_DataObject::factory('Person');
156 $u->whereAdd('LENGTH(passwd) > 1');
157 //$u->company_id = $this->company->id;
159 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
160 $this->jerr('You typed the wrong Username or Password (0)');
164 $u->email = $_REQUEST['username'];
165 if ($u->count() > 1 || !$u->find(true)) {
166 $this->jerr('You typed the wrong Username or Password (1)');
171 $this->jerr('Account disabled');
174 if ($u->checkPassword($_REQUEST['password'])) {
176 $this->AddEvent("LOGIN");
177 if (!empty($_REQUEST['lang']) && $_REQUEST['lang'] != $u->lang) {
179 $uu->lang = $_REQUEST['lang'];
184 $this->sendAuthUserDetails();
191 $this->jerr('You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
195 function passwordRequest($n)
197 $u = DB_DataObject::factory('Person');
198 //$u->company_id = $this->company->id;
200 $u->whereAdd('LENGTH(passwd) > 1');
203 if ($u->count() > 1 || !$u->find(true)) {
204 $this->jerr('invalid User (1)');
206 // got a avlid user..
207 if (!strlen($u->passwd)) {
208 $this->jerr('invalid User (2)');
210 // check to see if we have sent a request before..
212 if ($u->no_reset_sent > 3) {
213 $this->jerr('We have issued to many resets - please contact the Administrator');
215 $this->authFrom = time();
216 $this->authKey = $u->genPassKey($this->authFrom);
217 $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
219 $ret = $u->sendTemplate('password_reset', $this);
220 if (is_object($ret)) {
221 $this->addEvent('SYSERR',false, $ret->getMessage());
222 $this->jerr($ret->getMessage());
224 $this->addEvent('PASSREQ',$u, $u->email);
226 $uu->no_reset_sent++;
232 function changePassword($r)
234 $au = $this->getAuthUser();
237 $au->setPassword($r['passwd1']);
241 // not logged in -> need to validate
242 if (empty($r['passwordReset'])) {
243 $this->jerr("invalid request");
245 // same code as reset pasword
247 $bits = explode('/', $r['passwordReset']);
250 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
252 if ($res !== false) {
255 // key is correct.. let's change password...
257 $u = DB_DataObject::factory('Person');
259 //$u->company_id = $this->company->id;
260 $u->whereAdd('LENGTH(passwd) > 1');
262 if (!$u->get($bits[0])) {
263 $this->jerr("invalid id"); // should not happen!!!!
266 $u->setPassword($r['passwd1']);