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