3 namespace wcf\data\bbcode\media\provider
;
5 use wcf\data\DatabaseObject
;
6 use wcf\system\bbcode\media\provider\IBBCodeMediaProvider
;
7 use wcf\system\cache\builder\BBCodeMediaProviderCacheBuilder
;
9 use wcf\system\request\IRouteController
;
11 use wcf\util\StringUtil
;
15 * Represents a BBCode media provider.
17 * @author Tim Duesterhus
18 * @copyright 2001-2020 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\Data\Bbcode\Media\Provider
22 * @property-read int $providerID unique id of the bbcode media provider
23 * @property-read string $title title of the bbcode media provider (shown in acp)
24 * @property-read string $regex regular expression to recognize media elements/element urls
25 * @property-read string $html html code used to render media elements
26 * @property-read string $className callback class name
28 class BBCodeMediaProvider
extends DatabaseObject
implements IRouteController
33 protected static $databaseTableName = 'bbcode_media_provider';
37 * @var BBCodeMediaProvider[]
39 protected static $cache = null;
42 * media provider callback instance
43 * @var IBBCodeMediaProvider
48 * Loads the provider cache.
50 * @return BBCodeMediaProvider[]
52 public static function getCache()
54 if (self
::$cache === null) {
55 self
::$cache = BBCodeMediaProviderCacheBuilder
::getInstance()->getData();
62 * Returns true if given URL is a media URL.
67 public static function isMediaURL($url)
69 foreach (static::getCache() as $provider) {
70 if ($provider->matches($url)) {
79 * Checks whether this provider matches the given URL.
84 public function matches($url)
86 $lines = \
explode("\n", StringUtil
::unifyNewlines($this->regex
));
88 foreach ($lines as $line) {
89 if (Regex
::compile($line)->match($url)) {
98 * Returns the html for this provider.
103 public function getOutput($url)
105 $lines = \
explode("\n", StringUtil
::unifyNewlines($this->regex
));
107 foreach ($lines as $line) {
108 $regex = new Regex($line);
109 if (!$regex->match($url)) {
113 if ($this->getCallback() !== null) {
114 return $this->getOutputForUserConsent($url, $this->getCallback()->parse($url, $regex->getMatches()));
116 $output = $this->html
;
117 foreach ($regex->getMatches() as $name => $value) {
118 $output = \
str_replace('{$' . $name . '}', $value, $output);
121 return $this->getOutputForUserConsent($url, $output);
131 public function getTitle()
137 * Returns media provider callback instance.
139 * @return IBBCodeMediaProvider|null
141 public function getCallback()
143 if (!$this->className
) {
147 if ($this->callback
=== null) {
148 $this->callback
= new $this->className();
151 return $this->callback
;
155 * Replaces embedded media with an approval dialog.
158 * @param string $html
161 protected function getOutputForUserConsent($url, $html)
163 if (!MESSAGE_ENABLE_USER_CONSENT
) {
167 if (WCF
::getUser()->userID
&& WCF
::getUser()->getUserOption('enableEmbeddedMedia')) {
171 return WCF
::getTPL()->fetch('messageUserConsent', 'wcf', [
172 'host' => Url
::parse($url)['host'],
173 'payload' => \base
64_encode
($html),