Passed
Pull Request — master (#89)
by Matt
01:47
created

main_module::get_sites()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 5
b 0
f 0
nc 2
nop 0
dl 0
loc 22
rs 9.8333
1
<?php
2
/**
3
 *
4
 * phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\mediaembed\acp;
12
13
/**
14
 * phpBB Media Embed Plugin ACP module.
15
 */
16
class main_module
17
{
18
	/** @var \phpbb\config\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 $config_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 $container */
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\language\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...
28
	protected $language;
29
30
	/** @var \phpbb\log\log $log */
0 ignored issues
show
Bug introduced by
The type phpbb\log\log 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 $log;
32
33
	/** @var \phpbb\mediaembed\cache\cache $media_cache */
34
	protected $media_cache;
35
36
	/** @var \phpbb\request\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 $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 \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user 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...
43
	protected $user;
44
45
	/** @var array $enabled_sites */
46
	protected $enabled_sites;
47
48
	/** @var string $page_title */
49
	public $page_title;
50
51
	/** @var string $tpl_name */
52
	public $tpl_name;
53
54
	/** @var string $u_action */
55
	public $u_action;
56
57
	/** @var array An array of errors */
58
	protected $errors = [];
59
60
	/**
61
	 * Constructor
62
	 *
63
	 * @throws \Exception
64
	 */
65
	public function __construct()
66
	{
67
		global $phpbb_container;
68
69
		$this->container   = $phpbb_container;
70
		$this->config      = $this->container->get('config');
71
		$this->config_text = $this->container->get('config_text');
72
		$this->language    = $this->container->get('language');
73
		$this->log         = $this->container->get('log');
74
		$this->media_cache = $this->container->get('phpbb.mediaembed.cache');
75
		$this->request     = $this->container->get('request');
76
		$this->template    = $this->container->get('template');
77
		$this->user        = $this->container->get('user');
78
79
		$this->language->add_lang('acp', 'phpbb/mediaembed');
80
	}
81
82
	/**
83
	 * Main ACP module
84
	 *
85
	 * @param int    $id   The module ID (not used)
86
	 * @param string $mode The module mode (manage|settings)
87
	 * @throws \Exception
88
	 */
89
	public function main($id, $mode)
90
	{
91
		$mode = strtolower($mode);
92
93
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . $mode;
94
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
95
96
		$form_key = 'phpbb/mediaembed';
97
		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

97
		/** @scrutinizer ignore-call */ 
98
  add_form_key($form_key);
Loading history...
98
99
		if ($this->request->is_set_post('action_purge_cache'))
100
		{
101
			$this->purge_mediaembed_cache();
102
		}
103
104
		if ($this->request->is_set_post('submit'))
105
		{
106
			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

106
			if (!/** @scrutinizer ignore-call */ check_form_key($form_key))
Loading history...
107
			{
108
				trigger_error('FORM_INVALID', E_USER_WARNING);
109
			}
110
111
			$this->{'save_' . $mode}();
112
		}
113
114
		$this->{'display_' . $mode}();
115
	}
116
117
	/**
118
	 * Add settings template vars to the form
119
	 */
120
	protected function display_settings()
121
	{
122
		$this->template->assign_vars([
123
			'S_MEDIA_EMBED_BBCODE'		=> $this->config['media_embed_bbcode'],
124
			'S_MEDIA_EMBED_ALLOW_SIG'	=> $this->config['media_embed_allow_sig'],
125
			'S_MEDIA_EMBED_PARSE_URLS'	=> $this->config['media_embed_parse_urls'],
126
			'S_MEDIA_EMBED_ENABLE_CACHE'=> $this->config['media_embed_enable_cache'],
127
			'S_MEDIA_EMBED_FULL_WIDTH'	=> $this->config['media_embed_full_width'],
128
			'S_MEDIA_EMBED_MAX_WIDTHS'	=> $this->get_media_embed_max_width(),
129
			'U_ACTION'					=> $this->u_action,
130
		]);
131
	}
132
133
	/**
134
	 * Add manage sites template vars to the form
135
	 *
136
	 * @throws \Exception
137
	 */
138
	protected function display_manage()
139
	{
140
		$this->template->assign_vars([
141
			'MEDIA_SITES'	=> $this->get_sites(),
142
			'U_ACTION'		=> $this->u_action,
143
			'ERRORS'		=> $this->errors,
144
		]);
145
	}
146
147
	/**
148
	 * Get a list of available sites
149
	 *
150
	 * @return array An array of available sites
151
	 * @throws \Exception
152
	 */
153
	protected function get_sites()
154
	{
155
		$sites = [];
156
157
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
158
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
159
		{
160
			$disabled = isset($configurator->BBCodes[$siteId]);
161
			$sites[$siteId] = [
162
				'id'		=> $siteId,
163
				'name'		=> $siteConfig['name'],
164
				'title'		=> $this->language->lang($disabled ? 'ACP_MEDIA_SITE_DISABLED' : 'ACP_MEDIA_SITE_TITLE', $siteId),
165
				'enabled'	=> in_array($siteId, $this->get_enabled_sites()),
166
				'disabled'	=> $disabled,
167
			];
168
		}
169
170
		ksort($sites);
171
172
		$this->errors = array_diff($this->get_enabled_sites(), array_keys($sites));
173
174
		return $sites;
175
	}
176
177
	/**
178
	 * Get enabled media sites stored in the database
179
	 *
180
	 * @return array An array of enabled sites
181
	 */
182
	protected function get_enabled_sites()
183
	{
184
		if ($this->enabled_sites === null)
185
		{
186
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
187
			$this->enabled_sites = is_array($sites) ? $sites : [];
188
		}
189
190
		return $this->enabled_sites;
191
	}
192
193
	/**
194
	 * Save site managed data to the database
195
	 */
196
	protected function save_manage()
197
	{
198
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
199
200
		$this->media_cache->purge_textformatter_cache();
201
202
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
203
204
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
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

204
		trigger_error($this->language->lang('CONFIG_UPDATED') . /** @scrutinizer ignore-call */ adm_back_link($this->u_action));
Loading history...
205
	}
206
207
	/**
208
	 * Save settings data to the database
209
	 */
210
	protected function save_settings()
211
	{
212
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
213
		$this->config->set('media_embed_allow_sig', $this->request->variable('media_embed_allow_sig', 0));
214
		$this->config->set('media_embed_parse_urls', $this->request->variable('media_embed_parse_urls', 0));
215
		$this->config->set('media_embed_enable_cache', $this->request->variable('media_embed_enable_cache', 0));
216
		$this->config->set('media_embed_full_width', $this->request->variable('media_embed_full_width', 0));
217
218
		$this->set_media_embed_max_width();
219
220
		$this->media_cache->purge_textformatter_cache();
221
222
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
223
224
		if (count($this->errors))
225
		{
226
			trigger_error($this->language->lang('ACP_MEDIA_ERROR_MSG', 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

226
			trigger_error($this->language->lang('ACP_MEDIA_ERROR_MSG', implode('<br>', $this->errors)) . /** @scrutinizer ignore-call */ adm_back_link($this->u_action), E_USER_WARNING);
Loading history...
227
		}
228
229
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
230
	}
231
232
	/**
233
	 * Purge all MediaEmbed cache files
234
	 */
235
	protected function purge_mediaembed_cache()
236
	{
237
		$this->media_cache->purge_mediaembed_cache();
238
239
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_CACHE_PURGED');
240
241
		trigger_error($this->language->lang('PURGE_CACHE_SUCCESS') . adm_back_link($this->u_action));
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

241
		trigger_error($this->language->lang('PURGE_CACHE_SUCCESS') . /** @scrutinizer ignore-call */ adm_back_link($this->u_action));
Loading history...
242
	}
243
244
	/**
245
	 * Store the media embed max width value to the config text as JSON,
246
	 * with some basic input validation and array formatting.
247
	 */
248
	protected function set_media_embed_max_width()
249
	{
250
		$input = $this->request->variable('media_embed_max_width', '');
251
252
		if ($input)
253
		{
254
			$lines = array_unique(explode("\n", $input));
255
256
			foreach ($lines as $key => $line)
257
			{
258
				$parts = explode(':', $line);
259
				if (count($parts) !== 2)
260
				{
261
					unset($lines[$key]);
262
					continue;
263
				}
264
265
				$lines[$key] = array_combine(['site', 'width'], array_map('trim', $parts));
266
			}
267
268
			$input = json_encode(array_filter($lines, [$this, 'validate']));
269
		}
270
271
		$this->config_text->set('media_embed_max_width', strtolower($input));
272
	}
273
274
	/**
275
	 * Get the stored media embed max width data from config text and convert
276
	 * from JSON to the formatting used in the ACP textarea field.
277
	 *
278
	 * @return string
279
	 */
280
	protected function get_media_embed_max_width()
281
	{
282
		$config = json_decode($this->config_text->get('media_embed_max_width'), true);
283
284
		if ($config)
285
		{
286
			foreach ($config as &$item)
287
			{
288
				$item = implode(':', $item);
289
			}
290
291
			unset($item);
292
		}
293
294
		return $config ? implode("\n", $config) : '';
295
	}
296
297
	/**
298
	 * Validate the input for media embed max widths
299
	 * 'site' key value should be a word
300
	 * 'width' key value should be a number appended with either px or %
301
	 *
302
	 * @param array $input The array to check
303
	 * @return bool True if array contains valid values, false if not
304
	 * @throws \Exception
305
	 */
306
	protected function validate($input)
307
	{
308
		// First, lets get all the available media embed site IDs
309
		static $default_sites;
310
311
		if (null === $default_sites)
312
		{
313
			$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
314
			$default_sites = array_keys(iterator_to_array($configurator->MediaEmbed->defaultSites));
315
		}
316
317
		// Next create an array to hold any errors
318
		$errors = [];
319
320
		// Check to see if the site id provided exists in Media Embed
321
		if (!in_array($input['site'], $default_sites))
322
		{
323
			$errors[] = $this->language->lang('ACP_MEDIA_INVALID_SITE', $input['site'], $input['width']);
324
		}
325
326
		// Check to see if the width provided is a valid number followed px or %
327
		if (!preg_match('/^\d+(?:%|px)$/', $input['width']))
328
		{
329
			$errors[] = $this->language->lang('ACP_MEDIA_INVALID_WIDTH', $input['site'], $input['width']);
330
		}
331
332
		// Update the errors object with any new errors
333
		$this->errors = array_merge($this->errors, $errors);
334
335
		return empty($errors);
336
	}
337
}
338