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