aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/textreparser/base.php
blob: 3e5ee248a13ecbdcb99707034dafbef117435874 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?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
{
	/**
	* @var bool Whether to save changes to the database
	*/
	protected $save_changes = true;

	/**
	* {@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']))
		{
			if (isset($record['options']))
			{
				$record += array(
					'enable_bbcode'    => (bool) ($record['options'] & OPTION_FLAG_BBCODE),
					'enable_smilies'   => (bool) ($record['options'] & OPTION_FLAG_SMILIES),
					'enable_magic_url' => (bool) ($record['options'] & OPTION_FLAG_LINKS),
				);
			}
			else
			{
				$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;
	}

	/**
	* Disable saving changes to the database
	*/
	public function disable_save()
	{
		$this->save_changes = false;
	}

	/**
	* Enable saving changes to the database
	*/
	public function enable_save()
	{
		$this->save_changes = true;
	}

	/**
	* 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);
		$flags = ($record['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0;
		$flags |= ($record['enable_smilies']) ? OPTION_FLAG_SMILIES : 0;
		$flags |= ($record['enable_magic_url']) ? OPTION_FLAG_LINKS : 0;
		$unparsed = array_merge(
			$record,
			generate_text_for_edit($record['text'], $record['bbcode_uid'], $flags)
		);

		// 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
		$text = html_entity_decode($unparsed['text'], ENT_QUOTES, 'UTF-8');
		$bitfield = $flags = null;
		generate_text_for_storage(
			$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 and it's not a dry run
		if ($text !== $record['text'] && $this->save_changes)
		{
			$record['text'] = $text;
			$this->save_record($record);
		}
	}
}
喏6,dZ2wj;OY+ާ}8|q V~ŢZHJg ٽuEUwegt`T0 i]Mi+I<Ӎ@ڄ!֧ ccu+%bC<6)iZu\kVL锗R6kyX枠hu TIF? v.2@2+j~;mA 'n*V|^?i.k/&:$s[UΈ/X%Pv6Xs51js 3^OEifpI~dLGOjTwش·L{AP͑C7u%]IZ_Vdhv(_Y(,.2=EO22lmkѱee|.0=0I ' :zics%/8%ߧىZ OgHيˁb%Wе/{w|) ? u,Y`T*5]W;#i@MTˮN-wq=\@/X?x՟>(Ԙ@P # ϋXoGT 43pI^trmadi$C& OV7p]DioO氊n1&/$uh%_O /Pf{$I˺U-7`xW=رdmB~/D6.Hc=@Od0s01hhr8ۭTW%P&LJ9:SHBn`!pT kj x5zzR[,Bsb.[[=aT9KYo PElW#'~!JoqؽF.L<3)qmϲ2{c4#c0%U Ь&q֗r)}mH,)?w[-5VOe A-M A]m+I،02_\ Tj4q XBLF/Pg͸R5 q.JYF~Aܧ|ĨW^vYTMij0 1Ɋ?JX=I"kE~_W0:[[ū^E"w\|0/ʀKI7m-x3:63?QՉC{I5K2 JUi 1=( "X  p{L7'CB&#ƱǮy2RKو0InFZ],.}t9>[(WJ dϔw9,@z ?Zng8:Y3#:kQ1gA +d/w9[rUTEֻvBd_-WLiB(_Q7۔\*61f$pi,w6-Kr3P  B uϤ!~uƸs :8o\#Viv=nwYn>iᑻ׈XVт/=cI -Iӝ+bq_X~]Za~R19^dEPyKo$Ѽ'G2Pp%a0uunp_نY MV񹞔?CfS3қlNLpT n.auVCc$ѝr 2SEsPC$vԾCC4oXdMh5s kYwĂ}xB.PMu)%Y}Bue֘pjCML{Hz[HX9S9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