[TASK] Remove unneeded parenthesis on array-access
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Controller / UserElementsController.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Controller;
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 use Psr\Http\Message\ServerRequestInterface;
17 use Psr\Http\Message\ResponseInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * User defined content for htmlArea RTE
22 */
23 class UserElementsController
24 {
25 /**
26 * @var string
27 */
28 public $content;
29
30 /**
31 * @var array
32 */
33 public $modData;
34
35 /**
36 * @var string
37 */
38 public $siteUrl;
39
40 /**
41 * document template object
42 *
43 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
44 */
45 public $doc;
46
47 /**
48 * @var string
49 */
50 public $editorNo;
51
52 /**
53 * Initialize language files
54 */
55 public function __construct()
56 {
57 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_userelementscontroller.xlf');
58 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_dialogs.xlf');
59
60 $this->init();
61 }
62
63 /**
64 * @return void
65 */
66 public function init()
67 {
68 $this->editorNo = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('editorNo');
69 $this->siteUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
70 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
71 $this->doc->bodyTagAdditions = 'onload="Init();"';
72 $this->doc->form = '
73 <form action="" id="process" name="process" method="post">
74 <input type="hidden" name="processContent" value="" />
75 <input type="hidden" name="returnUrl" value="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')) . '" />
76 ';
77 $JScode = '
78 var plugin = window.parent.RTEarea["' . $this->editorNo . '"].editor.getPlugin("UserElements");
79 var HTMLArea = window.parent.HTMLArea;
80 var editor = plugin.editor;
81
82 function Init() {
83 };
84 function insertHTML(content,noHide) {
85 plugin.restoreSelection();
86 editor.getSelection().insertHtml(content);
87 if(!noHide) plugin.close();
88 };
89 function wrapHTML(wrap1,wrap2,noHide) {
90 plugin.restoreSelection();
91 if(!editor.getSelection().isEmpty()) {
92 editor.getSelection().surroundHtml(wrap1,wrap2);
93 } else {
94 alert(' . GeneralUtility::quoteJSvalue($GLOBALS['LANG']->getLL('noTextSelection')) . ');
95 }
96 if(!noHide) plugin.close();
97 };
98 function processSelection(script) {
99 plugin.restoreSelection();
100 document.process.action = script;
101 document.process.processContent.value = editor.getSelection().getHtml();
102 document.process.submit();
103 };
104 function jumpToUrl(URL) {
105 var RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams')) . '";
106 var editorNo = "&editorNo=' . rawurlencode($this->editorNo) . '";
107 theLocation = URL+RTEtsConfigParams+editorNo;
108 window.location.href = theLocation;
109 }
110 ';
111
112 // unset the default jumpToUrl() function
113 unset($this->doc->JScodeArray['jumpToUrl']);
114
115 $this->doc->JScode = $this->doc->wrapScriptTags($JScode);
116 $this->modData = $GLOBALS['BE_USER']->getModuleData('user.php', 'ses');
117 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key')) {
118 $parts = explode('|', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key'));
119 $this->modData['openKeys'][$parts[1]] = $parts[0] == 'O' ? 1 : 0;
120 $GLOBALS['BE_USER']->pushModuleData('user.php', $this->modData);
121 }
122 }
123
124 /**
125 * Injects the request object for the current request or subrequest
126 * As this controller goes only through the main_user() method, it is rather simple for now
127 *
128 * @param ServerRequestInterface $request the current request
129 * @param ResponseInterface $response the prepared response object
130 * @return ResponseInterface the response with the content
131 */
132 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
133 {
134 $content = $this->main_user($this->modData['openKeys']);
135 $response->getBody()->write($content);
136 return $response;
137 }
138
139 /**
140 * Main function
141 *
142 * @return void
143 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use main_user() instead
144 */
145 public function main()
146 {
147 GeneralUtility::logDeprecatedFunction();
148 $this->content = '';
149 $this->content .= $this->main_user($this->modData['openKeys']);
150 }
151
152 /**
153 * Print content
154 *
155 * @return void
156 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use mainAction() instead
157 */
158 public function printContent()
159 {
160 GeneralUtility::logDeprecatedFunction();
161 echo $this->content;
162 }
163
164 /********************************
165 *
166 * Other functions
167 *
168 *********************************/
169 /**
170 * @param array $imgInfo
171 * @param int $maxW
172 * @param int $maxH
173 * @return array
174 */
175 public function calcWH($imgInfo, $maxW = 380, $maxH = 500)
176 {
177 $IW = $imgInfo[0];
178 $IH = $imgInfo[1];
179 if ($IW > $maxW) {
180 $IH = ceil($IH / $IW * $maxW);
181 $IW = $maxW;
182 }
183 if ($IH > $maxH) {
184 $IW = ceil($IW / $IH * $maxH);
185 $IH = $maxH;
186 }
187 $imgInfo[3] = 'width="' . $IW . '" height="' . $IH . '"';
188 return $imgInfo;
189 }
190
191 /**
192 * Rich Text Editor (RTE) user element selector
193 *
194 * @param array $openKeys
195 * @return string
196 */
197 public function main_user($openKeys)
198 {
199 // Starting content:
200 $content = $this->doc->startPage($GLOBALS['LANG']->getLL('Insert Custom Element', true));
201 $RTEtsConfigParts = explode(':', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams'));
202 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
203 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
204 if (is_array($thisConfig['userElements.'])) {
205 $categories = array();
206 foreach ($thisConfig['userElements.'] as $k => $value) {
207 $ki = (int)$k;
208 $v = $thisConfig['userElements.'][$ki . '.'];
209 if (substr($k, -1) == '.' && is_array($v)) {
210 $subcats = array();
211 $openK = $ki;
212 if ($openKeys[$openK]) {
213 $mArray = '';
214 if ($v['load'] === 'images_from_folder') {
215 $mArray = array();
216 if ($v['path'] && @is_dir((PATH_site . $v['path']))) {
217 $files = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_site . $v['path'], 'gif,jpg,jpeg,png', 0, '');
218 if (is_array($files)) {
219 $c = 0;
220 foreach ($files as $filename) {
221 $iInfo = @getimagesize((PATH_site . $v['path'] . $filename));
222 $iInfo = $this->calcWH($iInfo, 50, 100);
223 $ks = (string)(100 + $c);
224 $mArray[$ks] = $filename;
225 $mArray[$ks . '.'] = array(
226 'content' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" />',
227 '_icon' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" ' . $iInfo[3] . ' />',
228 'description' => $GLOBALS['LANG']->getLL('filesize') . ': ' . str_replace('&nbsp;', ' ', \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize(@filesize((PATH_site . $v['path'] . $filename)))) . ', ' . $GLOBALS['LANG']->getLL('pixels', 1) . ': ' . $iInfo[0] . 'x' . $iInfo[1]
229 );
230 $c++;
231 }
232 }
233 }
234 }
235 if (is_array($mArray)) {
236 if ($v['merge']) {
237 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($mArray, $v);
238 $v = $mArray;
239 } else {
240 $v = $mArray;
241 }
242 }
243 foreach ($v as $k2 => $dummyValue) {
244 $k2i = (int)$k2;
245 if (substr($k2, -1) == '.' && is_array($v[$k2i . '.'])) {
246 $title = trim($v[$k2i]);
247 if (!$title) {
248 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', true) . ']';
249 } else {
250 $title = $GLOBALS['LANG']->sL($title, true);
251 }
252 $description = $GLOBALS['LANG']->sL($v[$k2i . '.']['description'], true) . '<br />';
253 if (!$v[$k2i . '.']['dontInsertSiteUrl']) {
254 $v[$k2i . '.']['content'] = str_replace('###_URL###', $this->siteUrl, $v[$k2i . '.']['content']);
255 }
256 $logo = $v[$k2i . '.']['_icon'] ?: '';
257 $onClickEvent = '';
258 switch ((string)$v[$k2i . '.']['mode']) {
259 case 'wrap':
260 $wrap = explode('|', $v[$k2i . '.']['content']);
261 $onClickEvent = 'wrapHTML(' . GeneralUtility::quoteJSvalue($wrap[0]) . ',' . GeneralUtility::quoteJSvalue($wrap[1]) . ',false);';
262 break;
263 case 'processor':
264 $script = trim($v[$k2i . '.']['submitToScript']);
265 if (substr($script, 0, 4) != 'http') {
266 $script = $this->siteUrl . $script;
267 }
268 if ($script) {
269 $onClickEvent = 'processSelection(' . GeneralUtility::quoteJSvalue($script) . ');';
270 }
271 break;
272 case 'insert':
273
274 default:
275 $onClickEvent = 'insertHTML(' . GeneralUtility::quoteJSvalue($v[$k2i . '.']['content']) . ');';
276 }
277 $A = array('<a href="#" onClick="' . $onClickEvent . 'return false;">', '</a>');
278 $subcats[$k2i] = '<tr>
279 <td></td>
280 <td class="bgColor4" valign="top">' . $A[0] . $logo . $A[1] . '</td>
281 <td class="bgColor4" valign="top">' . $A[0] . '<strong>' . $title . '</strong><br />' . $description . $A[1] . '</td>
282 </tr>';
283 }
284 }
285 ksort($subcats);
286 }
287 $categories[$ki] = implode('', $subcats);
288 }
289 }
290 ksort($categories);
291 // Render menu of the items:
292 $lines = array();
293 foreach ($categories as $k => $v) {
294 $title = trim($thisConfig['userElements.'][$k]);
295 $openK = $k;
296 if (!$title) {
297 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', true) . ']';
298 } else {
299 $title = $GLOBALS['LANG']->sL($title, true);
300 }
301
302 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
303 $url = (string)$uriBuilder->buildUriFromRoute('rtehtmlarea_wizard_user_elements', array('OC_key' => ($openKeys[$openK] ? 'C|' : 'O|') . $openK));
304
305 $lines[] = '<tr><td colspan="3" class="bgColor5"><a href="#" title="' . $GLOBALS['LANG']->getLL('expand', true) . '" onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($url) . ');return false;"><i class="fa fa-caret-square-o-' . ($openKeys[$openK] ? 'left' : 'right') . '" title="' . $GLOBALS['LANG']->getLL('expand', true) . '"></i><strong>' . $title . '</strong></a></td></tr>';
306 $lines[] = $v;
307 }
308 $content .= '<table border="0" cellpadding="1" cellspacing="1">' . implode('', $lines) . '</table>';
309 }
310 $content .= $this->doc->endPage();
311 return $content;
312 }
313 }