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