239476606f885b61e9a415a4a6adc696cf3f4068
[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                 
60                 session_regenerate_id(true);
61                 $u->logout();
62             }
63             // log it..
64             
65             $_SESSION['Pman_I18N'] = array();
66             
67         }
68         
69         // general query...
70         if (!empty($_REQUEST['getAuthUser'])) {
71             //DB_Dataobject::debugLevel(5);
72             $this->sendAuthUserDetails();
73             exit;
74            
75         }
76         if (!empty($_REQUEST['username'])) {
77             $this->post();
78         }
79         $this->jerr("INVALID REQUEST");
80         exit;
81     }
82     
83     function sendAuthUserDetails()
84     {
85        // DB_DataObject::debugLevel(1);
86         $ff = HTML_FlexyFramework::get();
87         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
88         
89         $u = DB_DataObject::factory($tbl);
90         if (!$u->isAuth()) {
91             $this->jok(array('id' => 0)); // not logged in..
92             exit;
93         }
94         $au = $u->getAuthUser();
95         
96         $aur = $au->authUserArray();
97          
98         /** -- these need modulizing somehow! **/
99         
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();
105         }
106         
107         if ($this->hasModule('Documents')) {
108         // inbox...
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();
115         }
116         
117         //echo '<PRE>';print_r($aur);
118         
119         $this->jok($aur);
120         exit;
121         
122             
123     }
124
125     
126     var $domObj = false;
127     function post()
128     {
129         //DB_DataObject::debugLevel(1);
130         if (!empty($_REQUEST['getAuthUser'])) {
131             $this->sendAuthUserDetails();
132             exit;
133         }
134         
135         
136         if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
137             
138             return $this->passwordRequest($_REQUEST['passwordRequest']);
139             
140         }
141         
142         if (!empty($_REQUEST['changePassword'])) {
143             return $this->changePassword($_REQUEST);
144         }
145         
146         // login attempt..
147         
148         $ff = HTML_FlexyFramework::get();
149         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
150         
151        
152         $u = DB_DataObject::factory($tbl);
153         //$u->active = 1;
154         
155         
156         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
157             $this->jerr('You typed the wrong Username or Password (0)');
158             exit;
159         }
160         
161         $u->authUserName($_REQUEST['username']);
162         
163         
164         if ($u->count() > 1 || !$u->find(true)) {
165             $this->jerr('You typed the wrong Username or Password  (1)');
166             exit;
167         }
168         
169         if (!$u->active()) {
170             $this->jerr('Account disabled');
171         }
172         
173         if ($u->checkPassword($_REQUEST['password'])) {
174             $u->login();
175             $this->addEvent("LOGIN", false, session_id());
176             if (!empty($_REQUEST['lang'])) {
177                 $u->lang($_REQUEST['lang']);
178             }
179              // log it..
180             
181             $this->sendAuthUserDetails();
182             exit;
183
184             //exit;
185         }
186         
187          
188         $this->jerr('You typed the wrong Username or Password  (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
189         exit;
190     }
191     
192     function passwordRequest($n) 
193     {
194         $u = DB_DataObject::factory('Person');
195         //$u->company_id = $this->company->id;
196         
197         $u->whereAdd('LENGTH(passwd) > 1');
198         $u->email = $n;
199         $u->active = 1;
200         if ($u->count() > 1 || !$u->find(true)) {
201             $this->jerr('invalid User (1)');
202         }
203         // got a avlid user..
204         if (!strlen($u->passwd)) {
205             $this->jerr('invalid User (2)');
206         }
207         // check to see if we have sent a request before..
208         
209         if ($u->no_reset_sent > 3) {
210             $this->jerr('We have issued to many resets - please contact the Administrator');
211         }
212         $this->authFrom = time();
213         $this->authKey = $u->genPassKey($this->authFrom);
214         $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
215         
216         $ret =  $u->sendTemplate('password_reset', $this);
217         if (is_object($ret)) {
218             $this->addEvent('SYSERR',false, $ret->getMessage());
219             $this->jerr($ret->getMessage());
220         }
221         $this->addEvent('PASSREQ',$u, $u->email);
222         $uu = clone($u);
223         $uu->no_reset_sent++;
224         $uu->update($u);
225         $this->jok("done");
226         
227         
228     }
229     function changePassword($r)
230     {
231         $au = $this->getAuthUser();
232         if ($au) {
233             $uu = clone($au);
234             $au->setPassword($r['passwd1']);
235             $au->update($uu);
236             $this->jok($au);
237         }
238         // not logged in -> need to validate 
239         if (empty($r['passwordReset'])) {
240             $this->jerr("invalid request");
241         }
242         // same code as reset pasword
243        
244         $bits = explode('/', $r['passwordReset']);
245         //print_R($bits);
246       
247         $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
248           
249         if ($res !== false) {
250             $this->jerr($res);
251         }
252         // key is correct.. let's change password...
253         
254         $u = DB_DataObject::factory('Person');
255         
256         //$u->company_id = $this->company->id;
257         $u->whereAdd('LENGTH(passwd) > 1');
258         $u->active = 1;
259         if (!$u->get($bits[0])) {
260            $this->jerr("invalid id"); // should not happen!!!!
261         }
262         $uu = clone($u);
263         $u->setPassword($r['passwd1']);
264         $u->update($uu);
265         $u->login();
266         
267         $this->jok($u);
268     }
269     
270     
271     
272 }
273