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         $u = DB_DataObject::factory('Person');
93         //$u->active = 1;
94         $u->whereAdd('LENGTH(passwd) > 1');
95         //$u->company_id = $this->company->id;
96         
97         if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
98             return $this->errmsg('bad_1');
99             
100         }
101          
102         $u->email = $_REQUEST['username'];
103         if ($u->count() > 1 || !$u->find(true)) {
104             return $this->errmsg('bad_2');
105             
106         }
107         
108         //if (!$u->active) {
109         //    return $this->errmsg('disabled');
110         //}
111         
112         if ($u->checkPassword($_REQUEST['password'])) {
113             $u->login();
114             $this->addEvent("LOGIN");
115             //if (!empty($_REQUEST['lang']) && $_REQUEST['lang'] != $u->lang) {
116             //    $uu = clone($u);
117             ////    $uu->lang = $_REQUEST['lang'];
118             //    $uu->update();
119             //}
120              // log it..
121              $this->jok('OK');
122             HTML_FlexyFramework::run(''); // 
123             
124             //$this->sendAuthUserDetails();
125             exit;
126
127             //exit;
128         }
129         
130          
131         return $this->errmsg('bad_3'); // - " . htmlspecialchars(print_r($_POST,true))."'");
132         
133     }
134     
135     
136     
137     
138 }