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