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