|
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
|
|
|
* Strip special 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) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->strip_bbcode_contents($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 $message; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Strip special BBCodes and their contents |
|
124
|
|
|
* Uses recursion to handle nested BBCodes |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $message Message text |
|
127
|
|
|
* @return string Stripped message text |
|
128
|
|
|
* @access protected |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function strip_bbcode_contents($message) |
|
131
|
|
|
{ |
|
132
|
|
|
// Create the data string of bbcodes to strip |
|
133
|
|
|
if (!isset($this->strip_bbcodes) || is_array($this->strip_bbcodes)) |
|
134
|
|
|
{ |
|
135
|
|
|
$strip_bbcodes = (!empty($this->config['topic_preview_strip_bbcodes'])) ? 'flash|' . trim($this->config['topic_preview_strip_bbcodes']) : 'flash'; |
|
136
|
|
|
$this->strip_bbcodes = '#\[(' . $strip_bbcodes . ')[^\[\]]*\]((?:(?!\[\1[^\[\]]*\]).)*)\[\/\1[^\[\]]*\]#Usi'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// Strip the bbcodes from the message |
|
140
|
|
|
if (preg_match($this->strip_bbcodes, $message)) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->strip_bbcode_contents(preg_replace($this->strip_bbcodes, '', $message)); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $message; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Convert and preserve line breaks |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $message Message text |
|
152
|
|
|
* @return string Message text with line breaks |
|
153
|
|
|
* @access protected |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function tp_nl2br($message) |
|
156
|
|
|
{ |
|
157
|
|
|
// http://stackoverflow.com/questions/816085/removing-redundant-line-breaks-with-regular-expressions |
|
158
|
|
|
return nl2br(preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $message)); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|