[BUGFIX] Untrusted GP data is unserialized in wizard_colorpicker.php and view_help.php
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_clipboard.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains class for TYPO3 clipboard for records and files
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 * XHTML compliant
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35
36
37 /**
38 * TYPO3 clipboard for records and files
39 *
40 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
41 * @package TYPO3
42 * @subpackage t3lib
43 */
44 class t3lib_clipboard {
45 var $numberTabs = 3;
46
47 /**
48 * Clipboard data kept here
49 *
50 * Keys:
51 * 'normal'
52 * 'tab_[x]' where x is >=1 and denotes the pad-number
53 * \ 'mode' : 'copy' means copy-mode, default = moving ('cut')
54 * \ 'el' : Array of elements:
55 * DB: keys = '[tablename]|[uid]' eg. 'tt_content:123'
56 * DB: values = 1 (basically insignificant)
57 * FILE: keys = '_FILE|[shortmd5 of path]' eg. '_FILE|9ebc7e5c74'
58 * FILE: values = The full filepath, eg. '/www/htdocs/typo3/32/dummy/fileadmin/sem1_3_examples/alternative_index.php' or 'C:/www/htdocs/typo3/32/dummy/fileadmin/sem1_3_examples/alternative_index.php'
59 *
60 * 'current' pointer to current tab (among the above...)
61 * '_setThumb' boolean: If set, file thumbnails are shown.
62 *
63 * The virtual tablename '_FILE' will always indicate files/folders. When checking for elements from eg. 'all tables' (by using an empty string) '_FILE' entries are excluded (so in effect only DB elements are counted)
64 *
65 */
66 var $clipData = array();
67
68 var $changed = 0;
69 var $current = '';
70 var $backPath = '';
71 var $lockToNormal = 0;
72 var $fileMode = 0; // If set, clipboard is displaying files.
73
74
75 /*****************************************
76 *
77 * Initialize
78 *
79 ****************************************/
80
81 /**
82 * Initialize the clipboard from the be_user session
83 *
84 * @return void
85 */
86 function initializeClipboard() {
87 $this->backPath = $GLOBALS['BACK_PATH'];
88
89 // Get data
90 $clipData = $GLOBALS['BE_USER']->getModuleData('clipboard', $GLOBALS['BE_USER']->getTSConfigVal('options.saveClipboard') ? '' : 'ses');
91
92 // NumberTabs
93 $clNP = $GLOBALS['BE_USER']->getTSConfigVal('options.clipboardNumberPads');
94 if (t3lib_utility_Math::canBeInterpretedAsInteger($clNP) && $clNP >= 0) {
95 $this->numberTabs = t3lib_utility_Math::forceIntegerInRange($clNP, 0, 20);
96 }
97
98 // Resets/reinstates the clipboard pads
99 $this->clipData['normal'] = is_array($clipData['normal']) ? $clipData['normal'] : array();
100 for ($a = 1; $a <= $this->numberTabs; $a++) {
101 $this->clipData['tab_' . $a] = is_array($clipData['tab_' . $a]) ? $clipData['tab_' . $a] : array();
102 }
103
104 // Setting the current pad pointer ($this->current) and _setThumb (which determines whether or not do show file thumbnails)
105 $this->clipData['current'] = $this->current = isset($this->clipData[$clipData['current']]) ? $clipData['current'] : 'normal';
106 $this->clipData['_setThumb'] = $clipData['_setThumb'];
107 }
108
109 /**
110 * Call this method after initialization if you want to lock the clipboard to operate on the normal pad only. Trying to switch pad through ->setCmd will not work
111 * This is used by the clickmenu since it only allows operation on single elements at a time (that is the "normal" pad)
112 *
113 * @return void
114 */
115 function lockToNormal() {
116 $this->lockToNormal = 1;
117 $this->current = 'normal';
118 }
119
120 /**
121 * The array $cmd may hold various keys which notes some action to take.
122 * Normally perform only one action at a time.
123 * In scripts like db_list.php / file_list.php the GET-var CB is used to control the clipboard.
124 *
125 * Selecting / Deselecting elements
126 * Array $cmd['el'] has keys = element-ident, value = element value (see description of clipData array in header)
127 * Selecting elements for 'copy' should be done by simultaneously setting setCopyMode.
128 *
129 * @param array Array of actions, see function description
130 * @return void
131 */
132 function setCmd($cmd) {
133 if (is_array($cmd['el'])) {
134 foreach ($cmd['el'] as $k => $v) {
135 if ($this->current == 'normal') {
136 unset($this->clipData['normal']);
137 }
138 if ($v) {
139 $this->clipData[$this->current]['el'][$k] = $v;
140 } else {
141 $this->removeElement($k);
142 }
143 $this->changed = 1;
144 }
145 }
146 // Change clipboard pad (if not locked to normal)
147 if ($cmd['setP']) {
148 $this->setCurrentPad($cmd['setP']);
149 }
150 // Remove element (value = item ident: DB; '[tablename]|[uid]' FILE: '_FILE|[shortmd5 hash of path]'
151 if ($cmd['remove']) {
152 $this->removeElement($cmd['remove']);
153 $this->changed = 1;
154 }
155 // Remove all on current pad (value = pad-ident)
156 if ($cmd['removeAll']) {
157 $this->clipData[$cmd['removeAll']] = array();
158 $this->changed = 1;
159 }
160 // Set copy mode of the tab
161 if (isset($cmd['setCopyMode'])) {
162 $this->clipData[$this->current]['mode'] = $this->isElements() ? ($cmd['setCopyMode'] ? 'copy' : '') : '';
163 $this->changed = 1;
164 }
165 // Toggle thumbnail display for files on/off
166 if (isset($cmd['setThumb'])) {
167 $this->clipData['_setThumb'] = $cmd['setThumb'];
168 $this->changed = 1;
169 }
170 }
171
172 /**
173 * Setting the current pad on clipboard
174 *
175 * @param string Key in the array $this->clipData
176 * @return void
177 */
178 function setCurrentPad($padIdent) {
179 // Change clipboard pad (if not locked to normal)
180 if (!$this->lockToNormal && $this->current != $padIdent) {
181 if (isset($this->clipData[$padIdent])) {
182 $this->clipData['current'] = $this->current = $padIdent;
183 }
184 if ($this->current != 'normal' || !$this->isElements()) {
185 $this->clipData[$this->current]['mode'] = '';
186 } // Setting mode to default (move) if no items on it or if not 'normal'
187 $this->changed = 1;
188 }
189 }
190
191 /**
192 * Call this after initialization and setCmd in order to save the clipboard to the user session.
193 * The function will check if the internal flag ->changed has been set and if so, save the clipboard. Else not.
194 *
195 * @return void
196 */
197 function endClipboard() {
198 if ($this->changed) {
199 $this->saveClipboard();
200 }
201 $this->changed = 0;
202 }
203
204 /**
205 * Cleans up an incoming element array $CBarr (Array selecting/deselecting elements)
206 *
207 * @param array Element array from outside ("key" => "selected/deselected")
208 * @param string $table is the 'table which is allowed'. Must be set.
209 * @param boolean $removeDeselected can be set in order to remove entries which are marked for deselection.
210 * @return array Processed input $CBarr
211 */
212 function cleanUpCBC($CBarr, $table, $removeDeselected = 0) {
213 if (is_array($CBarr)) {
214 foreach ($CBarr as $k => $v) {
215 $p = explode('|', $k);
216 if ((string) $p[0] != (string) $table || ($removeDeselected && !$v)) {
217 unset($CBarr[$k]);
218 }
219 }
220 }
221 return $CBarr;
222 }
223
224
225 /*****************************************
226 *
227 * Clipboard HTML renderings
228 *
229 ****************************************/
230
231 /**
232 * Prints the clipboard
233 *
234 * @return string HTML output
235 */
236 function printClipboard() {
237 $out = array();
238 $elCount = count($this->elFromTable($this->fileMode ? '_FILE' : ''));
239
240 // Upper header
241 $out[] = '
242 <tr class="t3-row-header">
243 <td colspan="3">' . t3lib_BEfunc::wrapInHelp('xMOD_csh_corebe', 'list_clipboard', $this->clLabel('clipboard', 'buttons')) . '</td>
244 </tr>';
245
246 // Button/menu header:
247 $thumb_url = t3lib_div::linkThisScript(array('CB' => array('setThumb' => $this->clipData['_setThumb'] ? 0 : 1)));
248 $rmall_url = t3lib_div::linkThisScript(array('CB' => array('removeAll' => $this->current)));
249
250 // Copymode Selector menu
251 $copymode_url = t3lib_div::linkThisScript();
252 $moveLabel = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.php:moveElements'));
253 $copyLabel = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.php:copyElements'));
254 $opt = array();
255 $opt[] = '<option style="padding-left: 20px; background-image: url(\'' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/clip_cut.gif', '', 1) . '\'); background-repeat: no-repeat;" value="" ' . (($this->currentMode() == 'copy') ? '' : 'selected="selected"') . '>' . $moveLabel . '</option>';
256 $opt[] = '<option style="padding-left: 20px; background-image: url(\'' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/clip_copy.gif', '', 1) . '\'); background-repeat: no-repeat;" value="1" ' . (($this->currentMode() == 'copy') ? 'selected="selected"' : '') . '>' . $copyLabel . '</option>';
257
258 $copymode_selector = ' <select name="CB[setCopyMode]" onchange="this.form.method=\'POST\'; this.form.action=\'' . htmlspecialchars($copymode_url . '&CB[setCopyMode]=') . '\'+(this.options[this.selectedIndex].value); this.form.submit(); return true;" >' . implode('', $opt) . '</select>';
259
260 // Selector menu + clear button
261 $opt = array();
262 $opt[] = '<option value="" selected="selected">' . $this->clLabel('menu', 'rm') . '</option>';
263 // Import / Export link:
264 if ($elCount && t3lib_extMgm::isLoaded('impexp')) {
265 $opt[] = '<option value="' . htmlspecialchars("window.location.href='" . $this->backPath . t3lib_extMgm::extRelPath('impexp') . 'app/index.php' . $this->exportClipElementParameters() . '\';') . '">' . $this->clLabel('export', 'rm') . '</option>';
266 }
267 // Edit:
268 if (!$this->fileMode && $elCount) {
269 $opt[] = '<option value="' . htmlspecialchars("window.location.href='" . $this->editUrl() . "&returnUrl='+top.rawurlencode(window.location.href);") . '">' . $this->clLabel('edit', 'rm') . '</option>';
270 }
271 // Delete:
272 if ($elCount) {
273 if ($GLOBALS['BE_USER']->jsConfirmation(4)) {
274 $js = "
275 if(confirm(" . $GLOBALS['LANG']->JScharCode(sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:mess.deleteClip'), $elCount)) . ")){
276 window.location.href='" . $this->deleteUrl(0, $this->fileMode ? 1 : 0) . "&redirect='+top.rawurlencode(window.location.href);
277 }
278 ";
279 } else {
280 $js = " window.location.href='" . $this->deleteUrl(0, $this->fileMode ? 1 : 0) . "&redirect='+top.rawurlencode(window.location.href); ";
281 }
282 $opt[] = '<option value="' . htmlspecialchars($js) . '">' . $this->clLabel('delete', 'rm') . '</option>';
283 }
284 $selector_menu = '<select name="_clipMenu" onchange="eval(this.options[this.selectedIndex].value);this.selectedIndex=0;">' . implode('', $opt) . '</select>';
285
286 $out[] = '
287 <tr class="typo3-clipboard-head">
288 <td nowrap="nowrap">' .
289 '<a href="' . htmlspecialchars($thumb_url) . '#clip_head">' .
290 '<img' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/thumb_' . ($this->clipData['_setThumb'] ? 's' : 'n') . '.gif', 'width="21" height="16"') . ' vspace="2" border="0" title="' . $this->clLabel('thumbmode_clip') . '" alt="" />' .
291 '</a>' .
292 '</td>
293 <td width="95%" nowrap="nowrap">' .
294 $copymode_selector . ' ' .
295 $selector_menu .
296 '</td>
297 <td>' .
298 '<a href="' . htmlspecialchars($rmall_url) . '#clip_head">' .
299 t3lib_iconWorks::getSpriteIcon('actions-document-close', array('title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:buttons.clear', TRUE))) .
300 '</a></td>
301 </tr>';
302
303
304 // Print header and content for the NORMAL tab:
305 $out[] = '
306 <tr class="bgColor5">
307 <td colspan="3"><a href="' . htmlspecialchars(t3lib_div::linkThisScript(array('CB' => array('setP' => 'normal')))) . '#clip_head">' .
308 t3lib_iconWorks::getSpriteIcon('actions-view-table-' . (($this->current == 'normal') ? 'collapse' : 'expand')) .
309 $this->padTitleWrap('Normal', 'normal') .
310 '</a></td>
311 </tr>';
312 if ($this->current == 'normal') {
313 $out = array_merge($out, $this->printContentFromTab('normal'));
314 }
315
316 // Print header and content for the NUMERIC tabs:
317 for ($a = 1; $a <= $this->numberTabs; $a++) {
318 $out[] = '
319 <tr class="bgColor5">
320 <td colspan="3"><a href="' . htmlspecialchars(t3lib_div::linkThisScript(array('CB' => array('setP' => 'tab_' . $a)))) . '#clip_head">' .
321 t3lib_iconWorks::getSpriteIcon('actions-view-table-' . (($this->current == 'tab_' . $a) ? 'collapse' : 'expand')) .
322 $this->padTitleWrap($this->clLabel('cliptabs') . $a, 'tab_' . $a) .
323 '</a></td>
324 </tr>';
325 if ($this->current == 'tab_' . $a) {
326 $out = array_merge($out, $this->printContentFromTab('tab_' . $a));
327 }
328 }
329
330 // Wrap accumulated rows in a table:
331 $output = '<a name="clip_head"></a>
332
333 <!--
334 TYPO3 Clipboard:
335 -->
336 <table cellpadding="0" cellspacing="1" border="0" width="290" id="typo3-clipboard">
337 ' . implode('', $out) . '
338 </table>';
339
340 // Wrap in form tag:
341 $output = '<form action="">' . $output . '</form>';
342
343 // Return the accumulated content:
344 return $output;
345 }
346
347 /**
348 * Print the content on a pad. Called from ->printClipboard()
349 *
350 * @param string Pad reference
351 * @return array Array with table rows for the clipboard.
352 * @access private
353 */
354 function printContentFromTab($pad) {
355 $lines = array();
356 if (is_array($this->clipData[$pad]['el'])) {
357 foreach ($this->clipData[$pad]['el'] as $k => $v) {
358 if ($v) {
359 list($table, $uid) = explode('|', $k);
360 $bgColClass = ($table == '_FILE' && $this->fileMode) || ($table != '_FILE' && !$this->fileMode) ? 'bgColor4-20' : 'bgColor4';
361
362 if ($table == '_FILE') { // Rendering files/directories on the clipboard:
363 if (file_exists($v) && t3lib_div::isAllowedAbsPath($v)) {
364 $fI = pathinfo($v);
365 $icon = is_dir($v) ? 'folder.gif' : t3lib_BEfunc::getFileIcon(strtolower($fI['extension']));
366 $size = ' (' . t3lib_div::formatSize(filesize($v)) . 'bytes)';
367 $icon = t3lib_iconWorks::getSpriteIconForFile(is_dir($v) ? 'folder' : strtolower($fI['extension']), array('style' => 'margin: 0 20px;', 'title' => htmlspecialchars($fI['basename'] . $size)));
368 $thumb = $this->clipData['_setThumb'] ? (t3lib_div::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fI['extension']) ? t3lib_BEfunc::getThumbNail($this->backPath . 'thumbs.php', $v, ' vspace="4"') : '') : '';
369
370 $lines[] = '
371 <tr>
372 <td class="' . $bgColClass . '">' . $icon . '</td>
373 <td class="' . $bgColClass . '" nowrap="nowrap" width="95%">&nbsp;' . $this->linkItemText(htmlspecialchars(t3lib_div::fixed_lgd_cs(basename($v), $GLOBALS['BE_USER']->uc['titleLen'])), $v) .
374 ($pad == 'normal' ? (' <strong>(' . ($this->clipData['normal']['mode'] == 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) . ')</strong>') : '') . '&nbsp;' . ($thumb ? '<br />' . $thumb : '') . '</td>
375 <td class="' . $bgColClass . '" align="center" nowrap="nowrap">' .
376 '<a href="#" onclick="' . htmlspecialchars('top.launchView(\'' . $v . '\', \'\'); return false;') . '">' . t3lib_iconWorks::getSpriteIcon('actions-document-info', array('title' => $this->clLabel('info', 'cm'))) . '</a>' .
377 '<a href="' . htmlspecialchars($this->removeUrl('_FILE', t3lib_div::shortmd5($v))) . '#clip_head">' . t3lib_iconWorks::getSpriteIcon('actions-selection-delete', array('title' => $this->clLabel('removeItem'))) . '</a>' .
378 '</td>
379 </tr>';
380 } else {
381 // If the file did not exist (or is illegal) then it is removed from the clipboard immediately:
382 unset($this->clipData[$pad]['el'][$k]);
383 $this->changed = 1;
384 }
385 } else { // Rendering records:
386 $rec = t3lib_BEfunc::getRecordWSOL($table, $uid);
387 if (is_array($rec)) {
388 $lines[] = '
389 <tr>
390 <td class="' . $bgColClass . '">' . $this->linkItemText(t3lib_iconWorks::getSpriteIconForRecord($table, $rec, array('style' => 'margin: 0 20px;', 'title' => htmlspecialchars(t3lib_BEfunc::getRecordIconAltText($rec, $table)))), $rec, $table) . '</td>
391 <td class="' . $bgColClass . '" nowrap="nowrap" width="95%">&nbsp;' . $this->linkItemText(htmlspecialchars(t3lib_div::fixed_lgd_cs(t3lib_BEfunc::getRecordTitle($table, $rec), $GLOBALS['BE_USER']->uc['titleLen'])), $rec, $table) .
392 ($pad == 'normal' ? (' <strong>(' . ($this->clipData['normal']['mode'] == 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) . ')</strong>') : '') . '&nbsp;</td>
393 <td class="' . $bgColClass . '" align="center" nowrap="nowrap">' .
394 '<a href="#" onclick="' . htmlspecialchars('top.launchView(\'' . $table . '\', \'' . intval($uid) . '\'); return false;') . '">' . t3lib_iconWorks::getSpriteIcon('actions-document-info', array('title' => $this->clLabel('info', 'cm'))) . '</a>' .
395 '<a href="' . htmlspecialchars($this->removeUrl($table, $uid)) . '#clip_head">' . t3lib_iconWorks::getSpriteIcon('actions-selection-delete', array('title' => $this->clLabel('removeItem'))) . '</a>' .
396 '</td>
397 </tr>';
398
399 $localizationData = $this->getLocalizations($table, $rec, $bgColClass, $pad);
400 if ($localizationData) {
401 $lines[] = $localizationData;
402 }
403
404 } else {
405 unset($this->clipData[$pad]['el'][$k]);
406 $this->changed = 1;
407 }
408 }
409 }
410 }
411 }
412 if (!count($lines)) {
413 $lines[] = '
414 <tr>
415 <td class="bgColor4"><img src="clear.gif" width="56" height="1" alt="" /></td>
416 <td colspan="2" class="bgColor4" nowrap="nowrap" width="95%">&nbsp;<em>(' . $this->clLabel('clipNoEl') . ')</em>&nbsp;</td>
417 </tr>';
418 }
419
420 $this->endClipboard();
421 return $lines;
422 }
423
424
425 /**
426 * Gets all localizations of the current record.
427 *
428 * @param string the table
429 * @param array the current record
430 * @return string HTML table rows
431 */
432 function getLocalizations($table, $parentRec, $bgColClass, $pad) {
433 $lines = array();
434 $tcaCtrl = $GLOBALS['TCA'][$table]['ctrl'];
435
436 if ($table != 'pages' && t3lib_BEfunc::isTableLocalizable($table) && !$tcaCtrl['transOrigPointerTable']) {
437 $where = array();
438 $where[] = $tcaCtrl['transOrigPointerField'] . '=' . intval($parentRec['uid']);
439 $where[] = $tcaCtrl['languageField'] . '!=0';
440
441 if (isset($tcaCtrl['delete']) && $tcaCtrl['delete']) {
442 $where[] = $tcaCtrl['delete'] . '=0';
443 }
444
445 if (isset($tcaCtrl['versioningWS']) && $tcaCtrl['versioningWS']) {
446 $where[] = 't3ver_wsid=' . $parentRec['t3ver_wsid'];
447 }
448
449 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', $table, implode(' AND ', $where));
450
451 if (is_array($rows)) {
452 $modeData = '';
453 if ($pad == 'normal') {
454 $mode = ($this->clipData['normal']['mode'] == 'copy' ? 'copy' : 'cut');
455 $modeData = ' <strong>(' . $this->clLabel($mode, 'cm') . ')</strong>';
456 }
457
458 foreach ($rows as $rec) {
459 $lines[] = '
460 <tr>
461 <td class="' . $bgColClass . '">' .
462 t3lib_iconWorks::getSpriteIconForRecord($table, $rec, array('style' => "margin-left: 38px;")) . '</td>
463 <td class="' . $bgColClass . '" nowrap="nowrap" width="95%">&nbsp;' . htmlspecialchars(
464 t3lib_div::fixed_lgd_cs(t3lib_BEfunc::getRecordTitle($table, $rec), $GLOBALS['BE_USER']->uc['titleLen'])) .
465 $modeData . '&nbsp;</td>
466 <td class="' . $bgColClass . '" align="center" nowrap="nowrap">&nbsp;</td>
467 </tr>';
468 }
469 }
470 }
471 return implode('', $lines);
472 }
473
474
475 /**
476 * Wraps title of pad in bold-tags and maybe the number of elements if any.
477 *
478 * @param string String (already htmlspecialchars()'ed)
479 * @param string Pad reference
480 * @return string HTML output (htmlspecialchar'ed content inside of tags.)
481 */
482 function padTitleWrap($str, $pad) {
483 $el = count($this->elFromTable($this->fileMode ? '_FILE' : '', $pad));
484 if ($el) {
485 return '<strong>' . $str . '</strong> (' . ($pad == 'normal' ? ($this->clipData['normal']['mode'] == 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) : htmlspecialchars($el)) . ')';
486 } else {
487 return $GLOBALS['TBE_TEMPLATE']->dfw($str);
488 }
489 }
490
491 /**
492 * Wraps the title of the items listed in link-tags. The items will link to the page/folder where they originate from
493 *
494 * @param string Title of element - must be htmlspecialchar'ed on beforehand.
495 * @param mixed If array, a record is expected. If string, its a path
496 * @param string Table name
497 * @return string
498 */
499 function linkItemText($str, $rec, $table = '') {
500 if (is_array($rec) && $table) {
501 if ($this->fileMode) {
502 $str = $GLOBALS['TBE_TEMPLATE']->dfw($str);
503 } else {
504 if (t3lib_extMgm::isLoaded('recordlist')) {
505 $str = '<a href="' . htmlspecialchars(
506 t3lib_BEfunc::getModuleUrl(
507 'web_list',
508 array('id' => $rec['pid']),
509 $this->backPath)
510 ) . '">' . $str . '</a>';
511 }
512 }
513 } elseif (file_exists($rec)) {
514 if (!$this->fileMode) {
515 $str = $GLOBALS['TBE_TEMPLATE']->dfw($str);
516 } else {
517 if (t3lib_extMgm::isLoaded('filelist')) {
518 $str = '<a href="' . htmlspecialchars(
519 $this->backPath . t3lib_extMgm::extRelPath('filelist') . 'mod1/file_list.php?id=' . dirname($rec)
520 ) . '">' . $str . '</a>';
521 }
522 }
523 }
524 return $str;
525 }
526
527 /**
528 * Returns the select-url for database elements
529 *
530 * @param string Table name
531 * @param integer Uid of record
532 * @param boolean If set, copymode will be enabled
533 * @param boolean If set, the link will deselect, otherwise select.
534 * @param array The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
535 * @return string URL linking to the current script but with the CB array set to select the element with table/uid
536 */
537 function selUrlDB($table, $uid, $copy = 0, $deselect = 0, $baseArray = array()) {
538 $CB = array('el' => array(rawurlencode($table . '|' . $uid) => $deselect ? 0 : 1));
539 if ($copy) {
540 $CB['setCopyMode'] = 1;
541 }
542 $baseArray['CB'] = $CB;
543 return t3lib_div::linkThisScript($baseArray);
544 }
545
546 /**
547 * Returns the select-url for files
548 *
549 * @param string Filepath
550 * @param boolean If set, copymode will be enabled
551 * @param boolean If set, the link will deselect, otherwise select.
552 * @param array The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
553 * @return string URL linking to the current script but with the CB array set to select the path
554 */
555 function selUrlFile($path, $copy = 0, $deselect = 0, $baseArray = array()) {
556 $CB = array('el' => array(rawurlencode('_FILE|' . t3lib_div::shortmd5($path)) => $deselect ? '' : $path));
557 if ($copy) {
558 $CB['setCopyMode'] = 1;
559 }
560 $baseArray['CB'] = $CB;
561 return t3lib_div::linkThisScript($baseArray);
562 }
563
564 /**
565 * pasteUrl of the element (database and file)
566 * For the meaning of $table and $uid, please read from ->makePasteCmdArray!!!
567 * The URL will point to tce_file or tce_db depending in $table
568 *
569 * @param string Tablename (_FILE for files)
570 * @param mixed "destination": can be positive or negative indicating how the paste is done (paste into / paste after)
571 * @param boolean If set, then the redirect URL will point back to the current script, but with CB reset.
572 * @return string
573 */
574 function pasteUrl($table, $uid, $setRedirect = 1) {
575 $rU = $this->backPath . ($table == '_FILE' ? 'tce_file.php' : 'tce_db.php') . '?' .
576 ($setRedirect ? 'redirect=' . rawurlencode(t3lib_div::linkThisScript(array('CB' => ''))) : '') .
577 '&vC=' . $GLOBALS['BE_USER']->veriCode() .
578 '&prErr=1&uPT=1' .
579 '&CB[paste]=' . rawurlencode($table . '|' . $uid) .
580 '&CB[pad]=' . $this->current .
581 t3lib_BEfunc::getUrlToken('tceAction');
582 return $rU;
583 }
584
585 /**
586 * deleteUrl for current pad
587 *
588 * @param boolean If set, then the redirect URL will point back to the current script, but with CB reset.
589 * @param boolean If set, then the URL will link to the tce_file.php script in the typo3/ dir.
590 * @return string
591 */
592 function deleteUrl($setRedirect = 1, $file = 0) {
593 $rU = $this->backPath . ($file ? 'tce_file.php' : 'tce_db.php') . '?' .
594 ($setRedirect ? 'redirect=' . rawurlencode(t3lib_div::linkThisScript(array('CB' => ''))) : '') .
595 '&vC=' . $GLOBALS['BE_USER']->veriCode() .
596 '&prErr=1&uPT=1' .
597 '&CB[delete]=1' .
598 '&CB[pad]=' . $this->current .
599 t3lib_BEfunc::getUrlToken('tceAction');
600 return $rU;
601 }
602
603 /**
604 * editUrl of all current elements
605 * ONLY database
606 * Links to alt_doc.php
607 *
608 * @return string The URL to alt_doc.php with parameters.
609 */
610 function editUrl() {
611 $elements = $this->elFromTable(''); // all records
612 $editCMDArray = array();
613 foreach ($elements as $tP => $value) {
614 list($table, $uid) = explode('|', $tP);
615 $editCMDArray[] = '&edit[' . $table . '][' . $uid . ']=edit';
616 }
617
618 $rU = $this->backPath . 'alt_doc.php?' . implode('', $editCMDArray);
619 return $rU;
620 }
621
622 /**
623 * Returns the remove-url (file and db)
624 * for file $table='_FILE' and $uid = shortmd5 hash of path
625 *
626 * @param string Tablename
627 * @param string uid integer/shortmd5 hash
628 * @return string URL
629 */
630 function removeUrl($table, $uid) {
631 return t3lib_div::linkThisScript(array('CB' => array('remove' => $table . '|' . $uid)));
632 }
633
634 /**
635 * Returns confirm JavaScript message
636 *
637 * @param string Table name
638 * @param mixed For records its an array, for files its a string (path)
639 * @param string Type-code
640 * @param array Array of selected elements
641 * @return string JavaScript "confirm" message
642 */
643 function confirmMsg($table, $rec, $type, $clElements) {
644 if ($GLOBALS['BE_USER']->jsConfirmation(2)) {
645 $labelKey = 'LLL:EXT:lang/locallang_core.php:mess.' . ($this->currentMode() == 'copy' ? 'copy' : 'move') . ($this->current == 'normal' ? '' : 'cb') . '_' . $type;
646 $msg = $GLOBALS['LANG']->sL($labelKey);
647
648 if ($table == '_FILE') {
649 $thisRecTitle = basename($rec);
650 if ($this->current == 'normal') {
651 $selItem = reset($clElements);
652 $selRecTitle = basename($selItem);
653 } else {
654 $selRecTitle = count($clElements);
655 }
656 } else {
657 $thisRecTitle = (
658 $table == 'pages' && !is_array($rec) ?
659 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] :
660 t3lib_BEfunc::getRecordTitle($table, $rec)
661 );
662
663 if ($this->current == 'normal') {
664 $selItem = $this->getSelectedRecord();
665 $selRecTitle = $selItem['_RECORD_TITLE'];
666 } else {
667 $selRecTitle = count($clElements);
668 }
669 }
670
671 // Message:
672 $conf = 'confirm(' . $GLOBALS['LANG']->JScharCode(sprintf(
673 $msg,
674 t3lib_div::fixed_lgd_cs($selRecTitle, 30),
675 t3lib_div::fixed_lgd_cs($thisRecTitle, 30)
676 )) . ')';
677 } else {
678 $conf = '';
679 }
680 return $conf;
681 }
682
683 /**
684 * Clipboard label - getting from "EXT:lang/locallang_core.php:"
685 *
686 * @param string Label Key
687 * @param string Alternative key to "labels"
688 * @return string
689 */
690 function clLabel($key, $Akey = 'labels') {
691 return htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:' . $Akey . '.' . $key));
692 }
693
694 /**
695 * Creates GET parameters for linking to the export module.
696 *
697 * @return string GET parameters for current clipboard content to be exported.
698 */
699 function exportClipElementParameters() {
700
701 // Init:
702 $pad = $this->current;
703 $params = array();
704 $params[] = 'tx_impexp[action]=export';
705
706 // Traverse items:
707 if (is_array($this->clipData[$pad]['el'])) {
708 foreach ($this->clipData[$pad]['el'] as $k => $v) {
709 if ($v) {
710 list($table, $uid) = explode('|', $k);
711
712 if ($table == '_FILE') { // Rendering files/directories on the clipboard:
713 if (file_exists($v) && t3lib_div::isAllowedAbsPath($v)) {
714 $params[] = 'tx_impexp[' . (is_dir($v) ? 'dir' : 'file') . '][]=' . rawurlencode($v);
715 }
716 } else { // Rendering records:
717 $rec = t3lib_BEfunc::getRecord($table, $uid);
718 if (is_array($rec)) {
719 $params[] = 'tx_impexp[record][]=' . rawurlencode($table . ':' . $uid);
720 }
721 }
722 }
723 }
724 }
725
726 return '?' . implode('&', $params);
727 }
728
729
730 /*****************************************
731 *
732 * Helper functions
733 *
734 ****************************************/
735
736 /**
737 * Removes element on clipboard
738 *
739 * @param string Key of element in ->clipData array
740 * @return void
741 */
742 function removeElement($el) {
743 unset($this->clipData[$this->current]['el'][$el]);
744 $this->changed = 1;
745 }
746
747 /**
748 * Saves the clipboard, no questions asked.
749 * Use ->endClipboard normally (as it checks if changes has been done so saving is necessary)
750 *
751 * @return void
752 * @access private
753 */
754 function saveClipboard() {
755 $GLOBALS['BE_USER']->pushModuleData('clipboard', $this->clipData);
756 }
757
758 /**
759 * Returns the current mode, 'copy' or 'cut'
760 *
761 * @return string "copy" or "cut"
762 */
763 function currentMode() {
764 return $this->clipData[$this->current]['mode'] == 'copy' ? 'copy' : 'cut';
765 }
766
767 /**
768 * This traverses the elements on the current clipboard pane
769 * and unsets elements which does not exist anymore or are disabled.
770 *
771 * @return void
772 */
773 function cleanCurrent() {
774 if (is_array($this->clipData[$this->current]['el'])) {
775 foreach ($this->clipData[$this->current]['el'] as $k => $v) {
776 list($table, $uid) = explode('|', $k);
777 if ($table != '_FILE') {
778 if (!$v || !is_array(t3lib_BEfunc::getRecord($table, $uid, 'uid'))) {
779 unset($this->clipData[$this->current]['el'][$k]);
780 $this->changed = 1;
781 }
782 } else {
783 if (!$v || !file_exists($v)) {
784 unset($this->clipData[$this->current]['el'][$k]);
785 $this->changed = 1;
786 }
787 }
788 }
789 }
790 }
791
792 /**
793 * Counts the number of elements from the table $matchTable. If $matchTable is blank, all tables (except '_FILE' of course) is counted.
794 *
795 * @param string Table to match/count for.
796 * @param string $pad can optionally be used to set another pad than the current.
797 * @return array Array with keys from the CB.
798 */
799 function elFromTable($matchTable = '', $pad = '') {
800 $pad = $pad ? $pad : $this->current;
801 $list = array();
802 if (is_array($this->clipData[$pad]['el'])) {
803 foreach ($this->clipData[$pad]['el'] as $k => $v) {
804 if ($v) {
805 list($table, $uid) = explode('|', $k);
806 if ($table != '_FILE') {
807 if ((!$matchTable || (string) $table == (string) $matchTable) && $GLOBALS['TCA'][$table]) {
808 $list[$k] = ($pad == 'normal' ? $v : $uid);
809 }
810 } else {
811 if ((string) $table == (string) $matchTable) {
812 $list[$k] = $v;
813 }
814 }
815 }
816 }
817 }
818 return $list;
819 }
820
821 /**
822 * Verifies if the item $table/$uid is on the current pad.
823 * If the pad is "normal", the mode value is returned if the element existed. Thus you'll know if the item was copy or cut moded...
824 *
825 * @param string Table name, (_FILE for files...)
826 * @param integer Element uid (path for files)
827 * @return string
828 */
829 function isSelected($table, $uid) {
830 $k = $table . '|' . $uid;
831 return $this->clipData[$this->current]['el'][$k] ? ($this->current == 'normal' ? $this->currentMode() : 1) : '';
832 }
833
834 /**
835 * Returns item record $table,$uid if selected on current clipboard
836 * If table and uid is blank, the first element is returned.
837 * Makes sense only for DB records - not files!
838 *
839 * @param string Table name
840 * @param integer Element uid
841 * @return array Element record with extra field _RECORD_TITLE set to the title of the record...
842 */
843 function getSelectedRecord($table = '', $uid = '') {
844 if (!$table && !$uid) {
845 $elArr = $this->elFromTable('');
846 reset($elArr);
847 list($table, $uid) = explode('|', key($elArr));
848 }
849 if ($this->isSelected($table, $uid)) {
850 $selRec = t3lib_BEfunc::getRecordWSOL($table, $uid);
851 $selRec['_RECORD_TITLE'] = t3lib_BEfunc::getRecordTitle($table, $selRec);
852 return $selRec;
853 }
854 }
855
856 /**
857 * Reports if the current pad has elements (does not check file/DB type OR if file/DBrecord exists or not. Only counting array)
858 *
859 * @return boolean TRUE if elements exist.
860 */
861 function isElements() {
862 return is_array($this->clipData[$this->current]['el']) && count($this->clipData[$this->current]['el']);
863 }
864
865
866 /*****************************************
867 *
868 * FOR USE IN tce_db.php:
869 *
870 ****************************************/
871
872 /**
873 * Applies the proper paste configuration in the $cmd array send to tce_db.php.
874 * $ref is the target, see description below.
875 * The current pad is pasted
876 *
877 * $ref: [tablename]:[paste-uid].
878 * tablename is the name of the table from which elements *on the current clipboard* is pasted with the 'pid' paste-uid.
879 * No tablename means that all items on the clipboard (non-files) are pasted. This requires paste-uid to be positive though.
880 * so 'tt_content:-3' means 'paste tt_content elements on the clipboard to AFTER tt_content:3 record
881 * 'tt_content:30' means 'paste tt_content elements on the clipboard into page with id 30
882 * ':30' means 'paste ALL database elements on the clipboard into page with id 30
883 * ':-30' not valid.
884 *
885 * @param string [tablename]:[paste-uid], see description
886 * @param array Command-array
887 * @return array Modified Command-array
888 */
889 function makePasteCmdArray($ref, $CMD) {
890 list($pTable, $pUid) = explode('|', $ref);
891 $pUid = intval($pUid);
892
893 if ($pTable || $pUid >= 0) { // pUid must be set and if pTable is not set (that means paste ALL elements) the uid MUST be positive/zero (pointing to page id)
894 $elements = $this->elFromTable($pTable);
895
896 $elements = array_reverse($elements); // So the order is preserved.
897 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
898
899 // Traverse elements and make CMD array
900 foreach ($elements as $tP => $value) {
901 list($table, $uid) = explode('|', $tP);
902 if (!is_array($CMD[$table])) {
903 $CMD[$table] = array();
904 }
905 $CMD[$table][$uid][$mode] = $pUid;
906 if ($mode == 'move') {
907 $this->removeElement($tP);
908 }
909 }
910 $this->endClipboard();
911 }
912 return $CMD;
913 }
914
915 /**
916 * Delete record entries in CMD array
917 *
918 * @param array Command-array
919 * @return array Modified Command-array
920 */
921 function makeDeleteCmdArray($CMD) {
922 $elements = $this->elFromTable(''); // all records
923 foreach ($elements as $tP => $value) {
924 list($table, $uid) = explode('|', $tP);
925 if (!is_array($CMD[$table])) {
926 $CMD[$table] = array();
927 }
928 $CMD[$table][$uid]['delete'] = 1;
929 $this->removeElement($tP);
930 }
931 $this->endClipboard();
932 return $CMD;
933 }
934
935
936 /*****************************************
937 *
938 * FOR USE IN tce_file.php:
939 *
940 ****************************************/
941
942 /**
943 * Applies the proper paste configuration in the $file array send to tce_file.php.
944 * The current pad is pasted
945 *
946 * @param string Reference to element (splitted by "|")
947 * @param array Command-array
948 * @return array Modified Command-array
949 */
950 function makePasteCmdArray_file($ref, $FILE) {
951 list($pTable, $pUid) = explode('|', $ref);
952 $elements = $this->elFromTable('_FILE');
953 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
954
955 // Traverse elements and make CMD array
956 foreach ($elements as $tP => $path) {
957 $FILE[$mode][] = array('data' => $path, 'target' => $pUid, 'altName' => 1);
958 if ($mode == 'move') {
959 $this->removeElement($tP);
960 }
961 }
962 $this->endClipboard();
963
964 return $FILE;
965 }
966
967 /**
968 * Delete files in CMD array
969 *
970 * @param array Command-array
971 * @return array Modified Command-array
972 */
973 function makeDeleteCmdArray_file($FILE) {
974 $elements = $this->elFromTable('_FILE');
975 // Traverse elements and make CMD array
976 foreach ($elements as $tP => $path) {
977 $FILE['delete'][] = array('data' => $path);
978 $this->removeElement($tP);
979 }
980 $this->endClipboard();
981
982 return $FILE;
983 }
984 }
985
986
987 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_clipboard.php'])) {
988 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_clipboard.php']);
989 }
990
991 ?>