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