[TASK] Remove unneeded parenthesis on array-access
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / RteHtmlAreaApi.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Lang\LanguageService;
19 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
20 use TYPO3\CMS\Core\FrontendEditing\FrontendEditingController;
21
22 /**
23 * API for extending htmlArea RTE
24 */
25 abstract class RteHtmlAreaApi
26 {
27 /**
28 * The key of the extension that is extending htmlArea RTE
29 *
30 * @var string
31 */
32 protected $extensionKey = 'rtehtmlarea';
33
34 /**
35 * The name of the plugin registered by the extension
36 *
37 * @var string
38 */
39 protected $pluginName;
40
41 /**
42 * Path to the skin (css) file that should be added to the RTE skin when the registered plugin is enabled, relative to the extension dir
43 *
44 * @var string
45 */
46 protected $relativePathToSkin = '';
47
48 /**
49 * Toolbar array
50 *
51 * @var array
52 */
53 protected $toolbar;
54
55 /**
56 * The comma-separated list of button names that the registered plugin is adding to the htmlArea RTE toolbar
57 *
58 * @var string
59 */
60 protected $pluginButtons = '';
61
62 /**
63 * The comma-separated list of label names that the registered plugin is adding to the htmlArea RTE toolbar
64 *
65 * @var string
66 */
67 protected $pluginLabels = '';
68
69 /**
70 * Boolean indicating whether the plugin is adding buttons or not
71 *
72 * @var bool
73 */
74 protected $pluginAddsButtons = true;
75
76 /**
77 * The name-converting array, converting the button names used in the RTE PageTSConfing to the button id's used by the JS scripts
78 *
79 * @var array
80 */
81 protected $convertToolbarForHtmlAreaArray = array();
82
83 /**
84 * TRUE if the registered plugin requires the PageTSConfig Classes configuration
85 *
86 * @var bool
87 */
88 protected $requiresClassesConfiguration = false;
89
90 /**
91 * The comma-separated list of names of prerequisite plugins
92 *
93 * @var string
94 */
95 protected $requiredPlugins = '';
96
97 /**
98 * Configuration array with settings given down from calling class
99 *
100 * @var array
101 */
102 protected $configuration;
103
104 /**
105 * Returns TRUE if the plugin is available and correctly initialized
106 *
107 * @param array $configuration Configuration array given from calling object down to the single plugins
108 * @return bool TRUE if this plugin object should be made available in the current environment and is correctly initialized
109 */
110 public function main(array $configuration)
111 {
112 $this->configuration = $configuration;
113 // Set the value of this boolean based on the initial value of $this->pluginButtons
114 $this->pluginAddsButtons = !empty($this->pluginButtons);
115 // Check if the plugin should be disabled in frontend
116 if ($this->isFrontend() && $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rtehtmlarea']['plugins'][$this->pluginName]['disableInFE']) {
117 return false;
118 }
119 return true;
120 }
121
122 /**
123 * Return JS configuration of the htmlArea plugins registered by the extension
124 *
125 * @return string JS configuration for registered plugins
126 */
127 public function buildJavascriptConfiguration()
128 {
129 $jsArray = array();
130 $pluginButtons = GeneralUtility::trimExplode(',', $this->pluginButtons, true);
131 foreach ($pluginButtons as $button) {
132 if (in_array($button, $this->toolbar)) {
133 if (!is_array($this->configuration['thisConfig']['buttons.']) || !is_array($this->configuration['thisConfig']['buttons.'][$button . '.'])) {
134 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . ' = new Object();';
135 }
136 }
137 }
138 return implode(LF, $jsArray);
139 }
140
141 /**
142 * Returns the extension key
143 *
144 * @return string the extension key
145 */
146 public function getExtensionKey()
147 {
148 return $this->extensionKey;
149 }
150
151 /**
152 * Returns a boolean indicating whether the plugin adds buttons or not to the toolbar
153 *
154 * @return bool
155 */
156 public function addsButtons()
157 {
158 return $this->pluginAddsButtons;
159 }
160
161 /**
162 * Returns the list of buttons implemented by the plugin
163 *
164 * @return string the list of buttons implemented by the plugin
165 */
166 public function getPluginButtons()
167 {
168 return $this->pluginButtons;
169 }
170
171 /**
172 * Returns the list of toolbar labels implemented by the plugin
173 *
174 * @return string the list of labels implemented by the plugin
175 */
176 public function getPluginLabels()
177 {
178 return $this->pluginLabels;
179 }
180
181 /**
182 * Returns the conversion array from TYPO3 button names to htmlArea button names
183 *
184 * @return array the conversion array from TYPO3 button names to htmlArea button names
185 */
186 public function getConvertToolbarForHtmlAreaArray()
187 {
188 return $this->convertToolbarForHtmlAreaArray;
189 }
190
191 /**
192 * Returns TRUE if the extension requires the PageTSConfig Classes configuration
193 *
194 * @return bool TRUE if the extension requires the PageTSConfig Classes configuration
195 */
196 public function requiresClassesConfiguration()
197 {
198 return $this->requiresClassesConfiguration;
199 }
200
201 /**
202 * Returns the list of plugins required by the plugin
203 *
204 * @return string the list of plugins required by the plugin
205 */
206 public function getRequiredPlugins()
207 {
208 return $this->requiredPlugins;
209 }
210
211 /**
212 * Set toolbal
213 *
214 * @param array $toolbar
215 */
216 public function setToolbar(array $toolbar)
217 {
218 $this->toolbar = $toolbar;
219 }
220
221 /**
222 * Clean list
223 *
224 * @param string $str
225 * @return string
226 */
227 protected function cleanList($str)
228 {
229 if (strstr($str, '*')) {
230 $str = '*';
231 } else {
232 $str = implode(',', array_unique(GeneralUtility::trimExplode(',', $str, true)));
233 }
234 return $str;
235 }
236
237 /**
238 * Resolve a label and do some funny quoting.
239 *
240 * @param string $string Given label name
241 * @return string Resolved label
242 */
243 protected function getPageConfigLabel($string)
244 {
245 $label = $this->getLanguageService()->sL(trim($string));
246 // @todo: find out why this is done and if it could be substituted with quoteJSvalue
247 $label = str_replace('"', '\\"', str_replace('\\\'', '\'', $label));
248 return $label;
249 }
250
251 /**
252 * Return TRUE if we are in the FE, but not in the FE editing feature of BE.
253 *
254 * @return bool
255 */
256 protected function isFrontend()
257 {
258 return is_object($GLOBALS['TSFE'])
259 && !$this->isFrontendEditActive()
260 && TYPO3_MODE == 'FE';
261 }
262
263 /**
264 * Checks whether frontend editing is active.
265 *
266 * @return bool
267 */
268 protected function isFrontendEditActive()
269 {
270 return is_object($GLOBALS['TSFE'])
271 && $GLOBALS['TSFE']->beUserLogin
272 && $GLOBALS['BE_USER']->frontendEdit instanceof FrontendEditingController;
273 }
274
275 /**
276 * Make a file name relative to the PATH_site or to the PATH_typo3
277 *
278 * @param string $filename: a file name of the form EXT:.... or relative to the PATH_site
279 * @return string the file name relative to the PATH_site if in frontend or relative to the PATH_typo3 if in backend
280 */
281 protected function getFullFileName($filename)
282 {
283 if (substr($filename, 0, 4) === 'EXT:') {
284 // extension
285 list($extKey, $local) = explode('/', substr($filename, 4), 2);
286 $newFilename = '';
287 if ((string)$extKey !== '' && ExtensionManagementUtility::isLoaded($extKey) && (string)$local !== '') {
288 $newFilename = ($this->isFrontend() || $this->isFrontendEditActive()
289 ? ExtensionManagementUtility::siteRelPath($extKey)
290 : ExtensionManagementUtility::extRelPath($extKey))
291 . $local;
292 }
293 } else {
294 $path = ($this->isFrontend() || $this->isFrontendEditActive() ? '' : '../');
295 $newFilename = $path . ($filename[0] === '/' ? substr($filename, 1) : $filename);
296 }
297 return GeneralUtility::resolveBackPath($newFilename);
298 }
299
300 /**
301 * Writes contents in a file in typo3temp and returns the file name
302 *
303 * @param string $label: A label to insert at the beginning of the name of the file
304 * @param string $fileExtension: The file extension of the file, defaulting to 'js'
305 * @param string $contents: The contents to write into the file
306 * @return string The name of the file written to typo3temp
307 * @throws \RuntimeException If writing to file failed
308 */
309 protected function writeTemporaryFile($label, $fileExtension = 'js', $contents = '')
310 {
311 $relativeFilename = 'typo3temp/RteHtmlArea/' . str_replace('-', '_', $label) . '_' . GeneralUtility::shortMD5($contents, 20) . '.' . $fileExtension;
312 $destination = PATH_site . $relativeFilename;
313 if (!file_exists($destination)) {
314 $minifiedJavaScript = '';
315 if ($fileExtension === 'js' && $contents !== '') {
316 $minifiedJavaScript = GeneralUtility::minifyJavaScript($contents);
317 }
318 $failure = GeneralUtility::writeFileToTypo3tempDir($destination, $minifiedJavaScript ? $minifiedJavaScript : $contents);
319 if ($failure) {
320 throw new \RuntimeException($failure, 1294585668);
321 }
322 }
323 if ($this->isFrontend() || $this->isFrontendEditActive()) {
324 $fileName = $relativeFilename;
325 } else {
326 $fileName = '../' . $relativeFilename;
327 }
328 return GeneralUtility::resolveBackPath($fileName);
329 }
330
331 /**
332 * Get language service, instantiate if not there, yet
333 *
334 * @return LanguageService
335 */
336 protected function getLanguageService()
337 {
338 return $GLOBALS['LANG'];
339 }
340 }