aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/textreparser/base.php
blob: 2b8cacb092359bea13810b18807a310fe5745d4e (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?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\textreparser;

abstract class base implements reparser_interface
{
	/**
	* {@inheritdoc}
	*/
	abstract public function get_max_id();

	/**
	* Return all records in given range
	*
	* @param  integer $min_id Lower bound
	* @param  integer $max_id Upper bound
	* @return array           Array of records
	*/
	abstract protected function get_records_by_range($min_id, $max_id);

	/**
	* {@inheritdoc}
	*/
	abstract protected function save_record(array $record);

	/**
	* Add fields to given record, if applicable
	*
	* The enable_* fields are not always saved to the database. Sometimes we need to guess their
	* original value based on the text content or possibly other fields
	*
	* @param  array $record Original record
	* @return array         Complete record
	*/
	protected function add_missing_fields(array $record)
	{
		if (!isset($record['enable_bbcode'], $record['enable_smilies'], $record['enable_magic_url']))
		{
			$record += array(
				'enable_bbcode'    => $this->guess_bbcodes($record),
				'enable_smilies'   => $this->guess_smilies($record),
				'enable_magic_url' => $this->guess_magic_url($record),
			);
		}

		// Those BBCodes are disabled based on context and user permissions and that value is never
		// stored in the database. Here we test whether they were used in the original text.
		$bbcodes = array('flash', 'img', 'quote', 'url');
		foreach ($bbcodes as $bbcode)
		{
			$field_name = 'enable_' . $bbcode . '_bbcode';
			$record[$field_name] = $this->guess_bbcode($record, $bbcode);
		}

		// Magic URLs are tied to the URL BBCode, that's why if magic URLs are enabled we make sure
		// that the URL BBCode is also enabled
		if ($record['enable_magic_url'])
		{
			$record['enable_url_bbcode'] = true;
		}

		return $record;
	}

	/**
	* Guess whether given BBCode is in use in given record
	*
	* @param  array  $record
	* @param  string $bbcode
	* @return bool
	*/
	protected function guess_bbcode(array $record, $bbcode)
	{
		if (!empty($record['bbcode_uid']))
		{
			// Look for the closing tag, e.g. [/url]
			$match = '[/' . $bbcode . ':' . $record['bbcode_uid'];
			if (strpos($record['text'], $match) !== false)
			{
				return true;
			}
		}

		if (substr($record['text'], 0, 2) == '<r')
		{
			// Look for the closing tag inside of a e element, in an element of the same name, e.g.
			// <e>[/url]</e></URL>
			$match = '<e>[/' . $bbcode . ']</e></' . strtoupper($bbcode) . '>';
			if (strpos($record['text'], $match) !== false)
			{
				return true;
			}
		}

		return false;
	}

	/**
	* Guess whether any BBCode is in use in given record
	*
	* @param  array $record
	* @return bool
	*/
	protected function guess_bbcodes(array $record)
	{
		if (!empty($record['bbcode_uid']))
		{
			// Test whether the bbcode_uid is in use
			$match = ':' . $record['bbcode_uid'];
			if (strpos($record['text'], $match) !== false)
			{
				return true;
			}
		}

		if (substr($record['text'], 0, 2) == '<r')
		{
			// Look for a closing tag inside of an e element
			return (bool) preg_match('(<e>\\[/\\w+\\]</e>)', $match);
		}

		return false;
	}

	/**
	* Guess whether magic URLs are in use in given record
	*
	* @param  array $record
	* @return bool
	*/
	protected function guess_magic_url(array $record)
	{
		// Look for <!-- m --> or for a URL tag that's not immediately followed by <s>
		return (strpos($record['text'], '<!-- m -->') !== false || preg_match('(<URL [^>]++>(?!<s>))', $record['text']));
	}

	/**
	* Guess whether smilies are in use in given record
	*
	* @param  array $record
	* @return bool
	*/
	protected function guess_smilies(array $record)
	{
		return (strpos($record['text'], '<!-- s') !== false || strpos($record['text'], '<E>') !== false);
	}

	/**
	* {@inheritdoc}
	*/
	public function reparse_range($min_id, $max_id)
	{
		foreach ($this->get_records_by_range($min_id, $max_id) as $record)
		{
			$this->reparse_record($record);
		}
	}

	/**
	* Reparse given record
	*
	* @param  array $record Associative array containing the record's data
	*/
	protected function reparse_record(array $record)
	{
		$record = $this->add_missing_fields($record);
		$unparsed = array_merge(
			$record,
			generate_text_for_edit(
				$record['text'],
				$record['bbcode_uid'],
				OPTION_FLAG_BBCODE | OPTION_FLAG_SMILIES | OPTION_FLAG_LINKS
			)
		);
		// generate_text_for_edit() and decode_message() actually return the text as HTML. It has to
		// be decoded to plain text before it can be reparsed
		$parsed_text = html_entity_decode($unparsed['text'], ENT_QUOTES, 'UTF-8');
		$bitfield = $flags = null;
		generate_text_for_storage(
			$parsed_text,
			$unparsed['bbcode_uid'],
			$bitfield,
			$flags,
			$unparsed['enable_bbcode'],
			$unparsed['enable_magic_url'],
			$unparsed['enable_smilies'],
			$unparsed['enable_img_bbcode'],
			$unparsed['enable_flash_bbcode'],
			$unparsed['enable_quote_bbcode'],
			$unparsed['enable_url_bbcode']
		);

		// Save the new text if it has changed
		if ($parsed_text !== $record['text'])
		{
			$record['text'] = $parsed_text;
			$this->save_record($record);
		}
	}
}