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