Fixed issue #17284: Formprotection persistToken method is called too often, causing...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_arraybrowser.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 * Class for displaying an array as a tree
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
32 * XHTML compliant
33 *
34 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 77: class t3lib_arrayBrowser
42 * 96: function tree($arr, $depth_in, $depthData)
43 * 160: function wrapValue($theValue,$depth)
44 * 172: function wrapArrayKey($label,$depth,$theValue)
45 * 196: function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
46 * 228: function fixed_lgd($string,$chars)
47 * 245: function depthKeys($arr,$settings)
48 *
49 * TOTAL FUNCTIONS: 6
50 * (This index is automatically created/updated by the extension "extdeveval")
51 *
52 */
53
54
55 /**
56 * Class for displaying an array as a tree
57 * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
58 *
59 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
60 * @package TYPO3
61 * @subpackage t3lib
62 * @see SC_mod_tools_config_index::main()
63 */
64 class t3lib_arrayBrowser {
65 var $expAll = FALSE; // If set, will expand all (depthKeys is obsolete then) (and no links are applied)
66 var $dontLinkVar = FALSE; // If set, the variable keys are not linked.
67 var $depthKeys = array(); // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
68 var $searchKeys = array(); // After calling the getSearchKeys function this array is populated with the key-positions in the array which contains values matching the search.
69 var $fixedLgd = 1; // If set, the values are truncated with "..." appended if longer than a certain length.
70 var $regexMode = 0; // If set, search for string with regex, otherwise stristr()
71 var $searchKeysToo = FALSE; // If set, array keys are subject to the search too.
72 var $varName = ''; // Set var name here if you want links to the variable name.
73
74 /**
75 * Make browsable tree
76 * Before calling this function you may want to set some of the internal vars like depthKeys, regexMode and fixedLgd. For examples see SC_mod_tools_config_index::main()
77 *
78 * @param array The array to display
79 * @param string Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
80 * @param string Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
81 * @return string HTML for the tree
82 * @see SC_mod_tools_config_index::main()
83 */
84 function tree($arr, $depth_in, $depthData) {
85 $HTML = '';
86 $a = 0;
87
88 if ($depth_in) {
89 $depth_in = $depth_in . '.';
90 }
91
92 $c = count($arr);
93 foreach ($arr as $key => $value) {
94 $a++;
95 $depth = $depth_in . $key;
96 $goto = 'a' . substr(md5($depth), 0, 6);
97
98 $deeper = (is_array($arr[$key]) && ($this->depthKeys[$depth] || $this->expAll)) ? 1 : 0;
99 $PM = 'join';
100 $LN = ($a == $c) ? 'blank' : 'line';
101 $BTM = ($a == $c) ? 'bottom' : '';
102 $PM = is_array($arr[$key]) ? ($deeper ? 'minus' : 'plus') : 'join';
103
104
105 $HTML .= $depthData;
106 $theIcon = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/ol/' . $PM . $BTM . '.gif', 'width="18" height="16"') .
107 ' align="top" border="0" alt="" />';
108 if ($PM == 'join') {
109 $HTML .= $theIcon;
110 } else {
111 $HTML .=
112 ($this->expAll ? '' : '<a id="' . $goto . '" href="' . htmlspecialchars('index.php?node[' .
113 $depth . ']=' . ($deeper ? 0 : 1) . '#' . $goto) . '">') .
114 $theIcon .
115 ($this->expAll ? '' : '</a>');
116 }
117
118 $label = $key;
119 $HTML .= $this->wrapArrayKey($label, $depth, !is_array($arr[$key]) ? $arr[$key] : '');
120
121 if (!is_array($arr[$key])) {
122 $theValue = $arr[$key];
123 if ($this->fixedLgd) {
124 $imgBlocks = ceil(1 + strlen($depthData) / 77);
125 // debug($imgBlocks);
126 $lgdChars = 68 - ceil(strlen('[' . $key . ']') * 0.8) - $imgBlocks * 3;
127 $theValue = $this->fixed_lgd($theValue, $lgdChars);
128 }
129 if ($this->searchKeys[$depth]) {
130 $HTML .= '=<span style="color:red;">' . $this->wrapValue($theValue, $depth) . '</span>';
131 } else {
132 $HTML .= '=' . $this->wrapValue($theValue, $depth);
133 }
134 }
135 $HTML .= '<br />';
136
137 if ($deeper) {
138 $HTML .= $this->tree($arr[$key], $depth, $depthData . '<img' .
139 t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/ol/' . $LN . '.gif', 'width="18" height="16"') .
140 ' align="top" alt="" />');
141 }
142 }
143 return $HTML;
144 }
145
146 /**
147 * Wrapping the value in bold tags etc.
148 *
149 * @param string The title string
150 * @param string Depth path
151 * @return string Title string, htmlspecialchars()'ed
152 */
153 function wrapValue($theValue, $depth) {
154 $wrappedValue = '';
155 if (strlen($theValue) > 0) {
156 $wrappedValue = '<strong>' . htmlspecialchars($theValue) . '</strong>';
157 }
158 return $wrappedValue;
159 }
160
161 /**
162 * Wrapping the value in bold tags etc.
163 *
164 * @param string The title string
165 * @param string Depth path
166 * @param string The value for the array entry.
167 * @return string Title string, htmlspecialchars()'ed
168 */
169 function wrapArrayKey($label, $depth, $theValue) {
170
171 // Protect label:
172 $label = htmlspecialchars($label);
173
174 // If varname is set:
175 if ($this->varName && !$this->dontLinkVar) {
176 $variableName = $this->varName . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = ' .
177 (!t3lib_div::testInt($theValue) ? '\'' . addslashes($theValue) . '\'' : $theValue) . '; ';
178 $label = '<a href="index.php?varname=' . urlencode($variableName) . '#varname">' . $label . '</a>';
179 }
180
181 // Return:
182 return '[' . $label . ']';
183 }
184
185 /**
186 * Creates an array with "depthKeys" which will expand the array to show the search results
187 *
188 * @param array The array to search for the value
189 * @param string Depth string - blank for first call (will build up during recursive calling creating an id of the position: [key1].[key2].[key3]
190 * @param string The string to search for
191 * @param array Key array, for first call pass empty array
192 * @return array
193 */
194 function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
195 $c = count($keyArr);
196 if ($depth_in) {
197 $depth_in = $depth_in . '.';
198 }
199 foreach ($keyArr as $key => $value) {
200 $depth = $depth_in . $key;
201 $deeper = is_array($keyArr[$key]);
202
203 if ($this->regexMode) {
204 if (preg_match('/' . $searchString . '/', $keyArr[$key]) || ($this->searchKeysToo && preg_match('/' . $searchString . '/', $key))) {
205 $this->searchKeys[$depth] = 1;
206 }
207 } else {
208 if ((!$deeper && stristr($keyArr[$key], $searchString)) || ($this->searchKeysToo && stristr($key, $searchString))) {
209 $this->searchKeys[$depth] = 1;
210 }
211 }
212
213 if ($deeper) {
214 $cS = count($this->searchKeys);
215 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
216 if ($cS != count($this->searchKeys)) {
217 $keyArray[$depth] = 1;
218 }
219 }
220 }
221 return $keyArray;
222 }
223
224 /**
225 * Fixed length function
226 *
227 * @param string String to process
228 * @param integer Max number of chars
229 * @return string Processed string
230 */
231 function fixed_lgd($string, $chars) {
232 if ($chars >= 4) {
233 if (strlen($string) > $chars) {
234 return substr($string, 0, $chars - 3) . '...';
235 }
236 }
237 return $string;
238 }
239
240 /**
241 * Function modifying the depthKey array
242 *
243 * @param array Array with instructions to open/close nodes.
244 * @param array Input depth_key array
245 * @return array Output depth_key array with entries added/removed based on $arr
246 * @see SC_mod_tools_config_index::main()
247 */
248 function depthKeys($arr, $settings) {
249 $tsbrArray = array();
250 foreach ($arr as $theK => $theV) {
251 $theKeyParts = explode('.', $theK);
252 $depth = '';
253 $c = count($theKeyParts);
254 $a = 0;
255 foreach ($theKeyParts as $p) {
256 $a++;
257 $depth .= ($depth ? '.' : '') . $p;
258 $tsbrArray[$depth] = ($c == $a) ? $theV : 1;
259 }
260 }
261 // Modify settings
262 foreach ($tsbrArray as $theK => $theV) {
263 if ($theV) {
264 $settings[$theK] = 1;
265 } else {
266 unset($settings[$theK]);
267 }
268 }
269 return $settings;
270 }
271 }
272
273 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php'])) {
274 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']);
275 }
276 ?>