Completed
Push — master ( 79ce7c...7a05d7 )
by Matt
02:30
created

trim_tools::remove_bbcode_contents()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
rs 6.7272
cc 7
eloc 9
nc 5
nop 1
1
<?php
2
/**
3
*
4
* Topic Preview
5
*
6
* @copyright (c) 2014 Matt Friedman
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace vse\topicpreview\core;
12
13
class trim_tools
14
{
15
	/** @var \phpbb\config\config */
16
	protected $config;
17
18
	/** @var \phpbb\textformatter\s9e\utils|null */
19
	protected $text_formatter_utils;
20
21
	/** @var string|array BBcodes to be removed */
22
	protected $strip_bbcodes;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param \phpbb\config\config $config
28
	 * @param \phpbb\textformatter\s9e\utils|null $text_formatter_utils
29
	 * @access public
30
	 */
31
	public function __construct(\phpbb\config\config $config, \phpbb\textformatter\s9e\utils $text_formatter_utils = null)
32
	{
33
		$this->config = $config;
34
		$this->text_formatter_utils = $text_formatter_utils;
35
	}
36
37
	/**
38
	 * Trim and clean text
39
	 *
40
	 * @param string $message Message text
41
	 * @param int    $length  The length to trim text to
42
	 * @return string Trimmed message text
43
	 * @access protected
44
	 */
45
	public function trim_text($message, $length)
46
	{
47
		$message = $this->remove_markup($message);
48
49
		if (utf8_strlen($message) <= $length)
50
		{
51
			return $this->tp_nl2br($message);
52
		}
53
54
		// trim the text to the last whitespace character before the cut-off
55
		$message = preg_replace('/\s+?(\S+)?$/', '', utf8_substr($message, 0, $length));
56
57
		return $this->tp_nl2br($message) . '...';
58
	}
59
60
	/**
61
	 * Strip BBCodes, tags and links from text
62
	 *
63
	 * @param string $message Message text
64
	 * @return string Cleaned message text
65
	 * @access protected
66
	 */
67
	protected function remove_markup($message)
68
	{
69
		$message = smiley_text($message, true); // display smileys as text :)
70
71
		$message = $this->remove_bbcode_contents($message);
72
73
		static $patterns = array();
74
75
		if (empty($patterns))
76
		{
77
			// RegEx patterns based on Topic Text Hover Mod by RMcGirr83
78
			$patterns = array(
79
				'#<!-- [lmw] --><a class="postlink[^>]*>(.*<\/a[^>]*>)?<!-- [lmw] -->#Usi', // Magic URLs
80
				'#<[^>]*>(.*<[^>]*>)?#Usi', // HTML code
81
				'#\[/?[^\[\]]+\]#mi', // All BBCode tags
82
				'#(http|https|ftp|mailto)(:|\&\#58;)\/\/[^\s]+#i', // Remaining URLs
83
				'#"#', // Possible un-encoded quotes from older board conversions
84
				'#[ \t]{2,}#' // Multiple spaces #[\s]+#
85
			);
86
		}
87
88
		return trim(preg_replace($patterns, ' ', $message));
89
	}
90
91
	/**
92
	 * Remove specified BBCodes and their contents
93
	 *
94
	 * @param string $message Message text
95
	 * @return string Stripped message text
96
	 * @access protected
97
	 */
98
	protected function remove_bbcode_contents($message)
99
	{
100
		// If text formatter is not available, use legacy bbcode stripper
101
		if ($this->text_formatter_utils === null || !$this->s9e_format($message))
102
		{
103
			return $this->remove_bbcode_contents_legacy($message);
104
		}
105
106
		// Create the data array of bbcodes to strip
107
		if (!isset($this->strip_bbcodes) || !is_array($this->strip_bbcodes))
108
		{
109
			$this->strip_bbcodes = (!empty($this->config['topic_preview_strip_bbcodes'])) ? explode('|', $this->config['topic_preview_strip_bbcodes']) : array();
110
			array_unshift($this->strip_bbcodes, 'flash');
111
		}
112
113
		// Strip the bbcodes from the message
114
		foreach ($this->strip_bbcodes as $bbcode)
115
		{
116
			$message = $this->text_formatter_utils->remove_bbcode($message, $bbcode);
117
		}
118
119
		return $this->text_formatter_utils->unparse($message);
120
	}
121
122
	/**
123
	 * Remove specified BBCodes and their contents
124
	 * Uses recursion to handle nested BBCodes
125
	 * This method for b.c. with phpBB 3.1.x
126
	 *
127
	 * @param string $message Message text
128
	 * @return string Stripped message text
129
	 * @access protected
130
	 */
131
	protected function remove_bbcode_contents_legacy($message)
132
	{
133
		// Create the data string of bbcodes to strip
134
		if (!isset($this->strip_bbcodes) || is_array($this->strip_bbcodes))
135
		{
136
			$strip_bbcodes = (!empty($this->config['topic_preview_strip_bbcodes'])) ? 'flash|' . trim($this->config['topic_preview_strip_bbcodes']) : 'flash';
137
			$this->strip_bbcodes = '#\[(' . $strip_bbcodes . ')[^\[\]]*\]((?:(?!\[\1[^\[\]]*\]).)*)\[\/\1[^\[\]]*\]#Usi';
138
		}
139
140
		// Strip the bbcodes from the message
141
		if (preg_match($this->strip_bbcodes, $message))
142
		{
143
			return $this->remove_bbcode_contents_legacy(preg_replace($this->strip_bbcodes, '', $message));
144
		}
145
146
		return $message;
147
	}
148
149
	/**
150
	 * Convert and preserve line breaks
151
	 *
152
	 * @param string $message Message text
153
	 * @return string Message text with line breaks
154
	 * @access protected
155
	 */
156
	protected function tp_nl2br($message)
157
	{
158
		// http://stackoverflow.com/questions/816085/removing-redundant-line-breaks-with-regular-expressions
159
		return nl2br(preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $message));
160
	}
161
162
	/**
163
	 * Is the message s9e formatted
164
	 *
165
	 * @param string $message Message text
166
	 * @return bool True if message is s9e formatted, false otherwise
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
167
	 */
168
	protected function s9e_format($message)
169
	{
170
		return preg_match('/^<[rt][ >]/s', $message);
171
	}
172
}
173