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