|
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 paul999\tfa\modules\module_interface; |
|
15
|
|
|
use phpbb\config\config; |
|
16
|
|
|
use phpbb\controller\helper; |
|
17
|
|
|
use phpbb\db\driver\driver_interface; |
|
18
|
|
|
use phpbb\request\request_interface; |
|
19
|
|
|
use phpbb\template\template; |
|
20
|
|
|
use phpbb\user; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Controller |
|
26
|
|
|
*/ |
|
27
|
|
|
class main_controller |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var helper |
|
31
|
|
|
*/ |
|
32
|
|
|
private $controller_helper; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var template |
|
36
|
|
|
*/ |
|
37
|
|
|
private $template; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var driver_interface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $db; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var user |
|
46
|
|
|
*/ |
|
47
|
|
|
private $user; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var request_interface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $request; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var config |
|
56
|
|
|
*/ |
|
57
|
|
|
private $config; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var session_helper_interface |
|
61
|
|
|
*/ |
|
62
|
|
|
private $session_helper; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
private $root_path; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
private $php_ext; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Constructor |
|
76
|
|
|
* |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @param helper $controller_helper |
|
79
|
|
|
* @param driver_interface $db |
|
80
|
|
|
* @param template $template |
|
81
|
|
|
* @param user $user |
|
82
|
|
|
* @param request_interface $request |
|
83
|
|
|
* @param config $config |
|
84
|
|
|
* @param session_helper_interface $session_helper |
|
85
|
|
|
* @param string $root_path |
|
86
|
|
|
* @param string $php_ext |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
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) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$this->controller_helper = $controller_helper; |
|
91
|
|
|
$this->template = $template; |
|
92
|
|
|
$this->db = $db; |
|
93
|
|
|
$this->user = $user; |
|
94
|
|
|
$this->request = $request; |
|
95
|
|
|
$this->config = $config; |
|
96
|
|
|
$this->session_helper = $session_helper; |
|
97
|
|
|
$this->root_path = $root_path; |
|
98
|
|
|
$this->php_ext = $php_ext; |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param int $user_id |
|
104
|
|
|
* @param bool $admin |
|
105
|
|
|
* @param bool $auto_login |
|
106
|
|
|
* @param bool $viewonline |
|
107
|
|
|
* @param string $class |
|
108
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
109
|
|
|
*/ |
|
110
|
|
|
public function display($user_id, $admin, $auto_login, $viewonline, $class) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->user->add_lang_ext('paul999/tfa', 'common'); |
|
113
|
|
|
|
|
114
|
|
|
if ($this->config['tfa_mode'] == session_helper_interface::MODE_DISABLED) |
|
115
|
|
|
{ |
|
116
|
|
|
throw new AccessDeniedHttpException('TFA_DISABLED'); |
|
117
|
|
|
} |
|
118
|
|
|
if (($this->user->data['user_id'] != ANONYMOUS && !$admin) || $user_id == ANONYMOUS || ($user_id != $this->user->data['user_id'] && $admin)) |
|
119
|
|
|
{ |
|
120
|
|
|
throw new AccessDeniedHttpException('TFA_NO_ACCESS'); |
|
121
|
|
|
} |
|
122
|
|
|
$modules = $this->session_helper->getModules(); |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @var module_interface $module |
|
126
|
|
|
*/ |
|
127
|
|
|
if (!empty($default)) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$module = $this->session_helper->findModule($class); |
|
130
|
|
|
} |
|
131
|
|
|
else |
|
132
|
|
|
{ |
|
133
|
|
|
foreach ($modules as $row) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($module->is_usable($user_id)) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
$module = $row; |
|
138
|
|
|
} |
|
139
|
|
|
break; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
if ($module == null) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @var module_interface $row |
|
149
|
|
|
*/ |
|
150
|
|
|
foreach ($modules as $row) |
|
151
|
|
|
{ |
|
152
|
|
|
if ($row->is_usable($user_id)) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->template->assign_block_vars('', array( |
|
155
|
|
|
'U_CHANGE_CLASS' => $this->controller_helper->route('paul999_tfa_read_controller', array( |
|
156
|
|
|
'user_id' => $user_id, |
|
157
|
|
|
'admin' => $admin, |
|
158
|
|
|
'auto_login' => $auto_login, |
|
159
|
|
|
'viewonline' => $viewonline, |
|
160
|
|
|
'class' => get_class($row), |
|
161
|
|
|
)), |
|
162
|
|
|
)); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
$module->login_start($user_id); |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
$this->template->assign_vars(array( |
|
169
|
|
|
'REDIRECT' => $this->request->variable('redirect', ''), |
|
170
|
|
|
'U_SUBMIT_AUTH' => $this->controller_helper->route('paul999_tfa_read_controller_submit', array( |
|
171
|
|
|
'user_id' => $user_id, |
|
172
|
|
|
'admin' => $admin, |
|
173
|
|
|
'auto_login' => $auto_login, |
|
174
|
|
|
'viewonline' => $viewonline, |
|
175
|
|
|
'class' => get_class($module), |
|
176
|
|
|
)), |
|
177
|
|
|
)); |
|
178
|
|
|
|
|
179
|
|
|
return $this->controller_helper->render('@paul999_tfa/authenticate_main.html'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param int $user_id |
|
184
|
|
|
* @param bool $admin |
|
185
|
|
|
* @param bool $auto_login |
|
186
|
|
|
* @param bool $viewonline |
|
187
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
188
|
|
|
* @throws AccessDeniedHttpException |
|
189
|
|
|
*/ |
|
190
|
|
|
public function submit($user_id, $admin, $auto_login, $viewonline, $class) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->user->add_lang_ext('paul999/tfa', 'common'); |
|
193
|
|
|
|
|
194
|
|
|
if (empty($class)) |
|
195
|
|
|
{ |
|
196
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$module = $this->session_helper->findModule($class); |
|
200
|
|
|
|
|
201
|
|
|
if ($module == null) |
|
202
|
|
|
{ |
|
203
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
|
204
|
|
|
} |
|
205
|
|
|
$module->login($user_id); |
|
206
|
|
|
|
|
207
|
|
|
$old_session_id = $this->user->session_id; |
|
208
|
|
|
|
|
209
|
|
|
if ($admin) |
|
210
|
|
|
{ |
|
211
|
|
|
$cookie_expire = time() - 31536000; |
|
212
|
|
|
$this->user->set_cookie('u', '', $cookie_expire); |
|
213
|
|
|
$this->user->set_cookie('sid', '', $cookie_expire); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$result = $this->user->session_create($user_id, $admin, $auto_login, $viewonline); |
|
217
|
|
|
|
|
218
|
|
|
// Successful session creation |
|
219
|
|
|
if ($result === true) |
|
220
|
|
|
{ |
|
221
|
|
|
// If admin re-authentication we remove the old session entry because a new one has been created... |
|
222
|
|
|
if ($admin) |
|
223
|
|
|
{ |
|
224
|
|
|
// the login array is used because the user ids do not differ for re-authentication |
|
225
|
|
|
$sql = 'DELETE FROM ' . SESSIONS_TABLE . " |
|
226
|
|
|
WHERE session_id = '" . $this->db->sql_escape($old_session_id) . "' |
|
227
|
|
|
AND session_user_id = {$user_id}"; |
|
228
|
|
|
$this->db->sql_query($sql); |
|
229
|
|
|
|
|
230
|
|
|
redirect(append_sid("{$this->root_path}adm/index.{$this->php_ext}", false, true, $this->user->data['session_id'])); |
|
231
|
|
|
} |
|
232
|
|
|
$redirect = $this->request->variable('redirect', "{$this->root_path}/index.{$this->php_ext}"); |
|
233
|
|
|
redirect(append_sid($redirect, false, true, $this->user->data['session_id'])); |
|
234
|
|
|
} |
|
235
|
|
|
throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG')); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
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.