-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathBBCodeMediaProvider.class.php
More file actions
177 lines (153 loc) · 4.38 KB
/
BBCodeMediaProvider.class.php
File metadata and controls
177 lines (153 loc) · 4.38 KB
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
<?php
namespace wcf\data\bbcode\media\provider;
use wcf\data\DatabaseObject;
use wcf\system\bbcode\media\provider\IBBCodeMediaProvider;
use wcf\system\cache\builder\BBCodeMediaProviderCacheBuilder;
use wcf\system\Regex;
use wcf\system\request\IRouteController;
use wcf\system\WCF;
use wcf\util\StringUtil;
use wcf\util\Url;
/**
* Represents a BBCode media provider.
*
* @author Tim Duesterhus
* @copyright 2001-2020 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @property-read int $providerID unique id of the bbcode media provider
* @property-read string $title title of the bbcode media provider (shown in acp)
* @property-read string $regex regular expression to recognize media elements/element urls
* @property-read string $html html code used to render media elements
* @property-read string $className callback class name
* @property-read int $isDisabled
*/
class BBCodeMediaProvider extends DatabaseObject implements IRouteController
{
/**
* @inheritDoc
*/
protected static $databaseTableName = 'bbcode_media_provider';
/**
* cached providers
* @var BBCodeMediaProvider[]
*/
protected static $cache;
/**
* media provider callback instance
* @var IBBCodeMediaProvider
*/
protected $callback;
/**
* Loads the provider cache.
*
* @return BBCodeMediaProvider[]
*/
public static function getCache()
{
if (self::$cache === null) {
self::$cache = BBCodeMediaProviderCacheBuilder::getInstance()->getData();
}
return self::$cache;
}
/**
* Returns true if given URL is a media URL.
*
* @param string $url
* @return bool
*/
public static function isMediaURL($url)
{
foreach (static::getCache() as $provider) {
if ($provider->matches($url)) {
return true;
}
}
return false;
}
/**
* Checks whether this provider matches the given URL.
*
* @param string $url
* @return bool
*/
public function matches($url)
{
$lines = \explode("\n", StringUtil::unifyNewlines($this->regex));
foreach ($lines as $line) {
if (Regex::compile($line)->match($url)) {
return true;
}
}
return false;
}
/**
* Returns the html for this provider.
*
* @param string $url
* @return string
*/
public function getOutput($url)
{
$lines = \explode("\n", StringUtil::unifyNewlines($this->regex));
foreach ($lines as $line) {
$regex = new Regex($line);
if (!$regex->match($url)) {
continue;
}
if ($this->getCallback() !== null) {
return $this->getOutputForUserConsent($url, $this->getCallback()->parse($url, $regex->getMatches()));
} else {
$output = $this->html;
foreach ($regex->getMatches() as $name => $value) {
$output = \str_replace('{$' . $name . '}', $value, $output);
}
return $this->getOutputForUserConsent($url, $output);
}
}
return '';
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return $this->title;
}
/**
* Returns media provider callback instance.
*
* @return IBBCodeMediaProvider|null
*/
public function getCallback()
{
if (!$this->className) {
return null;
}
if ($this->callback === null) {
$this->callback = new $this->className();
}
return $this->callback;
}
/**
* Replaces embedded media with an approval dialog.
*
* @param string $url
* @param string $html
* @return string
*/
protected function getOutputForUserConsent($url, $html)
{
if (!MESSAGE_ENABLE_USER_CONSENT) {
return $html;
}
if (WCF::getUser()->userID && WCF::getUser()->getUserOption('enableEmbeddedMedia')) {
return $html;
}
return WCF::getTPL()->fetch('messageUserConsent', 'wcf', [
'host' => Url::parse($url)['host'],
'payload' => \base64_encode($html),
'url' => $url,
]);
}
}