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