f33919c0844836032535070337b062ec70273b32
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / User.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea;
3
4 use TYPO3\CMS\Core\Utility\GeneralUtility;
5
6 /***************************************************************
7 * Copyright notice
8 *
9 * (c) 1999-2013 Kasper Skårhøj (kasper@typo3.com)
10 * (c) 2005-2013 Stanislas Rolland <typo3(arobas)sjbr.ca>
11 * All rights reserved
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 * A copy is found in the text file GPL.txt and important notices to the license
22 * from the author is found in LICENSE.txt distributed with these scripts.
23 *
24 *
25 * This script is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * This copyright notice MUST APPEAR in all copies of the script!
31 ***************************************************************/
32 /**
33 * User defined content for htmlArea RTE
34 *
35 * @author Kasper Skårhøj <kasper@typo3.com>
36 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
37 */
38 class User {
39
40 /**
41 * @todo Define visibility
42 */
43 public $content;
44
45 /**
46 * @todo Define visibility
47 */
48 public $modData;
49
50 /**
51 * @todo Define visibility
52 */
53 public $siteUrl;
54
55 /**
56 * document template object
57 *
58 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
59 * @todo Define visibility
60 */
61 public $doc;
62
63 /**
64 * @todo Define visibility
65 */
66 public $editorNo;
67
68 /**
69 * Initialize language files
70 */
71 public function __construct() {
72 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/mod5/locallang.xlf');
73 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/htmlarea/locallang_dialogs.xlf');
74 }
75
76 /**
77 * @return [type] ...
78 * @todo Define visibility
79 */
80 public function init() {
81 $this->editorNo = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('editorNo');
82 $this->siteUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
83 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
84 $this->doc->backPath = $GLOBALS['BACK_PATH'];
85 $this->doc->bodyTagAdditions = 'onload="Init();"';
86 $this->doc->form = '
87 <form action="" id="process" name="process" method="post">
88 <input type="hidden" name="processContent" value="" />
89 <input type="hidden" name="returnUrl" value="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')) . '" />
90 ';
91 $JScode = '
92 var plugin = window.parent.RTEarea["' . $this->editorNo . '"].editor.getPlugin("UserElements");
93 var HTMLArea = window.parent.HTMLArea;
94 var editor = plugin.editor;
95
96 function Init() {
97 };
98 function insertHTML(content,noHide) {
99 plugin.restoreSelection();
100 editor.getSelection().insertHtml(content);
101 if(!noHide) plugin.close();
102 };
103 function wrapHTML(wrap1,wrap2,noHide) {
104 plugin.restoreSelection();
105 if(!editor.getSelection().isEmpty()) {
106 editor.getSelection().surroundHtml(wrap1,wrap2);
107 } else {
108 alert(' . GeneralUtility::quoteJSvalue($GLOBALS['LANG']->getLL('noTextSelection')) . ');
109 }
110 if(!noHide) plugin.close();
111 };
112 function processSelection(script) {
113 plugin.restoreSelection();
114 document.process.action = script;
115 document.process.processContent.value = editor.getSelection().getHtml();
116 document.process.submit();
117 };
118 function jumpToUrl(URL) {
119 var RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams')) . '";
120 var editorNo = "&editorNo=' . rawurlencode($this->editorNo) . '";
121 theLocation = "' . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('SCRIPT_NAME') . '"+URL+RTEtsConfigParams+editorNo;
122 window.location.href = theLocation;
123 }
124 ';
125
126 // unset the default jumpToUrl() function
127 unset($this->doc->JScodeArray['jumpToUrl']);
128
129 $this->doc->JScode = $this->doc->wrapScriptTags($JScode);
130 $this->modData = $GLOBALS['BE_USER']->getModuleData('user.php', 'ses');
131 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key')) {
132 $parts = explode('|', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key'));
133 $this->modData['openKeys'][$parts[1]] = $parts[0] == 'O' ? 1 : 0;
134 $GLOBALS['BE_USER']->pushModuleData('user.php', $this->modData);
135 }
136 }
137
138 /**
139 * [Describe function...]
140 *
141 * @return [type] ...
142 * @todo Define visibility
143 */
144 public function main() {
145 $this->content = '';
146 $this->content .= $this->main_user($this->modData['openKeys']);
147 }
148
149 /**
150 * [Describe function...]
151 *
152 * @return [type] ...
153 * @todo Define visibility
154 */
155 public function printContent() {
156 echo $this->content;
157 }
158
159 /********************************
160 *
161 * Other functions
162 *
163 *********************************/
164 /**
165 * @param [type] $imgInfo: ...
166 * @param [type] $maxW: ...
167 * @param [type] $maxH: ...
168 * @return [type] ...
169 * @todo Define visibility
170 */
171 public function calcWH($imgInfo, $maxW = 380, $maxH = 500) {
172 $IW = $imgInfo[0];
173 $IH = $imgInfo[1];
174 if ($IW > $maxW) {
175 $IH = ceil($IH / $IW * $maxW);
176 $IW = $maxW;
177 }
178 if ($IH > $maxH) {
179 $IW = ceil($IW / $IH * $maxH);
180 $IH = $maxH;
181 }
182 $imgInfo[3] = 'width="' . $IW . '" height="' . $IH . '"';
183 return $imgInfo;
184 }
185
186 /**
187 * Rich Text Editor (RTE) user element selector
188 *
189 * @param [type] $openKeys: ...
190 * @return [type] ...
191 * @todo Define visibility
192 */
193 public function main_user($openKeys) {
194 // Starting content:
195 $content = $this->doc->startPage($GLOBALS['LANG']->getLL('Insert Custom Element', TRUE));
196 $RTEtsConfigParts = explode(':', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams'));
197 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
198 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
199 if (is_array($thisConfig['userElements.'])) {
200 $categories = array();
201 foreach ($thisConfig['userElements.'] as $k => $value) {
202 $ki = (int)$k;
203 $v = $thisConfig['userElements.'][$ki . '.'];
204 if (substr($k, -1) == '.' && is_array($v)) {
205 $subcats = array();
206 $openK = $ki;
207 if ($openKeys[$openK]) {
208 $mArray = '';
209 if ($v['load'] === 'images_from_folder') {
210 $mArray = array();
211 if ($v['path'] && @is_dir((PATH_site . $v['path']))) {
212 $files = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_site . $v['path'], 'gif,jpg,jpeg,png', 0, '');
213 if (is_array($files)) {
214 $c = 0;
215 foreach ($files as $filename) {
216 $iInfo = @getimagesize((PATH_site . $v['path'] . $filename));
217 $iInfo = $this->calcWH($iInfo, 50, 100);
218 $ks = (string) (100 + $c);
219 $mArray[$ks] = $filename;
220 $mArray[$ks . '.'] = array(
221 'content' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" />',
222 '_icon' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" ' . $iInfo[3] . ' />',
223 '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]
224 );
225 $c++;
226 }
227 }
228 }
229 }
230 if (is_array($mArray)) {
231 if ($v['merge']) {
232 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($mArray, $v);
233 $v = $mArray;
234 } else {
235 $v = $mArray;
236 }
237 }
238 foreach ($v as $k2 => $dummyValue) {
239 $k2i = (int)$k2;
240 if (substr($k2, -1) == '.' && is_array($v[$k2i . '.'])) {
241 $title = trim($v[$k2i]);
242 if (!$title) {
243 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', TRUE) . ']';
244 } else {
245 $title = $GLOBALS['LANG']->sL($title, TRUE);
246 }
247 $description = $GLOBALS['LANG']->sL($v[($k2i . '.')]['description'], TRUE) . '<br />';
248 if (!$v[($k2i . '.')]['dontInsertSiteUrl']) {
249 $v[$k2i . '.']['content'] = str_replace('###_URL###', $this->siteUrl, $v[$k2i . '.']['content']);
250 }
251 $logo = $v[$k2i . '.']['_icon'] ?: '';
252 $onClickEvent = '';
253 switch ((string) $v[($k2i . '.')]['mode']) {
254 case 'wrap':
255 $wrap = explode('|', $v[$k2i . '.']['content']);
256 $onClickEvent = 'wrapHTML(' . GeneralUtility::quoteJSvalue($wrap[0]) . ',' . GeneralUtility::quoteJSvalue($wrap[1]) . ',false);';
257 break;
258 case 'processor':
259 $script = trim($v[$k2i . '.']['submitToScript']);
260 if (substr($script, 0, 4) != 'http') {
261 $script = $this->siteUrl . $script;
262 }
263 if ($script) {
264 $onClickEvent = 'processSelection(' . GeneralUtility::quoteJSvalue($script) . ');';
265 }
266 break;
267 case 'insert':
268
269 default:
270 $onClickEvent = 'insertHTML(' . GeneralUtility::quoteJSvalue($v[($k2i . '.')]['content']) . ');';
271 }
272 $A = array('<a href="#" onClick="' . $onClickEvent . 'return false;">', '</a>');
273 $subcats[$k2i] = '<tr>
274 <td><img src="clear.gif" width="18" height="1" /></td>
275 <td class="bgColor4" valign="top">' . $A[0] . $logo . $A[1] . '</td>
276 <td class="bgColor4" valign="top">' . $A[0] . '<strong>' . $title . '</strong><br />' . $description . $A[1] . '</td>
277 </tr>';
278 }
279 }
280 ksort($subcats);
281 }
282 $categories[$ki] = implode('', $subcats);
283 }
284 }
285 ksort($categories);
286 // Render menu of the items:
287 $lines = array();
288 foreach ($categories as $k => $v) {
289 $title = trim($thisConfig['userElements.'][$k]);
290 $openK = $k;
291 if (!$title) {
292 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', TRUE) . ']';
293 } else {
294 $title = $GLOBALS['LANG']->sL($title, TRUE);
295 }
296 $lines[] = '<tr><td colspan="3" class="bgColor5"><a href="#" title="' . $GLOBALS['LANG']->getLL('expand', TRUE) . '" onClick="jumpToUrl(' . \TYPO3\CMS\Core\Utility\GeneralUtility::quoteJSvalue('?OC_key=' . ($openKeys[$openK] ? 'C|' : 'O|') . $openK) . ');return false;"><img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], ('gfx/ol/' . ($openKeys[$openK] ? 'minus' : 'plus') . 'bullet.gif'), 'width="18" height="16"') . ' title="' . $GLOBALS['LANG']->getLL('expand', TRUE) . '" /><strong>' . $title . '</strong></a></td></tr>';
297 $lines[] = $v;
298 }
299 $content .= '<table border="0" cellpadding="1" cellspacing="1">' . implode('', $lines) . '</table>';
300 }
301 $content .= $this->doc->endPage();
302 return $content;
303 }
304
305 }