Pman/Login.php
[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               
55                 session_regenerate_id(true);
56                 $u->logout();
57             }
58             // log it..
59             
60             $_SESSION['Pman_I18N'] = array();
61             
62         }
63         
64         // general query...
65         if (!empty($_REQUEST['getAuthUser'])) {
66             //DB_Dataobject::debugLevel(5);
67             $this->sendAuthUserDetails();
68             exit;
69            
70         }
71         if (!empty($_REQUEST['username'])) {
72             $this->post();
73         }
74         if (!empty($_REQUEST['switch'])) {
75             $this->switchUser($_REQUEST['switch']);
76         }
77         
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         // might occur on shared systems.
96         $ff= HTML_FlexyFramework::get();
97         if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $au->company()->comptype) {
98             $au->logout();
99             $this->jerr("Login not permited to outside companies - please reload");
100         }
101         
102         
103         $au = $u->getAuthUser();
104         
105         $aur = $au->authUserArray();
106          
107         /** -- these need modulizing somehow! **/
108         
109         
110         
111         // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
112         echo '<PRE>'; print_r($this->modules());
113         foreach($this->modules() as $m) {
114             if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
115                 continue;
116             }
117             $cls = 'Pman_'.$m.'_Login';
118             require_once 'Pman/'.$m.'/Login.php';
119             $x = new $cls;
120             $aur = $x->sendAuthUserDetails($aur);
121         }
122         
123                 
124         if ($this->hasModule('Fax')) {
125             // should check fax module???
126             $f = DB_DataObject::factory('Fax_Queue');
127             $aur['faxMax'] = $f->getMaxId();
128             $aur['faxNumPending'] = $f->getNumPending();
129         }
130         
131         if ($this->hasModule('Documents')) {
132         // inbox...
133             $d = DB_DataObject::factory('Documents_Tracking');
134             $d->person_id = $au->id;
135             //$d->status = 0; // unread
136             $d->whereAdd('date_read IS NULL');
137             $d->applyFilters(array('query'=> array('unread' => 1)), $au);
138             $aur['inbox_unread'] = $d->count();
139         }
140         
141         //echo '<PRE>';print_r($aur);
142         
143         $this->jok($aur);
144         exit;
145         
146             
147     }
148
149     
150     function switchUser($id)
151     {
152         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
153         $u = DB_DataObject::factory($tbl);
154         if (!$u->isAuth()) {
155             $this->err("not logged in");
156         }
157         
158         $au = $u->getAuthUser();
159         
160         
161         // first check they have perms to do this..
162         if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
163             $this->jerr("User switching not permitted");
164         }
165         
166         
167         $u = DB_DataObject::factory($tbl);
168         $u->get($id);
169         if (!$u->active()) {
170             $this->jerr('Account disabled');
171         }
172         $u->login();
173             // we might need this later..
174         $this->addEvent("SWITCH USER", false, $au->name . ' TO ' . $u->name);
175         $this->jok("SWITCH");
176         
177     }
178     
179     
180     var $domObj = false;
181     function post()
182     {
183         //DB_DataObject::debugLevel(1);
184         if (!empty($_REQUEST['getAuthUser'])) {
185             $this->sendAuthUserDetails();
186             exit;
187         }
188         
189         
190         if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
191             
192             return $this->passwordRequest($_REQUEST['passwordRequest']);
193             
194         }
195         
196         if (!empty($_REQUEST['changePassword'])) {
197             return $this->changePassword($_REQUEST);
198         }
199         
200         // login attempt..
201         
202         $ff = HTML_FlexyFramework::get();
203         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
204         
205        
206         $u = DB_DataObject::factory($tbl);
207         //$u->active = 1;
208         
209         
210         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
211             $this->jerr('You typed the wrong Username or Password (0)');
212             exit;
213         }
214         
215         $u->authUserName($_REQUEST['username']);
216         
217         
218         if ($u->count() > 1 || !$u->find(true)) {
219             $this->jerr('You typed the wrong Username or Password  (1)');
220             exit;
221         }
222         
223         if (!$u->active()) {
224             $this->jerr('Account disabled');
225         }
226         
227         // check if config allows non-owner passwords.
228         // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
229         // perhaps it should support arrays..
230         $ff= HTML_FlexyFramework::get();
231         if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
232             $this->jerr("Login not permited to outside companies");
233         }
234         
235         
236         
237         if ($u->checkPassword($_REQUEST['password'])) {
238             $u->login();
239             // we might need this later..
240             $this->addEvent("LOGIN", false, session_id());
241             if (!empty($_REQUEST['lang'])) {
242                 $u->lang($_REQUEST['lang']);
243             }
244              // log it..
245             
246             $this->sendAuthUserDetails();
247             exit;
248
249             //exit;
250         }
251         
252          
253         $this->jerr('You typed the wrong Username or Password  (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
254         exit;
255     }
256     
257     function passwordRequest($n) 
258     {
259         $u = DB_DataObject::factory('Person');
260         //$u->company_id = $this->company->id;
261         
262         $u->whereAdd('LENGTH(passwd) > 1');
263         $u->email = $n;
264         $u->active = 1;
265         if ($u->count() > 1 || !$u->find(true)) {
266             $this->jerr('invalid User (1)');
267         }
268         // got a avlid user..
269         if (!strlen($u->passwd)) {
270             $this->jerr('invalid User (2)');
271         }
272         // check to see if we have sent a request before..
273         
274         if ($u->no_reset_sent > 3) {
275             $this->jerr('We have issued to many resets - please contact the Administrator');
276         }
277         $this->authFrom = time();
278         $this->authKey = $u->genPassKey($this->authFrom);
279         $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
280         
281         $ret =  $u->sendTemplate('password_reset', $this);
282         if (is_object($ret)) {
283             $this->addEvent('SYSERR',false, $ret->getMessage());
284             $this->jerr($ret->getMessage());
285         }
286         $this->addEvent('PASSREQ',$u, $u->email);
287         $uu = clone($u);
288         $uu->no_reset_sent++;
289         $uu->update($u);
290         $this->jok("done");
291         
292         
293     }
294     function changePassword($r)
295     {
296         $au = $this->getAuthUser();
297         if ($au) {
298             $uu = clone($au);
299             $au->setPassword($r['passwd1']);
300             $au->update($uu);
301             $this->jok($au);
302         }
303         // not logged in -> need to validate 
304         if (empty($r['passwordReset'])) {
305             $this->jerr("invalid request");
306         }
307         // same code as reset pasword
308        
309         $bits = explode('/', $r['passwordReset']);
310         //print_R($bits);
311       
312         $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
313           
314         if ($res !== false) {
315             $this->jerr($res);
316         }
317         // key is correct.. let's change password...
318         
319         $u = DB_DataObject::factory('Person');
320         
321         //$u->company_id = $this->company->id;
322         $u->whereAdd('LENGTH(passwd) > 1');
323         $u->active = 1;
324         if (!$u->get($bits[0])) {
325            $this->jerr("invalid id"); // should not happen!!!!
326         }
327         $uu = clone($u);
328         $u->setPassword($r['passwd1']);
329         $u->update($uu);
330         $u->login();
331         
332         $this->jok($u);
333     }
334     
335     
336     
337 }
338