Passed
Push — master ( ef56ac...a0448a )
by Matt
01:44
created

abbc3_module::validate_google_fonts()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2020 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\acp;
12
13
class abbc3_module
14
{
15
	/** @var \phpbb\cache\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\cache\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
	protected $cache;
17
18
	/** @var \phpbb\config\config */
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
	protected $config;
20
21
	/** @var \phpbb\config\db_text */
0 ignored issues
show
Bug introduced by
The type phpbb\config\db_text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
	protected $config_text;
23
24
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
	protected $container;
26
27
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
	protected $db;
29
30
	/** @var \phpbb\extension\manager */
0 ignored issues
show
Bug introduced by
The type phpbb\extension\manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
	protected $ext_manager;
32
33
	/** @var \phpbb\language\language */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
	protected $language;
35
36
	/** @var \phpbb\request\request */
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
	protected $request;
38
39
	/** @var \phpbb\template\template */
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
	protected $template;
41
42
	/** @var string */
43
	public $page_title;
44
45
	/** @var string */
46
	public $tpl_name;
47
48
	/** @var string */
49
	public $u_action;
50
51
	/** @var array */
52
	protected $errors = [];
53
54
	/**
55
	 * Constructor
56
	 *
57
	 * @throws \Exception
58
	 */
59
	public function __construct()
60
	{
61
		global $phpbb_container;
62
63
		$this->container   = $phpbb_container;
64
		$this->cache       = $this->container->get('cache');
65
		$this->config      = $this->container->get('config');
66
		$this->config_text = $this->container->get('config_text');
67
		$this->db          = $this->container->get('dbal.conn');
68
		$this->ext_manager = $this->container->get('ext.manager');
69
		$this->language    = $this->container->get('language');
70
		$this->request     = $this->container->get('request');
71
		$this->template    = $this->container->get('template');
72
	}
73
74
	/**
75
	 * Main ACP module
76
	 */
77
	public function main()
78
	{
79
		$this->language->add_lang('abbc3', 'vse/abbc3');
80
81
		$this->tpl_name   = 'acp_abbc3_settings';
82
		$this->page_title = $this->language->lang('ACP_ABBC3_SETTINGS');
83
84
		$form_key = 'vse/abbc3';
85
		add_form_key($form_key);
0 ignored issues
show
Bug introduced by
The function add_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
		/** @scrutinizer ignore-call */ 
86
  add_form_key($form_key);
Loading history...
86
87
		if ($this->request->is_set_post('submit'))
88
		{
89
			if (!check_form_key($form_key))
0 ignored issues
show
Bug introduced by
The function check_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
			if (!/** @scrutinizer ignore-call */ check_form_key($form_key))
Loading history...
90
			{
91
				trigger_error('FORM_INVALID', E_USER_WARNING);
92
			}
93
94
			$this->save_settings();
95
		}
96
97
		$this->display_settings();
98
	}
99
100
	/**
101
	 * Add settings template vars to the form
102
	 */
103
	protected function display_settings()
104
	{
105
		$this->template->assign_vars([
106
			'S_ABBC3_PIPES'			=> $this->config['abbc3_pipes'],
107
			'S_ABBC3_BBCODE_BAR'	=> $this->config['abbc3_bbcode_bar'],
108
			'S_ABBC3_QR_BBCODES'	=> $this->config['abbc3_qr_bbcodes'],
109
			'S_ABBC3_AUTO_VIDEO'	=> $this->config['abbc3_auto_video'],
110
			'S_ABBC3_ICONS_TYPE'	=> build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
0 ignored issues
show
Bug introduced by
The function build_select was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
			'S_ABBC3_ICONS_TYPE'	=> /** @scrutinizer ignore-call */ build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
Loading history...
111
			'S_ABBC3_GOOGLE_FONTS'	=> $this->show_google_fonts(),
112
			'S_ABBC3_MEDIA_EMBED'	=> (int) $this->ext_manager->is_enabled('phpbb/mediaembed'),
113
			'U_ACTION'				=> $this->u_action,
114
		]);
115
	}
116
117
	/**
118
	 * Save settings data to the database
119
	 */
120
	protected function save_settings()
121
	{
122
		$this->config->set('abbc3_bbcode_bar', $this->request->variable('abbc3_bbcode_bar', 0));
123
		$this->config->set('abbc3_qr_bbcodes', $this->request->variable('abbc3_qr_bbcodes', 0));
124
		$this->config->set('abbc3_auto_video', $this->request->variable('abbc3_auto_video', 0));
125
		$this->config->set('abbc3_icons_type', $this->request->variable('abbc3_icons_type', 'png'));
126
		$this->save_pipes();
127
		$this->save_google_fonts();
128
129
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
130
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
131
132
		if ($this->errors)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133
		{
134
			trigger_error(implode('<br>', $this->errors) . adm_back_link($this->u_action), E_USER_WARNING);
0 ignored issues
show
Bug introduced by
The function adm_back_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
			trigger_error(implode('<br>', $this->errors) . /** @scrutinizer ignore-call */ adm_back_link($this->u_action), E_USER_WARNING);
Loading history...
135
		}
136
137
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
138
	}
139
140
	/**
141
	 * Save the Pipes Table setting.
142
	 * - Set the config
143
	 * - Show or hide the Pipes BBCode button
144
	 * - Purge BBCode caches.
145
	 */
146
	protected function save_pipes()
147
	{
148
		$enable_pipes = $this->request->variable('abbc3_pipes', 0);
149
150
		$this->config->set('abbc3_pipes', $enable_pipes);
151
152
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\acp\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
153
			SET display_on_posting = ' . (int) $enable_pipes . "
154
			WHERE bbcode_tag = 'pipes'";
155
		$this->db->sql_query($sql);
156
	}
157
158
	/**
159
	 * Get the Google font setting data and format it for the form.
160
	 *
161
	 * @return string
162
	 */
163
	protected function show_google_fonts()
164
	{
165
		$fonts = json_decode($this->config_text->get('abbc3_google_fonts'), true);
166
		return $fonts ? implode("\n", $fonts) : '';
167
	}
168
169
	/**
170
	 * Save the Google fonts setting.
171
	 * - If field has data, explode it to an array and save as JSON data.
172
	 * - If field is empty, store just an empty string.
173
	 */
174
	protected function save_google_fonts()
175
	{
176
		$fonts = $this->request->variable('abbc3_google_fonts', '');
177
		$fonts = explode("\n", $fonts);
178
		$this->validate_google_fonts($fonts);
179
		$this->config_text->set('abbc3_google_fonts', json_encode($fonts));
180
	}
181
182
	/**
183
	 * Validate Google Font exists.
184
	 *
185
	 * @param array $fonts
186
	 * @return void
187
	 */
188
	protected function validate_google_fonts(&$fonts)
189
	{
190
		foreach ($fonts as $key => $font)
191
		{
192
			if (empty($font) || $this->valid_url('https://fonts.googleapis.com/css?family=' . urlencode($font)))
193
			{
194
				continue;
195
			}
196
197
			$this->errors[] = $this->language->lang('ABBC3_INVALID_FONT', $font);
198
			unset($fonts[$key]);
199
		}
200
	}
201
202
	/**
203
	 * Check for valid URL headers
204
	 *
205
	 * @param string $url
206
	 * @return bool Return true if URL is valid, false if not. Return true if unable to check URL.
207
	 */
208
	private function valid_url($url)
209
	{
210
		$file_headers = @get_headers($url);
211
		return !$file_headers || $file_headers[0] === 'HTTP/1.1 200 OK';
212
	}
213
}
214