[BUGFIX] Untrusted GP data is unserialized in wizard_colorpicker.php and view_help.php
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_foldertree.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 * Generate a folder tree
29 *
30 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 * @coauthor René Fritz <r.fritz@colorcube.de>
34 */
35
36
37 /**
38 * Extension class for the t3lib_treeView class, specially made for browsing folders in the File module
39 *
40 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
41 * @coauthor René Fritz <r.fritz@colorcube.de>
42 * @package TYPO3
43 * @subpackage t3lib
44 * @see class t3lib_treeView
45 */
46 class t3lib_folderTree extends t3lib_treeView {
47
48 /**
49 * Constructor function of the class
50 *
51 * @return void
52 */
53 function __construct() {
54 parent::init();
55
56 $this->MOUNTS = $GLOBALS['FILEMOUNTS'];
57
58 $this->treeName = 'folder';
59 $this->titleAttrib = ''; //don't apply any title
60 $this->domIdPrefix = 'folder';
61 }
62
63 /**
64 * Compatibility constructor.
65 *
66 * @deprecated since TYPO3 4.6 and will be removed in TYPO3 4.8. Use __construct() instead.
67 */
68 public function t3lib_folderTree() {
69 t3lib_div::logDeprecatedFunction();
70 // Note: we cannot call $this->__construct() here because it would call the derived class constructor and cause recursion
71 // This code uses official PHP behavior (http://www.php.net/manual/en/language.oop5.basic.php) when $this in the
72 // statically called non-static method inherits $this from the caller's scope.
73 t3lib_folderTree::__construct();
74 }
75
76 /**
77 * Wrapping the folder icon
78 *
79 * @param string The image tag for the icon
80 * @param array The row for the current element
81 * @return string The processed icon input value.
82 * @access private
83 */
84 function wrapIcon($icon, $row) {
85 // Add title attribute to input icon tag
86 $theFolderIcon = $this->addTagAttributes($icon, ($this->titleAttrib ? $this->titleAttrib . '="' . $this->getTitleAttrib($row) . '"' : ''));
87
88 // Wrap icon in click-menu link.
89 if (!$this->ext_IconMode) {
90 $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon, $row['path'], '', 0);
91 } elseif (!strcmp($this->ext_IconMode, 'titlelink')) {
92 $aOnClick = 'return jumpTo(\'' . $this->getJumpToParam($row) . '\',this,\'' . $this->domIdPrefix . $this->getId($row) . '\',' . $this->bank . ');';
93 $theFolderIcon = '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">' . $theFolderIcon . '</a>';
94 }
95 return $theFolderIcon;
96 }
97
98 /**
99 * Wrapping $title in a-tags.
100 *
101 * @param string Title string
102 * @param string Item record
103 * @param integer Bank pointer (which mount point number)
104 * @return string
105 * @access private
106 */
107 function wrapTitle($title, $row, $bank = 0) {
108 $aOnClick = 'return jumpTo(\'' . $this->getJumpToParam($row) . '\',this,\'' . $this->domIdPrefix . $this->getId($row) . '\',' . $bank . ');';
109 $CSM = '';
110 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
111 $CSM = ' oncontextmenu="' . htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('', $row['path'], '', 0, '', '', TRUE)) . '"';
112 }
113 return '<a href="#" title="' . htmlspecialchars($row['title']) . '" onclick="' . htmlspecialchars($aOnClick) . '"' . $CSM . '>' . $title . '</a>';
114 }
115
116 /**
117 * Returns the id from the record - for folders, this is an md5 hash.
118 *
119 * @param array Record array
120 * @return integer The "uid" field value.
121 */
122 function getId($v) {
123 return t3lib_div::md5Int($v['path']);
124 }
125
126 /**
127 * Returns jump-url parameter value.
128 *
129 * @param array The record array.
130 * @return string The jump-url parameter.
131 */
132 function getJumpToParam($v) {
133 return rawurlencode($v['path']);
134 }
135
136 /**
137 * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
138 * '_title' is used for setting an alternative title for folders.
139 *
140 * @param array The input row array (where the key "_title" is used for the title)
141 * @param integer Title length (30)
142 * @return string The title.
143 */
144 function getTitleStr($row, $titleLen = 30) {
145 return $row['_title'] ? $row['_title'] : parent::getTitleStr($row, $titleLen);
146 }
147
148 /**
149 * Will create and return the HTML code for a browsable tree of folders.
150 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
151 *
152 * @return string HTML code for the browsable tree
153 */
154 function getBrowsableTree() {
155
156 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
157 $this->initializePositionSaving();
158
159 // Init done:
160 $titleLen = intval($this->BE_USER->uc['titleLen']);
161 $treeArr = array();
162
163 // Traverse mounts:
164 foreach ($this->MOUNTS as $key => $val) {
165 $md5_uid = md5($val['path']);
166 $specUID = hexdec(substr($md5_uid, 0, 6));
167 $this->specUIDmap[$specUID] = $val['path'];
168
169 // Set first:
170 $this->bank = $val['nkey'];
171 $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
172 $this->reset();
173
174 // Set PM icon:
175 $cmd = $this->bank . '_' . ($isOpen ? '0_' : '1_') . $specUID . '_' . $this->treeName;
176 $icon = '<img' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/ol/' . ($isOpen ? 'minus' : 'plus') . 'only.gif', 'width="18" height="16"') . ' alt="" />';
177 $firstHtml = $this->PM_ATagWrap($icon, $cmd);
178
179 switch ($val['type']) {
180 case 'user':
181 $icon = 'gfx/i/_icon_ftp_user.gif';
182 break;
183 case 'group':
184 $icon = 'gfx/i/_icon_ftp_group.gif';
185 break;
186 case 'readonly':
187 $icon = 'gfx/i/_icon_ftp_readonly.gif';
188 break;
189 default:
190 $icon = 'gfx/i/_icon_ftp.gif';
191 break;
192 }
193
194 // Preparing rootRec for the mount
195 $firstHtml .= $this->wrapIcon('<img' . t3lib_iconWorks::skinImg($this->backPath, $icon, 'width="18" height="16"') . ' alt="" />', $val);
196 $row = array();
197 $row['path'] = $val['path'];
198 $row['uid'] = $specUID;
199 $row['title'] = $val['name'];
200
201 // Add the root of the mount to ->tree
202 $this->tree[] = array('HTML' => $firstHtml, 'row' => $row, 'bank' => $this->bank);
203
204 // If the mount is expanded, go down:
205 if ($isOpen) {
206 // Set depth:
207 $depthD = '<img' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/ol/blank.gif', 'width="18" height="16"') . ' alt="" />';
208 $this->getFolderTree($val['path'], 999, $depthD, $val['type']);
209 }
210
211 // Add tree:
212 $treeArr = array_merge($treeArr, $this->tree);
213 }
214 return $this->printTree($treeArr);
215 }
216
217 /**
218 * Fetches the data for the tree
219 *
220 * @param string Abs file path
221 * @param integer Max depth (recursivity limit)
222 * @param string HTML-code prefix for recursive calls.
223 * @return integer The count of items on the level
224 * @see getBrowsableTree()
225 */
226 function getFolderTree($files_path, $depth = 999, $depthData = '', $type = '') {
227
228 // This generates the directory tree
229 $dirs = t3lib_div::get_dirs($files_path);
230
231 $c = 0;
232 if (is_array($dirs)) {
233 $depth = intval($depth);
234 $HTML = '';
235 $a = 0;
236 $c = count($dirs);
237 sort($dirs);
238
239 foreach ($dirs as $key => $val) {
240 $a++;
241 $this->tree[] = array(); // Reserve space.
242 end($this->tree);
243 $treeKey = key($this->tree); // Get the key for this space
244 $LN = ($a == $c) ? 'blank' : 'line';
245
246 $val = preg_replace('/^\.\//', '', $val);
247 $title = $val;
248 $path = $files_path . $val . '/';
249 $webpath = t3lib_BEfunc::getPathType_web_nonweb($path);
250
251 $md5_uid = md5($path);
252 $specUID = hexdec(substr($md5_uid, 0, 6));
253 $this->specUIDmap[$specUID] = $path;
254 $row = array();
255 $row['path'] = $path;
256 $row['uid'] = $specUID;
257 $row['title'] = $title;
258
259 if ($depth > 1 && $this->expandNext($specUID)) {
260 $nextCount = $this->getFolderTree(
261 $path,
262 $depth - 1,
263 $this->makeHTML ? $depthData . '<img' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/ol/' . $LN . '.gif', 'width="18" height="16"') . ' alt="" />' : '',
264 $type
265 );
266 $exp = 1; // Set "did expand" flag
267 } else {
268 $nextCount = $this->getCount($path);
269 $exp = 0; // Clear "did expand" flag
270 }
271
272 // Set HTML-icons, if any:
273 if ($this->makeHTML) {
274 $HTML = $depthData . $this->PMicon($row, $a, $c, $nextCount, $exp);
275
276 $icon = 'gfx/i/_icon_' . $webpath . 'folders' . ($type == 'readonly' ? '_ro' : '') . '.gif';
277 if ($val == '_temp_') {
278 $icon = 'gfx/i/sysf.gif';
279 $row['title'] = $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:temp', TRUE);
280 $row['_title'] = '<strong>' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:temp', TRUE) . '</strong>';
281 }
282 if ($val == '_recycler_') {
283 $icon = 'gfx/i/recycler.gif';
284 $row['title'] = $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', TRUE);
285 $row['_title'] = '<strong>' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', TRUE) . '</strong>';
286 }
287 $HTML .= $this->wrapIcon('<img' . t3lib_iconWorks::skinImg($this->backPath, $icon, 'width="18" height="16"') . ' alt="" />', $row);
288 }
289
290 // Finally, add the row/HTML content to the ->tree array in the reserved key.
291 $this->tree[$treeKey] = Array(
292 'row' => $row,
293 'HTML' => $HTML,
294 'bank' => $this->bank
295 );
296 }
297 }
298 return $c;
299 }
300
301 /**
302 * Counts the number of directories in a file path.
303 *
304 * @param string File path.
305 * @return integer
306 */
307 function getCount($files_path) {
308 // This generates the directory tree
309 $dirs = t3lib_div::get_dirs($files_path);
310 $c = 0;
311 if (is_array($dirs)) {
312 $c = count($dirs);
313 }
314 return $c;
315 }
316
317 /**
318 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
319 *
320 * @return void
321 * @access private
322 */
323 function initializePositionSaving() {
324 // Get stored tree structure:
325 $this->stored = unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
326
327 // Mapping md5-hash to shorter number:
328 $hashMap = array();
329 foreach ($this->MOUNTS as $key => $val) {
330 $nkey = hexdec(substr($key, 0, 4));
331 $hashMap[$nkey] = $key;
332 $this->MOUNTS[$key]['nkey'] = $nkey;
333 }
334
335 // PM action:
336 // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
337 $PM = explode('_', t3lib_div::_GP('PM')); // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
338 if (count($PM) == 4 && $PM[3] == $this->treeName) {
339 if (isset($this->MOUNTS[$hashMap[$PM[0]]])) {
340 if ($PM[1]) { // set
341 $this->stored[$PM[0]][$PM[2]] = 1;
342 $this->savePosition($this->treeName);
343 } else { // clear
344 unset($this->stored[$PM[0]][$PM[2]]);
345 $this->savePosition($this->treeName);
346 }
347 }
348 }
349 }
350 }
351
352 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php'])) {
353 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']);
354 }
355
356 ?>