[BUGFIX] feedit: Double encoding of labels
[Packages/TYPO3.CMS.git] / typo3 / sysext / feedit / Classes / FrontendEditPanel.php
1 <?php
2 namespace TYPO3\CMS\Feedit;
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 TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\DatabaseConnection;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\MathUtility;
25 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
26 use TYPO3\CMS\Frontend\View\AdminPanelView;
27
28 /**
29 * View class for the edit panels in frontend editing.
30 */
31 class FrontendEditPanel
32 {
33 /**
34 * The Content Object Renderer
35 *
36 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
37 */
38 protected $cObj;
39
40 /**
41 * Property for accessing TypoScriptFrontendController centrally
42 *
43 * @var TypoScriptFrontendController
44 */
45 protected $frontendController;
46
47 /**
48 * Property for accessing DatabaseConnection centrally
49 *
50 * @var DatabaseConnection
51 */
52 protected $databaseConnection;
53
54 /**
55 * @var FrontendBackendUserAuthentication
56 */
57 protected $backendUser;
58
59 /**
60 * @var \TYPO3\CMS\Core\Imaging\IconFactory
61 */
62 protected $iconFactory;
63
64 /**
65 * Constructor for the edit panel
66 *
67 * @param DatabaseConnection $databaseConnection
68 * @param TypoScriptFrontendController $frontendController
69 * @param FrontendBackendUserAuthentication $backendUser
70 */
71 public function __construct(DatabaseConnection $databaseConnection = null, TypoScriptFrontendController $frontendController = null, FrontendBackendUserAuthentication $backendUser = null)
72 {
73 $this->databaseConnection = $databaseConnection ?: $GLOBALS['TYPO3_DB'];
74 $this->frontendController = $frontendController ?: $GLOBALS['TSFE'];
75 $this->backendUser = $backendUser ?: $GLOBALS['BE_USER'];
76 $this->cObj = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
77 $this->cObj->start(array());
78 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
79 }
80
81 /**
82 * Generates the "edit panels" which can be shown for a page or records on a page when the Admin Panel is enabled for a backend users surfing the frontend.
83 * With the "edit panel" the user will see buttons with links to editing, moving, hiding, deleting the element
84 * This function is used for the cObject EDITPANEL and the stdWrap property ".editPanel"
85 *
86 * @param string $content A content string containing the content related to the edit panel. For cObject "EDITPANEL" this is empty but not so for the stdWrap property. The edit panel is appended to this string and returned.
87 * @param array $conf TypoScript configuration properties for the editPanel
88 * @param string $currentRecord The "table:uid" of the record being shown. If empty string then $this->currentRecord is used. For new records (set by $conf['newRecordFromTable']) it's auto-generated to "[tablename]:NEW
89 * @param array $dataArr Alternative data array to use. Default is $this->data
90 * @param string $table
91 * @param array $allow
92 * @param int $newUID
93 * @param array $hiddenFields
94 * @return string The input content string with the editPanel appended. This function returns only an edit panel appended to the content string if a backend user is logged in (and has the correct permissions). Otherwise the content string is directly returned.
95 */
96 public function editPanel($content, array $conf, $currentRecord = '', array $dataArr = array(), $table = '', array $allow = array(), $newUID = 0, array $hiddenFields = array())
97 {
98 $hiddenFieldString = $command = '';
99
100 // Special content is about to be shown, so the cache must be disabled.
101 $this->frontendController->set_no_cache('Frontend edit panel is shown', true);
102
103 $formName = 'TSFE_EDIT_FORM_' . substr($this->frontendController->uniqueHash(), 0, 4);
104 $formTag = '<form name="' . $formName . '" id ="' . $formName . '" action="' . htmlspecialchars(GeneralUtility::getIndpEnv('REQUEST_URI')) . '" method="post" enctype="multipart/form-data" onsubmit="return TBE_EDITOR.checkSubmit(1);">';
105 $sortField = $GLOBALS['TCA'][$table]['ctrl']['sortby'];
106 $labelField = $GLOBALS['TCA'][$table]['ctrl']['label'];
107 $hideField = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'];
108
109 $panel = '';
110 if (isset($allow['toolbar']) && $this->backendUser->adminPanel instanceof AdminPanelView) {
111 $panel .= $this->backendUser->adminPanel->ext_makeToolBar();
112 }
113 if (isset($allow['edit'])) {
114 $icon = '<span title="' . $this->backendUser->extGetLL('p_editRecord') . '">' . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() . '</span>';
115 $panel .= $this->editPanelLinkWrap($icon, $formName, 'edit', $dataArr['_LOCALIZED_UID'] ? $table . ':' . $dataArr['_LOCALIZED_UID'] : $currentRecord);
116 }
117 // Hiding in workspaces because implementation is incomplete
118 if (isset($allow['move']) && $sortField && $this->backendUser->workspace === 0) {
119 $icon = '<span title="' . $this->backendUser->extGetLL('p_moveUp') . '">' . $this->iconFactory->getIcon('actions-move-up', Icon::SIZE_SMALL)->render() . '</span>';
120 $panel .= $this->editPanelLinkWrap($icon, $formName, 'up');
121 $icon = '<span title="' . $this->backendUser->extGetLL('p_moveDown') . '">' . $this->iconFactory->getIcon('actions-move-down', Icon::SIZE_SMALL)->render() . '</span>';
122 $panel .= $this->editPanelLinkWrap($icon, $formName, 'down');
123 }
124 // Hiding in workspaces because implementation is incomplete
125 // Hiding for localizations because it is unknown what should be the function in that case
126 if (isset($allow['hide']) && $hideField && $this->backendUser->workspace === 0 && !$dataArr['_LOCALIZED_UID']) {
127 if ($dataArr[$hideField]) {
128 $icon = $this->iconFactory->getIcon('actions-edit-unhide', Icon::SIZE_SMALL)->render();
129 $panel .= $this->editPanelLinkWrap($icon, $formName, 'unhide');
130 } else {
131 $icon = $this->iconFactory->getIcon('actions-edit-hide', Icon::SIZE_SMALL)->render();
132 $panel .= $this->editPanelLinkWrap($icon, $formName, 'hide', '', $this->backendUser->extGetLL('p_hideConfirm'));
133 }
134 }
135 if (isset($allow['new'])) {
136 if ($table === 'pages') {
137 $icon = '<span title="' . $this->backendUser->extGetLL('p_newSubpage') . '">'
138 . $this->iconFactory->getIcon('actions-page-new', Icon::SIZE_SMALL)->render()
139 . '</span>';
140 $panel .= $this->editPanelLinkWrap($icon, $formName, 'new', $currentRecord, '');
141 } else {
142 $icon = '<span title="' . $this->backendUser->extGetLL('p_newRecordAfter') . '">'
143 . $this->iconFactory->getIcon('actions-document-new', Icon::SIZE_SMALL)->render()
144 . '</span>';
145 $panel .= $this->editPanelLinkWrap($icon, $formName, 'new', $currentRecord, '', $newUID);
146 }
147 }
148 // Hiding in workspaces because implementation is incomplete
149 // Hiding for localizations because it is unknown what should be the function in that case
150 if (isset($allow['delete']) && $this->backendUser->workspace === 0 && !$dataArr['_LOCALIZED_UID']) {
151 $icon = '<span title="' . $this->backendUser->extGetLL('p_delete') . '">'
152 . $this->iconFactory->getIcon('actions-edit-delete', Icon::SIZE_SMALL)->render()
153 . '</span>';
154 $panel .= $this->editPanelLinkWrap($icon, $formName, 'delete', '', $this->backendUser->extGetLL('p_deleteConfirm'));
155 }
156 // Final
157 $labelTxt = $this->cObj->stdWrap($conf['label'], $conf['label.']);
158 foreach ((array)$hiddenFields as $name => $value) {
159 $hiddenFieldString .= '<input type="hidden" name="TSFE_EDIT[' . htmlspecialchars($name) . ']" value="' . htmlspecialchars($value) . '"/>' . LF;
160 }
161
162 $panel = '<!-- BE_USER Edit Panel: -->
163 ' . $formTag . $hiddenFieldString . '
164 <input type="hidden" name="TSFE_EDIT[cmd]" value="" />
165 <input type="hidden" name="TSFE_EDIT[record]" value="' . $currentRecord . '" />
166 <div class="typo3-editPanel">'
167 . '<div class="btn-group">'
168 . $panel
169 . '</div>' .
170 ($labelTxt ? '<div class="typo3-editPanel-label">' . sprintf($labelTxt, htmlspecialchars(GeneralUtility::fixed_lgd_cs($dataArr[$labelField], 50))) . '</div>' : '') . '
171 </div>
172 </form>';
173
174 // Wrap the panel
175 if ($conf['innerWrap']) {
176 $panel = $this->cObj->wrap($panel, $conf['innerWrap']);
177 }
178 if ($conf['innerWrap.']) {
179 $panel = $this->cObj->stdWrap($panel, $conf['innerWrap.']);
180 }
181
182 // Wrap the complete panel
183 if ($conf['outerWrap']) {
184 $panel = $this->cObj->wrap($panel, $conf['outerWrap']);
185 }
186 if ($conf['outerWrap.']) {
187 $panel = $this->cObj->stdWrap($panel, $conf['outerWrap.']);
188 }
189 if ($conf['printBeforeContent']) {
190 $finalOut = $panel . $content;
191 } else {
192 $finalOut = $content . $panel;
193 }
194
195 $hidden = $this->isDisabled($table, $dataArr) ? ' typo3-feedit-element-hidden' : '';
196 $outerWrapConfig = isset($conf['stdWrap.'])
197 ? $conf['stdWrap.']
198 : array('wrap' => '<div class="typo3-feedit-element' . $hidden . '">|</div>');
199 $finalOut = $this->cObj->stdWrap($finalOut, $outerWrapConfig);
200
201 return $finalOut;
202 }
203
204 /**
205 * Adds an edit icon to the content string. The edit icon links to EditDocumentController with proper parameters for editing the table/fields of the context.
206 * This implements TYPO3 context sensitive editing facilities. Only backend users will have access (if properly configured as well).
207 *
208 * @param string $content The content to which the edit icons should be appended
209 * @param string $params The parameters defining which table and fields to edit. Syntax is [tablename]:[fieldname],[fieldname],[fieldname],... OR [fieldname],[fieldname],[fieldname],... (basically "[tablename]:" is optional, default table is the one of the "current record" used in the function). The fieldlist is sent as "&columnsOnly=" parameter to EditDocumentController
210 * @param array $conf TypoScript properties for configuring the edit icons.
211 * @param string $currentRecord The "table:uid" of the record being shown. If empty string then $this->currentRecord is used. For new records (set by $conf['newRecordFromTable']) it's auto-generated to "[tablename]:NEW
212 * @param array $dataArr Alternative data array to use. Default is $this->data
213 * @param string $addUrlParamStr Additional URL parameters for the link pointing to EditDocumentController
214 * @param string $table
215 * @param int $editUid
216 * @param string $fieldList
217 * @return string The input content string, possibly with edit icons added (not necessarily in the end but just after the last string of normal content.
218 */
219 public function editIcons($content, $params, array $conf = array(), $currentRecord = '', array $dataArr = array(), $addUrlParamStr = '', $table, $editUid, $fieldList)
220 {
221 // Special content is about to be shown, so the cache must be disabled.
222 $this->frontendController->set_no_cache('Display frontend edit icons', true);
223 $iconTitle = $this->cObj->stdWrap($conf['iconTitle'], $conf['iconTitle.']);
224 $iconImg = '<span title="' . htmlspecialchars($iconTitle, ENT_COMPAT, 'UTF-8', false) . '" class="frontEndEditIcons" style="' . ($conf['styleAttribute'] ? htmlspecialchars($conf['styleAttribute']) : '') . '">'
225 . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render()
226 . '</span>';
227 $nV = GeneralUtility::_GP('ADMCMD_view') ? 1 : 0;
228
229 $url = BackendUtility::getModuleUrl(
230 'record_edit',
231 array(
232 'edit[' . $table . '][' . $editUid . ']' => 'edit',
233 'columnsOnly' => $fieldList,
234 'noView' => $nV,
235 'feEdit' => 1
236 )
237 ) . $addUrlParamStr;
238 $icon = $this->editPanelLinkWrap_doWrap($iconImg, $url, 'content-link');
239 if ($conf['beforeLastTag'] < 0) {
240 $content = $icon . $content;
241 } elseif ($conf['beforeLastTag'] > 0) {
242 $cBuf = rtrim($content);
243 $secureCount = 30;
244 while ($secureCount && substr($cBuf, -1) == '>' && substr($cBuf, -4) != '</a>') {
245 $cBuf = rtrim(preg_replace('/<[^<]*>$/', '', $cBuf));
246 $secureCount--;
247 }
248 $content = strlen($cBuf) && $secureCount ? substr($content, 0, strlen($cBuf)) . $icon . substr($content, strlen($cBuf)) : ($content = $icon . $content);
249 } else {
250 $content .= $icon;
251 }
252 return $content;
253 }
254
255 /**
256 * Helper function for editPanel() which wraps icons in the panel in a link with the action of the panel.
257 * The links are for some of them not simple hyperlinks but onclick-actions which submits a little form which the panel is wrapped in.
258 *
259 * @param string $string The string to wrap in a link, typ. and image used as button in the edit panel.
260 * @param string $formName The name of the form wrapping the edit panel.
261 * @param string $cmd The command of the link. There is a predefined list available: edit, new, up, down etc.
262 * @param string $currentRecord The "table:uid" of the record being processed by the panel.
263 * @param string $confirm Text string with confirmation message; If set a confirm box will be displayed before carrying out the action (if Yes is pressed)
264 * @param int|string $nPid "New pid" - for new records
265 * @return string A <a> tag wrapped string.
266 */
267 protected function editPanelLinkWrap($string, $formName, $cmd, $currentRecord = '', $confirm = '', $nPid = '')
268 {
269 $nV = GeneralUtility::_GP('ADMCMD_view') ? 1 : 0;
270 if ($cmd == 'edit') {
271 $rParts = explode(':', $currentRecord);
272 $out = $this->editPanelLinkWrap_doWrap($string, BackendUtility::getModuleUrl('record_edit', array('edit[' . $rParts[0] . '][' . $rParts[1] . ']' => 'edit', 'noView' => $nV, 'feEdit' => 1)), $currentRecord);
273 } elseif ($cmd == 'new') {
274 $rParts = explode(':', $currentRecord);
275 if ($rParts[0] == 'pages') {
276 $out = $this->editPanelLinkWrap_doWrap($string, BackendUtility::getModuleUrl('db_new', ['id' => $rParts[1], 'pagesOnly' => 1]), $currentRecord);
277 } else {
278 if (!(int)$nPid) {
279 $nPid = MathUtility::canBeInterpretedAsInteger($rParts[1]) ? -$rParts[1] : $this->frontendController->id;
280 }
281 $out = $this->editPanelLinkWrap_doWrap($string, BackendUtility::getModuleUrl('record_edit', array('edit[' . $rParts[0] . '][' . $nPid . ']' => 'new', 'noView' => $nV)), $currentRecord);
282 }
283 } else {
284 if ($confirm && $this->backendUser->jsConfirmation(JsConfirmation::FE_EDIT)) {
285 // Gets htmlspecialchared later
286 $cf1 = 'if (confirm(' . GeneralUtility::quoteJSvalue($confirm, true) . ')) {';
287 $cf2 = '}';
288 } else {
289 $cf1 = ($cf2 = '');
290 }
291 $out = '<a href="#" class="btn btn-default btn-sm" onclick="' . htmlspecialchars(($cf1 . 'document.' . $formName . '[\'TSFE_EDIT[cmd]\'].value=\'' . $cmd . '\'; document.' . $formName . '.submit();' . $cf2 . ' return false;')) . '">' . $string . '</a>';
292 }
293 return $out;
294 }
295
296 /**
297 * Creates a link to a script (eg. EditDocumentController or NewRecordController) which either opens in the current frame OR in a pop-up window.
298 *
299 * @param string $string The string to wrap in a link, typ. and image used as button in the edit panel.
300 * @param string $url The URL of the link. Should be absolute if supposed to work with <base> path set.
301 * @param string $additionalClasses Additional CSS classes
302 * @return string A <a> tag wrapped string.
303 * @see editPanelLinkWrap()
304 */
305 protected function editPanelLinkWrap_doWrap($string, $url, $additionalClasses = '')
306 {
307 $onclick = 'vHWin=window.open(' . GeneralUtility::quoteJSvalue($url . '&returnUrl=' . ExtensionManagementUtility::extRelPath('backend') . 'Resources/Private/Templates/Close.html') . ',\'FEquickEditWindow\',\'width=690,height=500,status=0,menubar=0,scrollbars=1,resizable=1\');vHWin.focus();return false;';
308 return '<a href="#" class="btn btn-default btn-sm ' . htmlspecialchars($additionalClasses) . '" onclick="' . htmlspecialchars($onclick) . '" class="frontEndEditIconLinks">' . $string . '</a>';
309 }
310
311 /**
312 * Returns TRUE if the input table/row would be hidden in the frontend, according to the current time and simulate user group
313 *
314 * @param string $table The table name
315 * @param array $row The data record
316 * @return bool
317 */
318 protected function isDisabled($table, array $row)
319 {
320 $status = false;
321 if (
322 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'] &&
323 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled']] ||
324 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group'] &&
325 $this->frontendController->simUserGroup &&
326 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group']] == $this->frontendController->simUserGroup ||
327 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['starttime'] &&
328 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['starttime']] > $GLOBALS['EXEC_TIME'] ||
329 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['endtime'] &&
330 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['endtime']] &&
331 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['endtime']] < $GLOBALS['EXEC_TIME']
332 ) {
333 $status = true;
334 }
335
336 return $status;
337 }
338 }