main_controller::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\db\driver\driver_interface;
15
use phpbb\exception\http_exception;
16
use phpbb\log\log;
17
use phpbb\request\request_interface;
18
use phpbb\template\template;
19
use phpbb\user;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * Controller
24
 */
25
class main_controller
26
{
27
28
	/**
29
	 * @var template
30
	 */
31
	private $template;
32
33
	/**
34
	 * @var driver_interface
35
	 */
36
	private $db;
37
38
	/**
39
	 * @var user
40
	 */
41
	private $user;
42
43
	/**
44
	 * @var request_interface
45
	 */
46
	private $request;
47
48
	/**
49
	 * @var session_helper_interface
50
	 */
51
	private $session_helper;
52
53
	/**
54
	 * @var string
55
	 */
56
	private $root_path;
57
58
	/**
59
	 * @var string
60
	 */
61
	private $php_ext;
62
	/**
63
	 * @var log
64
	 */
65
	private $log;
66
67
	/**
68
	 * Constructor
69
	 *
70
	 * @access public
71
	 * @param driver_interface $db
72
	 * @param template $template
73
	 * @param user $user
74
	 * @param request_interface $request
75
	 * @param log $log
76
	 * @param session_helper_interface $session_helper
77
	 * @param string $root_path
78
	 * @param string $php_ext
79
	 */
80 View Code Duplication
	public function __construct(driver_interface $db, template $template, user $user, request_interface $request, log $log, session_helper_interface $session_helper, $root_path, $php_ext)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
81
	{
82
		$this->template 			= $template;
83
		$this->db					= $db;
84
		$this->user					= $user;
85
		$this->request				= $request;
86
		$this->session_helper		= $session_helper;
87
		$this->root_path			= $root_path;
88
		$this->php_ext				= $php_ext;
89
		$this->log                  = $log;
90
	}
91
92
	/**
93
	 * @param int  $user_id
94
	 * @param bool $admin
95
	 * @param bool $auto_login
96
	 * @param bool $viewonline
97
	 * @param string $class
98
	 * @return Response
99
	 * @throws http_exception
100
	 */
101
	public function submit($user_id, $admin, $auto_login, $viewonline, $class)
102
	{
103
		$this->user->add_lang_ext('paul999/tfa', 'common');
104
105
		if (!check_form_key('tfa_login_page'))
106
		{
107
			throw new http_exception(403, 'FORM_INVALID');
108
		}
109
110
		if (empty($this->user->data['tfa_random']) || $user_id != $this->user->data['tfa_uid'])
111
		{
112
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
113
		}
114
		$random = $this->request->variable('random', '');
115
116
		if ($this->user->data['tfa_random'] !== $random || strlen($random) !== 40)
117
		{
118
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
119
		}
120
		$sql_ary = array(
121
			'tfa_random' => '',
122
			'tfa_uid'    => 0,
123
		);
124
		$sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . "
125
			WHERE
126
				session_id = '" . $this->db->sql_escape($this->user->data['session_id']) . "' AND
127
				session_user_id = " . (int) $this->user->data['user_id'];
128
		$this->db->sql_query($sql);
129
130
		if (empty($class))
131
		{
132
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
133
		}
134
135
		$module = $this->session_helper->find_module($class);
136
137
		if ($module == null)
138
		{
139
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
140
		}
141
142
		$redirect = $this->request->variable('redirect', "{$this->root_path}/index.{$this->php_ext}");
143
		try
144
		{
145
			if (!$module->login($user_id))
146
			{
147
				$this->log->add('critical', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION',false, ['TFA_INCORRECT_KEY']);
148
				$this->template->assign_var('S_ERROR', $this->user->lang('TFA_INCORRECT_KEY'));
149
				$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect);
150
			}
151
		}
152
		catch (http_exception $ex) // @TODO: Replace exception with own exception
0 ignored issues
show
Bug introduced by
The class phpbb\exception\http_exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
153
		{
154
155
			$this->log->add('critical', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', false, [$ex->getMessage()]);
156
157
			if ($admin)
158
			{
159
				// Also log it to admin  log just to be sure.
160
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', false, [$ex->getMessage()]);
161
			}
162
			if ($ex->getStatusCode() == 400)
163
			{
164
				$this->template->assign_var('S_ERROR', $this->user->lang($ex->getMessage()));
165
				$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect);
166
			}
167
			else
168
			{
169
				throw $ex;
170
			}
171
		}
172
173
		$old_session_id = $this->user->session_id;
174
		if ($admin)
175
		{
176
			$cookie_expire = time() - 31536000;
177
			$this->user->set_cookie('u', '', $cookie_expire);
178
			$this->user->set_cookie('sid', '', $cookie_expire);
179
		}
180
		$result = $this->user->session_create($user_id, $admin, $auto_login, $viewonline);
181
182
		// Successful session creation
183
		if ($result === true)
184
		{
185
			// Remove our cookie that causes filling in a key.
186
			$this->user->set_cookie('rn', '', time() + 3600 * 24, true);
187
			// If admin re-authentication we remove the old session entry because a new one has been created...
188
			if ($admin)
189
			{
190
				// the login array is used because the user ids do not differ for re-authentication
191
				$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
192
					WHERE session_id = '" . $this->db->sql_escape($old_session_id) . "'
193
					AND session_user_id = " . (int) $user_id;
194
				$this->db->sql_query($sql);
195
196
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_ADMIN_AUTH_SUCCESS');
197
198
				redirect(append_sid("{$this->root_path}adm/index.{$this->php_ext}", false, true, $this->user->data['session_id']));
199
			}
200
201
			redirect(append_sid($redirect, false, true, $this->user->data['session_id']));
202
		}
203
		throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
204
	}
205
}
206