1
|
|
|
<?php namespace ivuorinen\BBCode; |
2
|
|
|
|
3
|
|
|
use \ivuorinen\BBCode\Traits\ArrayTrait; |
|
|
|
|
4
|
|
|
|
5
|
|
|
class BBCodeParser |
6
|
|
|
{ |
7
|
|
|
use ArrayTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var array[] These are our BBCode parsers |
11
|
|
|
*/ |
12
|
|
|
public $parsers = [ |
13
|
|
|
'bold' => [ |
14
|
|
|
'pattern' => '/\[b\](.*?)\[\/b\]/s', |
15
|
|
|
'replace' => '<strong>$1</strong>', |
16
|
|
|
'content' => '$1' |
17
|
|
|
], |
18
|
|
|
'italic' => [ |
19
|
|
|
'pattern' => '/\[i\](.*?)\[\/i\]/s', |
20
|
|
|
'replace' => '<em>$1</em>', |
21
|
|
|
'content' => '$1' |
22
|
|
|
], |
23
|
|
|
'underline' => [ |
24
|
|
|
'pattern' => '/\[u\](.*?)\[\/u\]/s', |
25
|
|
|
'replace' => '<u>$1</u>', |
26
|
|
|
'content' => '$1' |
27
|
|
|
], |
28
|
|
|
'linethrough' => [ |
29
|
|
|
'pattern' => '/\[s\](.*?)\[\/s\]/s', |
30
|
|
|
'replace' => '<del>$1</del>', |
31
|
|
|
'content' => '$1' |
32
|
|
|
], |
33
|
|
|
'size' => [ |
34
|
|
|
'pattern' => '/\[size\=([1-7])\](.*?)\[\/size\]/s', |
35
|
|
|
'replace' => '<font size="$1">$2</font>', |
36
|
|
|
'content' => '$2' |
37
|
|
|
], |
38
|
|
|
'color' => [ |
39
|
|
|
'pattern' => '/\[color\=(#[A-f0-9]{6}|#[A-f0-9]{3})\](.*?)\[\/color\]/s', |
40
|
|
|
'replace' => '<font color="$1">$2</font>', |
41
|
|
|
'content' => '$2' |
42
|
|
|
], |
43
|
|
|
'center' => [ |
44
|
|
|
'pattern' => '/\[center\](.*?)\[\/center\]/s', |
45
|
|
|
'replace' => '<div style="text-align:center;">$1</div>', |
46
|
|
|
'content' => '$1' |
47
|
|
|
], |
48
|
|
|
'left' => [ |
49
|
|
|
'pattern' => '/\[left\](.*?)\[\/left\]/s', |
50
|
|
|
'replace' => '<div style="text-align:left;">$1</div>', |
51
|
|
|
'content' => '$1' |
52
|
|
|
], |
53
|
|
|
'right' => [ |
54
|
|
|
'pattern' => '/\[right\](.*?)\[\/right\]/s', |
55
|
|
|
'replace' => '<div style="text-align:right;">$1</div>', |
56
|
|
|
'content' => '$1' |
57
|
|
|
], |
58
|
|
|
'quote' => [ |
59
|
|
|
'pattern' => '/\[quote\](.*?)\[\/quote\]/s', |
60
|
|
|
'replace' => '<blockquote>$1</blockquote>', |
61
|
|
|
'content' => '$1' |
62
|
|
|
], |
63
|
|
|
'namedquote' => [ |
64
|
|
|
'pattern' => '/\[quote\=(.*?)\](.*)\[\/quote\]/s', |
65
|
|
|
'replace' => '<blockquote><small>$1</small>$2</blockquote>', |
66
|
|
|
'content' => '$2' |
67
|
|
|
], |
68
|
|
|
'link' => [ |
69
|
|
|
'pattern' => '/\[url\](.*?)\[\/url\]/s', |
70
|
|
|
'replace' => '<a href="$1">$1</a>', |
71
|
|
|
'content' => '$1' |
72
|
|
|
], |
73
|
|
|
'namedlink' => [ |
74
|
|
|
'pattern' => '/\[url\=(.*?)\](.*?)\[\/url\]/s', |
75
|
|
|
'replace' => '<a href="$1">$2</a>', |
76
|
|
|
'content' => '$2' |
77
|
|
|
], |
78
|
|
|
'image' => [ |
79
|
|
|
'pattern' => '/\[img\](.*?)\[\/img\]/s', |
80
|
|
|
'replace' => '<img src="$1">', |
81
|
|
|
'content' => '$1' |
82
|
|
|
], |
83
|
|
|
'orderedlistnumerical' => [ |
84
|
|
|
'pattern' => '/\[list=1\](.*?)\[\/list\]/s', |
85
|
|
|
'replace' => '<ol>$1</ol>', |
86
|
|
|
'content' => '$1' |
87
|
|
|
], |
88
|
|
|
'orderedlistalpha' => [ |
89
|
|
|
'pattern' => '/\[list=a\](.*?)\[\/list\]/s', |
90
|
|
|
'replace' => '<ol type="a">$1</ol>', |
91
|
|
|
'content' => '$1' |
92
|
|
|
], |
93
|
|
|
'unorderedlist' => [ |
94
|
|
|
'pattern' => '/\[list\](.*?)\[\/list\]/s', |
95
|
|
|
'replace' => '<ul>$1</ul>', |
96
|
|
|
'content' => '$1' |
97
|
|
|
], |
98
|
|
|
'listitem' => [ |
99
|
|
|
'pattern' => '/\[\*\](.*)/', |
100
|
|
|
'replace' => '<li>$1</li>', |
101
|
|
|
'content' => '$1' |
102
|
|
|
], |
103
|
|
|
'code' => [ |
104
|
|
|
'pattern' => '/\[code\](.*?)\[\/code\]/s', |
105
|
|
|
'replace' => '<code>$1</code>', |
106
|
|
|
'content' => '$1' |
107
|
|
|
], |
108
|
|
|
'youtube' => [ |
109
|
|
|
'pattern' => '/\[youtube\](.*?)\[\/youtube\]/s', |
110
|
|
|
'replace' => '<iframe width="560" height="315" src="//www.youtube.com/embed/$1"' . |
111
|
|
|
' frameborder="0" allowfullscreen></iframe>', |
112
|
|
|
'content' => '$1' |
113
|
|
|
], |
114
|
|
|
'linebreak' => [ |
115
|
|
|
'pattern' => '/\r\n/', |
116
|
|
|
'replace' => '<br />', |
117
|
|
|
'content' => '' |
118
|
|
|
] |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @var array[] Our enabled parsers available with getParsers() |
123
|
|
|
*/ |
124
|
|
|
private $enabledParsers; |
125
|
|
|
|
126
|
12 |
|
public function __construct() |
127
|
|
|
{ |
128
|
12 |
|
$this->enabledParsers = $this->parsers; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Parses the BBCode string |
133
|
|
|
* @param string $source String containing the BBCode |
134
|
|
|
* @param bool $caseInsensitive |
135
|
|
|
* @return string Parsed string |
136
|
|
|
*/ |
137
|
3 |
|
public function parse($source, $caseInsensitive = false) |
138
|
|
|
{ |
139
|
3 |
|
foreach ($this->enabledParsers as $name => $parser) { |
140
|
3 |
|
$pattern = ($caseInsensitive) ? $parser['pattern'] . 'i' : $parser['pattern']; |
141
|
|
|
|
142
|
3 |
|
$source = $this->searchAndReplace($pattern, $parser['replace'], $source); |
143
|
|
|
} |
144
|
3 |
|
return $source; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Remove all BBCode |
149
|
|
|
* @param string $source |
150
|
|
|
* @return string Parsed text |
151
|
|
|
*/ |
152
|
1 |
|
public function stripBBCodeTags($source) |
153
|
|
|
{ |
154
|
1 |
|
foreach ($this->parsers as $name => $parser) { |
155
|
1 |
|
$source = $this->searchAndReplace($parser['pattern'] . 'i', $parser['content'], $source); |
156
|
|
|
} |
157
|
1 |
|
return $source; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Searches after a specified pattern and replaces it with provided structure |
162
|
|
|
* @param string $pattern Search pattern |
163
|
|
|
* @param string $replace Replacement structure |
164
|
|
|
* @param string $source Text to search in |
165
|
|
|
* @return string Parsed text |
166
|
|
|
*/ |
167
|
4 |
|
protected function searchAndReplace($pattern, $replace, $source) |
168
|
|
|
{ |
169
|
4 |
|
while (preg_match($pattern, $source)) { |
170
|
4 |
|
$source = preg_replace($pattern, $replace, $source); |
171
|
|
|
} |
172
|
|
|
|
173
|
4 |
|
return $source; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Helper function to parse case sensitive |
178
|
|
|
* @param string $source String containing the BBCode |
179
|
|
|
* @return string Parsed text |
180
|
|
|
*/ |
181
|
1 |
|
public function parseCaseSensitive($source) |
182
|
|
|
{ |
183
|
1 |
|
return $this->parse($source, false); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Helper function to parse case insensitive |
188
|
|
|
* @param string $source String containing the BBCode |
189
|
|
|
* @return string Parsed text |
190
|
|
|
*/ |
191
|
1 |
|
public function parseCaseInsensitive($source) |
192
|
|
|
{ |
193
|
1 |
|
return $this->parse($source, true); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Limits the parsers to only those you specify |
198
|
|
|
* @param mixed $only parsers |
199
|
|
|
* @return object BBCodeParser object |
200
|
|
|
*/ |
201
|
1 |
|
public function only($only = null) |
202
|
|
|
{ |
203
|
1 |
|
$only = (is_array($only)) ? $only : func_get_args(); |
204
|
1 |
|
$this->enabledParsers = $this->arrayOnly($this->parsers, $only); |
205
|
1 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Removes the parsers you want to exclude |
210
|
|
|
* @param mixed $except parsers |
211
|
|
|
* @return BBCodeParser |
212
|
|
|
*/ |
213
|
2 |
|
public function except($except = null) |
214
|
|
|
{ |
215
|
2 |
|
$except = (is_array($except)) ? $except : func_get_args(); |
216
|
2 |
|
$this->enabledParsers = $this->arrayExcept($this->parsers, $except); |
217
|
2 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* List of chosen parsers |
222
|
|
|
* @return array array of parsers |
223
|
|
|
*/ |
224
|
7 |
|
public function getParsers() |
225
|
|
|
{ |
226
|
7 |
|
return $this->enabledParsers; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Sets the parser pattern and replace. |
231
|
|
|
* This can be used for new parsers or overwriting existing ones. |
232
|
|
|
* @param string $name Parser name |
233
|
|
|
* @param string $pattern Pattern |
234
|
|
|
* @param string $replace Replace pattern |
235
|
|
|
* @param string $content Parsed text pattern |
236
|
|
|
* @return void |
237
|
|
|
*/ |
238
|
1 |
|
public function setParser($name, $pattern, $replace, $content) |
239
|
|
|
{ |
240
|
1 |
|
$this->parsers[$name] = array( |
241
|
1 |
|
'pattern' => $pattern, |
242
|
1 |
|
'replace' => $replace, |
243
|
1 |
|
'content' => $content |
244
|
1 |
|
); |
245
|
|
|
|
246
|
1 |
|
$this->enabledParsers[$name] = array( |
247
|
1 |
|
'pattern' => $pattern, |
248
|
1 |
|
'replace' => $replace, |
249
|
1 |
|
'content' => $content |
250
|
1 |
|
); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths