aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/textformatter/s9e/quote_helper.php
blob: 3011ec88dc9443e2c3ba11629405fe31ea5bc8ed (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?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;

class quote_helper
{
	/**
	* @var string Base URL for a post link, uses {POST_ID} as placeholder
	*/
	protected $post_url;

	/**
	* @var string Base URL for a private message link, uses {MSG_ID} as placeholder
	*/
	protected $msg_url;

	/**
	* @var string Base URL for a profile link, uses {USER_ID} as placeholder
	*/
	protected $profile_url;

	/**
	* @var \phpbb\user
	*/
	protected $user;

	/**
	* Constructor
	*
	* @param \phpbb\user $user
	* @param string $root_path
	* @param string $php_ext
	*/
	public function __construct(\phpbb\user $user, $root_path, $php_ext)
	{
		$this->post_url = append_sid($root_path . 'viewtopic.' . $php_ext, 'p={POST_ID}#p{POST_ID}', false);
		$this->msg_url = append_sid($root_path . 'ucp.' . $php_ext, 'i=pm&mode=view&p={MSG_ID}', false);
		$this->profile_url = append_sid($root_path . 'memberlist.' . $php_ext, 'mode=viewprofile&u={USER_ID}', false);
		$this->user = $user;
	}

	/**
	* Inject dynamic metadata into QUOTE tags in given XML
	*
	* @param  string $xml Original XML
	* @return string      Modified XML
	*/
	public function inject_metadata($xml)
	{
		return \s9e\TextFormatter\Utils::replaceAttributes(
			$xml,
			'QUOTE',
			function ($attributes)
			{
				if (isset($attributes['post_id']))
				{
					$attributes['post_url'] = str_replace('{POST_ID}', $attributes['post_id'], $this->post_url);
				}
				if (isset($attributes['msg_id']))
				{
					$attributes['msg_url'] = str_replace('{MSG_ID}', $attributes['msg_id'], $this->msg_url);
				}
				if (isset($attributes['time']))
				{
					$attributes['date'] = $this->user->format_date($attributes['time']);
				}
				if (isset($attributes['user_id']))
				{
					$attributes['profile_url'] = str_replace('{USER_ID}', $attributes['user_id'], $this->profile_url);
				}

				return $attributes;
			}
		);
	}
}