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