aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/formatted_text.php
blob: 418c88699810c460bbb3fa5c07ee1197c0c8b839 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
 * Formatted text class to handle any text that can contain BBCodes, smilies, magic URLs or under word censor.
 */
class formatted_text
{
	/**
	 * Unformated text.
	 *
	 * @var string
	 */
	private $text;

	/**
	 * Internal representation (first_pass data).
	 *
	 * @var string
	 */
	private $meta;

	/**
	 * Formatting options as bit flag.
	 *
	 * @see bbcode_parser
	 *
	 * @var int
	 */
	private $flags;

	/**
	 * Compiled $text. For dispaly.
	 *
	 * @var string
	 */
	private $compiled;

	/**
	 * Set to true if text, meta or flags have been changed, false otherwise.
	 *
	 * @var bool
	 */
	private $changed;

	/**
	 * DB table to update
	 *
	 * @var string
	 */
	private $update_table = '';

	/**
	 * Column in $update_table to update.
	 *
	 * @var string
	 */
	private $update_column = '';

	/**
	 * Where clause for auto update.
	 *
	 * @var string
	 */
	private $update_where = '';

	/**
	 * Creates a new instance.
	 *
	 * @param string $text
	 * @param string $meta
	 * @param int $flags
	 */
	public function __construct($text, $meta = '', $flags = 0)
	{
		$this->text		= $text;
		$this->meta		= $meta;
		$this->flags	= $flags;
		$this->compiled	= '';

		if ($meta == '')
		{
			$this->changed = true;
		}
		else
		{
			$this->changed = false;
		}
	}

	public function __destruct()
	{
		if ($this->changed && $this->update_table)
		{
			$this->to_db($this->update_table, $this->update_column, $this->update_where);
		}
	}

	/**
	 * Convieniently initialize a formatted_text object from
	 * a database result set. The array must contain the following indexes:
	 * $column, {$column}_meta and {$column}_flags
	 *
	 *
	 * @param array $data
	 * @param string $column
	 * @return formatted_text
	 */
	public static function from_db_data(array $data, $column)
	{
		return new formatted_text($data[$column], $data[$column . '_meta'], (int) $data[$column . '_flags']);
	}

	/**
	 * Returns the $text formatted, ready to be displayed on a webpage.
	 *
	 * @return string
	 */
	public function to_display()
	{
		$this->set_compiled();
		return $this->compiled;
	}

	/**
	 * Updates $table, sets $column, {$column}_meta and {$column}_flags.
	 * All 3 columns must exist.
	 *
	 * @param string $table
	 * @param string $column
	 * @param string $where
	 * @return bool
	 */
	public function to_db($table, $column, $where = '1')
	{
		$this->changed = false;

		$sql = 'UPDATE ' . $table . ' SET ' . $db->sql_build_query('UPDATE', $this->to_db_data($column))
					. ' WHERE ' . $where;
		return (bool) $db->sql_query($sql);
	}

	/**
	 * Returns an array containing $column, {$column}_meta and {$column}_flags
	 * indexes to be used with query generating functions.
	 *
	 * @param string $column
	 * @return array
	 */
	public function to_db_data($column)
	{
		$this->set_meta();
		return array($column => $this->text, $column . '_meta' => $this->meta, $column . '_flags' => $this->flags);
	}

	/**
	 * Enable automatic database update on
	 *
	 * @param string $table
	 * @param string $column
	 * @param string $where
	 */
	public function set_auto_update($table, $column, $where = '1')
	{
		$this->update_table = $table;
		$this->update_column = $column;
		$this->update_where = $where;
	}

	/**
	 * Sets $meta if not set.
	 */
	private function set_meta()
	{
		if (strlen($this->meta))
		{
			return;
		}

		$parser = new phpbb_bbcode_parser;
		$this->meta = $parser->first_pass($this->text);
	}

	/**
	 * Sets $compiled if not set.
	 */
	private function set_compiled()
	{
		$this->set_meta();

		if (strlen($this->compiled))
		{
			return;
		}

		$parser = new phpbb_bbcode_parser;
		$parser->set_flags($this->flags);
		$this->compiled = $parser->second_pass($this->meta);
	}

	/**
	 * Sets $text.
	 *
	 * @param string $text
	 */
	public function set_text($text)
	{
		if ($this->text != $text)
		{
			$this->text = (string) $text;
			$this->meta = '';
			$this->compiled = '';
		}
	}

	/**
	 * Sets $flags.
	 *
	 * @param int $flags
	 */
	public function set_flags($flags)
	{
		$flags = (int) $flags;
		if ($this->flags != $flags)
		{
			$this->flags = $flags;
			$this->compiled = '';
		}
	}

	/**
	 * Returns the current text.
	 *
	 * @return string
	 */
	public function get_text()
	{
		return $this->text;
	}

	/**
	 * Returns the current(!!) metadata.
	 *
	 * @return string
	 */
	public function get_meta()
	{
		return $this->meta;
	}

	/**
	 * Returns the current flags.
	 *
	 * @return int
	 */
	public function get_flags()
	{
		return $this->flags;
	}

	/**
	 * Returns true if $this is equal to $other.
	 * Objects are only equal if $text and $flags are equal.
	 *
	 * @param formatted_text $other
	 * @return bool
	 */
	public function eq(formatted_text $other)
	{
		return $this->flags == $other->get_flags() && $this->text == $other->get_text();
	}

	/**
	 * Cast to string. Object is represented by the formatted version of $text with respect to $flags.
	 *
	 * @return string
	 */
	public function __toString()
	{
		return $this->to_display();
	}
}