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