1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\textformatter\s9e;
15
16/**
17* s9e\TextFormatter\Renderer adapter
18*/
19class renderer implements \phpbb\textformatter\renderer_interface
20{
21	/**
22	* @var \s9e\TextFormatter\Plugins\Censor\Helper
23	*/
24	protected $censor;
25
26	/**
27	* @var \phpbb\event\dispatcher_interface
28	*/
29	protected $dispatcher;
30
31	/**
32	* @var quote_helper
33	*/
34	protected $quote_helper;
35
36	/**
37	* @var \s9e\TextFormatter\Renderer
38	*/
39	protected $renderer;
40
41	/**
42	* @var bool Status of the viewcensors option
43	*/
44	protected $viewcensors = false;
45
46	/**
47	* @var bool Status of the viewflash option
48	*/
49	protected $viewflash = false;
50
51	/**
52	* @var bool Status of the viewimg option
53	*/
54	protected $viewimg = false;
55
56	/**
57	* @var bool Status of the viewsmilies option
58	*/
59	protected $viewsmilies = false;
60
61	/**
62	* Constructor
63	*
64	* @param \phpbb\cache\driver\driver_interface $cache
65	* @param string $cache_dir Path to the cache dir
66	* @param string $key Cache key
67	* @param factory $factory
68	* @param \phpbb\event\dispatcher_interface $dispatcher
69	*/
70	public function __construct(\phpbb\cache\driver\driver_interface $cache, $cache_dir, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher)
71	{
72		$renderer_data = $cache->get($key);
73		if ($renderer_data)
74		{
75			$class = $renderer_data['class'];
76			if (!class_exists($class, false))
77			{
78				// Try to load the renderer class from its cache file
79				$cache_file = $cache_dir . $class . '.php';
80
81				if (file_exists($cache_file))
82				{
83					include($cache_file);
84				}
85			}
86			if (class_exists($class, false))
87			{
88				$renderer = new $class;
89			}
90			if (isset($renderer_data['censor']))
91			{
92				$censor = $renderer_data['censor'];
93			}
94		}
95		if (!isset($renderer))
96		{
97			$objects  = $factory->regenerate();
98			$renderer = $objects['renderer'];
99		}
100
101		if (isset($censor))
102		{
103			$this->censor = $censor;
104		}
105		$this->dispatcher = $dispatcher;
106		$this->renderer = $renderer;
107		$renderer = $this;
108
109		/**
110		* Configure the renderer service
111		*
112		* @event core.text_formatter_s9e_renderer_setup
113		* @var \phpbb\textformatter\s9e\renderer renderer This renderer service
114		* @since 3.2.0-a1
115		*/
116		$vars = array('renderer');
117		extract($dispatcher->trigger_event('core.text_formatter_s9e_renderer_setup', compact($vars)));
118	}
119
120	/**
121	* Configure the quote_helper object used to display extended information in quotes
122	*
123	* @param  quote_helper $quote_helper
124	*/
125	public function configure_quote_helper(quote_helper $quote_helper)
126	{
127		$this->quote_helper = $quote_helper;
128	}
129
130	/**
131	* Automatically set the smilies path based on config
132	*
133	* @param  \phpbb\config\config $config
134	* @param  \phpbb\path_helper   $path_helper
135	* @return null
136	*/
137	public function configure_smilies_path(\phpbb\config\config $config, \phpbb\path_helper $path_helper)
138	{
139		/**
140		* @see smiley_text()
141		*/
142		$root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $path_helper->get_web_root_path();
143
144		$this->set_smilies_path($root_path . $config['smilies_path']);
145	}
146
147	/**
148	* Configure this renderer as per the user's settings
149	*
150	* Should set the locale as well as the viewcensor/viewflash/viewimg/viewsmilies options.
151	*
152	* @param  \phpbb\user          $user
153	* @param  \phpbb\config\config $config
154	* @param  \phpbb\auth\auth     $auth
155	* @return null
156	*/
157	public function configure_user(\phpbb\user $user, \phpbb\config\config $config, \phpbb\auth\auth $auth)
158	{
159		$censor = $user->optionget('viewcensors') || !$config['allow_nocensors'] || !$auth->acl_get('u_chgcensors');
160
161		$this->set_viewcensors($censor);
162		$this->set_viewflash($user->optionget('viewflash'));
163		$this->set_viewimg($user->optionget('viewimg'));
164		$this->set_viewsmilies($user->optionget('viewsmilies'));
165
166		// Set the stylesheet parameters
167		foreach (array_keys($this->renderer->getParameters()) as $param_name)
168		{
169			if (strpos($param_name, 'L_') === 0)
170			{
171				// L_FOO is set to $user->lang('FOO')
172				$this->renderer->setParameter($param_name, $user->lang(substr($param_name, 2)));
173			}
174		}
175
176		// Set this user's style id and other parameters
177		$this->renderer->setParameters(array(
178			'S_IS_BOT'          => $user->data['is_bot'],
179			'S_REGISTERED_USER' => $user->data['is_registered'],
180			'S_USER_LOGGED_IN'  => ($user->data['user_id'] != ANONYMOUS),
181			'STYLE_ID'          => $user->style['style_id'],
182		));
183	}
184
185	/**
186	* Return the instance of s9e\TextFormatter\Renderer used by this object
187	*
188	* @return \s9e\TextFormatter\Renderer
189	*/
190	public function get_renderer()
191	{
192		return $this->renderer;
193	}
194
195	/**
196	* {@inheritdoc}
197	*/
198	public function get_viewcensors()
199	{
200		return $this->viewcensors;
201	}
202
203	/**
204	* {@inheritdoc}
205	*/
206	public function get_viewflash()
207	{
208		return $this->viewflash;
209	}
210
211	/**
212	* {@inheritdoc}
213	*/
214	public function get_viewimg()
215	{
216		return $this->viewimg;
217	}
218
219	/**
220	* {@inheritdoc}
221	*/
222	public function get_viewsmilies()
223	{
224		return $this->viewsmilies;
225	}
226
227	/**
228	* {@inheritdoc}
229	*/
230	public function render($xml)
231	{
232		if (isset($this->quote_helper))
233		{
234			$xml = $this->quote_helper->inject_metadata($xml);
235		}
236
237		$renderer = $this;
238
239		/**
240		* Modify a parsed text before it is rendered
241		*
242		* @event core.text_formatter_s9e_render_before
243		* @var \phpbb\textformatter\s9e\renderer renderer This renderer service
244		* @var string xml The parsed text, in its XML form
245		* @since 3.2.0-a1
246		*/
247		$vars = array('renderer', 'xml');
248		extract($this->dispatcher->trigger_event('core.text_formatter_s9e_render_before', compact($vars)));
249
250		$html = $this->renderer->render($xml);
251		if (isset($this->censor) && $this->viewcensors)
252		{
253			$html = $this->censor->censorHtml($html, true);
254		}
255
256		/**
257		* Modify a rendered text
258		*
259		* @event core.text_formatter_s9e_render_after
260		* @var string html The rendered text's HTML
261		* @var \phpbb\textformatter\s9e\renderer renderer This renderer service
262		* @since 3.2.0-a1
263		*/
264		$vars = array('html', 'renderer');
265		extract($this->dispatcher->trigger_event('core.text_formatter_s9e_render_after', compact($vars)));
266
267		return $html;
268	}
269
270	/**
271	* {@inheritdoc}
272	*/
273	public function set_smilies_path($path)
274	{
275		$this->renderer->setParameter('T_SMILIES_PATH', $path);
276	}
277
278	/**
279	* {@inheritdoc}
280	*/
281	public function set_viewcensors($value)
282	{
283		$this->viewcensors = $value;
284		$this->renderer->setParameter('S_VIEWCENSORS', $value);
285	}
286
287	/**
288	* {@inheritdoc}
289	*/
290	public function set_viewflash($value)
291	{
292		$this->viewflash = $value;
293		$this->renderer->setParameter('S_VIEWFLASH', $value);
294	}
295
296	/**
297	* {@inheritdoc}
298	*/
299	public function set_viewimg($value)
300	{
301		$this->viewimg = $value;
302		$this->renderer->setParameter('S_VIEWIMG', $value);
303	}
304
305	/**
306	* {@inheritdoc}
307	*/
308	public function set_viewsmilies($value)
309	{
310		$this->viewsmilies = $value;
311		$this->renderer->setParameter('S_VIEWSMILIES', $value);
312	}
313}
314