PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/src/community/phpbb/textformatter/s9e/parser.php

https://bitbucket.org/ke2083/transfans.co.uk-website
PHP | 399 lines | 204 code | 45 blank | 150 comment | 32 complexity | f203bed916e135b92f7b452efbccd4a8 MD5 | raw file
  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. use s9e\TextFormatter\Parser\AttributeFilters\UrlFilter;
  15. use s9e\TextFormatter\Parser\Logger;
  16. /**
  17. * s9e\TextFormatter\Parser adapter
  18. */
  19. class parser implements \phpbb\textformatter\parser_interface
  20. {
  21. /**
  22. * @var \phpbb\event\dispatcher_interface
  23. */
  24. protected $dispatcher;
  25. /**
  26. * @var \s9e\TextFormatter\Parser
  27. */
  28. protected $parser;
  29. /**
  30. * Constructor
  31. *
  32. * @param \phpbb\cache\driver_interface $cache
  33. * @param string $key Cache key
  34. * @param factory $factory
  35. * @param \phpbb\event\dispatcher_interface $dispatcher
  36. */
  37. public function __construct(\phpbb\cache\driver\driver_interface $cache, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher)
  38. {
  39. $parser = $cache->get($key);
  40. if (!$parser)
  41. {
  42. $objects = $factory->regenerate();
  43. $parser = $objects['parser'];
  44. }
  45. $this->dispatcher = $dispatcher;
  46. $this->parser = $parser;
  47. $parser = $this;
  48. /**
  49. * Configure the parser service
  50. *
  51. * Can be used to:
  52. * - toggle features or BBCodes
  53. * - register variables or custom parsers in the s9e\TextFormatter parser
  54. * - configure the s9e\TextFormatter parser's runtime settings
  55. *
  56. * @event core.text_formatter_s9e_parser_setup
  57. * @var \phpbb\textformatter\s9e\parser parser This parser service
  58. * @since 3.2.0-a1
  59. */
  60. $vars = array('parser');
  61. extract($dispatcher->trigger_event('core.text_formatter_s9e_parser_setup', compact($vars)));
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function parse($text)
  67. {
  68. $parser = $this;
  69. /**
  70. * Modify a text before it is parsed
  71. *
  72. * @event core.text_formatter_s9e_parse_before
  73. * @var \phpbb\textformatter\s9e\parser parser This parser service
  74. * @var string text The original text
  75. * @since 3.2.0-a1
  76. */
  77. $vars = array('parser', 'text');
  78. extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_before', compact($vars)));
  79. $xml = $this->parser->parse($text);
  80. /**
  81. * Modify a parsed text in its XML form
  82. *
  83. * @event core.text_formatter_s9e_parse_after
  84. * @var \phpbb\textformatter\s9e\parser parser This parser service
  85. * @var string xml The parsed text, in XML
  86. * @since 3.2.0-a1
  87. */
  88. $vars = array('parser', 'xml');
  89. extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_after', compact($vars)));
  90. return $xml;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function disable_bbcode($name)
  96. {
  97. $this->parser->disableTag(strtoupper($name));
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function disable_bbcodes()
  103. {
  104. $this->parser->disablePlugin('BBCodes');
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function disable_censor()
  110. {
  111. $this->parser->disablePlugin('Censor');
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function disable_magic_url()
  117. {
  118. $this->parser->disablePlugin('Autoemail');
  119. $this->parser->disablePlugin('Autolink');
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function disable_smilies()
  125. {
  126. $this->parser->disablePlugin('Emoticons');
  127. $this->parser->disablePlugin('Emoji');
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function enable_bbcode($name)
  133. {
  134. $this->parser->enableTag(strtoupper($name));
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function enable_bbcodes()
  140. {
  141. $this->parser->enablePlugin('BBCodes');
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function enable_censor()
  147. {
  148. $this->parser->enablePlugin('Censor');
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function enable_magic_url()
  154. {
  155. $this->parser->enablePlugin('Autoemail');
  156. $this->parser->enablePlugin('Autolink');
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function enable_smilies()
  162. {
  163. $this->parser->enablePlugin('Emoticons');
  164. $this->parser->enablePlugin('Emoji');
  165. }
  166. /**
  167. * {@inheritdoc}
  168. *
  169. * This will convert the log entries found in s9e\TextFormatter's logger into phpBB error
  170. * messages
  171. */
  172. public function get_errors()
  173. {
  174. $errors = array();
  175. foreach ($this->parser->getLogger()->getLogs() as $entry)
  176. {
  177. list(, $msg, $context) = $entry;
  178. if ($msg === 'Tag limit exceeded')
  179. {
  180. if ($context['tagName'] === 'E')
  181. {
  182. $errors[] = array('TOO_MANY_SMILIES', $context['tagLimit']);
  183. }
  184. else if ($context['tagName'] === 'URL')
  185. {
  186. $errors[] = array('TOO_MANY_URLS', $context['tagLimit']);
  187. }
  188. }
  189. else if ($msg === 'MAX_FONT_SIZE_EXCEEDED')
  190. {
  191. $errors[] = array($msg, $context['max_size']);
  192. }
  193. else if (preg_match('/^MAX_(?:FLASH|IMG)_(HEIGHT|WIDTH)_EXCEEDED$/D', $msg, $m))
  194. {
  195. $errors[] = array($msg, $context['max_' . strtolower($m[1])]);
  196. }
  197. else if ($msg === 'Tag is disabled')
  198. {
  199. $name = strtolower($context['tag']->getName());
  200. $errors[] = array('UNAUTHORISED_BBCODE', '[' . $name . ']');
  201. }
  202. else if ($msg === 'UNABLE_GET_IMAGE_SIZE')
  203. {
  204. $errors[] = array($msg);
  205. }
  206. }
  207. // Deduplicate error messages. array_unique() only works on strings so we have to serialize
  208. if (!empty($errors))
  209. {
  210. $errors = array_map('unserialize', array_unique(array_map('serialize', $errors)));
  211. }
  212. return $errors;
  213. }
  214. /**
  215. * Return the instance of s9e\TextFormatter\Parser used by this object
  216. *
  217. * @return \s9e\TextFormatter\Parser
  218. */
  219. public function get_parser()
  220. {
  221. return $this->parser;
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function set_var($name, $value)
  227. {
  228. if ($name === 'max_smilies')
  229. {
  230. $this->parser->setTagLimit('E', $value ?: PHP_INT_MAX);
  231. }
  232. else if ($name === 'max_urls')
  233. {
  234. $this->parser->setTagLimit('URL', $value ?: PHP_INT_MAX);
  235. }
  236. else
  237. {
  238. $this->parser->registeredVars[$name] = $value;
  239. }
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. public function set_vars(array $vars)
  245. {
  246. foreach ($vars as $name => $value)
  247. {
  248. $this->set_var($name, $value);
  249. }
  250. }
  251. /**
  252. * Filter a flash object's height
  253. *
  254. * @see bbcode_firstpass::bbcode_flash()
  255. *
  256. * @param string $height
  257. * @param integer $max_height
  258. * @param Logger $logger
  259. * @return mixed Original value if valid, FALSE otherwise
  260. */
  261. static public function filter_flash_height($height, $max_height, Logger $logger)
  262. {
  263. if ($max_height && $height > $max_height)
  264. {
  265. $logger->err('MAX_FLASH_HEIGHT_EXCEEDED', array('max_height' => $max_height));
  266. return false;
  267. }
  268. return $height;
  269. }
  270. /**
  271. * Filter a flash object's width
  272. *
  273. * @see bbcode_firstpass::bbcode_flash()
  274. *
  275. * @param string $width
  276. * @param integer $max_width
  277. * @param Logger $logger
  278. * @return mixed Original value if valid, FALSE otherwise
  279. */
  280. static public function filter_flash_width($width, $max_width, Logger $logger)
  281. {
  282. if ($max_width && $width > $max_width)
  283. {
  284. $logger->err('MAX_FLASH_WIDTH_EXCEEDED', array('max_width' => $max_width));
  285. return false;
  286. }
  287. return $width;
  288. }
  289. /**
  290. * Filter the value used in a [size] BBCode
  291. *
  292. * @see bbcode_firstpass::bbcode_size()
  293. *
  294. * @param string $size Original size
  295. * @param integer $max_size Maximum allowed size
  296. * @param Logger $logger
  297. * @return mixed Original value if valid, FALSE otherwise
  298. */
  299. static public function filter_font_size($size, $max_size, Logger $logger)
  300. {
  301. if ($max_size && $size > $max_size)
  302. {
  303. $logger->err('MAX_FONT_SIZE_EXCEEDED', array('max_size' => $max_size));
  304. return false;
  305. }
  306. if ($size < 1)
  307. {
  308. return false;
  309. }
  310. return $size;
  311. }
  312. /**
  313. * Filter an image's URL to enforce restrictions on its dimensions
  314. *
  315. * @see bbcode_firstpass::bbcode_img()
  316. *
  317. * @param string $url Original URL
  318. * @param array $url_config Config used by the URL filter
  319. * @param Logger $logger
  320. * @param integer $max_height Maximum height allowed
  321. * @param integer $max_width Maximum width allowed
  322. * @return string|bool Original value if valid, FALSE otherwise
  323. */
  324. static public function filter_img_url($url, array $url_config, Logger $logger, $max_height, $max_width)
  325. {
  326. // Validate the URL
  327. $url = UrlFilter::filter($url, $url_config, $logger);
  328. if ($url === false)
  329. {
  330. return false;
  331. }
  332. if ($max_height || $max_width)
  333. {
  334. $imagesize = new \FastImageSize\FastImageSize();
  335. $size_info = $imagesize->getImageSize($url);
  336. if ($size_info === false)
  337. {
  338. $logger->err('UNABLE_GET_IMAGE_SIZE');
  339. return false;
  340. }
  341. if ($max_height && $max_height < $size_info['height'])
  342. {
  343. $logger->err('MAX_IMG_HEIGHT_EXCEEDED', array('max_height' => $max_height));
  344. return false;
  345. }
  346. if ($max_width && $max_width < $size_info['width'])
  347. {
  348. $logger->err('MAX_IMG_WIDTH_EXCEEDED', array('max_width' => $max_width));
  349. return false;
  350. }
  351. }
  352. return $url;
  353. }
  354. }