Issues (95)

controller/acp_controller.php (12 issues)

Labels
Severity
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2020, 2023 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\controller;
12
13
use phpbb\cache\driver\driver_interface as cache;
0 ignored issues
show
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...
14
use phpbb\config\config;
0 ignored issues
show
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...
15
use phpbb\config\db_text;
0 ignored issues
show
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...
16
use phpbb\db\driver\driver_interface as db;
0 ignored issues
show
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...
17
use phpbb\extension\manager as ext_manager;
0 ignored issues
show
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...
18
use phpbb\language\language;
0 ignored issues
show
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...
19
use phpbb\request\request;
0 ignored issues
show
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...
20
use phpbb\template\template;
0 ignored issues
show
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...
21
22
class acp_controller
23
{
24
	/** @var cache */
25
	protected $cache;
26
27
	/** @var config */
28
	protected $config;
29
30
	/** @var db_text */
31
	protected $config_text;
32
33
	/** @var db */
34
	protected $db;
35
36
	/** @var ext_manager */
37
	protected $ext_manager;
38
39
	/** @var language */
40
	protected $language;
41
42
	/** @var request */
43
	protected $request;
44
45
	/** @var template */
46
	protected $template;
47
48
	/** @var string */
49
	protected $parser_key;
50
51
	/** @var string */
52
	protected $renderer_key;
53
54
	/** @var string */
55
	public $u_action;
56
57
	/** @var array */
58
	protected $errors = [];
59
60
	/**
61
	 * Constructor
62
	 *
63
	 * @param cache $cache
64
	 * @param config $config
65
	 * @param db_text $db_text
66
	 * @param db $db
67
	 * @param ext_manager $ext_manager
68
	 * @param language $language
69
	 * @param request $request
70
	 * @param template $template
71
	 * @param $parser_key
72
	 * @param $renderer_key
73
	 */
74
	public function __construct(cache $cache, config $config, db_text $db_text, db $db, ext_manager $ext_manager, language $language, request $request, template $template, $parser_key, $renderer_key)
75
	{
76
		$this->cache = $cache;
77
		$this->config = $config;
78
		$this->config_text = $db_text;
79
		$this->db = $db;
80
		$this->ext_manager = $ext_manager;
81
		$this->language = $language;
82
		$this->request = $request;
83
		$this->template = $template;
84
		$this->parser_key = $parser_key;
85
		$this->renderer_key = $renderer_key;
86
	}
87
88
	/**
89
	 * Main handler for this controller
90
	 *
91
	 * @throws \RuntimeException
92
	 */
93
	public function handle()
94
	{
95
		$this->language->add_lang('acp_abbc3', 'vse/abbc3');
96
97
		$form_key = 'vse/abbc3';
98
		add_form_key($form_key);
0 ignored issues
show
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

98
		/** @scrutinizer ignore-call */ 
99
  add_form_key($form_key);
Loading history...
99
100
		if ($this->request->is_set_post('submit'))
101
		{
102
			if (!check_form_key($form_key))
0 ignored issues
show
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

102
			if (!/** @scrutinizer ignore-call */ check_form_key($form_key))
Loading history...
103
			{
104
				throw new \RuntimeException($this->language->lang('FORM_INVALID'), E_USER_WARNING);
105
			}
106
107
			$this->save_settings();
108
		}
109
110
		$this->display_settings();
111
	}
112
113
	/**
114
	 * Add settings template vars to the form
115
	 */
116
	protected function display_settings()
117
	{
118
		$this->template->assign_vars([
119
			'S_ABBC3_PIPES'			=> $this->config['abbc3_pipes'],
120
			'S_ABBC3_BBCODE_BAR'	=> $this->config['abbc3_bbcode_bar'],
121
			'S_ABBC3_QR_BBCODES'	=> $this->config['abbc3_qr_bbcodes'],
122
			'S_ABBC3_AUTO_VIDEO'	=> $this->config['abbc3_auto_video'],
123
			'S_ABBC3_ICONS_TYPE'	=> build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
0 ignored issues
show
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

123
			'S_ABBC3_ICONS_TYPE'	=> /** @scrutinizer ignore-call */ build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
Loading history...
124
			'S_ABBC3_GOOGLE_FONTS'	=> $this->get_google_fonts(),
125
			'S_ABBC3_MEDIA_EMBED'	=> (int) $this->ext_manager->is_enabled('phpbb/mediaembed'),
126
			'U_ACTION'				=> $this->u_action,
127
		]);
128
	}
129
130
	/**
131
	 * Save settings data to the database
132
	 *
133
	 * @throws \RuntimeException
134
	 */
135
	protected function save_settings()
136
	{
137
		$this->config->set('abbc3_bbcode_bar', $this->request->variable('abbc3_bbcode_bar', 0));
138
		$this->config->set('abbc3_qr_bbcodes', $this->request->variable('abbc3_qr_bbcodes', 0));
139
		$this->config->set('abbc3_auto_video', $this->request->variable('abbc3_auto_video', 0));
140
		$this->config->set('abbc3_icons_type', $this->request->variable('abbc3_icons_type', 'png'));
141
		$this->save_pipes();
142
		$this->save_google_fonts();
143
144
		$this->cache->destroy($this->parser_key);
145
		$this->cache->destroy($this->renderer_key);
146
147
		if (!empty($this->errors))
148
		{
149
			throw new \RuntimeException(implode('<br>', $this->errors), E_USER_WARNING);
150
		}
151
152
		throw new \RuntimeException($this->language->lang('CONFIG_UPDATED'), E_USER_NOTICE);
153
	}
154
155
	/**
156
	 * Save the Pipes Table setting.
157
	 * - Set the config
158
	 * - Show or hide the Pipes BBCode button
159
	 * - Purge BBCode caches.
160
	 */
161
	protected function save_pipes()
162
	{
163
		$enable_pipes = $this->request->variable('abbc3_pipes', 0);
164
165
		$this->config->set('abbc3_pipes', $enable_pipes);
166
167
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
The constant vse\abbc3\controller\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
168
			SET display_on_posting = ' . (int) $enable_pipes . "
169
			WHERE bbcode_tag = 'pipes'";
170
		$this->db->sql_query($sql);
171
	}
172
173
	/**
174
	 * Get the Google font setting data and format it for the form.
175
	 *
176
	 * @return string
177
	 */
178
	protected function get_google_fonts()
179
	{
180
		$fonts = json_decode($this->config_text->get('abbc3_google_fonts'), true);
181
		return $fonts ? implode("\n", $fonts) : '';
182
	}
183
184
	/**
185
	 * Save the Google fonts setting.
186
	 * - If field has data, explode it to an array and save as JSON data.
187
	 * - If field is empty, store just an empty string.
188
	 */
189
	protected function save_google_fonts()
190
	{
191
		$fonts = $this->request->variable('abbc3_google_fonts', '');
192
193
		if (!empty($fonts))
194
		{
195
			$fonts = array_filter(
196
				array_map('trim', explode("\n", $fonts)),
197
				[$this, 'validate_google_fonts']
198
			);
199
200
			$fonts = $fonts ? json_encode(array_values($fonts)) : '';
201
		}
202
203
		$this->config_text->set('abbc3_google_fonts', $fonts);
204
	}
205
206
	/**
207
	 * Validate Google Font names link to an existing CSS file
208
	 *
209
	 * @param string $font
210
	 * @return bool
211
	 */
212
	protected function validate_google_fonts($font)
213
	{
214
		if ($font === '')
215
		{
216
			return false;
217
		}
218
219
		$url = 'https://fonts.googleapis.com/css?family=' . urlencode($font);
220
221
		if ($this->valid_url($url))
222
		{
223
			return true;
224
		}
225
226
		$this->errors[] = $this->language->lang('ABBC3_INVALID_FONT', $font);
227
		return false;
228
	}
229
230
	/**
231
	 * Check for valid URL headers if possible
232
	 *
233
	 * @param string $url
234
	 * @return bool Return false only if URL could be checked and wasn't found, otherwise true.
235
	 */
236
	protected function valid_url($url)
237
	{
238
		$headers = function_exists('get_headers') ? @get_headers($url) : false;
239
		return !$headers || stripos($headers[0], '200 OK') !== false;
240
	}
241
242
	/**
243
	 * Set the u_action variable
244
	 *
245
	 * @param string $u_action
246
	 * @return acp_controller
247
	 */
248
	public function set_u_action($u_action)
249
	{
250
		$this->u_action = $u_action;
251
		return $this;
252
	}
253
}
254