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