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