Completed
Push — master ( b5a957...040c1d )
by Paul
03:38
created

tfa_module   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 21
Bugs 3 Features 5
Metric Value
wmc 16
c 21
b 3
f 5
lcom 1
cbo 5
dl 0
loc 172
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 7 1
A main() 0 13 1
A register_security_key() 0 22 3
B createPage() 0 48 6
B delete_keys() 0 21 5
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\ucp;
12
13
use paul999\tfa\helper\registration_helper;
14
use paul999\tfa\helper\session_helper;
15
use paul999\u2f\Exceptions\U2fError;
16
use paul999\u2f\U2F;
17
use phpbb\db\driver\driver_interface;
18
use phpbb\request\request_interface;
19
use phpbb\template\template;
20
use phpbb\user;
21
22
class tfa_module
23
{
24
	/**
25
	 * @var string
26
	 */
27
	public $u_action;
28
29
	/**
30
	 * @var  string
31
	 */
32
	public $page_title;
33
34
	/**
35
	 * @var string
36
	 */
37
	public $tpl_name;
38
39
	/**
40
	 * @var user
41
	 */
42
	private $user;
43
44
	/**
45
	 * @var template
46
	 */
47
	private $template;
48
49
	/**
50
	 * @var request_interface
51
	 */
52
	private $request;
53
54
	/**
55
	 * @var session_helper
56
	 */
57
	private $session_helper;
58
59
	/**
60
	 * @param user $user
61
	 * @param template $template
62
	 * @param request_interface $request
63
	 * @param session_helper $session_helper
64
	 */
65
	private function setup(user $user, template $template, request_interface $request, session_helper $session_helper)
66
	{
67
		$this->user 				= $user;
68
		$this->template 			= $template;
69
		$this->request 				= $request;
70
		$this->session_helper 		= $session_helper;
71
	}
72
73
	/**
74
	 * @param $id
75
	 * @param $mode
76
	 */
77
	public function main($id, $mode)
78
	{
79
		global $user, $template;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
80
		global $request, $phpbb_container;
81
82
		$user->add_lang('posting');
83
		$user->add_lang_ext('paul999/tfa', 'ucp_tfa');
84
		$user->add_lang_ext('paul999/tfa', 'common');
85
86
		$this->setup($user, $template, $request, $phpbb_container->get('paul999.2fa.sessionHelper'));
87
88
		$this->createPage();
89
	}
90
91
	/**
92
	 * @param array $error
93
	 */
94
	private function register_security_key(&$error)
95
	{
96
		try {
97
			$class = $this->request->variable('class', '');
98
			$module = $this->session_helper->findModule($class);
99
100
			if ($module != null) {
101
				$module->register();
102
103
				meta_refresh(3, $this->u_action);
104
				$message = $this->user->lang['TFA_KEY_ADDED'] . '<br /><br />' . sprintf($this->user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
105
				trigger_error($message);
106
			}
107
			else
108
			{
109
				$error[] = $this->user->lang('TFA_MODULE_NOT_FOUND', $class);
110
			}
111
		}
112
		catch (\Exception $e) {
113
			$error[] = $e->getMessage();
114
		}
115
	}
116
117
	/**
118
	 *
119
	 */
120
	private function createPage()
121
	{
122
		$submit = $this->request->variable('md', false, false, \phpbb\request\request_interface::POST);
123
		$error = array();
124
		$s_hidden_fields = '';
125
126
		add_form_key('ucp_tfa_keys');
127
128
		if ($submit)
129
		{
130
			$mode = $this->request->variable('md', '');
131
			if (!check_form_key('ucp_tfa_keys'))
132
			{
133
				$error[] = 'FORM_INVALID';
134
			}
135
			else
136
			{
137
				switch ($mode)
138
				{
139
					case 'delete':
140
						$this->delete_keys();
141
						break;
142
143
					case 'register':
144
						$this->register_security_key($error);
145
					break;
146
147
					default:
148
						$error[] = 'TFA_NO_MODE';
149
				}
150
			}
151
152
			// Replace "error" strings with their real, localised form
153
			$error = array_map(array($this->user, 'lang'), $error);
154
		}
155
156
157
		$this->template->assign_vars(array(
158
			'ERROR' 			=> (sizeof($error)) ? implode('<br />', $error) : '',
159
			'L_TITLE' 			=> $this->user->lang['UCP_TFA'],
160
			'S_HIDDEN_FIELDS' 	=> $s_hidden_fields,
161
			'S_UCP_ACTION' 		=> $this->u_action,
162
		));
163
164
		// Set desired template
165
		$this->tpl_name = 'ucp_tfa';
166
		$this->page_title = 'UCP_TFA';
167
	}
168
169
	/**
170
	 *
171
	 */
172
	private function delete_keys()
173
	{
174
		$keys = $this->request->variable('keys', array(''));
175
		if (!empty($keys))
176
		{
177
			foreach ($keys as $row)
178
			{
179
				if (isset($row['class']))
180
				{
181
					$module = $this->session_helper->findModule($row['class']);
182
					if ($module != null)
183
					{
184
						$module->delete($row);
185
					}
186
				}
187
			}
188
			meta_refresh(3, $this->u_action);
189
			$message = $this->user->lang['TFA_KEYS_DELETED'] . '<br /><br />' . sprintf($this->user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
190
			trigger_error($message);
191
		}
192
	}
193
}
194