Merge remote branch 'remotes/origin/HEAD'
[web.mtrack] / MTrackWeb / Login.php
1 <?php
2
3 require_once 'MTrackWeb.php';
4
5
6 class MTrackWeb_Login extends MTrackWeb
7 {
8     var $template = 'login.html';
9     
10     function getAuth()
11     {
12         return true;
13     }
14     
15     function get($u='', $ar=array())
16     {
17         
18         if (!empty($u)) {
19             /** ---------- reset passwords ----- */
20             $uu = explode('/', $u);
21             if ($uu[0] != 'Reset') {
22                 $this->err('404', 'Invalid URL');
23             }
24             $u = DB_DataObject::factory('Person');
25             if (!$u->get($uu[1])) {
26                 $this->err('404', 'Invalid URL');
27             }
28             $uu[2] = (int) $uu[2];
29             
30             if ($uu[2] <  (time() - (60 * 60 * 24 * 2))) { // older than 2 days.
31                 HTML_FlexyFramework::run('ForgotPassword', array('errors' => array('reset_expired' => 1)));
32                 exit;
33             }
34            // print_R($u->genPassKey($uu[2]));
35             if ($uu[3] != $u->genPassKey($uu[2])) {
36                 HTML_FlexyFramework::run('ForgotPassword', array('errors' => array('reset_invalid' => 1)));
37                 exit;
38             }
39             // do reset!!!.
40             
41             $pp = clone($u);
42             $u->generatePassword();
43             $u->update($pp);
44             $u->sendTemplate('reset_password', $this);
45             
46             $this->warnings = array('password_sent' => true);
47             
48         }
49         
50         if (!empty($ar)) {
51             foreach($ar as $k=>$v) {
52                 $this->$k = $v;
53             }
54         }
55         
56         
57         if (!empty($_REQUEST['logout'])) {
58             if ($this->getAuthUser()) {
59                 $this->getAuthUser()->logout();
60             }
61             HTML_FlexyFramework::run(''); // 
62             
63             
64         }
65         
66         if (!empty($_POST)) {
67             require_once 'HTML/Template/Flexy/Factory.php';
68             $this->elements = HTML_Template_Flexy_Factory::fromArray ( $_POST ,$this->elements);
69             
70         }
71         
72         return;
73     }
74     
75     function post()
76     {
77         
78         $u = DB_DataObject::factory('Person');
79         //$u->active = 1;
80         $u->whereAdd('LENGTH(passwd) > 1');
81         //$u->company_id = $this->company->id;
82         
83         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
84             return $this->errmsg('bad_1');
85             
86         }
87          
88         $u->email = $_REQUEST['username'];
89         if ($u->count() > 1 || !$u->find(true)) {
90             return $this->errmsg('bad_2');
91             
92         }
93         
94         //if (!$u->active) {
95         //    return $this->errmsg('disabled');
96         //}
97         
98         if ($u->checkPassword($_REQUEST['password'])) {
99             $u->login();
100             $this->addEvent("LOGIN");
101             //if (!empty($_REQUEST['lang']) && $_REQUEST['lang'] != $u->lang) {
102             //    $uu = clone($u);
103             ////    $uu->lang = $_REQUEST['lang'];
104             //    $uu->update();
105             //}
106              // log it..
107             HTML_FlexyFramework::run(''); // 
108             
109             //$this->sendAuthUserDetails();
110             exit;
111
112             //exit;
113         }
114         
115          
116         return $this->errmsg('bad_3'); // - " . htmlspecialchars(print_r($_POST,true))."'");
117         
118     }
119     
120     
121     
122     
123 }