Completed
Push — master ( cc7bc1...e7eec3 )
by Paul
03:46
created

tfa_module::main()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 17
Bugs 3 Features 4
Metric Value
c 17
b 3
f 4
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
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\session_helper;
14
use phpbb\request\request_interface;
15
use phpbb\template\template;
16
use phpbb\user;
17
18
class tfa_module
19
{
20
	/**
21
	 * @var string
22
	 */
23
	public $u_action;
24
25
	/**
26
	 * @var  string
27
	 */
28
	public $page_title;
29
30
	/**
31
	 * @var string
32
	 */
33
	public $tpl_name;
34
35
	/**
36
	 * @var user
37
	 */
38
	private $user;
39
40
	/**
41
	 * @var template
42
	 */
43
	private $template;
44
45
	/**
46
	 * @var request_interface
47
	 */
48
	private $request;
49
50
	/**
51
	 * @var session_helper
52
	 */
53
	private $session_helper;
54
55
	/**
56
	 * @param user              $user
57
	 * @param template          $template
58
	 * @param request_interface $request
59
	 * @param session_helper    $session_helper
60
	 */
61
	private function setup(user $user, template $template, request_interface $request, session_helper $session_helper)
62
	{
63
		$this->user = $user;
64
		$this->template = $template;
65
		$this->request = $request;
66
		$this->session_helper = $session_helper;
67
	}
68
69
	/**
70
	 * @param $id
71
	 * @param $mode
72
	 */
73
	public function main($id, $mode)
74
	{
75
		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...
76
		global $request, $phpbb_container;
77
78
		$user->add_lang('posting');
79
		$user->add_lang_ext('paul999/tfa', 'ucp_tfa');
80
		$user->add_lang_ext('paul999/tfa', 'common');
81
82
		$this->setup($user, $template, $request, $phpbb_container->get('paul999.tfa.sessionHelper'));
83
84
		$this->createPage();
85
	}
86
87
	/**
88
	 * return array
89
	 */
90
	private function register_security_key()
91
	{
92
		try
93
		{
94
			$error = array();
95
			$class = $this->request->variable('class', '');
96
			$module = $this->session_helper->findModule($class);
97
			$submit = $this->request->variable('md', false, false, \phpbb\request\request_interface::POST);
98
99
			if ($module != null)
100
			{
101
				if ($submit)
102
				{
103
					$module->register();
104
					meta_refresh(3, $this->u_action);
105
					$message = $this->user->lang['TFA_KEY_ADDED'] . '<br /><br />' . sprintf($this->user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
106
					trigger_error($message);
107
				}
108
				if ($module->can_register())
109
				{
110
					$this->tpl_name = $module->register_start();
111
				}
112
				else
113
				{
114
					$error[] = 'TFA_MODULE_NO_REGISTER';
115
				}
116
			}
117
			else
118
			{
119
				$error[] = $this->user->lang('TFA_MODULE_NOT_FOUND', $class);
120
			}
121
		}
122
		catch (\Exception $e)
123
		{
124
			$error[] = $e->getMessage();
125
		}
126
127
		return $error;
128
	}
129
130
	/**
131
	 *
132
	 */
133
	private function createPage()
134
	{
135
		$error = array();
136
		$s_hidden_fields = '';
137
138
		add_form_key('ucp_tfa_keys');
139
140
		$module_row = $this->request->variable('md', '');
141
142
		// Set desired template
143
		$this->tpl_name = 'ucp_tfa';
144
		$this->page_title = 'UCP_TFA';
145
146
		if (!empty($module_row))
147
		{
148
			switch ($module_row)
149
			{
150
				case $this->user->lang('DELETE_MARKED'):
151
					if (!check_form_key('ucp_tfa_keys'))
152
					{
153
						$error[] = 'FORM_INVALID';
154
					}
155
					else
156
					{
157
						if ($this->request->variable('md', false, false, \phpbb\request\request_interface::POST))
158
						{
159
							$this->delete_keys();
160
						}
161
					}
162
					break;
163
164
				case $this->user->lang('TFA_NEW'):
165
					$error = array_merge($this->register_security_key(), $error);
166
167
					if (!sizeof($error))
168
					{
169
						return; // register_security_key has its own template stuff, so we return here.
170
					}
171
					break;
172
173
				default:
174
					$error[] = 'TFA_NO_MODE';
175
			}
176
		}
177
178
		// Replace "error" strings with their real, localised form
179
		$error = array_map(array(
180
			$this->user,
181
			'lang',
182
		), $error);
183
184
		/**
185
		 * @var $module_row \paul999\tfa\modules\module_interface
186
		 */
187
		foreach ($this->session_helper->getModules() as $module_row)
188
		{
189
			$module_row->show_ucp();
190
191
			if ($module_row->can_register())
192
			{
193
				$this->template->assign_block_vars('new_keys', array(
194
					'CLASS' => get_class($module_row),
195
					'NAME'	=> $this->user->lang($module_row->get_translatable_name()),
196
				));
197
			}
198
 		}
199
200
		$this->template->assign_vars(array(
201
			'ERROR'           => (sizeof($error)) ? implode('<br />', $error) : '',
202
			'L_TITLE'         => $this->user->lang['UCP_TFA'],
203
			'S_HIDDEN_FIELDS' => $s_hidden_fields,
204
			'S_UCP_ACTION'    => $this->u_action,
205
		));
206
	}
207
208
	/**
209
	 *
210
	 */
211
	private function delete_keys()
212
	{
213
		$keys = $this->request->variable('keys', array(''));
214
		if (!empty($keys))
215
		{
216
			foreach ($keys as $row)
217
			{
218
				$row = explode('_', $row);
219
				if (isset($row[0]))
220
				{
221
					$module = $this->session_helper->findModule($row['class']);
222
					if ($module != null)
223
					{
224
						$module->delete($row[1]);
225
					}
226
				}
227
			}
228
		}
229
		meta_refresh(3, $this->u_action);
230
		$message = $this->user->lang['TFA_KEYS_DELETED'] . '<br /><br />' . sprintf($this->user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
231
		trigger_error($message);
232
	}
233
}
234