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