|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class BBCodeModuleAbstract |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource BBCodeModuleAbstract.php |
|
6
|
|
|
* @created 24.04.2018 |
|
7
|
|
|
* @package chillerlan\BBCode\Output |
|
8
|
|
|
* @author smiley <[email protected]> |
|
9
|
|
|
* @copyright 2018 smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\BBCode\Output; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Traits\ContainerInterface; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
18
|
|
|
|
|
19
|
|
|
abstract class BBCodeModuleAbstract implements BBCodeModuleInterface{ |
|
20
|
|
|
|
|
21
|
|
|
protected $tag; |
|
22
|
|
|
protected $attributes; |
|
23
|
|
|
protected $content; |
|
24
|
|
|
protected $match; |
|
25
|
|
|
protected $callback_count; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* An array of tags the module is able to process |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $tags = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Holds an array of singletags |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $singletags = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Holds an array of noparse tags |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $noparse = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \chillerlan\BBCode\BBCodeOptions |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $options; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var \Psr\SimpleCache\CacheInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $cache; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var \Psr\Log\LoggerInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $logger; |
|
62
|
|
|
|
|
63
|
|
|
public function __construct(ContainerInterface $options, CacheInterface $cache, LoggerInterface $logger){ |
|
64
|
|
|
$this->options = $options; |
|
|
|
|
|
|
65
|
|
|
$this->cache = $cache; |
|
66
|
|
|
$this->logger = $logger; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $name |
|
71
|
|
|
* @param $arguments |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __call($name, $arguments):string{ |
|
76
|
|
|
|
|
77
|
|
|
if(in_array($name, $this->tags, true)){ |
|
78
|
|
|
[$this->tag, $this->attributes, $this->content, $this->match, $this->callback_count] = $arguments; |
|
79
|
|
|
|
|
80
|
|
|
if(method_exists($this, $name)){ |
|
81
|
|
|
return call_user_func([$this, $name]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->transform(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return '';//$this->match; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* this is the catch-all method for __call() |
|
92
|
|
|
* |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function transform():string{ |
|
96
|
|
|
return $this->match; // do nothing |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritdoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getTags():array { |
|
103
|
|
|
return $this->tags; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @inheritdoc |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getSingleTags():array { |
|
110
|
|
|
return $this->singletags; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @inheritdoc |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getNoparse():array { |
|
117
|
|
|
return $this->noparse; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function randomID():string { |
|
124
|
|
|
return hash('crc32b', random_bytes(64)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Clears all EOL placeholders from self::$content with the base modules EOL token |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $eol [optional] custom EOL token |
|
|
|
|
|
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function clearEOL(string $eol = null){ |
|
135
|
|
|
$eol = $eol ?? PHP_EOL; |
|
136
|
|
|
$this->content = str_replace($this->options->placeholder_eol, $eol, $this->content); |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Clears all pseudo closing single tag bbcodes like [/br] |
|
143
|
|
|
* |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function clearPseudoClosingTags(){ |
|
147
|
|
|
$this->content = preg_replace('#\[/('.implode('|', array_merge(['br', 'hr'], $this->singletags)).')]#is', '', $this->content); |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
/** |
|
152
|
|
|
* Retrieves an attribute's value by it's name |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $name the desired attributes name |
|
155
|
|
|
* @param mixed $default [optional] a default value in case the attribute isn't set, defaults to false |
|
156
|
|
|
* |
|
157
|
|
|
* @return mixed the attribute's value in case it exists, otherwise $default |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function getAttribute(string $name, $default = false){ |
|
160
|
|
|
return isset($this->attributes[$name]) && !empty($this->attributes[$name]) ? $this->attributes[$name] : $default; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* shorthand for self::getAttribute('__BBTAG__') |
|
165
|
|
|
* |
|
166
|
|
|
* @param mixed $default |
|
167
|
|
|
* |
|
168
|
|
|
* @return mixed $this->attributes['__BBTAG__'] |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function bbtag($default = false){ |
|
171
|
|
|
return $this->getAttribute($this->options->placeholder_bbtag, $default); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* shorthand for self::attributeIn('__BBTAG__', $array) |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $array |
|
178
|
|
|
* @param mixed $default |
|
179
|
|
|
* |
|
180
|
|
|
* @return mixed |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function bbtagIn(array $array, $default = false){ |
|
183
|
|
|
return $this->attributeIn($this->options->placeholder_bbtag, $array, $default); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Replaces the EOL placeholder in the given string with a custom token |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $str haystack |
|
190
|
|
|
* @param string $eol [optional] custom EOL token, default: '' |
|
191
|
|
|
* @param int $count [optional] replace first $count occurences |
|
|
|
|
|
|
192
|
|
|
* |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function eol(string $str, string $eol = '', int $count = null):string{ |
|
196
|
|
|
return str_replace($this->options->placeholder_eol, $eol, $str, $count); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Retrieves an attribute's value by it's name and checks if it's whitelisted |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $name the desired attributes name |
|
203
|
|
|
* @param array $whitelist an array with whitelisted values |
|
204
|
|
|
* @param mixed $default [optional] a default value in case the attribute isn't set, defaults to false |
|
205
|
|
|
* |
|
206
|
|
|
* @return mixed boolean if no $default is set, otherwise the attribute's value in case it exists and is whitelisted or $default |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function attributeIn(string $name, array $whitelist, $default = false){ |
|
209
|
|
|
return isset($this->attributes[$name]) && in_array($this->attributes[$name], $whitelist) |
|
210
|
|
|
? $default !== false |
|
211
|
|
|
? $this->attributes[$name] |
|
212
|
|
|
: true |
|
213
|
|
|
: $default; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Checks if an attribute exists and if it exists as key in a whitelist |
|
218
|
|
|
|
|
219
|
|
|
* @param string $name the desired attributes name |
|
220
|
|
|
* @param array $whitelist an array with whitelisted key -> value pairs |
|
221
|
|
|
* @param mixed $default [optional] a default value in case the attribute isn't set, defaults to false |
|
222
|
|
|
* |
|
223
|
|
|
* @return mixed boolean if no $default is set, otherwise the whitelist value to the given key in case it exists or $default |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function attributeKeyIn(string $name, array $whitelist, $default = false){ |
|
226
|
|
|
return isset($this->attributes[$name]) && array_key_exists($this->attributes[$name], $whitelist) |
|
227
|
|
|
? $default !== false ? $whitelist[$this->attributes[$name]] : true |
|
228
|
|
|
: $default; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Checks if the current tag is whitelisted |
|
233
|
|
|
* |
|
234
|
|
|
* @param array $whitelist an array with whitelisted tag names |
|
235
|
|
|
* @param mixed $default [optional] a default value in case the tag isn't whitelisted |
|
236
|
|
|
* |
|
237
|
|
|
* @return mixed boolean if no $default is set, otherwise the whitelisted tag or $default |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function tagIn(array $whitelist, $default = false){ |
|
240
|
|
|
return in_array($this->tag, $whitelist) |
|
241
|
|
|
? $default !== false ? $this->tag : true |
|
242
|
|
|
: $default; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.