main_listener::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 6
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.9332
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\event;
12
13
use phpbb\auth\auth;
0 ignored issues
show
Bug introduced by
The type phpbb\auth\auth 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
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...
15
use 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...
16
use 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...
17
use phpbb\mediaembed\collection\customsitescollection;
18
use phpbb\mediaembed\ext;
19
use 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...
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\EventD...ventSubscriberInterface 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
use Symfony\Component\Yaml\Yaml;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Yaml\Yaml 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
23
/**
24
 * Event listener
25
 */
26
class main_listener implements EventSubscriberInterface
27
{
28
	/** @var string A link to a rich content media site for demo purposes */
29
	public const MEDIA_DEMO_URL = 'https://youtu.be/Ne18ZQ7LLI0';
30
31
	/** @var auth */
32
	protected $auth;
33
34
	/** @var config $config */
35
	protected $config;
36
37
	/** @var db_text $config_text */
38
	protected $config_text;
39
40
	/** @var language $language */
41
	protected $language;
42
43
	/** @var template $template */
44
	protected $template;
45
46
	/** @var customsitescollection $custom_sites */
47
	protected $custom_sites;
48
49
	/** @var string $cache_dir */
50
	protected $cache_dir;
51
52
	/** @var bool Disable the media embed plugin (plain url parsing) */
53
	protected $disable_plugin = false;
54
55
	/** @var bool Disable the media tag (bbcode parsing) */
56
	protected $disable_tag = false;
57
58
	/**
59
	 * {@inheritDoc}
60
	 */
61
	public static function getSubscribedEvents()
62
	{
63
		return [
64
			'core.text_formatter_s9e_configure_after'	=> [['add_custom_sites', 3], ['enable_media_sites', 2], ['configure_url_parsing', 1]],
65
			'core.display_custom_bbcodes'				=> 'setup_media_bbcode',
66
			'core.permissions'							=> 'set_permissions',
67
			'core.help_manager_add_block_before'		=> 'media_embed_help',
68
			'core.posting_modify_message_text'			=> 'check_forum_permission',
69
			'core.ucp_pm_compose_modify_parse_before'	=> 'check_pm_permission',
70
			'core.message_parser_check_message'			=> [['check_signature'], ['check_magic_urls'], ['check_bbcode_enabled']],
71
			'core.text_formatter_s9e_parser_setup'		=> [['disable_media_embed'], ['setup_cache_dir']],
72
			'core.page_header' 							=> 'setup_media_configs',
73
		];
74
	}
75
76
	/**
77
	 * Constructor
78
	 *
79
	 * @param auth					$auth
80
	 * @param config					$config
81
	 * @param db_text				$config_text
82
	 * @param language				$language
83
	 * @param template				$template
84
	 * @param customsitescollection	$custom_sites
85
	 * @param string				$cache_dir
86
	 */
87
	public function __construct(auth $auth, config $config, db_text $config_text, language $language, template $template, customsitescollection $custom_sites, $cache_dir)
88
	{
89
		$this->auth = $auth;
90
		$this->config = $config;
91
		$this->language = $language;
92
		$this->template = $template;
93
		$this->config_text = $config_text;
94
		$this->custom_sites = $custom_sites;
95
		$this->cache_dir = $cache_dir;
96
	}
97
98
	/**
99
	 * Add any custom site definitions to the default MediaEmbed sites object
100
	 *
101
	 * @param \phpbb\event\data $event The event object
0 ignored issues
show
Bug introduced by
The type phpbb\event\data 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...
102
	 * @return void
103
	 */
104
	public function add_custom_sites($event)
105
	{
106
		foreach ($this->custom_sites->get_collection() as $site)
107
		{
108
			$event['configurator']->MediaEmbed->defaultSites->add(
109
				basename($site, ext::YML),
110
				Yaml::parse(file_get_contents($site))
111
			);
112
		}
113
	}
114
115
	/**
116
	 * Enable media sites
117
	 *
118
	 * @param \phpbb\event\data $event The event object
119
	 * @return void
120
	 */
121
	public function enable_media_sites($event)
122
	{
123
		foreach ($this->get_siteIds() as $siteId)
124
		{
125
			// skip media sites that already exist as a BBCode
126
			if (isset($event['configurator']->BBCodes[$siteId]))
127
			{
128
				continue;
129
			}
130
131
			try
132
			{
133
				$event['configurator']->MediaEmbed->add($siteId);
134
			}
135
			catch (\RuntimeException $e)
136
			{
137
				continue;
138
			}
139
		}
140
	}
141
142
	/**
143
	 * Configure plain URL parsing
144
	 *
145
	 * @param \phpbb\event\data $event The event object
146
	 * @return void
147
	 */
148
	public function configure_url_parsing($event)
149
	{
150
		// Disable plain url parsing?
151
		if (!$this->config->offsetGet('media_embed_parse_urls'))
152
		{
153
			$event['configurator']->MediaEmbed->finalize();
154
			unset($event['configurator']->MediaEmbed);
155
		}
156
	}
157
158
	/**
159
	 * Set template switch for displaying the [media] BBCode button
160
	 *
161
	 * @return void
162
	 */
163
	public function setup_media_bbcode()
164
	{
165
		$this->language->add_lang('common', 'phpbb/mediaembed');
166
		$this->template->assign_var('S_BBCODE_MEDIA', $this->config->offsetGet('media_embed_bbcode'));
167
	}
168
169
	/**
170
	 * Set media embed forum and user PM permission
171
	 *
172
	 * @param	\phpbb\event\data	$event	The event object
173
	 * @return	void
174
	 */
175
	public function set_permissions($event)
176
	{
177
		$event->update_subarray('permissions', 'f_mediaembed', ['lang' => 'ACL_F_MEDIAEMBED', 'cat' => 'content']);
178
		$event->update_subarray('permissions', 'u_pm_mediaembed', ['lang' => 'ACL_U_PM_MEDIAEMBED', 'cat' => 'pm']);
179
	}
180
181
	/**
182
	 * Add Media Embed help to the BBCode Guide
183
	 *
184
	 * @param \phpbb\event\data $event The event object
185
	 * @return void
186
	 */
187
	public function media_embed_help($event)
188
	{
189
		if ($event['block_name'] === 'HELP_BBCODE_BLOCK_OTHERS')
190
		{
191
			$this->language->add_lang('help', 'phpbb/mediaembed');
192
193
			$this->template->assign_block_vars('faq_block', [
194
				'BLOCK_TITLE'	=> $this->language->lang('HELP_EMBEDDING_MEDIA'),
195
				'SWITCH_COLUMN'	=> false,
196
			]);
197
198
			$uid = $bitfield = $flags = '';
199
			$demo_text = self::MEDIA_DEMO_URL;
200
			generate_text_for_storage($demo_text, $uid, $bitfield, $flags, true, true);
0 ignored issues
show
Bug introduced by
The function generate_text_for_storage 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

200
			/** @scrutinizer ignore-call */ 
201
   generate_text_for_storage($demo_text, $uid, $bitfield, $flags, true, true);
Loading history...
201
			$demo_display = generate_text_for_display($demo_text, $uid, $bitfield, $flags);
0 ignored issues
show
Bug introduced by
The function generate_text_for_display 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

201
			$demo_display = /** @scrutinizer ignore-call */ generate_text_for_display($demo_text, $uid, $bitfield, $flags);
Loading history...
202
			$list_sites = implode(', ', $this->get_siteIds());
203
204
			$this->template->assign_block_vars('faq_block.faq_row', [
205
				'FAQ_QUESTION'	=> $this->language->lang('HELP_EMBEDDING_MEDIA_QUESTION'),
206
				'FAQ_ANSWER'	=> $this->language->lang('HELP_EMBEDDING_MEDIA_ANSWER', self::MEDIA_DEMO_URL, $demo_display, $list_sites),
207
			]);
208
		}
209
	}
210
211
	/**
212
	 * Disable Media Embed plugin and tag if necessary
213
	 *
214
	 * @param \phpbb\event\data $event The event object
215
	 * @return void
216
	 */
217
	public function disable_media_embed($event)
218
	{
219
		/** @var \phpbb\textformatter\s9e\parser $service  */
220
		$service = $event['parser'];
221
		$parser = $service->get_parser();
222
223
		if ($this->disable_plugin)
224
		{
225
			$parser->disablePlugin('MediaEmbed');
226
		}
227
228
		if ($this->disable_tag)
229
		{
230
			$parser->disableTag('MEDIA');
231
		}
232
	}
233
234
	/**
235
	 * Set up a cache directory to improve scraping performance
236
	 *
237
	 * @param \phpbb\event\data $event The event object
238
	 * @return void
239
	 */
240
	public function setup_cache_dir($event)
241
	{
242
		if ($this->cache_dir && $this->config->offsetGet('media_embed_enable_cache'))
243
		{
244
			/** @var \phpbb\textformatter\s9e\parser $service  */
245
			$service = $event['parser'];
246
			$parser = $service->get_parser();
247
248
			$parser->registeredVars['cacheDir'] = $this->cache_dir;
249
		}
250
	}
251
252
	/**
253
	 * Check if forum permission allows Media Embed
254
	 *
255
	 * @param \phpbb\event\data $event The event object
256
	 * @return void
257
	 */
258
	public function check_forum_permission($event)
259
	{
260
		if (!$this->auth->acl_get('f_mediaembed', $event['forum_id']) || !$this->auth->acl_get('f_bbcode', $event['forum_id']))
261
		{
262
			$this->disable_plugin = true;
263
			$this->disable_tag = true;
264
		}
265
	}
266
267
	/**
268
	 * Check if user permission allows Media Embed in private messages
269
	 *
270
	 * @return void
271
	 */
272
	public function check_pm_permission()
273
	{
274
		if (!$this->auth->acl_get('u_pm_mediaembed'))
275
		{
276
			$this->disable_plugin = true;
277
			$this->disable_tag = true;
278
		}
279
	}
280
281
	/**
282
	 * Check if signature posting is allowed.
283
	 * Posting signatures is 'sig', reparsing signatures is 'user_signature'.
284
	 *
285
	 * @param \phpbb\event\data $event The event object
286
	 * @return void
287
	 */
288
	public function check_signature($event)
289
	{
290
		if (($event['mode'] === 'sig' || $event['mode'] === 'text_reparser.user_signature') && !$this->config->offsetGet('media_embed_allow_sig'))
291
		{
292
			$this->disable_plugin = true;
293
			$this->disable_tag = true;
294
		}
295
	}
296
297
	/**
298
	 * Check if magic urls is allowed.
299
	 *
300
	 * @param \phpbb\event\data $event The event object
301
	 * @return void
302
	 */
303
	public function check_magic_urls($event)
304
	{
305
		if (!$event['allow_magic_url'] || !$this->config->offsetGet('media_embed_parse_urls'))
306
		{
307
			$this->disable_plugin = true;
308
		}
309
	}
310
311
	/**
312
	 * Check if bbcodes are allowed.
313
	 *
314
	 * @param \phpbb\event\data $event The event object
315
	 * @return void
316
	 */
317
	public function check_bbcode_enabled($event)
318
	{
319
		if (!$event['allow_bbcode'])
320
		{
321
			// Want to leave plugin enabled, but it seems plugin won't work
322
			// when tag is disabled, so we have to disable both it seems.
323
			$this->disable_plugin = true;
324
			$this->disable_tag = true;
325
		}
326
	}
327
328
	public function setup_media_configs()
329
	{
330
		$this->template->assign_vars([
331
			'S_MEDIA_EMBED_FULL_WIDTH' => $this->config->offsetGet('media_embed_full_width'),
332
			'S_MEDIA_EMBED_MAX_WIDTHS' => json_decode($this->config_text->get('media_embed_max_width'), true),
333
		]);
334
	}
335
336
	/**
337
	 * Get allowed sites for media embedding
338
	 *
339
	 * @return array An array of sites
340
	 */
341
	protected function get_siteIds()
342
	{
343
		$siteIds = $this->config_text->get('media_embed_sites');
344
345
		return $siteIds ? json_decode($siteIds, true) : [];
346
	}
347
}
348