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