Completed
Pull Request — master (#73)
by rxu
01:18
created

main_module::purge_mediaembed_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\cache\driver\driver_interface $cache */
19
	protected $cache;
20
21
	/** @var \phpbb\config\config $config */
22
	protected $config;
23
24
	/** @var \phpbb\config\db_text $config_text */
25
	protected $config_text;
26
27
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
28
	protected $container;
29
30
	/** @var \phpbb\language\language $language */
31
	protected $language;
32
33
	/** @var \phpbb\log\log $log */
34
	protected $log;
35
36
	/** @var \phpbb\mediaembed\cache\cache $mediaembed_cache */
37
	protected $mediaembed_cache;
38
39
	/** @var \phpbb\request\request $request */
40
	protected $request;
41
42
	/** @var \phpbb\template\template $template */
43
	protected $template;
44
45
	/** @var \phpbb\user */
46
	protected $user;
47
48
	/** @var array $enabled_sites */
49
	protected $enabled_sites;
50
51
	/** @var string $page_title */
52
	public $page_title;
53
54
	/** @var string $tpl_name */
55
	public $tpl_name;
56
57
	/** @var string $u_action */
58
	public $u_action;
59
60
	/** @var array An array of errors */
61
	protected $errors = [];
62
63
	/**
64
	 * Constructor
65
	 *
66
	 * @throws \Exception
67
	 */
68
	public function __construct()
69
	{
70
		global $phpbb_container;
71
72
		$this->container   = $phpbb_container;
73
		$this->cache       = $this->container->get('cache.driver');
74
		$this->config      = $this->container->get('config');
75
		$this->config_text = $this->container->get('config_text');
76
		$this->language    = $this->container->get('language');
77
		$this->log         = $this->container->get('log');
78
		$this->mediaembed_cache = $this->container->get('phpbb.mediaembed.cache');
79
		$this->request     = $this->container->get('request');
80
		$this->template    = $this->container->get('template');
81
		$this->user        = $this->container->get('user');
82
83
		$this->language->add_lang('acp', 'phpbb/mediaembed');
84
	}
85
86
	/**
87
	 * Main ACP module
88
	 *
89
	 * @param int    $id   The module ID (not used)
90
	 * @param string $mode The module mode (manage|settings)
91
	 * @throws \Exception
92
	 */
93
	public function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
	{
95
		$mode = strtolower($mode);
96
97
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . $mode;
98
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
99
100
		$form_key = 'phpbb/mediaembed';
101
		add_form_key($form_key);
102
103
		if ($this->request->is_set_post('action_purge_cache'))
104
		{
105
			$this->purge_mediaembed_cache();
106
		}
107
108
		if ($this->request->is_set_post('submit'))
109
		{
110
			if (!check_form_key($form_key))
111
			{
112
				trigger_error('FORM_INVALID', E_USER_WARNING);
113
			}
114
115
			$this->{'save_' . $mode}();
116
		}
117
118
		$this->{'display_' . $mode}();
119
	}
120
121
	/**
122
	 * Add settings template vars to the form
123
	 */
124
	protected function display_settings()
125
	{
126
		$this->template->assign_vars([
127
			'S_MEDIA_EMBED_BBCODE'		=> $this->config['media_embed_bbcode'],
128
			'S_MEDIA_EMBED_ALLOW_SIG'	=> $this->config['media_embed_allow_sig'],
129
			'S_MEDIA_EMBED_PARSE_URLS'	=> $this->config['media_embed_parse_urls'],
130
			'S_MEDIA_EMBED_ENABLE_CACHE'=> $this->config['media_embed_enable_cache'],
131
			'U_ACTION'					=> $this->u_action,
132
		]);
133
	}
134
135
	/**
136
	 * Add manage sites template vars to the form
137
	 *
138
	 * @throws \Exception
139
	 */
140
	protected function display_manage()
141
	{
142
		$this->template->assign_vars([
143
			'MEDIA_SITES'	=> $this->get_sites(),
144
			'U_ACTION'		=> $this->u_action,
145
			'ERRORS'		=> $this->errors,
146
		]);
147
	}
148
149
	/**
150
	 * Get a list of available sites
151
	 *
152
	 * @return array An array of available sites
153
	 * @throws \Exception
154
	 */
155
	protected function get_sites()
156
	{
157
		$sites = [];
158
159
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
160
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
161
		{
162
			$disabled = isset($configurator->BBCodes[$siteId]);
163
			$sites[$siteId] = [
164
				'id'		=> $siteId,
165
				'name'		=> $siteConfig['name'],
166
				'title'		=> $this->language->lang($disabled ? 'ACP_MEDIA_SITE_DISABLED' : 'ACP_MEDIA_SITE_TITLE', $siteId),
167
				'enabled'	=> in_array($siteId, $this->get_enabled_sites()),
168
				'disabled'	=> $disabled,
169
			];
170
		}
171
172
		ksort($sites);
173
174
		$this->errors = array_diff($this->get_enabled_sites(), array_keys($sites));
175
176
		return $sites;
177
	}
178
179
	/**
180
	 * Get enabled media sites stored in the database
181
	 *
182
	 * @return array An array of enabled sites
183
	 */
184
	protected function get_enabled_sites()
185
	{
186
		if ($this->enabled_sites === null)
187
		{
188
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
189
			$this->enabled_sites = is_array($sites) ? $sites : [];
190
		}
191
192
		return $this->enabled_sites;
193
	}
194
195
	/**
196
	 * Save site managed data to the database
197
	 */
198
	protected function save_manage()
199
	{
200
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
201
202
		$this->purge_textformatter_cache();
203
204
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
205
206
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
207
	}
208
209
	/**
210
	 * Save settings data to the database
211
	 */
212
	protected function save_settings()
213
	{
214
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
215
		$this->config->set('media_embed_allow_sig', $this->request->variable('media_embed_allow_sig', 0));
216
		$this->config->set('media_embed_parse_urls', $this->request->variable('media_embed_parse_urls', 0));
217
		$this->config->set('media_embed_enable_cache', $this->request->variable('media_embed_enable_cache', 0));
218
219
		$this->purge_textformatter_cache();
220
221
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
222
223
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
224
	}
225
226
	/**
227
	 * Purge cached TextFormatter files
228
	 */
229
	protected function purge_textformatter_cache()
230
	{
231
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
232
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
233
	}
234
235
	/**
236
	 * Purge all MediaEmbed cache files
237
	 */
238
	protected function purge_mediaembed_cache()
239
	{
240
		$this->mediaembed_cache->purge();
241
242
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_CACHE_PURGED');
243
244
		trigger_error($this->language->lang('PURGE_CACHE_SUCCESS') . adm_back_link($this->u_action));
245
	}
246
}
247