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