5add22c1ec8360957ab69d57888732d54b29166f
[Pman.Base] / Pman / Login.php
1 <?php
2
3 require_once 'Pman.php';
4
5 /***
6
7 * Auth wrapper..
8
9 * User class must provide the following features.
10
11 * logout()
12 * isAuth() 
13 * getAuthUser();
14 * authUserArray() 
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'])) {
18 * login();
19 * lang(val) - to set the language..
20 */
21
22
23
24 class Pman_Login extends Pman
25
26     
27     var $masterTemplate = 'login.html';
28     
29     function getAuth() // everyone allowed in here..
30     {
31         parent::getAuth(); // load company..
32         return true;
33         
34     }
35     /**
36      * Accepts:
37      * logout =
38      * 
39      * 
40      */
41     function get() 
42     {
43         
44         
45         
46         
47         if (!empty($_REQUEST['logout'])) {
48             $u = $this->getAuthUser();
49             //print_r($u);
50             if ($u) {
51                 
52                 $this->addEvent('LOGOUT');
53                 $e = DB_DataObject::factory('Events');
54                 $e->query("UPDATE Events SET remarks = '' WHERE 
55                     person_id = {$u->id} AND
56                     action = 'LOGIN' AND
57                     remarks = '". $e->escape(session_id()) . "'");
58                     
59                 session_regenerate_id(true);
60                 $u->logout();
61             }
62             // log it..
63             
64             $_SESSION['Pman_I18N'] = array();
65             
66         }
67         
68         // general query...
69         if (!empty($_REQUEST['getAuthUser'])) {
70             //DB_Dataobject::debugLevel(5);
71             $this->sendAuthUserDetails();
72             exit;
73            
74         }
75         if (!empty($_REQUEST['username'])) {
76             $this->post();
77         }
78         $this->jerr("INVALID REQUEST");
79         exit;
80     }
81     
82     function sendAuthUserDetails()
83     {
84        // DB_DataObject::debugLevel(1);
85         $ff = HTML_FlexyFramework::get();
86         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
87         
88         $u = DB_DataObject::factory($tbl);
89         if (!$u->isAuth()) {
90             $this->jok(array('id' => 0)); // not logged in..
91             exit;
92         }
93         $au = $u->getAuthUser();
94         
95         $aur = $au->authUserArray();
96          
97         /** -- these need modulizing somehow! **/
98         
99         if ($this->hasModule('Fax')) {
100             // should check fax module???
101             $f = DB_DataObject::factory('Fax_Queue');
102             $aur['faxMax'] = $f->getMaxId();
103             $aur['faxNumPending'] = $f->getNumPending();
104         }
105         
106         if ($this->hasModule('Documents')) {
107         // inbox...
108             $d = DB_DataObject::factory('Documents_Tracking');
109             $d->person_id = $au->id;
110             //$d->status = 0; // unread
111             $d->whereAdd('date_read IS NULL');
112             $d->applyFilters(array('query'=> array('unread' => 1)), $au);
113             $aur['inbox_unread'] = $d->count();
114         }
115         
116         //echo '<PRE>';print_r($aur);
117         
118         $this->jok($aur);
119         exit;
120         
121             
122     }
123
124     
125     var $domObj = false;
126     function post()
127     {
128         //DB_DataObject::debugLevel(1);
129         if (!empty($_REQUEST['getAuthUser'])) {
130             $this->sendAuthUserDetails();
131             exit;
132         }
133         
134         
135         if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
136             
137             return $this->passwordRequest($_REQUEST['passwordRequest']);
138             
139         }
140         
141         if (!empty($_REQUEST['changePassword'])) {
142             return $this->changePassword($_REQUEST);
143         }
144         
145         // login attempt..
146         
147         $ff = HTML_FlexyFramework::get();
148         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
149         
150        
151         $u = DB_DataObject::factory($tbl);
152         //$u->active = 1;
153         
154         
155         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
156             $this->jerr('You typed the wrong Username or Password (0)');
157             exit;
158         }
159         
160         $u->authUserName($_REQUEST['username']);
161         
162         
163         if ($u->count() > 1 || !$u->find(true)) {
164             $this->jerr('You typed the wrong Username or Password  (1)');
165             exit;
166         }
167         
168         if (!$u->active()) {
169             $this->jerr('Account disabled');
170         }
171         
172         if ($u->checkPassword($_REQUEST['password'])) {
173             $u->login();
174             $this->addEvent("LOGIN", false, session_id());
175             if (!empty($_REQUEST['lang'])) {
176                 $u->lang($_REQUEST['lang']);
177             }
178              // log it..
179             
180             $this->sendAuthUserDetails();
181             exit;
182
183             //exit;
184         }
185         
186          
187         $this->jerr('You typed the wrong Username or Password  (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
188         exit;
189     }
190     
191     function passwordRequest($n) 
192     {
193         $u = DB_DataObject::factory('Person');
194         //$u->company_id = $this->company->id;
195         
196         $u->whereAdd('LENGTH(passwd) > 1');
197         $u->email = $n;
198         $u->active = 1;
199         if ($u->count() > 1 || !$u->find(true)) {
200             $this->jerr('invalid User (1)');
201         }
202         // got a avlid user..
203         if (!strlen($u->passwd)) {
204             $this->jerr('invalid User (2)');
205         }
206         // check to see if we have sent a request before..
207         
208         if ($u->no_reset_sent > 3) {
209             $this->jerr('We have issued to many resets - please contact the Administrator');
210         }
211         $this->authFrom = time();
212         $this->authKey = $u->genPassKey($this->authFrom);
213         $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
214         
215         $ret =  $u->sendTemplate('password_reset', $this);
216         if (is_object($ret)) {
217             $this->addEvent('SYSERR',false, $ret->getMessage());
218             $this->jerr($ret->getMessage());
219         }
220         $this->addEvent('PASSREQ',$u, $u->email);
221         $uu = clone($u);
222         $uu->no_reset_sent++;
223         $uu->update($u);
224         $this->jok("done");
225         
226         
227     }
228     function changePassword($r)
229     {
230         $au = $this->getAuthUser();
231         if ($au) {
232             $uu = clone($au);
233             $au->setPassword($r['passwd1']);
234             $au->update($uu);
235             $this->jok($au);
236         }
237         // not logged in -> need to validate 
238         if (empty($r['passwordReset'])) {
239             $this->jerr("invalid request");
240         }
241         // same code as reset pasword
242        
243         $bits = explode('/', $r['passwordReset']);
244         //print_R($bits);
245       
246         $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
247           
248         if ($res !== false) {
249             $this->jerr($res);
250         }
251         // key is correct.. let's change password...
252         
253         $u = DB_DataObject::factory('Person');
254         
255         //$u->company_id = $this->company->id;
256         $u->whereAdd('LENGTH(passwd) > 1');
257         $u->active = 1;
258         if (!$u->get($bits[0])) {
259            $this->jerr("invalid id"); // should not happen!!!!
260         }
261         $uu = clone($u);
262         $u->setPassword($r['passwd1']);
263         $u->update($uu);
264         $u->login();
265         
266         $this->jok($u);
267     }
268     
269     
270     
271 }
272