/
users.php
124 lines (111 loc) · 2.92 KB
/
users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* LitePress
* Copyright (C) 2011 Nirix
*
* @author Nirix <nrx@nirix.net>
* @copyright Nirix
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3-only
*/
/**
* User controller for handling login, logout,
* register and usercp pages.
*
* @author Nirix <nrx@nirix.net>
* @since 0.1
* @package LitePress
*/
class Controller_Users extends Controller_Frontend
{
public function action_login()
{
$this->title('Login');
$this->view = $this->theme->view('users/login');
if (Input::param() != array())
{
if ($user = Model_User::authenticate_login(Input::param('username'), Input::param('password')))
{
Cookie::set('_sess', Crypt::encode($user->login_hash));
Response::redirect(Input::param('redirect') ? Input::param('redirect') : '/');
}
else
{
if (Input::param('redirect'))
{
Session::set_flash('login_redirect', Input::param('redirect'));
}
$this->view->errors = array('Invalid username and/or password.');
}
}
}
public function action_logout()
{
Cookie::set('_sess', Crypt::encode(rand(100, 900)));
Session::set_flash('notice', 'You are now logged out.');
Response::redirect('/');
}
public function action_register()
{
$this->title('Register');
$this->view = $this->theme->view('users/register');
$user = Model_User::forge();
$this->view->set('user', $user);
if (Input::param() != array())
{
$user->values(array(
'name' => Input::param('name'),
'username' => Input::param('username'),
'password' => Input::param('password'),
'email' => Input::param('email')
));
if ($user->is_valid())
{
$user->save();
Response::redirect('login');
}
else
{
$this->view->errors = $user->errors();
}
}
}
public function action_usercp()
{
if (!$this->current_user->logged_in())
{
Session::set_flash('error', 'You need to be logged in to access is page');
Session::set_flash('login_redirect', Uri::current());
Response::redirect('login');
}
$this->title('UserCP');
$this->view = $this->theme->view('users/usercp');
if (Input::param() != array())
{
// Set name and email
$this->current_user->name = Input::param('name');
$this->current_user->email = Input::param('email');
// Set new password
if (Input::param('new_password'))
{
$this->current_user->password = Input::param('new_password');
}
// Check if the current password is valid...
$auth = Model_User::authenticate_login($this->current_user->username, Input::param('current_password'));
if ($this->current_user->is_valid() and $auth)
{
$this->current_user->save();
Session::set_flash('success', 'Details saved');
Response::redirect('usercp');
}
else
{
$errors = $this->current_user->errors();
if (!$auth)
{
$errors = array('Current password is invalid.') + $errors;
}
}
$this->view->set('errors', isset($errors) ? $errors : array());
}
}
}