Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
65 / 65
100.00% covered (success)
100.00%
15 / 15
CRAP
100.00% covered (success)
100.00%
1 / 1
renderer
100.00% covered (success)
100.00%
65 / 65
100.00% covered (success)
100.00%
15 / 15
31
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
8
 configure_mention_helper
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 configure_quote_helper
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 configure_smilies_path
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 configure_user
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 get_renderer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_viewcensors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_viewimg
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_viewsmilies
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 set_smilies_path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_viewcensors
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 set_viewimg
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 set_viewsmilies
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 set_usemention
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
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 mention_helper
33    */
34    protected $mention_helper;
35
36    /**
37    * @var quote_helper
38    */
39    protected $quote_helper;
40
41    /**
42    * @var \s9e\TextFormatter\Renderer
43    */
44    protected $renderer;
45
46    /**
47    * @var bool Status of the viewcensors option
48    */
49    protected $viewcensors = 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    * @var bool Whether the user is allowed to use mentions
63    */
64    protected $usemention = false;
65
66    /**
67    * Constructor
68    *
69    * @param \phpbb\cache\driver\driver_interface $cache
70    * @param string $cache_dir Path to the cache dir
71    * @param string $key Cache key
72    * @param factory $factory
73    * @param \phpbb\event\dispatcher_interface $dispatcher
74    */
75    public function __construct(\phpbb\cache\driver\driver_interface $cache, $cache_dir, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher)
76    {
77        $renderer_data = $cache->get($key);
78        if ($renderer_data)
79        {
80            $class = $renderer_data['class'];
81            if (!class_exists($class, false))
82            {
83                // Try to load the renderer class from its cache file
84                $cache_file = $cache_dir . $class . '.php';
85
86                if (file_exists($cache_file))
87                {
88                    include($cache_file);
89                }
90            }
91            if (class_exists($class, false))
92            {
93                $renderer = new $class;
94            }
95            if (isset($renderer_data['censor']))
96            {
97                $censor = $renderer_data['censor'];
98            }
99        }
100        if (!isset($renderer))
101        {
102            $objects  = $factory->regenerate();
103            $renderer = $objects['renderer'];
104        }
105
106        if (isset($censor))
107        {
108            $this->censor = $censor;
109        }
110        $this->dispatcher = $dispatcher;
111        $this->renderer = $renderer;
112        $renderer = $this;
113
114        /**
115        * Configure the renderer service
116        *
117        * @event core.text_formatter_s9e_renderer_setup
118        * @var \phpbb\textformatter\s9e\renderer renderer This renderer service
119        * @since 3.2.0-a1
120        * @psalm-ignore-var
121        */
122        $vars = ['renderer'];
123        extract($dispatcher->trigger_event('core.text_formatter_s9e_renderer_setup', compact($vars)));
124    }
125
126    /**
127    * Configure the mention_helper object used to display extended information in mentions
128    *
129    * @param  mention_helper $mention_helper
130    */
131    public function configure_mention_helper(mention_helper $mention_helper)
132    {
133        $this->mention_helper = $mention_helper;
134    }
135
136    /**
137    * Configure the quote_helper object used to display extended information in quotes
138    *
139    * @param  quote_helper $quote_helper
140    */
141    public function configure_quote_helper(quote_helper $quote_helper)
142    {
143        $this->quote_helper = $quote_helper;
144    }
145
146    /**
147    * Automatically set the smilies path based on config
148    *
149    * @param  \phpbb\config\config $config
150    * @param  \phpbb\path_helper   $path_helper
151    * @return void
152    */
153    public function configure_smilies_path(\phpbb\config\config $config, \phpbb\path_helper $path_helper)
154    {
155        /**
156        * @see smiley_text()
157        */
158        $root_path = $path_helper->get_web_root_path();
159
160        $this->set_smilies_path($root_path . $config['smilies_path']);
161    }
162
163    /**
164    * Configure this renderer as per the user's settings
165    *
166    * Should set the locale as well as the viewcensor/viewimg/viewsmilies options.
167    *
168    * @param  \phpbb\user          $user
169    * @param  \phpbb\config\config $config
170    * @param  \phpbb\auth\auth     $auth
171    * @return void
172    */
173    public function configure_user(\phpbb\user $user, \phpbb\config\config $config, \phpbb\auth\auth $auth)
174    {
175        $censor = $user->optionget('viewcensors') || !$config['allow_nocensors'] || !$auth->acl_get('u_chgcensors');
176
177        $this->set_viewcensors($censor);
178        $this->set_viewimg($user->optionget('viewimg'));
179        $this->set_viewsmilies($user->optionget('viewsmilies'));
180        $this->set_usemention($config['allow_mentions'] && $auth->acl_get('u_mention'));
181
182        // Set the stylesheet parameters
183        foreach (array_keys($this->renderer->getParameters()) as $param_name)
184        {
185            if (strpos($param_name, 'L_') === 0)
186            {
187                // L_FOO is set to $user->lang('FOO')
188                $this->renderer->setParameter($param_name, $user->lang(substr($param_name, 2)));
189            }
190        }
191
192        // Set this user's style id and other parameters
193        $this->renderer->setParameters(array(
194            'S_IS_BOT'            => $user->data['is_bot'] ?? false,
195            'S_REGISTERED_USER'    => $user->data['is_registered'] ?? false,
196            'S_USER_LOGGED_IN'    => ($user->data['user_id'] != ANONYMOUS),
197            'STYLE_ID'            => $user->style['style_id'],
198        ));
199    }
200
201    /**
202    * Return the instance of s9e\TextFormatter\Renderer used by this object
203    *
204    * @return \s9e\TextFormatter\Renderer
205    */
206    public function get_renderer()
207    {
208        return $this->renderer;
209    }
210
211    /**
212    * {@inheritdoc}
213    */
214    public function get_viewcensors()
215    {
216        return $this->viewcensors;
217    }
218
219    /**
220    * {@inheritdoc}
221    */
222    public function get_viewimg()
223    {
224        return $this->viewimg;
225    }
226
227    /**
228    * {@inheritdoc}
229    */
230    public function get_viewsmilies()
231    {
232        return $this->viewsmilies;
233    }
234
235    /**
236    * {@inheritdoc}
237    */
238    public function render($text)
239    {
240        if (isset($this->mention_helper))
241        {
242            $text = $this->mention_helper->inject_metadata($text);
243        }
244
245        if (isset($this->quote_helper))
246        {
247            $text = $this->quote_helper->inject_metadata($text);
248        }
249
250        $renderer = $this;
251
252        /**
253        * Modify a parsed text before it is rendered
254        *
255        * @event core.text_formatter_s9e_render_before
256        * @var \phpbb\textformatter\s9e\renderer renderer This renderer service
257        * @var string text The parsed text, in its XML form
258        * @since 3.2.0-a1
259        * @psalm-ignore-var
260        */
261        $vars = ['renderer', 'text'];
262        extract($this->dispatcher->trigger_event('core.text_formatter_s9e_render_before', compact($vars)));
263
264        $html = $this->renderer->render($text);
265        if (isset($this->censor) && $this->viewcensors)
266        {
267            $html = $this->censor->censorHtml($html, true);
268        }
269
270        /**
271        * Modify a rendered text
272        *
273        * @event core.text_formatter_s9e_render_after
274        * @var string html The rendered text's HTML
275        * @var \phpbb\textformatter\s9e\renderer renderer This renderer service
276        * @since 3.2.0-a1
277        * @psalm-ignore-var
278        */
279        $vars = ['html', 'renderer'];
280        extract($this->dispatcher->trigger_event('core.text_formatter_s9e_render_after', compact($vars)));
281
282        return $html;
283    }
284
285    /**
286    * {@inheritdoc}
287    */
288    public function set_smilies_path($path)
289    {
290        $this->renderer->setParameter('T_SMILIES_PATH', $path);
291    }
292
293    /**
294    * {@inheritdoc}
295    */
296    public function set_viewcensors($value)
297    {
298        $this->viewcensors = $value;
299        $this->renderer->setParameter('S_VIEWCENSORS', $value);
300    }
301
302    /**
303    * {@inheritdoc}
304    */
305    public function set_viewimg($value)
306    {
307        $this->viewimg = $value;
308        $this->renderer->setParameter('S_VIEWIMG', $value);
309    }
310
311    /**
312    * {@inheritdoc}
313    */
314    public function set_viewsmilies($value)
315    {
316        $this->viewsmilies = $value;
317        $this->renderer->setParameter('S_VIEWSMILIES', $value);
318    }
319
320    /**
321    * {@inheritdoc}
322    */
323    public function set_usemention($value)
324    {
325        $this->usemention = $value;
326        $this->renderer->setParameter('S_VIEWMENTION', $value);
327    }
328}