Remove `BBCodeHandler::setAllowedBBCodes()` (#4318)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / bbcode / BBCodeHandler.class.php
CommitLineData
dcc2332d 1<?php
a9229942 2
dcc2332d 3namespace wcf\system\bbcode;
a9229942 4
7a23a706 5use wcf\data\bbcode\BBCode;
dcc2332d 6use wcf\data\bbcode\BBCodeCache;
74c960dd 7use wcf\system\application\ApplicationHandler;
dcc2332d 8use wcf\system\SingletonFactory;
74c960dd 9use wcf\util\ArrayUtil;
070c42e0 10use wcf\util\JSON;
dcc2332d
MW
11
12/**
13 * Handles BBCodes displayed as buttons within the WYSIWYG editor.
a9229942
TD
14 *
15 * @author Alexander Ebert
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\System\Bbcode
dcc2332d 19 */
a9229942
TD
20class BBCodeHandler extends SingletonFactory
21{
22 /**
23 * list of BBCodes displayed as buttons
24 * @var BBCode[]
25 */
26 protected $buttonBBCodes = [];
27
28 /**
29 * list of BBCodes disallowed for usage
30 * @var BBCode[]
31 */
32 protected $disallowedBBCodes = [];
33
34 /**
35 * list of BBCodes which contain raw code (disabled BBCode parsing)
36 * @var BBCode[]
37 */
38 protected $sourceBBCodes;
39
40 /**
41 * meta information about highlighters
42 * @var mixed[]
43 */
44 protected $highlighterMeta;
45
46 /**
47 * @inheritDoc
48 */
49 protected function init()
50 {
51 foreach (BBCodeCache::getInstance()->getBBCodes() as $bbcode) {
52 if ($bbcode->showButton) {
53 $this->buttonBBCodes[] = $bbcode;
54 }
55 }
56 }
57
58 /**
59 * Returns true if the BBCode with the given tag is available in the WYSIWYG editor.
60 *
61 * @param string $bbCodeTag
62 * @return bool
63 */
64 public function isAvailableBBCode($bbCodeTag)
65 {
66 return !\in_array($bbCodeTag, $this->disallowedBBCodes);
67 }
68
69 /**
70 * Returns all bbcodes.
71 *
72 * @return BBCode[]
73 */
74 public function getBBCodes()
75 {
76 return BBCodeCache::getInstance()->getBBCodes();
77 }
78
79 /**
80 * Returns a list of BBCodes displayed as buttons.
81 *
82 * @param bool $excludeCoreBBCodes do not return bbcodes that are available by default
83 * @return BBCode[]
84 */
85 public function getButtonBBCodes($excludeCoreBBCodes = false)
86 {
87 $buttons = [];
88 $coreBBCodes = [
89 'align',
90 'b',
91 'color',
92 'i',
93 'img',
94 'list',
95 's',
96 'size',
97 'sub',
98 'sup',
99 'quote',
100 'table',
101 'u',
102 'url',
103 ];
104 foreach ($this->buttonBBCodes as $bbcode) {
105 if ($excludeCoreBBCodes && \in_array($bbcode->bbcodeTag, $coreBBCodes)) {
106 continue;
107 }
108
109 if ($this->isAvailableBBCode($bbcode->bbcodeTag)) {
110 $buttons[] = $bbcode;
111 }
112 }
113
114 return $buttons;
115 }
116
a9229942
TD
117 /**
118 * Sets the disallowed BBCodes.
119 *
120 * @param string[] $bbCodes
121 */
122 public function setDisallowedBBCodes(array $bbCodes)
123 {
124 $this->disallowedBBCodes = $bbCodes;
125 }
126
127 /**
128 * Returns a list of BBCodes which contain raw code (disabled BBCode parsing)
129 *
130 * @return BBCode[]
131 * @deprecated 3.1 - This method is no longer supported.
132 */
133 public function getSourceBBCodes()
134 {
135 return [];
136 }
137
138 /**
139 * Returns metadata about the highlighters.
140 *
141 * @return string[][]
142 */
143 public function getHighlighterMeta()
144 {
145 if ($this->highlighterMeta === null) {
146 $this->highlighterMeta = JSON::decode(\preg_replace(
147 '/.*\/\*!START\*\/\s*const\s*metadata\s*=\s*(.*)\s*;\s*\/\*!END\*\/.*/s',
148 '\\1',
149 \file_get_contents(WCF_DIR . '/js/WoltLabSuite/Core/prism-meta.js')
150 ));
151 }
152
153 return $this->highlighterMeta;
154 }
155
156 /**
157 * Returns a list of known highlighters.
158 *
159 * @return string[]
160 */
161 public function getHighlighters()
162 {
163 return \array_keys($this->getHighlighterMeta());
164 }
165
166 /**
167 * Returns a list of hostnames that are permitted as image sources.
168 *
169 * @return string[]
170 * @since 5.2
171 */
172 public function getImageExternalSourceWhitelist()
173 {
174 $hosts = [];
175 // Hide these hosts unless external sources are actually denied.
176 if (!IMAGE_ALLOW_EXTERNAL_SOURCE) {
177 $hosts = ArrayUtil::trim(\explode("\n", IMAGE_EXTERNAL_SOURCE_WHITELIST));
178 }
179
180 foreach (ApplicationHandler::getInstance()->getApplications() as $application) {
181 $hosts[] = $application->domainName;
182 }
183
184 return \array_unique($hosts);
185 }
dcc2332d 186}