Passed
Pull Request — master (#8)
by
unknown
12:51
created

admin_controller::group_select_options()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 1
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
ccs 11
cts 11
cp 1
crap 4
1
<?php
2
/**
3
 *
4
 * Akismet. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2019 Jakub Senko <[email protected]>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace senky\akismet\controller;
12
13
/**
14
* Admin controller
15
*/
16
class admin_controller
17
{
18
	/** @var \phpbb\request\request */
19
	protected $request;
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\user */
25
	protected $user;
26
27
	/** @var \phpbb\log\log_interface */
28
	protected $log;
29
30
	/** @var \phpbb\config\config */
31
	protected $config;
32
33
	/** @var string Custom form action */
34
	protected $u_action;
35
36
	/** @var \phpbb\language\language */
37
	protected $language;
38
39
	/** @var \phpbb\group\helper */
40
	protected $group_helper;
41
42
	/** @var \phpbb\db\driver\driver_interface */
43
	protected $db;
44
45
	/** @var \Gothick\AkismetClient\Client */
46
	protected $akismet;
47
48
	/** @var string */
49
	protected $php_ext;
50
51
	/** @var string */
52
	protected $phpbb_root_path;
53
54
	/** @var string */
55
	protected $groups_table;
56
57
	const FORM_KEY = 'senky/akismet';
58
59
	/**
60
	 * Constructor
61
	 *
62
	 * @param \phpbb\request\request            $request      		Request object
63
	 * @param \phpbb\template\template          $template     		Template object
64
	 * @param \phpbb\user                       $user         		User object
65
	 * @param \phpbb\log\log_interface          $log          		Log object
66
	 * @param \phpbb\config\config              $config       		Config object
67
	 * @param \phpbb\language\language          $language     		Language object
68
	 * @param \phpbb\group\helper               $group_helper 		Group helper object
69
	 * @param \phpbb\db\driver\driver_interface $db           		Database drive
70
	 * @param \Gothick\AkismetClient\Client     $akismet			Akismet client class
71
	 * @param string                            $php_ext
72
	 * @param string                            $phpbb_root_path
73
	 * @param string							$groups_table
74
	 */
75 5
	public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, \phpbb\log\log_interface $log, \phpbb\config\config $config, \phpbb\language\language $language, \phpbb\group\helper $group_helper, \phpbb\db\driver\driver_interface $db, \Gothick\AkismetClient\Client $akismet, $php_ext, $phpbb_root_path, $groups_table)
76
	{
77 5
		$this->request = $request;
78 5
		$this->template = $template;
79 5
		$this->user = $user;
80 5
		$this->log = $log;
81 5
		$this->config = $config;
82 5
		$this->language = $language;
83 5
		$this->group_helper = $group_helper;
84 5
		$this->db = $db;
85 5
		$this->akismet = $akismet;
86 5
		$this->php_ext = $php_ext;
87 5
		$this->phpbb_root_path = $phpbb_root_path;
88 5
		$this->groups_table = $groups_table;
89 5
	}
90
91
	/**
92
	 * Akismet settings
93
	 */
94 4
	public function display_settings()
95
	{
96 4
		add_form_key(self::FORM_KEY);
97
98 4
		if ($this->request->is_set_post('submit'))
99 4
		{
100 3
			if (!check_form_key(self::FORM_KEY))
101 3
			{
102 1
				trigger_error('FORM_INVALID');
103
			}
104
105 2
			if (!$this->verify_key($this->request->variable('api_key', '')))
106 2
			{
107 1
				trigger_error($this->language->lang('ACP_AKISMET_API_KEY_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
108
			}
109
110 1
			$this->save_settings();
111
112 1
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'AKISMET_LOG_SETTING_CHANGED');
113
114 1
			trigger_error($this->language->lang('ACP_AKISMET_SETTING_SAVED') . adm_back_link($this->u_action));
115
		}
116
117 1
		$this->template->assign_vars(array(
118 1
			'U_ACTION'					=> $this->u_action,
119 1
			'API_KEY'					=> $this->config['senky_akismet_api_key'],
120 1
			'S_CHECK_REGISTRATIONS'		=> $this->config['senky_akismet_check_registrations'],
121 1
			'S_GROUP_LIST'				=> $this->group_select_options($this->config['senky_akismet_add_registering_spammers_to_group']),
122 1
			'S_GROUP_LIST_BLATANT'		=> $this->group_select_options($this->config['senky_akismet_add_registering_blatant_spammers_to_group']),
123 1
			'SKIP_CHECK_AFTER_N_POSTS'	=> $this->config['senky_akismet_skip_check_after_n_posts'],
124 1
			'S_CHECK_ADMIN_FORM'		=> $this->config['senky_akismet_check_admin_form'],
125 1
		));
126 1
	}
127
128
	/**
129
	 * Set action
130
	 *
131
	 * @param	string	$u_action	Action
132
	 */
133 5
	public function set_action($u_action)
134
	{
135 5
		$this->u_action = $u_action;
136 5
	}
137
138
	/**
139
	 * Generate list of groups for selection
140
	 *
141
	 * @param	integer	$selected_group_id	Group ID to mark as selected
142
	 * @return	string	List of groups in HTML format
143
	 */
144 1
	protected function group_select_options($selected_group_id = 0)
145
	{
146
		// Adapted from global function group_select_options in core file functions_admin.php and adapted.
147
148
		$sql = 'SELECT group_id, group_type, group_name
149 1
				FROM ' . $this->groups_table . '
150 1
				WHERE (group_type <> ' . GROUP_SPECIAL . " OR group_name = 'NEWLY_REGISTERED') ";
151 1
		$result = $this->db->sql_query($sql);
152
153 1
		$s_group_options = '';
154
155 1
		while ($row = $this->db->sql_fetchrow($result))
156
		{
157 1
			$selected = ($row['group_id'] == $selected_group_id) ? ' selected="selected"' : '';
158 1
			$s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . $this->group_helper->get_name($row['group_name']) . '</option>';
159 1
		}
160 1
		$this->db->sql_freeresult($result);
161
162 1
		return $s_group_options;
163
	}
164
165
	/**
166
	 * Save settings back to the DB
167
	 */
168 1
	protected function save_settings()
169
	{
170 1
		$this->config->set('senky_akismet_api_key', $this->request->variable('api_key', ''));
171 1
		$this->config->set('senky_akismet_check_registrations', $this->request->variable('check_registrations', 0));
172 1
		$this->config->set('senky_akismet_add_registering_spammers_to_group', $this->request->variable('add_registering_spammers_to_group', 0));
173 1
		$this->config->set('senky_akismet_add_registering_blatant_spammers_to_group', $this->request->variable('add_registering_blatant_spammers_to_group', 0));
174 1
		$this->config->set('senky_akismet_skip_check_after_n_posts', $this->request->variable('skip_check_after_n_posts', 0));
175 1
		$this->config->set('senky_akismet_check_admin_form', $this->request->variable('check_admin_form', 0));
176 1
	}
177
178 2
	protected function verify_key($key)
179
	{
180
		try
181
		{
182 2
			$result = $this->akismet->verifyKey($key);
183 2
			return $result->isValid();
184
		}
185
		catch (\Gothick\AkismetClient\AkismetException $e)
186
		{
187
			return false;
188
		}
189
	}
190
}
191