PageRenderTime 64ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

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

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