aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/textformatter/s9e/acp_utils.php
blob: c4a668020e6783772e7b46f75357cef1f90b256f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\textformatter\s9e;

use phpbb\textformatter\acp_utils_interface;
use s9e\TextFormatter\Configurator\Exceptions\UnsafeTemplateException;

class acp_utils implements acp_utils_interface
{
	/**
	* @var factory $factory
	*/
	protected $factory;

	/**
	* @param factory $factory
	*/
	public function __construct(factory $factory)
	{
		$this->factory = $factory;
	}

	/**
	* {@inheritdoc}
	*/
	public function analyse_bbcode(string $definition, string $template): array
	{
		$configurator = $this->factory->get_configurator();
		$return       = ['status' => self::BBCODE_STATUS_SAFE];

		// Capture and normalize the BBCode name manually because there's no easy way to retrieve
		// it in TextFormatter <= 2.x
		if (preg_match('(\\[([-\\w]++))', $definition, $m))
		{
			$return['name'] = strtoupper($m[1]);
		}

		try
		{
			$configurator->BBCodes->addCustom($definition, $template);
		}
		catch (UnsafeTemplateException $e)
		{
			$return['status']     = self::BBCODE_STATUS_UNSAFE;
			$return['error_text'] = $e->getMessage();
			$return['error_html'] = $e->highlightNode('<span class="highlight">');
		}
		catch (\Exception $e)
		{
			$return['status']     = (preg_match('(xml|xpath|xsl)i', $e->getMessage())) ? self::BBCODE_STATUS_INVALID_TEMPLATE : self::BBCODE_STATUS_INVALID_DEFINITION;
			$return['error_text'] = $e->getMessage();
		}

		return $return;
	}
}