1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* 2FA extension for the phpBB Forum Software package. |
5
|
|
|
* |
6
|
|
|
* @copyright (c) 2015 Paul Sohier |
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace paul999\tfa\controller; |
12
|
|
|
|
13
|
|
|
use paul999\tfa\helper\session_helper_interface; |
14
|
|
|
use phpbb\config\config; |
15
|
|
|
use phpbb\controller\helper; |
16
|
|
|
use phpbb\db\driver\driver_interface; |
17
|
|
|
use phpbb\request\request_interface; |
18
|
|
|
use phpbb\template\template; |
19
|
|
|
use phpbb\user; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
21
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Controller |
25
|
|
|
*/ |
26
|
|
|
class main_controller |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var helper |
30
|
|
|
*/ |
31
|
|
|
private $controller_helper; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var template |
35
|
|
|
*/ |
36
|
|
|
private $template; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var driver_interface |
40
|
|
|
*/ |
41
|
|
|
private $db; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var user |
45
|
|
|
*/ |
46
|
|
|
private $user; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var request_interface |
50
|
|
|
*/ |
51
|
|
|
private $request; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var config |
55
|
|
|
*/ |
56
|
|
|
private $config; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var session_helper_interface |
60
|
|
|
*/ |
61
|
|
|
private $session_helper; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $root_path; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $php_ext; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor |
75
|
|
|
* |
76
|
|
|
* @access public |
77
|
|
|
* @param helper $controller_helper |
78
|
|
|
* @param driver_interface $db |
79
|
|
|
* @param template $template |
80
|
|
|
* @param user $user |
81
|
|
|
* @param request_interface $request |
82
|
|
|
* @param config $config |
83
|
|
|
* @param session_helper_interface $session_helper |
84
|
|
|
* @param string $root_path |
85
|
|
|
* @param string $php_ext |
86
|
|
|
*/ |
87
|
|
|
public function __construct(helper $controller_helper, driver_interface $db, template $template, user $user, request_interface $request, config $config, session_helper_interface $session_helper, $root_path, $php_ext) |
88
|
|
|
{ |
89
|
|
|
$this->controller_helper = $controller_helper; |
90
|
|
|
$this->template = $template; |
91
|
|
|
$this->db = $db; |
92
|
|
|
$this->user = $user; |
93
|
|
|
$this->request = $request; |
94
|
|
|
$this->config = $config; |
95
|
|
|
$this->session_helper = $session_helper; |
96
|
|
|
$this->root_path = $root_path; |
97
|
|
|
$this->php_ext = $php_ext; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int $user_id |
103
|
|
|
* @param bool $admin |
104
|
|
|
* @param bool $auto_login |
105
|
|
|
* @param bool $viewonline |
106
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
107
|
|
|
* @throws AccessDeniedHttpException |
108
|
|
|
*/ |
109
|
|
|
public function submit($user_id, $admin, $auto_login, $viewonline, $class) |
110
|
|
|
{ |
111
|
|
|
$this->user->add_lang_ext('paul999/tfa', 'common'); |
112
|
|
|
|
113
|
|
|
if (!check_form_key('tfa_login_page')) |
114
|
|
|
{ |
115
|
|
|
throw new AccessDeniedHttpException($this->user->lang('FORM_INVALID')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (empty($this->user->data['tfa_random']) || $user_id != $this->user->data['tfa_uid']) |
119
|
|
|
{ |
120
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
121
|
|
|
} |
122
|
|
|
$random = $this->request->variable('random', ''); |
123
|
|
|
|
124
|
|
|
if ($this->user->data['tfa_random'] !== $random || strlen($random) != 40) |
125
|
|
|
{ |
126
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
127
|
|
|
} |
128
|
|
|
$sql_ary = array( |
129
|
|
|
'tfa_random' => '', |
130
|
|
|
'tfa_uid' => 0, |
131
|
|
|
); |
132
|
|
|
$sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' |
133
|
|
|
WHERE |
134
|
|
|
session_id = \'' . $this->db->sql_escape($this->user->data['session_id']) . '\' AND |
135
|
|
|
session_user_id = ' . (int)$this->user->data['user_id']; |
136
|
|
|
$this->db->sql_query($sql); |
137
|
|
|
|
138
|
|
|
if (empty($class)) |
139
|
|
|
{ |
140
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$module = $this->session_helper->findModule($class); |
144
|
|
|
|
145
|
|
|
if ($module == null) |
146
|
|
|
{ |
147
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$redirect = $this->request->variable('redirect', "{$this->root_path}/index.{$this->php_ext}"); |
151
|
|
|
try |
152
|
|
|
{ |
153
|
|
|
if (!$module->login($user_id)) |
154
|
|
|
{ |
155
|
|
|
$this->template->assign_var('S_ERROR', $this->user->lang('TFA_INCORRECT_KEY')); |
156
|
|
|
$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
catch (BadRequestHttpException $ex) // @TODO: Replace exception with own exception |
160
|
|
|
{ |
161
|
|
|
$this->template->assign_var('S_ERROR', $ex->getMessage()); |
162
|
|
|
$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$old_session_id = $this->user->session_id; |
166
|
|
|
|
167
|
|
|
if ($admin) |
168
|
|
|
{ |
169
|
|
|
$cookie_expire = time() - 31536000; |
170
|
|
|
$this->user->set_cookie('u', '', $cookie_expire); |
171
|
|
|
$this->user->set_cookie('sid', '', $cookie_expire); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$result = $this->user->session_create($user_id, $admin, $auto_login, $viewonline); |
175
|
|
|
|
176
|
|
|
// Successful session creation |
177
|
|
|
if ($result === true) |
178
|
|
|
{ |
179
|
|
|
// If admin re-authentication we remove the old session entry because a new one has been created... |
180
|
|
|
if ($admin) |
181
|
|
|
{ |
182
|
|
|
// the login array is used because the user ids do not differ for re-authentication |
183
|
|
|
$sql = 'DELETE FROM ' . SESSIONS_TABLE . " |
184
|
|
|
WHERE session_id = '" . $this->db->sql_escape($old_session_id) . "' |
185
|
|
|
AND session_user_id = " . (int) $user_id; |
186
|
|
|
$this->db->sql_query($sql); |
187
|
|
|
|
188
|
|
|
redirect(append_sid("{$this->root_path}adm/index.{$this->php_ext}", false, true, $this->user->data['session_id'])); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
redirect(append_sid($redirect, false, true, $this->user->data['session_id'])); |
192
|
|
|
} |
193
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|