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\exception\http_exception; |
18
|
|
|
use phpbb\request\request_interface; |
19
|
|
|
use phpbb\template\template; |
20
|
|
|
use phpbb\user; |
21
|
|
|
use phpbb\log\log; |
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
|
|
|
* @var log |
74
|
|
|
*/ |
75
|
|
|
private $log; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Constructor |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* @param helper $controller_helper |
82
|
|
|
* @param driver_interface $db |
83
|
|
|
* @param template $template |
84
|
|
|
* @param user $user |
85
|
|
|
* @param request_interface $request |
86
|
|
|
* @param config $config |
87
|
|
|
* @param log $log |
88
|
|
|
* @param session_helper_interface $session_helper |
89
|
|
|
* @param string $root_path |
90
|
|
|
* @param string $php_ext |
91
|
|
|
*/ |
92
|
|
|
public function __construct(helper $controller_helper, driver_interface $db, template $template, user $user, request_interface $request, config $config, log $log, session_helper_interface $session_helper, $root_path, $php_ext) |
93
|
|
|
{ |
94
|
|
|
$this->controller_helper = $controller_helper; |
95
|
|
|
$this->template = $template; |
96
|
|
|
$this->db = $db; |
97
|
|
|
$this->user = $user; |
98
|
|
|
$this->request = $request; |
99
|
|
|
$this->config = $config; |
100
|
|
|
$this->session_helper = $session_helper; |
101
|
|
|
$this->root_path = $root_path; |
102
|
|
|
$this->php_ext = $php_ext; |
103
|
|
|
$this->log = $log; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int $user_id |
108
|
|
|
* @param bool $admin |
109
|
|
|
* @param bool $auto_login |
110
|
|
|
* @param bool $viewonline |
111
|
|
|
* @param string $class |
112
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
113
|
|
|
* @throws http_exception |
114
|
|
|
*/ |
115
|
|
|
public function submit($user_id, $admin, $auto_login, $viewonline, $class) |
116
|
|
|
{ |
117
|
|
|
$this->user->add_lang_ext('paul999/tfa', 'common'); |
118
|
|
|
|
119
|
|
|
if (!check_form_key('tfa_login_page')) |
120
|
|
|
{ |
121
|
|
|
throw new http_exception(403, 'FORM_INVALID'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (empty($this->user->data['tfa_random']) || $user_id != $this->user->data['tfa_uid']) |
125
|
|
|
{ |
126
|
|
|
throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
127
|
|
|
} |
128
|
|
|
$random = $this->request->variable('random', ''); |
129
|
|
|
|
130
|
|
|
if ($this->user->data['tfa_random'] !== $random || strlen($random) !== 40) |
131
|
|
|
{ |
132
|
|
|
throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
133
|
|
|
} |
134
|
|
|
$sql_ary = array( |
135
|
|
|
'tfa_random' => '', |
136
|
|
|
'tfa_uid' => 0, |
137
|
|
|
); |
138
|
|
|
$sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . " |
139
|
|
|
WHERE |
140
|
|
|
session_id = '" . $this->db->sql_escape($this->user->data['session_id']) . "' AND |
141
|
|
|
session_user_id = " . (int) $this->user->data['user_id']; |
142
|
|
|
$this->db->sql_query($sql); |
143
|
|
|
|
144
|
|
|
if (empty($class)) |
145
|
|
|
{ |
146
|
|
|
throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$module = $this->session_helper->findModule($class); |
150
|
|
|
|
151
|
|
|
if ($module == null) |
152
|
|
|
{ |
153
|
|
|
throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$redirect = $this->request->variable('redirect', "{$this->root_path}/index.{$this->php_ext}"); |
157
|
|
|
try |
158
|
|
|
{ |
159
|
|
View Code Duplication |
if (!$module->login($user_id)) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this->template->assign_var('S_ERROR', $this->user->lang('TFA_INCORRECT_KEY')); |
162
|
|
|
$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
catch (http_exception $ex) // @TODO: Replace exception with own exception |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
|
168
|
|
|
$this->log->add('error', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', $ex->getMessage()); |
169
|
|
|
|
170
|
|
|
if ($admin) |
171
|
|
|
{ |
172
|
|
|
// Also log it to admin log just to be sure. |
173
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', $ex->getMessage()); |
174
|
|
|
} |
175
|
|
View Code Duplication |
if ($ex->getStatusCode() == 400) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$this->template->assign_var('S_ERROR', $this->user->lang($ex->getMessage())); |
178
|
|
|
$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect); |
179
|
|
|
} |
180
|
|
|
else |
181
|
|
|
{ |
182
|
|
|
throw $ex; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$old_session_id = $this->user->session_id; |
187
|
|
|
|
188
|
|
|
if ($admin) |
189
|
|
|
{ |
190
|
|
|
$cookie_expire = time() - 31536000; |
191
|
|
|
$this->user->set_cookie('u', '', $cookie_expire); |
192
|
|
|
$this->user->set_cookie('sid', '', $cookie_expire); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$result = $this->user->session_create($user_id, $admin, $auto_login, $viewonline); |
196
|
|
|
|
197
|
|
|
// Successful session creation |
198
|
|
|
if ($result === true) |
199
|
|
|
{ |
200
|
|
|
// If admin re-authentication we remove the old session entry because a new one has been created... |
201
|
|
|
if ($admin) |
202
|
|
|
{ |
203
|
|
|
// the login array is used because the user ids do not differ for re-authentication |
204
|
|
|
$sql = 'DELETE FROM ' . SESSIONS_TABLE . " |
205
|
|
|
WHERE session_id = '" . $this->db->sql_escape($old_session_id) . "' |
206
|
|
|
AND session_user_id = " . (int) $user_id; |
207
|
|
|
$this->db->sql_query($sql); |
208
|
|
|
|
209
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_ADMIN_AUTH_SUCCESS'); |
210
|
|
|
|
211
|
|
|
redirect(append_sid("{$this->root_path}adm/index.{$this->php_ext}", false, true, $this->user->data['session_id'])); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
redirect(append_sid($redirect, false, true, $this->user->data['session_id'])); |
215
|
|
|
} |
216
|
|
|
throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.