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         $this->jerr("INVALID REQUEST");
75         exit;
76     }
77     
78     function sendAuthUserDetails()
79     {
80        // DB_DataObject::debugLevel(1);
81         $ff = HTML_FlexyFramework::get();
82         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
83         
84         $u = DB_DataObject::factory($tbl);
85         if (!$u->isAuth()) {
86             $this->jok(array('id' => 0)); // not logged in..
87             exit;
88         }
89         $au = $u->getAuthUser();
90         
91         $aur = $au->authUserArray();
92          
93         /** -- these need modulizing somehow! **/
94         
95         if ($this->hasModule('Fax')) {
96             // should check fax module???
97             $f = DB_DataObject::factory('Fax_Queue');
98             $aur['faxMax'] = $f->getMaxId();
99             $aur['faxNumPending'] = $f->getNumPending();
100         }
101         
102         if ($this->hasModule('Documents')) {
103         // inbox...
104             $d = DB_DataObject::factory('Documents_Tracking');
105             $d->person_id = $au->id;
106             //$d->status = 0; // unread
107             $d->whereAdd('date_read IS NULL');
108             $d->applyFilters(array('query'=> array('unread' => 1)), $au);
109             $aur['inbox_unread'] = $d->count();
110         }
111         
112         //echo '<PRE>';print_r($aur);
113         
114         $this->jok($aur);
115         exit;
116         
117             
118     }
119
120     
121     var $domObj = false;
122     function post()
123     {
124         //DB_DataObject::debugLevel(1);
125         if (!empty($_REQUEST['getAuthUser'])) {
126             $this->sendAuthUserDetails();
127             exit;
128         }
129         
130         
131         if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
132             
133             return $this->passwordRequest($_REQUEST['passwordRequest']);
134             
135         }
136         
137         if (!empty($_REQUEST['changePassword'])) {
138             return $this->changePassword($_REQUEST);
139         }
140         
141         // login attempt..
142         
143         $ff = HTML_FlexyFramework::get();
144         $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
145         
146        
147         $u = DB_DataObject::factory($tbl);
148         //$u->active = 1;
149         
150         
151         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
152             $this->jerr('You typed the wrong Username or Password (0)');
153             exit;
154         }
155         
156         $u->authUserName($_REQUEST['username']);
157         
158         
159         if ($u->count() > 1 || !$u->find(true)) {
160             $this->jerr('You typed the wrong Username or Password  (1)');
161             exit;
162         }
163         
164         if (!$u->active()) {
165             $this->jerr('Account disabled');
166         }
167         
168         if ($u->checkPassword($_REQUEST['password'])) {
169             $u->login();
170             // we might need this later..
171             $this->addEvent("LOGIN", false, session_id());
172             if (!empty($_REQUEST['lang'])) {
173                 $u->lang($_REQUEST['lang']);
174             }
175              // log it..
176             
177             $this->sendAuthUserDetails();
178             exit;
179
180             //exit;
181         }
182         
183          
184         $this->jerr('You typed the wrong Username or Password  (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
185         exit;
186     }
187     
188     function passwordRequest($n) 
189     {
190         $u = DB_DataObject::factory('Person');
191         //$u->company_id = $this->company->id;
192         
193         $u->whereAdd('LENGTH(passwd) > 1');
194         $u->email = $n;
195         $u->active = 1;
196         if ($u->count() > 1 || !$u->find(true)) {
197             $this->jerr('invalid User (1)');
198         }
199         // got a avlid user..
200         if (!strlen($u->passwd)) {
201             $this->jerr('invalid User (2)');
202         }
203         // check to see if we have sent a request before..
204         
205         if ($u->no_reset_sent > 3) {
206             $this->jerr('We have issued to many resets - please contact the Administrator');
207         }
208         $this->authFrom = time();
209         $this->authKey = $u->genPassKey($this->authFrom);
210         $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
211         
212         $ret =  $u->sendTemplate('password_reset', $this);
213         if (is_object($ret)) {
214             $this->addEvent('SYSERR',false, $ret->getMessage());
215             $this->jerr($ret->getMessage());
216         }
217         $this->addEvent('PASSREQ',$u, $u->email);
218         $uu = clone($u);
219         $uu->no_reset_sent++;
220         $uu->update($u);
221         $this->jok("done");
222         
223         
224     }
225     function changePassword($r)
226     {
227         $au = $this->getAuthUser();
228         if ($au) {
229             $uu = clone($au);
230             $au->setPassword($r['passwd1']);
231             $au->update($uu);
232             $this->jok($au);
233         }
234         // not logged in -> need to validate 
235         if (empty($r['passwordReset'])) {
236             $this->jerr("invalid request");
237         }
238         // same code as reset pasword
239        
240         $bits = explode('/', $r['passwordReset']);
241         //print_R($bits);
242       
243         $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
244           
245         if ($res !== false) {
246             $this->jerr($res);
247         }
248         // key is correct.. let's change password...
249         
250         $u = DB_DataObject::factory('Person');
251         
252         //$u->company_id = $this->company->id;
253         $u->whereAdd('LENGTH(passwd) > 1');
254         $u->active = 1;
255         if (!$u->get($bits[0])) {
256            $this->jerr("invalid id"); // should not happen!!!!
257         }
258         $uu = clone($u);
259         $u->setPassword($r['passwd1']);
260         $u->update($uu);
261         $u->login();
262         
263         $this->jok($u);
264     }
265     
266     
267     
268 }
269