PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/textformatter/s9e/renderer.php

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