[TASK] Add helper method to deprecate ViewHelper attributes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / GeneralUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
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
17 use GuzzleHttp\Exception\RequestException;
18 use TYPO3\CMS\Core\Charset\CharsetConverter;
19 use TYPO3\CMS\Core\Core\ApplicationContext;
20 use TYPO3\CMS\Core\Core\ClassLoadingInformation;
21 use TYPO3\CMS\Core\Crypto\Random;
22 use TYPO3\CMS\Core\Database\ConnectionPool;
23 use TYPO3\CMS\Core\Http\RequestFactory;
24 use TYPO3\CMS\Core\Service\OpcodeCacheService;
25 use TYPO3\CMS\Core\SingletonInterface;
26 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27
28 /**
29 * The legendary "t3lib_div" class - Miscellaneous functions for general purpose.
30 * Most of the functions do not relate specifically to TYPO3
31 * However a section of functions requires certain TYPO3 features available
32 * See comments in the source.
33 * You are encouraged to use this library in your own scripts!
34 *
35 * USE:
36 * The class is intended to be used without creating an instance of it.
37 * So: Don't instantiate - call functions with "\TYPO3\CMS\Core\Utility\GeneralUtility::" prefixed the function name.
38 * So use \TYPO3\CMS\Core\Utility\GeneralUtility::[method-name] to refer to the functions, eg. '\TYPO3\CMS\Core\Utility\GeneralUtility::milliseconds()'
39 */
40 class GeneralUtility
41 {
42 // Severity constants used by \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog()
43 const SYSLOG_SEVERITY_INFO = 0;
44 const SYSLOG_SEVERITY_NOTICE = 1;
45 const SYSLOG_SEVERITY_WARNING = 2;
46 const SYSLOG_SEVERITY_ERROR = 3;
47 const SYSLOG_SEVERITY_FATAL = 4;
48
49 const ENV_TRUSTED_HOSTS_PATTERN_ALLOW_ALL = '.*';
50 const ENV_TRUSTED_HOSTS_PATTERN_SERVER_NAME = 'SERVER_NAME';
51
52 /**
53 * State of host header value security check
54 * in order to avoid unnecessary multiple checks during one request
55 *
56 * @var bool
57 */
58 protected static $allowHostHeaderValue = false;
59
60 /**
61 * Singleton instances returned by makeInstance, using the class names as
62 * array keys
63 *
64 * @var array<\TYPO3\CMS\Core\SingletonInterface>
65 */
66 protected static $singletonInstances = [];
67
68 /**
69 * Instances returned by makeInstance, using the class names as array keys
70 *
71 * @var array<array><object>
72 */
73 protected static $nonSingletonInstances = [];
74
75 /**
76 * Cache for makeInstance with given class name and final class names to reduce number of self::getClassName() calls
77 *
78 * @var array Given class name => final class name
79 */
80 protected static $finalClassNameCache = [];
81
82 /**
83 * The application context
84 *
85 * @var \TYPO3\CMS\Core\Core\ApplicationContext
86 */
87 protected static $applicationContext = null;
88
89 /**
90 * IDNA string cache
91 *
92 * @var array<string>
93 */
94 protected static $idnaStringCache = [];
95
96 /**
97 * IDNA converter
98 *
99 * @var \Mso\IdnaConvert\IdnaConvert
100 */
101 protected static $idnaConverter = null;
102
103 /**
104 * A list of supported CGI server APIs
105 * NOTICE: This is a duplicate of the SAME array in SystemEnvironmentBuilder
106 * @var array
107 */
108 protected static $supportedCgiServerApis = [
109 'fpm-fcgi',
110 'cgi',
111 'isapi',
112 'cgi-fcgi',
113 'srv', // HHVM with fastcgi
114 ];
115
116 /**
117 * @var array
118 */
119 protected static $indpEnvCache = [];
120
121 /*************************
122 *
123 * GET/POST Variables
124 *
125 * Background:
126 * Input GET/POST variables in PHP may have their quotes escaped with "\" or not depending on configuration.
127 * TYPO3 has always converted quotes to BE escaped if the configuration told that they would not be so.
128 * But the clean solution is that quotes are never escaped and that is what the functions below offers.
129 * Eventually TYPO3 should provide this in the global space as well.
130 * In the transitional phase (or forever..?) we need to encourage EVERY to read and write GET/POST vars through the API functions below.
131 * This functionality was previously needed to normalize between magic quotes logic, which was removed from PHP 5.4,
132 * so these methods are still in use, but not tackle the slash problem anymore.
133 *
134 *************************/
135 /**
136 * Returns the 'GLOBAL' value of incoming data from POST or GET, with priority to POST (that is equalent to 'GP' order)
137 * To enhance security in your scripts, please consider using GeneralUtility::_GET or GeneralUtility::_POST if you already
138 * know by which method your data is arriving to the scripts!
139 *
140 * @param string $var GET/POST var to return
141 * @return mixed POST var named $var and if not set, the GET var of the same name.
142 */
143 public static function _GP($var)
144 {
145 if (empty($var)) {
146 return;
147 }
148 if (isset($_POST[$var])) {
149 $value = $_POST[$var];
150 } elseif (isset($_GET[$var])) {
151 $value = $_GET[$var];
152 } else {
153 $value = null;
154 }
155 // This is there for backwards-compatibility, in order to avoid NULL
156 if (isset($value) && !is_array($value)) {
157 $value = (string)$value;
158 }
159 return $value;
160 }
161
162 /**
163 * Returns the global arrays $_GET and $_POST merged with $_POST taking precedence.
164 *
165 * @param string $parameter Key (variable name) from GET or POST vars
166 * @return array Returns the GET vars merged recursively onto the POST vars.
167 */
168 public static function _GPmerged($parameter)
169 {
170 $postParameter = isset($_POST[$parameter]) && is_array($_POST[$parameter]) ? $_POST[$parameter] : [];
171 $getParameter = isset($_GET[$parameter]) && is_array($_GET[$parameter]) ? $_GET[$parameter] : [];
172 $mergedParameters = $getParameter;
173 ArrayUtility::mergeRecursiveWithOverrule($mergedParameters, $postParameter);
174 return $mergedParameters;
175 }
176
177 /**
178 * Returns the global $_GET array (or value from) normalized to contain un-escaped values.
179 * ALWAYS use this API function to acquire the GET variables!
180 * This function was previously used to normalize between magic quotes logic, which was removed from PHP 5.5
181 *
182 * @param string $var Optional pointer to value in GET array (basically name of GET var)
183 * @return mixed If $var is set it returns the value of $_GET[$var]. If $var is NULL (default), returns $_GET itself. In any case *slashes are stipped from the output!*
184 * @see _POST(), _GP(), _GETset()
185 */
186 public static function _GET($var = null)
187 {
188 $value = $var === null ? $_GET : (empty($var) ? null : $_GET[$var]);
189 // This is there for backwards-compatibility, in order to avoid NULL
190 if (isset($value) && !is_array($value)) {
191 $value = (string)$value;
192 }
193 return $value;
194 }
195
196 /**
197 * Returns the global $_POST array (or value from) normalized to contain un-escaped values.
198 * ALWAYS use this API function to acquire the $_POST variables!
199 *
200 * @param string $var Optional pointer to value in POST array (basically name of POST var)
201 * @return mixed If $var is set it returns the value of $_POST[$var]. If $var is NULL (default), returns $_POST itself. In any case *slashes are stipped from the output!*
202 * @see _GET(), _GP()
203 */
204 public static function _POST($var = null)
205 {
206 $value = $var === null ? $_POST : (empty($var) ? null : $_POST[$var]);
207 // This is there for backwards-compatibility, in order to avoid NULL
208 if (isset($value) && !is_array($value)) {
209 $value = (string)$value;
210 }
211 return $value;
212 }
213
214 /**
215 * Writes input value to $_GET.
216 *
217 * @param mixed $inputGet
218 * @param string $key
219 * @return void
220 */
221 public static function _GETset($inputGet, $key = '')
222 {
223 if ($key != '') {
224 if (strpos($key, '|') !== false) {
225 $pieces = explode('|', $key);
226 $newGet = [];
227 $pointer = &$newGet;
228 foreach ($pieces as $piece) {
229 $pointer = &$pointer[$piece];
230 }
231 $pointer = $inputGet;
232 $mergedGet = $_GET;
233 ArrayUtility::mergeRecursiveWithOverrule($mergedGet, $newGet);
234 $_GET = $mergedGet;
235 $GLOBALS['HTTP_GET_VARS'] = $mergedGet;
236 } else {
237 $_GET[$key] = $inputGet;
238 $GLOBALS['HTTP_GET_VARS'][$key] = $inputGet;
239 }
240 } elseif (is_array($inputGet)) {
241 $_GET = $inputGet;
242 $GLOBALS['HTTP_GET_VARS'] = $inputGet;
243 }
244 }
245
246 /**
247 * Wrapper for the RemoveXSS function.
248 * Removes potential XSS code from an input string.
249 *
250 * Using an external class by Travis Puderbaugh <kallahar@quickwired.com>
251 *
252 * @param string $string Input string
253 * @return string Input string with potential XSS code removed
254 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
255 */
256 public static function removeXSS($string)
257 {
258 return \RemoveXSS::process($string);
259 }
260
261 /*************************
262 *
263 * IMAGE FUNCTIONS
264 *
265 *************************/
266
267 /*************************
268 *
269 * STRING FUNCTIONS
270 *
271 *************************/
272 /**
273 * Truncates a string with appended/prepended "..." and takes current character set into consideration.
274 *
275 * @param string $string String to truncate
276 * @param int $chars Must be an integer with an absolute value of at least 4. if negative the string is cropped from the right end.
277 * @param string $appendString Appendix to the truncated string
278 * @return string Cropped string
279 */
280 public static function fixed_lgd_cs($string, $chars, $appendString = '...')
281 {
282 /** @var CharsetConverter $charsetConverter */
283 $charsetConverter = self::makeInstance(\TYPO3\CMS\Core\Charset\CharsetConverter::class);
284 return $charsetConverter->crop('utf-8', $string, $chars, $appendString);
285 }
286
287 /**
288 * Match IP number with list of numbers with wildcard
289 * Dispatcher method for switching into specialised IPv4 and IPv6 methods.
290 *
291 * @param string $baseIP Is the current remote IP address for instance, typ. REMOTE_ADDR
292 * @param string $list Is a comma-list of IP-addresses to match with. *-wildcard allowed instead of number, plus leaving out parts in the IP number is accepted as wildcard (eg. 192.168.*.* equals 192.168). If list is "*" no check is done and the function returns TRUE immediately. An empty list always returns FALSE.
293 * @return bool TRUE if an IP-mask from $list matches $baseIP
294 */
295 public static function cmpIP($baseIP, $list)
296 {
297 $list = trim($list);
298 if ($list === '') {
299 return false;
300 } elseif ($list === '*') {
301 return true;
302 }
303 if (strpos($baseIP, ':') !== false && self::validIPv6($baseIP)) {
304 return self::cmpIPv6($baseIP, $list);
305 } else {
306 return self::cmpIPv4($baseIP, $list);
307 }
308 }
309
310 /**
311 * Match IPv4 number with list of numbers with wildcard
312 *
313 * @param string $baseIP Is the current remote IP address for instance, typ. REMOTE_ADDR
314 * @param string $list Is a comma-list of IP-addresses to match with. *-wildcard allowed instead of number, plus leaving out parts in the IP number is accepted as wildcard (eg. 192.168.*.* equals 192.168), could also contain IPv6 addresses
315 * @return bool TRUE if an IP-mask from $list matches $baseIP
316 */
317 public static function cmpIPv4($baseIP, $list)
318 {
319 $IPpartsReq = explode('.', $baseIP);
320 if (count($IPpartsReq) === 4) {
321 $values = self::trimExplode(',', $list, true);
322 foreach ($values as $test) {
323 $testList = explode('/', $test);
324 if (count($testList) === 2) {
325 list($test, $mask) = $testList;
326 } else {
327 $mask = false;
328 }
329 if ((int)$mask) {
330 // "192.168.3.0/24"
331 $lnet = ip2long($test);
332 $lip = ip2long($baseIP);
333 $binnet = str_pad(decbin($lnet), 32, '0', STR_PAD_LEFT);
334 $firstpart = substr($binnet, 0, $mask);
335 $binip = str_pad(decbin($lip), 32, '0', STR_PAD_LEFT);
336 $firstip = substr($binip, 0, $mask);
337 $yes = $firstpart === $firstip;
338 } else {
339 // "192.168.*.*"
340 $IPparts = explode('.', $test);
341 $yes = 1;
342 foreach ($IPparts as $index => $val) {
343 $val = trim($val);
344 if ($val !== '*' && $IPpartsReq[$index] !== $val) {
345 $yes = 0;
346 }
347 }
348 }
349 if ($yes) {
350 return true;
351 }
352 }
353 }
354 return false;
355 }
356
357 /**
358 * Match IPv6 address with a list of IPv6 prefixes
359 *
360 * @param string $baseIP Is the current remote IP address for instance
361 * @param string $list Is a comma-list of IPv6 prefixes, could also contain IPv4 addresses
362 * @return bool TRUE If an baseIP matches any prefix
363 */
364 public static function cmpIPv6($baseIP, $list)
365 {
366 // Policy default: Deny connection
367 $success = false;
368 $baseIP = self::normalizeIPv6($baseIP);
369 $values = self::trimExplode(',', $list, true);
370 foreach ($values as $test) {
371 $testList = explode('/', $test);
372 if (count($testList) === 2) {
373 list($test, $mask) = $testList;
374 } else {
375 $mask = false;
376 }
377 if (self::validIPv6($test)) {
378 $test = self::normalizeIPv6($test);
379 $maskInt = (int)$mask ?: 128;
380 // Special case; /0 is an allowed mask - equals a wildcard
381 if ($mask === '0') {
382 $success = true;
383 } elseif ($maskInt == 128) {
384 $success = $test === $baseIP;
385 } else {
386 $testBin = self::IPv6Hex2Bin($test);
387 $baseIPBin = self::IPv6Hex2Bin($baseIP);
388 $success = true;
389 // Modulo is 0 if this is a 8-bit-boundary
390 $maskIntModulo = $maskInt % 8;
391 $numFullCharactersUntilBoundary = (int)($maskInt / 8);
392 if (substr($testBin, 0, $numFullCharactersUntilBoundary) !== substr($baseIPBin, 0, $numFullCharactersUntilBoundary)) {
393 $success = false;
394 } elseif ($maskIntModulo > 0) {
395 // If not an 8-bit-boundary, check bits of last character
396 $testLastBits = str_pad(decbin(ord(substr($testBin, $numFullCharactersUntilBoundary, 1))), 8, '0', STR_PAD_LEFT);
397 $baseIPLastBits = str_pad(decbin(ord(substr($baseIPBin, $numFullCharactersUntilBoundary, 1))), 8, '0', STR_PAD_LEFT);
398 if (strncmp($testLastBits, $baseIPLastBits, $maskIntModulo) != 0) {
399 $success = false;
400 }
401 }
402 }
403 }
404 if ($success) {
405 return true;
406 }
407 }
408 return false;
409 }
410
411 /**
412 * Transform a regular IPv6 address from hex-representation into binary
413 *
414 * @param string $hex IPv6 address in hex-presentation
415 * @return string Binary representation (16 characters, 128 characters)
416 * @see IPv6Bin2Hex()
417 */
418 public static function IPv6Hex2Bin($hex)
419 {
420 return inet_pton($hex);
421 }
422
423 /**
424 * Transform an IPv6 address from binary to hex-representation
425 *
426 * @param string $bin IPv6 address in hex-presentation
427 * @return string Binary representation (16 characters, 128 characters)
428 * @see IPv6Hex2Bin()
429 */
430 public static function IPv6Bin2Hex($bin)
431 {
432 return inet_ntop($bin);
433 }
434
435 /**
436 * Normalize an IPv6 address to full length
437 *
438 * @param string $address Given IPv6 address
439 * @return string Normalized address
440 * @see compressIPv6()
441 */
442 public static function normalizeIPv6($address)
443 {
444 $normalizedAddress = '';
445 $stageOneAddress = '';
446 // According to RFC lowercase-representation is recommended
447 $address = strtolower($address);
448 // Normalized representation has 39 characters (0000:0000:0000:0000:0000:0000:0000:0000)
449 if (strlen($address) == 39) {
450 // Already in full expanded form
451 return $address;
452 }
453 // Count 2 if if address has hidden zero blocks
454 $chunks = explode('::', $address);
455 if (count($chunks) === 2) {
456 $chunksLeft = explode(':', $chunks[0]);
457 $chunksRight = explode(':', $chunks[1]);
458 $left = count($chunksLeft);
459 $right = count($chunksRight);
460 // Special case: leading zero-only blocks count to 1, should be 0
461 if ($left == 1 && strlen($chunksLeft[0]) == 0) {
462 $left = 0;
463 }
464 $hiddenBlocks = 8 - ($left + $right);
465 $hiddenPart = '';
466 $h = 0;
467 while ($h < $hiddenBlocks) {
468 $hiddenPart .= '0000:';
469 $h++;
470 }
471 if ($left == 0) {
472 $stageOneAddress = $hiddenPart . $chunks[1];
473 } else {
474 $stageOneAddress = $chunks[0] . ':' . $hiddenPart . $chunks[1];
475 }
476 } else {
477 $stageOneAddress = $address;
478 }
479 // Normalize the blocks:
480 $blocks = explode(':', $stageOneAddress);
481 $divCounter = 0;
482 foreach ($blocks as $block) {
483 $tmpBlock = '';
484 $i = 0;
485 $hiddenZeros = 4 - strlen($block);
486 while ($i < $hiddenZeros) {
487 $tmpBlock .= '0';
488 $i++;
489 }
490 $normalizedAddress .= $tmpBlock . $block;
491 if ($divCounter < 7) {
492 $normalizedAddress .= ':';
493 $divCounter++;
494 }
495 }
496 return $normalizedAddress;
497 }
498
499 /**
500 * Compress an IPv6 address to the shortest notation
501 *
502 * @param string $address Given IPv6 address
503 * @return string Compressed address
504 * @see normalizeIPv6()
505 */
506 public static function compressIPv6($address)
507 {
508 return inet_ntop(inet_pton($address));
509 }
510
511 /**
512 * Validate a given IP address.
513 *
514 * Possible format are IPv4 and IPv6.
515 *
516 * @param string $ip IP address to be tested
517 * @return bool TRUE if $ip is either of IPv4 or IPv6 format.
518 */
519 public static function validIP($ip)
520 {
521 return filter_var($ip, FILTER_VALIDATE_IP) !== false;
522 }
523
524 /**
525 * Validate a given IP address to the IPv4 address format.
526 *
527 * Example for possible format: 10.0.45.99
528 *
529 * @param string $ip IP address to be tested
530 * @return bool TRUE if $ip is of IPv4 format.
531 */
532 public static function validIPv4($ip)
533 {
534 return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
535 }
536
537 /**
538 * Validate a given IP address to the IPv6 address format.
539 *
540 * Example for possible format: 43FB::BB3F:A0A0:0 | ::1
541 *
542 * @param string $ip IP address to be tested
543 * @return bool TRUE if $ip is of IPv6 format.
544 */
545 public static function validIPv6($ip)
546 {
547 return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
548 }
549
550 /**
551 * Match fully qualified domain name with list of strings with wildcard
552 *
553 * @param string $baseHost A hostname or an IPv4/IPv6-address (will by reverse-resolved; typically REMOTE_ADDR)
554 * @param string $list A comma-list of domain names to match with. *-wildcard allowed but cannot be part of a string, so it must match the full host name (eg. myhost.*.com => correct, myhost.*domain.com => wrong)
555 * @return bool TRUE if a domain name mask from $list matches $baseIP
556 */
557 public static function cmpFQDN($baseHost, $list)
558 {
559 $baseHost = trim($baseHost);
560 if (empty($baseHost)) {
561 return false;
562 }
563 if (self::validIPv4($baseHost) || self::validIPv6($baseHost)) {
564 // Resolve hostname
565 // Note: this is reverse-lookup and can be randomly set as soon as somebody is able to set
566 // the reverse-DNS for his IP (security when for example used with REMOTE_ADDR)
567 $baseHostName = gethostbyaddr($baseHost);
568 if ($baseHostName === $baseHost) {
569 // Unable to resolve hostname
570 return false;
571 }
572 } else {
573 $baseHostName = $baseHost;
574 }
575 $baseHostNameParts = explode('.', $baseHostName);
576 $values = self::trimExplode(',', $list, true);
577 foreach ($values as $test) {
578 $hostNameParts = explode('.', $test);
579 // To match hostNameParts can only be shorter (in case of wildcards) or equal
580 $hostNamePartsCount = count($hostNameParts);
581 $baseHostNamePartsCount = count($baseHostNameParts);
582 if ($hostNamePartsCount > $baseHostNamePartsCount) {
583 continue;
584 }
585 $yes = true;
586 foreach ($hostNameParts as $index => $val) {
587 $val = trim($val);
588 if ($val === '*') {
589 // Wildcard valid for one or more hostname-parts
590 $wildcardStart = $index + 1;
591 // Wildcard as last/only part always matches, otherwise perform recursive checks
592 if ($wildcardStart < $hostNamePartsCount) {
593 $wildcardMatched = false;
594 $tempHostName = implode('.', array_slice($hostNameParts, $index + 1));
595 while ($wildcardStart < $baseHostNamePartsCount && !$wildcardMatched) {
596 $tempBaseHostName = implode('.', array_slice($baseHostNameParts, $wildcardStart));
597 $wildcardMatched = self::cmpFQDN($tempBaseHostName, $tempHostName);
598 $wildcardStart++;
599 }
600 if ($wildcardMatched) {
601 // Match found by recursive compare
602 return true;
603 } else {
604 $yes = false;
605 }
606 }
607 } elseif ($baseHostNameParts[$index] !== $val) {
608 // In case of no match
609 $yes = false;
610 }
611 }
612 if ($yes) {
613 return true;
614 }
615 }
616 return false;
617 }
618
619 /**
620 * Checks if a given URL matches the host that currently handles this HTTP request.
621 * Scheme, hostname and (optional) port of the given URL are compared.
622 *
623 * @param string $url URL to compare with the TYPO3 request host
624 * @return bool Whether the URL matches the TYPO3 request host
625 */
626 public static function isOnCurrentHost($url)
627 {
628 return stripos($url . '/', self::getIndpEnv('TYPO3_REQUEST_HOST') . '/') === 0;
629 }
630
631 /**
632 * Check for item in list
633 * Check if an item exists in a comma-separated list of items.
634 *
635 * @param string $list Comma-separated list of items (string)
636 * @param string $item Item to check for
637 * @return bool TRUE if $item is in $list
638 */
639 public static function inList($list, $item)
640 {
641 return strpos(',' . $list . ',', ',' . $item . ',') !== false;
642 }
643
644 /**
645 * Removes an item from a comma-separated list of items.
646 *
647 * If $element contains a comma, the behaviour of this method is undefined.
648 * Empty elements in the list are preserved.
649 *
650 * @param string $element Element to remove
651 * @param string $list Comma-separated list of items (string)
652 * @return string New comma-separated list of items
653 */
654 public static function rmFromList($element, $list)
655 {
656 $items = explode(',', $list);
657 foreach ($items as $k => $v) {
658 if ($v == $element) {
659 unset($items[$k]);
660 }
661 }
662 return implode(',', $items);
663 }
664
665 /**
666 * Expand a comma-separated list of integers with ranges (eg 1,3-5,7 becomes 1,3,4,5,7).
667 * Ranges are limited to 1000 values per range.
668 *
669 * @param string $list Comma-separated list of integers with ranges (string)
670 * @return string New comma-separated list of items
671 */
672 public static function expandList($list)
673 {
674 $items = explode(',', $list);
675 $list = [];
676 foreach ($items as $item) {
677 $range = explode('-', $item);
678 if (isset($range[1])) {
679 $runAwayBrake = 1000;
680 for ($n = $range[0]; $n <= $range[1]; $n++) {
681 $list[] = $n;
682 $runAwayBrake--;
683 if ($runAwayBrake <= 0) {
684 break;
685 }
686 }
687 } else {
688 $list[] = $item;
689 }
690 }
691 return implode(',', $list);
692 }
693
694 /**
695 * Returns TRUE if the current TYPO3 version (or compatibility version) is compatible to the input version
696 * Notice that this function compares branches, not versions (4.0.1 would be > 4.0.0 although they use the same compat_version)
697 *
698 * @param string $verNumberStr Minimum branch number required (format x.y / e.g. "4.0" NOT "4.0.0"!)
699 * @return bool Returns TRUE if this setup is compatible with the provided version number
700 * @todo Still needs a function to convert versions to branches
701 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
702 */
703 public static function compat_version($verNumberStr)
704 {
705 static::logDeprecatedFunction();
706 return VersionNumberUtility::convertVersionNumberToInteger(TYPO3_branch) >= VersionNumberUtility::convertVersionNumberToInteger($verNumberStr);
707 }
708
709 /**
710 * Makes a positive integer hash out of the first 7 chars from the md5 hash of the input
711 *
712 * @param string $str String to md5-hash
713 * @return int Returns 28bit integer-hash
714 */
715 public static function md5int($str)
716 {
717 return hexdec(substr(md5($str), 0, 7));
718 }
719
720 /**
721 * Returns the first 10 positions of the MD5-hash (changed from 6 to 10 recently)
722 *
723 * @param string $input Input string to be md5-hashed
724 * @param int $len The string-length of the output
725 * @return string Substring of the resulting md5-hash, being $len chars long (from beginning)
726 */
727 public static function shortMD5($input, $len = 10)
728 {
729 return substr(md5($input), 0, $len);
730 }
731
732 /**
733 * Returns a proper HMAC on a given input string and secret TYPO3 encryption key.
734 *
735 * @param string $input Input string to create HMAC from
736 * @param string $additionalSecret additionalSecret to prevent hmac being used in a different context
737 * @return string resulting (hexadecimal) HMAC currently with a length of 40 (HMAC-SHA-1)
738 */
739 public static function hmac($input, $additionalSecret = '')
740 {
741 $hashAlgorithm = 'sha1';
742 $hashBlocksize = 64;
743 $hmac = '';
744 $secret = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . $additionalSecret;
745 if (extension_loaded('hash') && function_exists('hash_hmac') && function_exists('hash_algos') && in_array($hashAlgorithm, hash_algos())) {
746 $hmac = hash_hmac($hashAlgorithm, $input, $secret);
747 } else {
748 // Outer padding
749 $opad = str_repeat(chr(92), $hashBlocksize);
750 // Inner padding
751 $ipad = str_repeat(chr(54), $hashBlocksize);
752 if (strlen($secret) > $hashBlocksize) {
753 // Keys longer than block size are shorten
754 $key = str_pad(pack('H*', call_user_func($hashAlgorithm, $secret)), $hashBlocksize, chr(0));
755 } else {
756 // Keys shorter than block size are zero-padded
757 $key = str_pad($secret, $hashBlocksize, chr(0));
758 }
759 $hmac = call_user_func($hashAlgorithm, ($key ^ $opad) . pack('H*', call_user_func($hashAlgorithm, (($key ^ $ipad) . $input))));
760 }
761 return $hmac;
762 }
763
764 /**
765 * Takes comma-separated lists and arrays and removes all duplicates
766 * If a value in the list is trim(empty), the value is ignored.
767 *
768 * @param string $in_list Accept multiple parameters which can be comma-separated lists of values and arrays.
769 * @param mixed $secondParameter Dummy field, which if set will show a warning!
770 * @return string Returns the list without any duplicates of values, space around values are trimmed
771 */
772 public static function uniqueList($in_list, $secondParameter = null)
773 {
774 if (is_array($in_list)) {
775 throw new \InvalidArgumentException('TYPO3 Fatal Error: TYPO3\\CMS\\Core\\Utility\\GeneralUtility::uniqueList() does NOT support array arguments anymore! Only string comma lists!', 1270853885);
776 }
777 if (isset($secondParameter)) {
778 throw new \InvalidArgumentException('TYPO3 Fatal Error: TYPO3\\CMS\\Core\\Utility\\GeneralUtility::uniqueList() does NOT support more than a single argument value anymore. You have specified more than one!', 1270853886);
779 }
780 return implode(',', array_unique(self::trimExplode(',', $in_list, true)));
781 }
782
783 /**
784 * Splits a reference to a file in 5 parts
785 *
786 * @param string $fileNameWithPath File name with path to be analysed (must exist if open_basedir is set)
787 * @return array Contains keys [path], [file], [filebody], [fileext], [realFileext]
788 */
789 public static function split_fileref($fileNameWithPath)
790 {
791 $reg = [];
792 if (preg_match('/(.*\\/)(.*)$/', $fileNameWithPath, $reg)) {
793 $info['path'] = $reg[1];
794 $info['file'] = $reg[2];
795 } else {
796 $info['path'] = '';
797 $info['file'] = $fileNameWithPath;
798 }
799 $reg = '';
800 // If open_basedir is set and the fileName was supplied without a path the is_dir check fails
801 if (!is_dir($fileNameWithPath) && preg_match('/(.*)\\.([^\\.]*$)/', $info['file'], $reg)) {
802 $info['filebody'] = $reg[1];
803 $info['fileext'] = strtolower($reg[2]);
804 $info['realFileext'] = $reg[2];
805 } else {
806 $info['filebody'] = $info['file'];
807 $info['fileext'] = '';
808 }
809 reset($info);
810 return $info;
811 }
812
813 /**
814 * Returns the directory part of a path without trailing slash
815 * If there is no dir-part, then an empty string is returned.
816 * Behaviour:
817 *
818 * '/dir1/dir2/script.php' => '/dir1/dir2'
819 * '/dir1/' => '/dir1'
820 * 'dir1/script.php' => 'dir1'
821 * 'd/script.php' => 'd'
822 * '/script.php' => ''
823 * '' => ''
824 *
825 * @param string $path Directory name / path
826 * @return string Processed input value. See function description.
827 */
828 public static function dirname($path)
829 {
830 $p = self::revExplode('/', $path, 2);
831 return count($p) === 2 ? $p[0] : '';
832 }
833
834 /**
835 * Returns TRUE if the first part of $str matches the string $partStr
836 *
837 * @param string $str Full string to check
838 * @param string $partStr Reference string which must be found as the "first part" of the full string
839 * @return bool TRUE if $partStr was found to be equal to the first part of $str
840 */
841 public static function isFirstPartOfStr($str, $partStr)
842 {
843 return $partStr != '' && strpos((string)$str, (string)$partStr, 0) === 0;
844 }
845
846 /**
847 * Formats the input integer $sizeInBytes as bytes/kilobytes/megabytes (-/K/M)
848 *
849 * @param int $sizeInBytes Number of bytes to format.
850 * @param string $labels Binary unit name "iec", decimal unit name "si" or labels for bytes, kilo, mega, giga, and so on separated by vertical bar (|) and possibly encapsulated in "". Eg: " | K| M| G". Defaults to "iec".
851 * @param int $base The unit base if not using a unit name. Defaults to 1024.
852 * @return string Formatted representation of the byte number, for output.
853 */
854 public static function formatSize($sizeInBytes, $labels = '', $base = 0)
855 {
856 $defaultFormats = [
857 'iec' => ['base' => 1024, 'labels' => [' ', ' Ki', ' Mi', ' Gi', ' Ti', ' Pi', ' Ei', ' Zi', ' Yi']],
858 'si' => ['base' => 1000, 'labels' => [' ', ' k', ' M', ' G', ' T', ' P', ' E', ' Z', ' Y']],
859 ];
860 // Set labels and base:
861 if (empty($labels)) {
862 $labels = 'iec';
863 }
864 if (isset($defaultFormats[$labels])) {
865 $base = $defaultFormats[$labels]['base'];
866 $labelArr = $defaultFormats[$labels]['labels'];
867 } else {
868 $base = (int)$base;
869 if ($base !== 1000 && $base !== 1024) {
870 $base = 1024;
871 }
872 $labelArr = explode('|', str_replace('"', '', $labels));
873 }
874 // @todo find out which locale is used for current BE user to cover the BE case as well
875 $oldLocale = setlocale(LC_NUMERIC, 0);
876 $newLocale = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE']->config['config']['locale_all'] : '';
877 if ($newLocale) {
878 setlocale(LC_NUMERIC, $newLocale);
879 }
880 $localeInfo = localeconv();
881 if ($newLocale) {
882 setlocale(LC_NUMERIC, $oldLocale);
883 }
884 $sizeInBytes = max($sizeInBytes, 0);
885 $multiplier = floor(($sizeInBytes ? log($sizeInBytes) : 0) / log($base));
886 $sizeInUnits = $sizeInBytes / pow($base, $multiplier);
887 if ($sizeInUnits > ($base * .9)) {
888 $multiplier++;
889 }
890 $multiplier = min($multiplier, count($labelArr) - 1);
891 $sizeInUnits = $sizeInBytes / pow($base, $multiplier);
892 return number_format($sizeInUnits, (($multiplier > 0) && ($sizeInUnits < 20)) ? 2 : 0, $localeInfo['decimal_point'], '') . $labelArr[$multiplier];
893 }
894
895 /**
896 * Returns microtime input to milliseconds
897 *
898 * @param string $microtime Microtime
899 * @return int Microtime input string converted to an integer (milliseconds)
900 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
901 */
902 public static function convertMicrotime($microtime)
903 {
904 static::logDeprecatedFunction();
905 $parts = explode(' ', $microtime);
906 return round(($parts[0] + $parts[1]) * 1000);
907 }
908
909 /**
910 * This splits a string by the chars in $operators (typical /+-*) and returns an array with them in
911 *
912 * @param string $string Input string, eg "123 + 456 / 789 - 4
913 * @param string $operators Operators to split by, typically "/+-*
914 * @return array Array with operators and operands separated.
915 * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::calc(), \TYPO3\CMS\Frontend\Imaging\GifBuilder::calcOffset()
916 */
917 public static function splitCalc($string, $operators)
918 {
919 $res = [];
920 $sign = '+';
921 while ($string) {
922 $valueLen = strcspn($string, $operators);
923 $value = substr($string, 0, $valueLen);
924 $res[] = [$sign, trim($value)];
925 $sign = substr($string, $valueLen, 1);
926 $string = substr($string, $valueLen + 1);
927 }
928 reset($res);
929 return $res;
930 }
931
932 /**
933 * Re-converts HTML entities if they have been converted by htmlspecialchars()
934 * Note: Use htmlspecialchars($str, ENT_COMPAT, 'UTF-8', FALSE) to avoid double encoding.
935 * This makes the call to this method obsolete.
936 *
937 * @param string $str String which contains eg. "&amp;amp;" which should stay "&amp;". Or "&amp;#1234;" to "&#1234;". Or "&amp;#x1b;" to "&#x1b;
938 * @return string Converted result.
939 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
940 */
941 public static function deHSCentities($str)
942 {
943 static::logDeprecatedFunction();
944 return preg_replace('/&amp;([#[:alnum:]]*;)/', '&\\1', $str);
945 }
946
947 /**
948 * This function is used to escape any ' -characters when transferring text to JavaScript!
949 *
950 * @param string $string String to escape
951 * @param bool $extended If set, also backslashes are escaped.
952 * @param string $char The character to escape, default is ' (single-quote)
953 * @return string Processed input string
954 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
955 */
956 public static function slashJS($string, $extended = false, $char = '\'')
957 {
958 static::logDeprecatedFunction();
959 if ($extended) {
960 $string = str_replace('\\', '\\\\', $string);
961 }
962 return str_replace($char, '\\' . $char, $string);
963 }
964
965 /**
966 * Version of rawurlencode() where all spaces (%20) are re-converted to space-characters.
967 * Useful when passing text to JavaScript where you simply url-encode it to get around problems with syntax-errors, linebreaks etc.
968 *
969 * @param string $str String to raw-url-encode with spaces preserved
970 * @return string Rawurlencoded result of input string, but with all %20 (space chars) converted to real spaces.
971 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, implement directly via PHP instead
972 */
973 public static function rawUrlEncodeJS($str)
974 {
975 static::logDeprecatedFunction();
976 return str_replace('%20', ' ', rawurlencode($str));
977 }
978
979 /**
980 * rawurlencode which preserves "/" chars
981 * Useful when file paths should keep the "/" chars, but have all other special chars encoded.
982 *
983 * @param string $str Input string
984 * @return string Output string
985 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use the PHP methods directly instead
986 */
987 public static function rawUrlEncodeFP($str)
988 {
989 static::logDeprecatedFunction();
990 return str_replace('%2F', '/', rawurlencode($str));
991 }
992
993 /**
994 * Checking syntax of input email address
995 *
996 * http://tools.ietf.org/html/rfc3696
997 * International characters are allowed in email. So the whole address needs
998 * to be converted to punicode before passing it to filter_var(). We convert
999 * the user- and domain part separately to increase the chance of hitting an
1000 * entry in self::$idnaStringCache.
1001 *
1002 * Also the @ sign may appear multiple times in an address. If not used as
1003 * a boundary marker between the user- and domain part, it must be escaped
1004 * with a backslash: \@. This mean we can not just explode on the @ sign and
1005 * expect to get just two parts. So we pop off the domain and then glue the
1006 * rest together again.
1007 *
1008 * @param string $email Input string to evaluate
1009 * @return bool Returns TRUE if the $email address (input string) is valid
1010 */
1011 public static function validEmail($email)
1012 {
1013 // Early return in case input is not a string
1014 if (!is_string($email)) {
1015 return false;
1016 }
1017 $atPosition = strrpos($email, '@');
1018 if (!$atPosition || $atPosition + 1 === strlen($email)) {
1019 // Return if no @ found or it is placed at the very beginning or end of the email
1020 return false;
1021 }
1022 $domain = substr($email, $atPosition + 1);
1023 $user = substr($email, 0, $atPosition);
1024 if (!preg_match('/^[a-z0-9.\\-]*$/i', $domain)) {
1025 $domain = self::idnaEncode($domain);
1026 }
1027 return filter_var($user . '@' . $domain, FILTER_VALIDATE_EMAIL) !== false;
1028 }
1029
1030 /**
1031 * Converts string to uppercase
1032 * The function converts all Latin characters (a-z, but no accents, etc) to
1033 * uppercase. It is safe for all supported character sets (incl. utf-8).
1034 * Unlike strtoupper() it does not honour the locale.
1035 *
1036 * @param string $str Input string
1037 * @return string Uppercase String
1038 * @deprecated since TYPO3 CMS v8, this method will be removed in TYPO3 CMS v9, use mb_strtoupper() instead
1039 */
1040 public static function strtoupper($str)
1041 {
1042 self::logDeprecatedFunction();
1043 return strtr((string)$str, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
1044 }
1045
1046 /**
1047 * Converts string to lowercase
1048 * The function converts all Latin characters (A-Z, but no accents, etc) to
1049 * lowercase. It is safe for all supported character sets (incl. utf-8).
1050 * Unlike strtolower() it does not honour the locale.
1051 *
1052 * @param string $str Input string
1053 * @return string Lowercase String
1054 * @deprecated since TYPO3 CMS v8, this method will be removed in TYPO3 CMS v9, use mb_strtolower() instead
1055 */
1056 public static function strtolower($str)
1057 {
1058 self::logDeprecatedFunction();
1059 return strtr((string)$str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
1060 }
1061
1062 /**
1063 * Returns a string of highly randomized bytes (over the full 8-bit range).
1064 *
1065 * @param int $bytesToReturn Number of bytes to return
1066 * @return string Random Bytes
1067 * @deprecated since TYPO3 CMS 8, this method will be removed in TYPO3 CMS 9. Use \TYPO3\CMS\Core\Crypto\Random->generateRandomBytes() instead
1068 */
1069 public static function generateRandomBytes($bytesToReturn)
1070 {
1071 self::logDeprecatedFunction();
1072 return self::makeInstance(Random::class)->generateRandomBytes($bytesToReturn);
1073 }
1074
1075 /**
1076 * Returns an ASCII string (punicode) representation of $value
1077 *
1078 * @param string $value
1079 * @return string An ASCII encoded (punicode) string
1080 */
1081 public static function idnaEncode($value)
1082 {
1083 if (isset(self::$idnaStringCache[$value])) {
1084 return self::$idnaStringCache[$value];
1085 } else {
1086 if (!self::$idnaConverter) {
1087 self::$idnaConverter = new \Mso\IdnaConvert\IdnaConvert(['idn_version' => 2008]);
1088 }
1089 self::$idnaStringCache[$value] = self::$idnaConverter->encode($value);
1090 return self::$idnaStringCache[$value];
1091 }
1092 }
1093
1094 /**
1095 * Returns a hex representation of a random byte string.
1096 *
1097 * @param int $count Number of hex characters to return
1098 * @return string Random Bytes
1099 * @deprecated since TYPO3 CMS 8, this method will be removed in TYPO3 CMS 9. Use \TYPO3\CMS\Core\Crypto\Random::generateRandomHexString() instead
1100 */
1101 public static function getRandomHexString($count)
1102 {
1103 self::logDeprecatedFunction();
1104 return self::makeInstance(Random::class)->generateRandomHexString($count);
1105 }
1106
1107 /**
1108 * Returns a given string with underscores as UpperCamelCase.
1109 * Example: Converts blog_example to BlogExample
1110 *
1111 * @param string $string String to be converted to camel case
1112 * @return string UpperCamelCasedWord
1113 */
1114 public static function underscoredToUpperCamelCase($string)
1115 {
1116 return str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($string))));
1117 }
1118
1119 /**
1120 * Returns a given string with underscores as lowerCamelCase.
1121 * Example: Converts minimal_value to minimalValue
1122 *
1123 * @param string $string String to be converted to camel case
1124 * @return string lowerCamelCasedWord
1125 */
1126 public static function underscoredToLowerCamelCase($string)
1127 {
1128 return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($string)))));
1129 }
1130
1131 /**
1132 * Returns a given CamelCasedString as an lowercase string with underscores.
1133 * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value
1134 *
1135 * @param string $string String to be converted to lowercase underscore
1136 * @return string lowercase_and_underscored_string
1137 */
1138 public static function camelCaseToLowerCaseUnderscored($string)
1139 {
1140 $value = preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $string);
1141 return mb_strtolower($value, 'utf-8');
1142 }
1143
1144 /**
1145 * Converts the first char of a string to lowercase if it is a latin character (A-Z).
1146 * Example: Converts "Hello World" to "hello World"
1147 *
1148 * @param string $string The string to be used to lowercase the first character
1149 * @return string The string with the first character as lowercase
1150 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
1151 */
1152 public static function lcfirst($string)
1153 {
1154 static::logDeprecatedFunction();
1155 return lcfirst($string);
1156 }
1157
1158 /**
1159 * Checks if a given string is a Uniform Resource Locator (URL).
1160 *
1161 * On seriously malformed URLs, parse_url may return FALSE and emit an
1162 * E_WARNING.
1163 *
1164 * filter_var() requires a scheme to be present.
1165 *
1166 * http://www.faqs.org/rfcs/rfc2396.html
1167 * Scheme names consist of a sequence of characters beginning with a
1168 * lower case letter and followed by any combination of lower case letters,
1169 * digits, plus ("+"), period ("."), or hyphen ("-"). For resiliency,
1170 * programs interpreting URI should treat upper case letters as equivalent to
1171 * lower case in scheme names (e.g., allow "HTTP" as well as "http").
1172 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
1173 *
1174 * Convert the domain part to punicode if it does not look like a regular
1175 * domain name. Only the domain part because RFC3986 specifies the the rest of
1176 * the url may not contain special characters:
1177 * http://tools.ietf.org/html/rfc3986#appendix-A
1178 *
1179 * @param string $url The URL to be validated
1180 * @return bool Whether the given URL is valid
1181 */
1182 public static function isValidUrl($url)
1183 {
1184 $parsedUrl = parse_url($url);
1185 if (!$parsedUrl || !isset($parsedUrl['scheme'])) {
1186 return false;
1187 }
1188 // HttpUtility::buildUrl() will always build urls with <scheme>://
1189 // our original $url might only contain <scheme>: (e.g. mail:)
1190 // so we convert that to the double-slashed version to ensure
1191 // our check against the $recomposedUrl is proper
1192 if (!self::isFirstPartOfStr($url, $parsedUrl['scheme'] . '://')) {
1193 $url = str_replace($parsedUrl['scheme'] . ':', $parsedUrl['scheme'] . '://', $url);
1194 }
1195 $recomposedUrl = HttpUtility::buildUrl($parsedUrl);
1196 if ($recomposedUrl !== $url) {
1197 // The parse_url() had to modify characters, so the URL is invalid
1198 return false;
1199 }
1200 if (isset($parsedUrl['host']) && !preg_match('/^[a-z0-9.\\-]*$/i', $parsedUrl['host'])) {
1201 $parsedUrl['host'] = self::idnaEncode($parsedUrl['host']);
1202 }
1203 return filter_var(HttpUtility::buildUrl($parsedUrl), FILTER_VALIDATE_URL) !== false;
1204 }
1205
1206 /*************************
1207 *
1208 * ARRAY FUNCTIONS
1209 *
1210 *************************/
1211
1212 /**
1213 * Explodes a $string delimited by $delim and casts each item in the array to (int).
1214 * Corresponds to \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(), but with conversion to integers for all values.
1215 *
1216 * @param string $delimiter Delimiter string to explode with
1217 * @param string $string The string to explode
1218 * @param bool $removeEmptyValues If set, all empty values (='') will NOT be set in output
1219 * @param int $limit If positive, the result will contain a maximum of limit elements,
1220 * @return array Exploded values, all converted to integers
1221 */
1222 public static function intExplode($delimiter, $string, $removeEmptyValues = false, $limit = 0)
1223 {
1224 $result = explode($delimiter, $string);
1225 foreach ($result as $key => &$value) {
1226 if ($removeEmptyValues && ($value === '' || trim($value) === '')) {
1227 unset($result[$key]);
1228 } else {
1229 $value = (int)$value;
1230 }
1231 }
1232 unset($value);
1233 if ($limit !== 0) {
1234 if ($limit < 0) {
1235 $result = array_slice($result, 0, $limit);
1236 } elseif (count($result) > $limit) {
1237 $lastElements = array_slice($result, $limit - 1);
1238 $result = array_slice($result, 0, $limit - 1);
1239 $result[] = implode($delimiter, $lastElements);
1240 }
1241 }
1242 return $result;
1243 }
1244
1245 /**
1246 * Reverse explode which explodes the string counting from behind.
1247 *
1248 * Note: The delimiter has to given in the reverse order as
1249 * it is occurring within the string.
1250 *
1251 * GeneralUtility::revExplode('[]', '[my][words][here]', 2)
1252 * ==> array('[my][words', 'here]')
1253 *
1254 * @param string $delimiter Delimiter string to explode with
1255 * @param string $string The string to explode
1256 * @param int $count Number of array entries
1257 * @return array Exploded values
1258 */
1259 public static function revExplode($delimiter, $string, $count = 0)
1260 {
1261 // 2 is the (currently, as of 2014-02) most-used value for $count in the core, therefore we check it first
1262 if ($count === 2) {
1263 $position = strrpos($string, strrev($delimiter));
1264 if ($position !== false) {
1265 return [substr($string, 0, $position), substr($string, $position + strlen($delimiter))];
1266 } else {
1267 return [$string];
1268 }
1269 } elseif ($count <= 1) {
1270 return [$string];
1271 } else {
1272 $explodedValues = explode($delimiter, strrev($string), $count);
1273 $explodedValues = array_map('strrev', $explodedValues);
1274 return array_reverse($explodedValues);
1275 }
1276 }
1277
1278 /**
1279 * Explodes a string and trims all values for whitespace in the end.
1280 * If $onlyNonEmptyValues is set, then all blank ('') values are removed.
1281 *
1282 * @param string $delim Delimiter string to explode with
1283 * @param string $string The string to explode
1284 * @param bool $removeEmptyValues If set, all empty values will be removed in output
1285 * @param int $limit If limit is set and positive, the returned array will contain a maximum of limit elements with
1286 * the last element containing the rest of string. If the limit parameter is negative, all components
1287 * except the last -limit are returned.
1288 * @return array Exploded values
1289 */
1290 public static function trimExplode($delim, $string, $removeEmptyValues = false, $limit = 0)
1291 {
1292 $result = explode($delim, $string);
1293 if ($removeEmptyValues) {
1294 $temp = [];
1295 foreach ($result as $value) {
1296 if (trim($value) !== '') {
1297 $temp[] = $value;
1298 }
1299 }
1300 $result = $temp;
1301 }
1302 if ($limit > 0 && count($result) > $limit) {
1303 $lastElements = array_splice($result, $limit - 1);
1304 $result[] = implode($delim, $lastElements);
1305 } elseif ($limit < 0) {
1306 $result = array_slice($result, 0, $limit);
1307 }
1308 $result = array_map('trim', $result);
1309 return $result;
1310 }
1311
1312 /**
1313 * Implodes a multidim-array into GET-parameters (eg. &param[key][key2]=value2&param[key][key3]=value3)
1314 *
1315 * @param string $name Name prefix for entries. Set to blank if you wish none.
1316 * @param array $theArray The (multidimensional) array to implode
1317 * @param string $str (keep blank)
1318 * @param bool $skipBlank If set, parameters which were blank strings would be removed.
1319 * @param bool $rawurlencodeParamName If set, the param name itself (for example "param[key][key2]") would be rawurlencoded as well.
1320 * @return string Imploded result, fx. &param[key][key2]=value2&param[key][key3]=value3
1321 * @see explodeUrl2Array()
1322 */
1323 public static function implodeArrayForUrl($name, array $theArray, $str = '', $skipBlank = false, $rawurlencodeParamName = false)
1324 {
1325 foreach ($theArray as $Akey => $AVal) {
1326 $thisKeyName = $name ? $name . '[' . $Akey . ']' : $Akey;
1327 if (is_array($AVal)) {
1328 $str = self::implodeArrayForUrl($thisKeyName, $AVal, $str, $skipBlank, $rawurlencodeParamName);
1329 } else {
1330 if (!$skipBlank || (string)$AVal !== '') {
1331 $str .= '&' . ($rawurlencodeParamName ? rawurlencode($thisKeyName) : $thisKeyName) . '=' . rawurlencode($AVal);
1332 }
1333 }
1334 }
1335 return $str;
1336 }
1337
1338 /**
1339 * Explodes a string with GETvars (eg. "&id=1&type=2&ext[mykey]=3") into an array
1340 *
1341 * @param string $string GETvars string
1342 * @param bool $multidim If set, the string will be parsed into a multidimensional array if square brackets are used in variable names (using PHP function parse_str())
1343 * @return array Array of values. All values AND keys are rawurldecoded() as they properly should be. But this means that any implosion of the array again must rawurlencode it!
1344 * @see implodeArrayForUrl()
1345 */
1346 public static function explodeUrl2Array($string, $multidim = false)
1347 {
1348 $output = [];
1349 if ($multidim) {
1350 parse_str($string, $output);
1351 } else {
1352 $p = explode('&', $string);
1353 foreach ($p as $v) {
1354 if ($v !== '') {
1355 list($pK, $pV) = explode('=', $v, 2);
1356 $output[rawurldecode($pK)] = rawurldecode($pV);
1357 }
1358 }
1359 }
1360 return $output;
1361 }
1362
1363 /**
1364 * Returns an array with selected keys from incoming data.
1365 * (Better read source code if you want to find out...)
1366 *
1367 * @param string $varList List of variable/key names
1368 * @param array $getArray Array from where to get values based on the keys in $varList
1369 * @param bool $GPvarAlt If set, then \TYPO3\CMS\Core\Utility\GeneralUtility::_GP() is used to fetch the value if not found (isset) in the $getArray
1370 * @return array Output array with selected variables.
1371 */
1372 public static function compileSelectedGetVarsFromArray($varList, array $getArray, $GPvarAlt = true)
1373 {
1374 $keys = self::trimExplode(',', $varList, true);
1375 $outArr = [];
1376 foreach ($keys as $v) {
1377 if (isset($getArray[$v])) {
1378 $outArr[$v] = $getArray[$v];
1379 } elseif ($GPvarAlt) {
1380 $outArr[$v] = self::_GP($v);
1381 }
1382 }
1383 return $outArr;
1384 }
1385
1386 /**
1387 * Takes a row and returns a CSV string of the values with $delim (default is ,) and $quote (default is ") as separator chars.
1388 *
1389 * @param array $row Input array of values
1390 * @param string $delim Delimited, default is comma
1391 * @param string $quote Quote-character to wrap around the values.
1392 * @return string A single line of CSV
1393 */
1394 public static function csvValues(array $row, $delim = ',', $quote = '"')
1395 {
1396 $out = [];
1397 foreach ($row as $value) {
1398 $out[] = str_replace($quote, $quote . $quote, $value);
1399 }
1400 $str = $quote . implode(($quote . $delim . $quote), $out) . $quote;
1401 return $str;
1402 }
1403
1404 /**
1405 * Removes dots "." from end of a key identifier of TypoScript styled array.
1406 * array('key.' => array('property.' => 'value')) --> array('key' => array('property' => 'value'))
1407 *
1408 * @param array $ts TypoScript configuration array
1409 * @return array TypoScript configuration array without dots at the end of all keys
1410 */
1411 public static function removeDotsFromTS(array $ts)
1412 {
1413 $out = [];
1414 foreach ($ts as $key => $value) {
1415 if (is_array($value)) {
1416 $key = rtrim($key, '.');
1417 $out[$key] = self::removeDotsFromTS($value);
1418 } else {
1419 $out[$key] = $value;
1420 }
1421 }
1422 return $out;
1423 }
1424
1425 /*************************
1426 *
1427 * HTML/XML PROCESSING
1428 *
1429 *************************/
1430 /**
1431 * Returns an array with all attributes of the input HTML tag as key/value pairs. Attributes are only lowercase a-z
1432 * $tag is either a whole tag (eg '<TAG OPTION ATTRIB=VALUE>') or the parameter list (ex ' OPTION ATTRIB=VALUE>')
1433 * If an attribute is empty, then the value for the key is empty. You can check if it existed with isset()
1434 *
1435 * @param string $tag HTML-tag string (or attributes only)
1436 * @return array Array with the attribute values.
1437 */
1438 public static function get_tag_attributes($tag)
1439 {
1440 $components = self::split_tag_attributes($tag);
1441 // Attribute name is stored here
1442 $name = '';
1443 $valuemode = false;
1444 $attributes = [];
1445 foreach ($components as $key => $val) {
1446 // Only if $name is set (if there is an attribute, that waits for a value), that valuemode is enabled. This ensures that the attribute is assigned it's value
1447 if ($val !== '=') {
1448 if ($valuemode) {
1449 if ($name) {
1450 $attributes[$name] = $val;
1451 $name = '';
1452 }
1453 } else {
1454 if ($key = strtolower(preg_replace('/[^[:alnum:]_\\:\\-]/', '', $val))) {
1455 $attributes[$key] = '';
1456 $name = $key;
1457 }
1458 }
1459 $valuemode = false;
1460 } else {
1461 $valuemode = true;
1462 }
1463 }
1464 return $attributes;
1465 }
1466
1467 /**
1468 * Returns an array with the 'components' from an attribute list from an HTML tag. The result is normally analyzed by get_tag_attributes
1469 * Removes tag-name if found
1470 *
1471 * @param string $tag HTML-tag string (or attributes only)
1472 * @return array Array with the attribute values.
1473 */
1474 public static function split_tag_attributes($tag)
1475 {
1476 $tag_tmp = trim(preg_replace('/^<[^[:space:]]*/', '', trim($tag)));
1477 // Removes any > in the end of the string
1478 $tag_tmp = trim(rtrim($tag_tmp, '>'));
1479 $value = [];
1480 // Compared with empty string instead , 030102
1481 while ($tag_tmp !== '') {
1482 $firstChar = $tag_tmp[0];
1483 if ($firstChar === '"' || $firstChar === '\'') {
1484 $reg = explode($firstChar, $tag_tmp, 3);
1485 $value[] = $reg[1];
1486 $tag_tmp = trim($reg[2]);
1487 } elseif ($firstChar === '=') {
1488 $value[] = '=';
1489 // Removes = chars.
1490 $tag_tmp = trim(substr($tag_tmp, 1));
1491 } else {
1492 // There are '' around the value. We look for the next ' ' or '>'
1493 $reg = preg_split('/[[:space:]=]/', $tag_tmp, 2);
1494 $value[] = trim($reg[0]);
1495 $tag_tmp = trim(substr($tag_tmp, strlen($reg[0]), 1) . $reg[1]);
1496 }
1497 }
1498 reset($value);
1499 return $value;
1500 }
1501
1502 /**
1503 * Implodes attributes in the array $arr for an attribute list in eg. and HTML tag (with quotes)
1504 *
1505 * @param array $arr Array with attribute key/value pairs, eg. "bgcolor"=>"red", "border"=>0
1506 * @param bool $xhtmlSafe If set the resulting attribute list will have a) all attributes in lowercase (and duplicates weeded out, first entry taking precedence) and b) all values htmlspecialchar()'ed. It is recommended to use this switch!
1507 * @param bool $dontOmitBlankAttribs If TRUE, don't check if values are blank. Default is to omit attributes with blank values.
1508 * @return string Imploded attributes, eg. 'bgcolor="red" border="0"'
1509 */
1510 public static function implodeAttributes(array $arr, $xhtmlSafe = false, $dontOmitBlankAttribs = false)
1511 {
1512 if ($xhtmlSafe) {
1513 $newArr = [];
1514 foreach ($arr as $p => $v) {
1515 if (!isset($newArr[strtolower($p)])) {
1516 $newArr[strtolower($p)] = htmlspecialchars($v);
1517 }
1518 }
1519 $arr = $newArr;
1520 }
1521 $list = [];
1522 foreach ($arr as $p => $v) {
1523 if ((string)$v !== '' || $dontOmitBlankAttribs) {
1524 $list[] = $p . '="' . $v . '"';
1525 }
1526 }
1527 return implode(' ', $list);
1528 }
1529
1530 /**
1531 * Wraps JavaScript code XHTML ready with <script>-tags
1532 * Automatic re-indenting of the JS code is done by using the first line as indent reference.
1533 * This is nice for indenting JS code with PHP code on the same level.
1534 *
1535 * @param string $string JavaScript code
1536 * @param null $_ unused, will be removed in TYPO3 CMS 9
1537 * @return string The wrapped JS code, ready to put into a XHTML page
1538 */
1539 public static function wrapJS($string, $_ = null)
1540 {
1541 if ($_ !== null) {
1542 self::deprecationLog('Parameter 2 of GeneralUtility::wrapJS is obsolete and can be omitted.');
1543 }
1544
1545 if (trim($string)) {
1546 // remove nl from the beginning
1547 $string = ltrim($string, LF);
1548 // re-ident to one tab using the first line as reference
1549 $match = [];
1550 if (preg_match('/^(\\t+)/', $string, $match)) {
1551 $string = str_replace($match[1], TAB, $string);
1552 }
1553 return '<script type="text/javascript">
1554 /*<![CDATA[*/
1555 ' . $string . '
1556 /*]]>*/
1557 </script>';
1558 }
1559 return '';
1560 }
1561
1562 /**
1563 * Parses XML input into a PHP array with associative keys
1564 *
1565 * @param string $string XML data input
1566 * @param int $depth Number of element levels to resolve the XML into an array. Any further structure will be set as XML.
1567 * @param array $parserOptions Options that will be passed to PHP's xml_parser_set_option()
1568 * @return mixed The array with the parsed structure unless the XML parser returns with an error in which case the error message string is returned.
1569 */
1570 public static function xml2tree($string, $depth = 999, $parserOptions = [])
1571 {
1572 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
1573 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
1574 $parser = xml_parser_create();
1575 $vals = [];
1576 $index = [];
1577 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1578 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
1579 foreach ($parserOptions as $option => $value) {
1580 xml_parser_set_option($parser, $option, $value);
1581 }
1582 xml_parse_into_struct($parser, $string, $vals, $index);
1583 libxml_disable_entity_loader($previousValueOfEntityLoader);
1584 if (xml_get_error_code($parser)) {
1585 return 'Line ' . xml_get_current_line_number($parser) . ': ' . xml_error_string(xml_get_error_code($parser));
1586 }
1587 xml_parser_free($parser);
1588 $stack = [[]];
1589 $stacktop = 0;
1590 $startPoint = 0;
1591 $tagi = [];
1592 foreach ($vals as $key => $val) {
1593 $type = $val['type'];
1594 // open tag:
1595 if ($type === 'open' || $type === 'complete') {
1596 $stack[$stacktop++] = $tagi;
1597 if ($depth == $stacktop) {
1598 $startPoint = $key;
1599 }
1600 $tagi = ['tag' => $val['tag']];
1601 if (isset($val['attributes'])) {
1602 $tagi['attrs'] = $val['attributes'];
1603 }
1604 if (isset($val['value'])) {
1605 $tagi['values'][] = $val['value'];
1606 }
1607 }
1608 // finish tag:
1609 if ($type === 'complete' || $type === 'close') {
1610 $oldtagi = $tagi;
1611 $tagi = $stack[--$stacktop];
1612 $oldtag = $oldtagi['tag'];
1613 unset($oldtagi['tag']);
1614 if ($depth == $stacktop + 1) {
1615 if ($key - $startPoint > 0) {
1616 $partArray = array_slice($vals, $startPoint + 1, $key - $startPoint - 1);
1617 $oldtagi['XMLvalue'] = self::xmlRecompileFromStructValArray($partArray);
1618 } else {
1619 $oldtagi['XMLvalue'] = $oldtagi['values'][0];
1620 }
1621 }
1622 $tagi['ch'][$oldtag][] = $oldtagi;
1623 unset($oldtagi);
1624 }
1625 // cdata
1626 if ($type === 'cdata') {
1627 $tagi['values'][] = $val['value'];
1628 }
1629 }
1630 return $tagi['ch'];
1631 }
1632
1633 /**
1634 * Turns PHP array into XML. See array2xml()
1635 *
1636 * @param array $array The input PHP array with any kind of data; text, binary, integers. Not objects though.
1637 * @param string $docTag Alternative document tag. Default is "phparray".
1638 * @param array $options Options for the compilation. See array2xml() for description.
1639 * @param string $charset Forced charset to prologue
1640 * @return string An XML string made from the input content in the array.
1641 * @see xml2array(),array2xml()
1642 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9.
1643 */
1644 public static function array2xml_cs(array $array, $docTag = 'phparray', array $options = [], $charset = '')
1645 {
1646 static::logDeprecatedFunction();
1647 // Set default charset unless explicitly specified
1648 $charset = $charset ?: 'utf-8';
1649 // Return XML:
1650 return '<?xml version="1.0" encoding="' . htmlspecialchars($charset) . '" standalone="yes" ?>' . LF . self::array2xml($array, '', 0, $docTag, 0, $options);
1651 }
1652
1653 /**
1654 * Converts a PHP array into an XML string.
1655 * The XML output is optimized for readability since associative keys are used as tag names.
1656 * This also means that only alphanumeric characters are allowed in the tag names AND only keys NOT starting with numbers (so watch your usage of keys!). However there are options you can set to avoid this problem.
1657 * Numeric keys are stored with the default tag name "numIndex" but can be overridden to other formats)
1658 * The function handles input values from the PHP array in a binary-safe way; All characters below 32 (except 9,10,13) will trigger the content to be converted to a base64-string
1659 * The PHP variable type of the data IS preserved as long as the types are strings, arrays, integers and booleans. Strings are the default type unless the "type" attribute is set.
1660 * The output XML has been tested with the PHP XML-parser and parses OK under all tested circumstances with 4.x versions. However, with PHP5 there seems to be the need to add an XML prologue a la <?xml version="1.0" encoding="[charset]" standalone="yes" ?> - otherwise UTF-8 is assumed! Unfortunately, many times the output from this function is used without adding that prologue meaning that non-ASCII characters will break the parsing!! This suchs of course! Effectively it means that the prologue should always be prepended setting the right characterset, alternatively the system should always run as utf-8!
1661 * However using MSIE to read the XML output didn't always go well: One reason could be that the character encoding is not observed in the PHP data. The other reason may be if the tag-names are invalid in the eyes of MSIE. Also using the namespace feature will make MSIE break parsing. There might be more reasons...
1662 *
1663 * @param array $array The input PHP array with any kind of data; text, binary, integers. Not objects though.
1664 * @param string $NSprefix tag-prefix, eg. a namespace prefix like "T3:"
1665 * @param int $level Current recursion level. Don't change, stay at zero!
1666 * @param string $docTag Alternative document tag. Default is "phparray".
1667 * @param int $spaceInd If greater than zero, then the number of spaces corresponding to this number is used for indenting, if less than zero - no indentation, if zero - a single TAB is used
1668 * @param array $options Options for the compilation. Key "useNindex" => 0/1 (boolean: whether to use "n0, n1, n2" for num. indexes); Key "useIndexTagForNum" => "[tag for numerical indexes]"; Key "useIndexTagForAssoc" => "[tag for associative indexes"; Key "parentTagMap" => array('parentTag' => 'thisLevelTag')
1669 * @param array $stackData Stack data. Don't touch.
1670 * @return string An XML string made from the input content in the array.
1671 * @see xml2array()
1672 */
1673 public static function array2xml(array $array, $NSprefix = '', $level = 0, $docTag = 'phparray', $spaceInd = 0, array $options = [], array $stackData = [])
1674 {
1675 // The list of byte values which will trigger binary-safe storage. If any value has one of these char values in it, it will be encoded in base64
1676 $binaryChars = chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5) . chr(6) . chr(7) . chr(8) . chr(11) . chr(12) . chr(14) . chr(15) . chr(16) . chr(17) . chr(18) . chr(19) . chr(20) . chr(21) . chr(22) . chr(23) . chr(24) . chr(25) . chr(26) . chr(27) . chr(28) . chr(29) . chr(30) . chr(31);
1677 // Set indenting mode:
1678 $indentChar = $spaceInd ? ' ' : TAB;
1679 $indentN = $spaceInd > 0 ? $spaceInd : 1;
1680 $nl = $spaceInd >= 0 ? LF : '';
1681 // Init output variable:
1682 $output = '';
1683 // Traverse the input array
1684 foreach ($array as $k => $v) {
1685 $attr = '';
1686 $tagName = $k;
1687 // Construct the tag name.
1688 // Use tag based on grand-parent + parent tag name
1689 if (isset($options['grandParentTagMap'][$stackData['grandParentTagName'] . '/' . $stackData['parentTagName']])) {
1690 $attr .= ' index="' . htmlspecialchars($tagName) . '"';
1691 $tagName = (string)$options['grandParentTagMap'][$stackData['grandParentTagName'] . '/' . $stackData['parentTagName']];
1692 } elseif (isset($options['parentTagMap'][$stackData['parentTagName'] . ':_IS_NUM']) && MathUtility::canBeInterpretedAsInteger($tagName)) {
1693 // Use tag based on parent tag name + if current tag is numeric
1694 $attr .= ' index="' . htmlspecialchars($tagName) . '"';
1695 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName'] . ':_IS_NUM'];
1696 } elseif (isset($options['parentTagMap'][$stackData['parentTagName'] . ':' . $tagName])) {
1697 // Use tag based on parent tag name + current tag
1698 $attr .= ' index="' . htmlspecialchars($tagName) . '"';
1699 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName'] . ':' . $tagName];
1700 } elseif (isset($options['parentTagMap'][$stackData['parentTagName']])) {
1701 // Use tag based on parent tag name:
1702 $attr .= ' index="' . htmlspecialchars($tagName) . '"';
1703 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName']];
1704 } elseif (MathUtility::canBeInterpretedAsInteger($tagName)) {
1705 // If integer...;
1706 if ($options['useNindex']) {
1707 // If numeric key, prefix "n"
1708 $tagName = 'n' . $tagName;
1709 } else {
1710 // Use special tag for num. keys:
1711 $attr .= ' index="' . $tagName . '"';
1712 $tagName = $options['useIndexTagForNum'] ?: 'numIndex';
1713 }
1714 } elseif ($options['useIndexTagForAssoc']) {
1715 // Use tag for all associative keys:
1716 $attr .= ' index="' . htmlspecialchars($tagName) . '"';
1717 $tagName = $options['useIndexTagForAssoc'];
1718 }
1719 // The tag name is cleaned up so only alphanumeric chars (plus - and _) are in there and not longer than 100 chars either.
1720 $tagName = substr(preg_replace('/[^[:alnum:]_-]/', '', $tagName), 0, 100);
1721 // If the value is an array then we will call this function recursively:
1722 if (is_array($v)) {
1723 // Sub elements:
1724 if ($options['alt_options'][$stackData['path'] . '/' . $tagName]) {
1725 $subOptions = $options['alt_options'][$stackData['path'] . '/' . $tagName];
1726 $clearStackPath = $subOptions['clearStackPath'];
1727 } else {
1728 $subOptions = $options;
1729 $clearStackPath = false;
1730 }
1731 if (empty($v)) {
1732 $content = '';
1733 } else {
1734 $content = $nl . self::array2xml($v, $NSprefix, ($level + 1), '', $spaceInd, $subOptions, [
1735 'parentTagName' => $tagName,
1736 'grandParentTagName' => $stackData['parentTagName'],
1737 'path' => ($clearStackPath ? '' : $stackData['path'] . '/' . $tagName)
1738 ]) . ($spaceInd >= 0 ? str_pad('', ($level + 1) * $indentN, $indentChar) : '');
1739 }
1740 // Do not set "type = array". Makes prettier XML but means that empty arrays are not restored with xml2array
1741 if ((int)$options['disableTypeAttrib'] != 2) {
1742 $attr .= ' type="array"';
1743 }
1744 } else {
1745 // Just a value:
1746 // Look for binary chars:
1747 $vLen = strlen($v);
1748 // Go for base64 encoding if the initial segment NOT matching any binary char has the same length as the whole string!
1749 if ($vLen && strcspn($v, $binaryChars) != $vLen) {
1750 // If the value contained binary chars then we base64-encode it an set an attribute to notify this situation:
1751 $content = $nl . chunk_split(base64_encode($v));
1752 $attr .= ' base64="1"';
1753 } else {
1754 // Otherwise, just htmlspecialchar the stuff:
1755 $content = htmlspecialchars($v);
1756 $dType = gettype($v);
1757 if ($dType === 'string') {
1758 if ($options['useCDATA'] && $content != $v) {
1759 $content = '<![CDATA[' . $v . ']]>';
1760 }
1761 } elseif (!$options['disableTypeAttrib']) {
1762 $attr .= ' type="' . $dType . '"';
1763 }
1764 }
1765 }
1766 if ((string)$tagName !== '') {
1767 // Add the element to the output string:
1768 $output .= ($spaceInd >= 0 ? str_pad('', ($level + 1) * $indentN, $indentChar) : '')
1769 . '<' . $NSprefix . $tagName . $attr . '>' . $content . '</' . $NSprefix . $tagName . '>' . $nl;
1770 }
1771 }
1772 // If we are at the outer-most level, then we finally wrap it all in the document tags and return that as the value:
1773 if (!$level) {
1774 $output = '<' . $docTag . '>' . $nl . $output . '</' . $docTag . '>';
1775 }
1776 return $output;
1777 }
1778
1779 /**
1780 * Converts an XML string to a PHP array.
1781 * This is the reverse function of array2xml()
1782 * This is a wrapper for xml2arrayProcess that adds a two-level cache
1783 *
1784 * @param string $string XML content to convert into an array
1785 * @param string $NSprefix The tag-prefix resolve, eg. a namespace like "T3:"
1786 * @param bool $reportDocTag If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
1787 * @return mixed If the parsing had errors, a string with the error message is returned. Otherwise an array with the content.
1788 * @see array2xml(),xml2arrayProcess()
1789 */
1790 public static function xml2array($string, $NSprefix = '', $reportDocTag = false)
1791 {
1792 static $firstLevelCache = [];
1793 $identifier = md5($string . $NSprefix . ($reportDocTag ? '1' : '0'));
1794 // Look up in first level cache
1795 if (!empty($firstLevelCache[$identifier])) {
1796 $array = $firstLevelCache[$identifier];
1797 } else {
1798 $array = self::xml2arrayProcess(trim($string), $NSprefix, $reportDocTag);
1799 // Store content in first level cache
1800 $firstLevelCache[$identifier] = $array;
1801 }
1802 return $array;
1803 }
1804
1805 /**
1806 * Converts an XML string to a PHP array.
1807 * This is the reverse function of array2xml()
1808 *
1809 * @param string $string XML content to convert into an array
1810 * @param string $NSprefix The tag-prefix resolve, eg. a namespace like "T3:"
1811 * @param bool $reportDocTag If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
1812 * @return mixed If the parsing had errors, a string with the error message is returned. Otherwise an array with the content.
1813 * @see array2xml()
1814 */
1815 protected static function xml2arrayProcess($string, $NSprefix = '', $reportDocTag = false)
1816 {
1817 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
1818 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
1819 // Create parser:
1820 $parser = xml_parser_create();
1821 $vals = [];
1822 $index = [];
1823 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1824 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
1825 // Default output charset is UTF-8, only ASCII, ISO-8859-1 and UTF-8 are supported!!!
1826 $match = [];
1827 preg_match('/^[[:space:]]*<\\?xml[^>]*encoding[[:space:]]*=[[:space:]]*"([^"]*)"/', substr($string, 0, 200), $match);
1828 $theCharset = $match[1] ?: 'utf-8';
1829 // us-ascii / utf-8 / iso-8859-1
1830 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $theCharset);
1831 // Parse content:
1832 xml_parse_into_struct($parser, $string, $vals, $index);
1833 libxml_disable_entity_loader($previousValueOfEntityLoader);
1834 // If error, return error message:
1835 if (xml_get_error_code($parser)) {
1836 return 'Line ' . xml_get_current_line_number($parser) . ': ' . xml_error_string(xml_get_error_code($parser));
1837 }
1838 xml_parser_free($parser);
1839 // Init vars:
1840 $stack = [[]];
1841 $stacktop = 0;
1842 $current = [];
1843 $tagName = '';
1844 $documentTag = '';
1845 // Traverse the parsed XML structure:
1846 foreach ($vals as $key => $val) {
1847 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
1848 $tagName = $val['tag'];
1849 if (!$documentTag) {
1850 $documentTag = $tagName;
1851 }
1852 // Test for name space:
1853 $tagName = $NSprefix && substr($tagName, 0, strlen($NSprefix)) == $NSprefix ? substr($tagName, strlen($NSprefix)) : $tagName;
1854 // Test for numeric tag, encoded on the form "nXXX":
1855 $testNtag = substr($tagName, 1);
1856 // Closing tag.
1857 $tagName = $tagName[0] === 'n' && MathUtility::canBeInterpretedAsInteger($testNtag) ? (int)$testNtag : $tagName;
1858 // Test for alternative index value:
1859 if ((string)$val['attributes']['index'] !== '') {
1860 $tagName = $val['attributes']['index'];
1861 }
1862 // Setting tag-values, manage stack:
1863 switch ($val['type']) {
1864 case 'open':
1865 // If open tag it means there is an array stored in sub-elements. Therefore increase the stackpointer and reset the accumulation array:
1866 // Setting blank place holder
1867 $current[$tagName] = [];
1868 $stack[$stacktop++] = $current;
1869 $current = [];
1870 break;
1871 case 'close':
1872 // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
1873 $oldCurrent = $current;
1874 $current = $stack[--$stacktop];
1875 // Going to the end of array to get placeholder key, key($current), and fill in array next:
1876 end($current);
1877 $current[key($current)] = $oldCurrent;
1878 unset($oldCurrent);
1879 break;
1880 case 'complete':
1881 // If "complete", then it's a value. If the attribute "base64" is set, then decode the value, otherwise just set it.
1882 if ($val['attributes']['base64']) {
1883 $current[$tagName] = base64_decode($val['value']);
1884 } else {
1885 // Had to cast it as a string - otherwise it would be evaluate FALSE if tested with isset()!!
1886 $current[$tagName] = (string)$val['value'];
1887 // Cast type:
1888 switch ((string)$val['attributes']['type']) {
1889 case 'integer':
1890 $current[$tagName] = (int)$current[$tagName];
1891 break;
1892 case 'double':
1893 $current[$tagName] = (double) $current[$tagName];
1894 break;
1895 case 'boolean':
1896 $current[$tagName] = (bool)$current[$tagName];
1897 break;
1898 case 'NULL':
1899 $current[$tagName] = null;
1900 break;
1901 case 'array':
1902 // MUST be an empty array since it is processed as a value; Empty arrays would end up here because they would have no tags inside...
1903 $current[$tagName] = [];
1904 break;
1905 }
1906 }
1907 break;
1908 }
1909 }
1910 if ($reportDocTag) {
1911 $current[$tagName]['_DOCUMENT_TAG'] = $documentTag;
1912 }
1913 // Finally return the content of the document tag.
1914 return $current[$tagName];
1915 }
1916
1917 /**
1918 * This implodes an array of XML parts (made with xml_parse_into_struct()) into XML again.
1919 *
1920 * @param array $vals An array of XML parts, see xml2tree
1921 * @return string Re-compiled XML data.
1922 */
1923 public static function xmlRecompileFromStructValArray(array $vals)
1924 {
1925 $XMLcontent = '';
1926 foreach ($vals as $val) {
1927 $type = $val['type'];
1928 // Open tag:
1929 if ($type === 'open' || $type === 'complete') {
1930 $XMLcontent .= '<' . $val['tag'];
1931 if (isset($val['attributes'])) {
1932 foreach ($val['attributes'] as $k => $v) {
1933 $XMLcontent .= ' ' . $k . '="' . htmlspecialchars($v) . '"';
1934 }
1935 }
1936 if ($type === 'complete') {
1937 if (isset($val['value'])) {
1938 $XMLcontent .= '>' . htmlspecialchars($val['value']) . '</' . $val['tag'] . '>';
1939 } else {
1940 $XMLcontent .= '/>';
1941 }
1942 } else {
1943 $XMLcontent .= '>';
1944 }
1945 if ($type === 'open' && isset($val['value'])) {
1946 $XMLcontent .= htmlspecialchars($val['value']);
1947 }
1948 }
1949 // Finish tag:
1950 if ($type === 'close') {
1951 $XMLcontent .= '</' . $val['tag'] . '>';
1952 }
1953 // Cdata
1954 if ($type === 'cdata') {
1955 $XMLcontent .= htmlspecialchars($val['value']);
1956 }
1957 }
1958 return $XMLcontent;
1959 }
1960
1961 /**
1962 * Extracts the attributes (typically encoding and version) of an XML prologue (header).
1963 *
1964 * @param string $xmlData XML data
1965 * @return array Attributes of the xml prologue (header)
1966 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9.
1967 */
1968 public static function xmlGetHeaderAttribs($xmlData)
1969 {
1970 self::logDeprecatedFunction();
1971 $match = [];
1972 if (preg_match('/^\\s*<\\?xml([^>]*)\\?\\>/', $xmlData, $match)) {
1973 return self::get_tag_attributes($match[1]);
1974 }
1975 }
1976
1977 /**
1978 * Minifies JavaScript
1979 *
1980 * @param string $script Script to minify
1981 * @param string $error Error message (if any)
1982 * @return string Minified script or source string if error happened
1983 */
1984 public static function minifyJavaScript($script, &$error = '')
1985 {
1986 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'])) {
1987 $fakeThis = false;
1988 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'] as $hookMethod) {
1989 try {
1990 $parameters = ['script' => $script];
1991 $script = static::callUserFunction($hookMethod, $parameters, $fakeThis);
1992 } catch (\Exception $e) {
1993 $errorMessage = 'Error minifying java script: ' . $e->getMessage();
1994 $error .= $errorMessage;
1995 static::devLog($errorMessage, \TYPO3\CMS\Core\Utility\GeneralUtility::class, 2, [
1996 'JavaScript' => $script,
1997 'Stack trace' => $e->getTrace(),
1998 'hook' => $hookMethod
1999 ]);
2000 }
2001 }
2002 }
2003 return $script;
2004 }
2005
2006 /*************************
2007 *
2008 * FILES FUNCTIONS
2009 *
2010 *************************/
2011 /**
2012 * Reads the file or url $url and returns the content
2013 * If you are having trouble with proxies when reading URLs you can configure your way out of that with settings within $GLOBALS['TYPO3_CONF_VARS']['HTTP'].
2014 *
2015 * @param string $url File/URL to read
2016 * @param int $includeHeader Whether the HTTP header should be fetched or not. 0=disable, 1=fetch header+content, 2=fetch header only
2017 * @param array $requestHeaders HTTP headers to be used in the request
2018 * @param array $report Error code/message and, if $includeHeader is 1, response meta data (HTTP status and content type)
2019 * @return mixed The content from the resource given as input. FALSE if an error has occurred.
2020 */
2021 public static function getUrl($url, $includeHeader = 0, $requestHeaders = null, &$report = null)
2022 {
2023 if (isset($report)) {
2024 $report['error'] = 0;
2025 $report['message'] = '';
2026 }
2027 // Looks like it's an external file, use Guzzle by default
2028 if (preg_match('/^(?:http|ftp)s?|s(?:ftp|cp):/', $url)) {
2029 /** @var RequestFactory $requestFactory */
2030 $requestFactory = static::makeInstance(RequestFactory::class);
2031 if (is_array($requestHeaders)) {
2032 $configuration = ['headers' => $requestHeaders];
2033 } else {
2034 $configuration = [];
2035 }
2036
2037 try {
2038 if (isset($report)) {
2039 $report['lib'] = 'GuzzleHttp';
2040 }
2041 $response = $requestFactory->request($url, 'GET', $configuration);
2042 } catch (RequestException $exception) {
2043 if (isset($report)) {
2044 $report['error'] = $exception->getHandlerContext()['errno'];
2045 $report['message'] = $exception->getMessage();
2046 $report['exception'] = $exception;
2047 }
2048 return false;
2049 }
2050
2051 $content = '';
2052
2053 // Add the headers to the output
2054 $includeHeader = (int)$includeHeader;
2055 if ($includeHeader) {
2056 $parsedURL = parse_url($url);
2057 $method = $includeHeader === 2 ? 'HEAD' : 'GET';
2058 $content = $method . ' ' . (isset($parsedURL['path']) ? $parsedURL['path'] : '/')
2059 . ($parsedURL['query'] ? '?' . $parsedURL['query'] : '') . ' HTTP/1.0' . CRLF
2060 . 'Host: ' . $parsedURL['host'] . CRLF
2061 . 'Connection: close' . CRLF;
2062 if (is_array($requestHeaders)) {
2063 $content .= implode(CRLF, $requestHeaders) . CRLF;
2064 }
2065 foreach ($response->getHeaders() as $headerName => $headerValues) {
2066 $content .= $headerName . ': ' . implode(', ', $headerValues) . CRLF;
2067 }
2068 // Headers are separated from the body with two CRLFs
2069 $content .= CRLF;
2070 }
2071 // If not just headers are requested, add the body
2072 if ($includeHeader !== 2) {
2073 $content .= $response->getBody()->getContents();
2074 }
2075 if (isset($report)) {
2076 $report['lib'] = 'http';
2077 if ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400) {
2078 $report['http_code'] = $response->getStatusCode();
2079 $report['content_type'] = $response->getHeader('Content-Type');
2080 $report['error'] = $response->getStatusCode();
2081 $report['message'] = $response->getReasonPhrase();
2082 } elseif (!empty($content)) {
2083 $report['error'] = $response->getStatusCode();
2084 $report['message'] = $response->getReasonPhrase();
2085 } elseif ($includeHeader) {
2086 // Set only for $includeHeader to work exactly like PHP variant
2087 $report['http_code'] = $response->getStatusCode();
2088 $report['content_type'] = $response->getHeader('Content-Type');
2089 }
2090 }
2091 } else {
2092 if (isset($report)) {
2093 $report['lib'] = 'file';
2094 }
2095 $content = @file_get_contents($url);
2096 if ($content === false && isset($report)) {
2097 $report['error'] = -1;
2098 $report['message'] = 'Couldn\'t get URL: ' . $url;
2099 }
2100 }
2101 return $content;
2102 }
2103
2104 /**
2105 * Writes $content to the file $file
2106 *
2107 * @param string $file Filepath to write to
2108 * @param string $content Content to write
2109 * @param bool $changePermissions If TRUE, permissions are forced to be set
2110 * @return bool TRUE if the file was successfully opened and written to.
2111 */
2112 public static function writeFile($file, $content, $changePermissions = false)
2113 {
2114 if (!@is_file($file)) {
2115 $changePermissions = true;
2116 }
2117 if ($fd = fopen($file, 'wb')) {
2118 $res = fwrite($fd, $content);
2119 fclose($fd);
2120 if ($res === false) {
2121 return false;
2122 }
2123 // Change the permissions only if the file has just been created
2124 if ($changePermissions) {
2125 static::fixPermissions($file);
2126 }
2127 return true;
2128 }
2129 return false;
2130 }
2131
2132 /**
2133 * Sets the file system mode and group ownership of a file or a folder.
2134 *
2135 * @param string $path Path of file or folder, must not be escaped. Path can be absolute or relative
2136 * @param bool $recursive If set, also fixes permissions of files and folders in the folder (if $path is a folder)
2137 * @return mixed TRUE on success, FALSE on error, always TRUE on Windows OS
2138 */
2139 public static function fixPermissions($path, $recursive = false)
2140 {
2141 if (TYPO3_OS === 'WIN') {
2142 return true;
2143 }
2144 $result = false;
2145 // Make path absolute
2146 if (!static::isAbsPath($path)) {
2147 $path = static::getFileAbsFileName($path);
2148 }
2149 if (static::isAllowedAbsPath($path)) {
2150 if (@is_file($path)) {
2151 $targetPermissions = isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask'])
2152 ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['fileCreateMask']
2153 : '0644';
2154 } elseif (@is_dir($path)) {
2155 $targetPermissions = isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask'])
2156 ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask']
2157 : '0755';
2158 }
2159 if (!empty($targetPermissions)) {
2160 // make sure it's always 4 digits
2161 $targetPermissions = str_pad($targetPermissions, 4, 0, STR_PAD_LEFT);
2162 $targetPermissions = octdec($targetPermissions);
2163 // "@" is there because file is not necessarily OWNED by the user
2164 $result = @chmod($path, $targetPermissions);
2165 }
2166 // Set createGroup if not empty
2167 if (
2168 isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['createGroup'])
2169 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['createGroup'] !== ''
2170 ) {
2171 // "@" is there because file is not necessarily OWNED by the user
2172 $changeGroupResult = @chgrp($path, $GLOBALS['TYPO3_CONF_VARS']['SYS']['createGroup']);
2173 $result = $changeGroupResult ? $result : false;
2174 }
2175 // Call recursive if recursive flag if set and $path is directory
2176 if ($recursive && @is_dir($path)) {
2177 $handle = opendir($path);
2178 if (is_resource($handle)) {
2179 while (($file = readdir($handle)) !== false) {
2180 $recursionResult = null;
2181 if ($file !== '.' && $file !== '..') {
2182 if (@is_file(($path . '/' . $file))) {
2183 $recursionResult = static::fixPermissions($path . '/' . $file);
2184 } elseif (@is_dir(($path . '/' . $file))) {
2185 $recursionResult = static::fixPermissions($path . '/' . $file, true);
2186 }
2187 if (isset($recursionResult) && !$recursionResult) {
2188 $result = false;
2189 }
2190 }
2191 }
2192 closedir($handle);
2193 }
2194 }
2195 }
2196 return $result;
2197 }
2198
2199 /**
2200 * Writes $content to a filename in the typo3temp/ folder (and possibly one or two subfolders...)
2201 * Accepts an additional subdirectory in the file path!
2202 *
2203 * @param string $filepath Absolute file path to write to inside "typo3temp/". First part of this string must match PATH_site."typo3temp/"
2204 * @param string $content Content string to write
2205 * @return string Returns NULL on success, otherwise an error string telling about the problem.
2206 */
2207 public static function writeFileToTypo3tempDir($filepath, $content)
2208 {
2209 if (!defined('PATH_site')) {
2210 return 'PATH_site constant was NOT defined!';
2211 }
2212
2213 // Parse filepath into directory and basename:
2214 $fI = pathinfo($filepath);
2215 $fI['dirname'] .= '/';
2216 // Check parts:
2217 if (!static::validPathStr($filepath) || !$fI['basename'] || strlen($fI['basename']) >= 60) {
2218 return 'Input filepath "' . $filepath . '" was generally invalid!';
2219 }
2220 // Setting main temporary directory name (standard)
2221 $dirName = PATH_site . 'typo3temp/';
2222 if (!@is_dir($dirName)) {
2223 return 'PATH_site + "typo3temp/" was not a directory!';
2224 }
2225 if (!static::isFirstPartOfStr($fI['dirname'], $dirName)) {
2226 return '"' . $fI['dirname'] . '" was not within directory PATH_site + "typo3temp/"';
2227 }
2228 // Checking if the "subdir" is found:
2229 $subdir = substr($fI['dirname'], strlen($dirName));
2230 if ($subdir) {
2231 if (preg_match('#^(?:[[:alnum:]_]+/)+$#', $subdir)) {
2232 $dirName .= $subdir;
2233 if (!@is_dir($dirName)) {
2234 static::mkdir_deep(PATH_site . 'typo3temp/', $subdir);
2235 }
2236 } else {
2237 return 'Subdir, "' . $subdir . '", was NOT on the form "[[:alnum:]_]/+"';
2238 }
2239 }
2240 // Checking dir-name again (sub-dir might have been created):
2241 if (@is_dir($dirName)) {
2242 if ($filepath == $dirName . $fI['basename']) {
2243 static::writeFile($filepath, $content);
2244 if (!@is_file($filepath)) {
2245 return 'The file was not written to the disk. Please, check that you have write permissions to the typo3temp/ directory.';
2246 }
2247 } else {
2248 return 'Calculated filelocation didn\'t match input "' . $filepath . '".';
2249 }
2250 } else {
2251 return '"' . $dirName . '" is not a directory!';
2252 }
2253 return null;
2254 }
2255
2256 /**
2257 * Wrapper function for mkdir.
2258 * Sets folder permissions according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask']
2259 * and group ownership according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['createGroup']
2260 *
2261 * @param string $newFolder Absolute path to folder, see PHP mkdir() function. Removes trailing slash internally.
2262 * @return bool TRUE if @mkdir went well!
2263 */
2264 public static function mkdir($newFolder)
2265 {
2266 $result = @mkdir($newFolder, octdec($GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask']));
2267 if ($result) {
2268 static::fixPermissions($newFolder);
2269 }
2270 return $result;
2271 }
2272
2273 /**
2274 * Creates a directory - including parent directories if necessary and
2275 * sets permissions on newly created directories.
2276 *
2277 * @param string $directory Target directory to create. Must a have trailing slash
2278 * @param string $deepDirectory Directory to create. This second parameter
2279 * @return void
2280 * @throws \InvalidArgumentException If $directory or $deepDirectory are not strings
2281 * @throws \RuntimeException If directory could not be created
2282 */
2283 public static function mkdir_deep($directory, $deepDirectory = '')
2284 {
2285 if (!is_string($directory)) {
2286 throw new \InvalidArgumentException('The specified directory is of type "' . gettype($directory) . '" but a string is expected.', 1303662955);
2287 }
2288 if (!is_string($deepDirectory)) {
2289 throw new \InvalidArgumentException('The specified directory is of type "' . gettype($deepDirectory) . '" but a string is expected.', 1303662956);
2290 }
2291 // Ensure there is only one slash
2292 $fullPath = rtrim($directory, '/') . '/' . ltrim($deepDirectory, '/');
2293 if ($fullPath !== '' && !is_dir($fullPath)) {
2294 $firstCreatedPath = static::createDirectoryPath($fullPath);
2295 if ($firstCreatedPath !== '') {
2296 static::fixPermissions($firstCreatedPath, true);
2297 }
2298 }
2299 }
2300
2301 /**
2302 * Creates directories for the specified paths if they do not exist. This
2303 * functions sets proper permission mask but does not set proper user and
2304 * group.
2305 *
2306 * @static
2307 * @param string $fullDirectoryPath
2308 * @return string Path to the the first created directory in the hierarchy
2309 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep
2310 * @throws \RuntimeException If directory could not be created
2311 */
2312 protected static function createDirectoryPath($fullDirectoryPath)
2313 {
2314 $currentPath = $fullDirectoryPath;
2315 $firstCreatedPath = '';
2316 $permissionMask = octdec($GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask']);
2317 if (!@is_dir($currentPath)) {
2318 do {
2319 $firstCreatedPath = $currentPath;
2320 $separatorPosition = strrpos($currentPath, DIRECTORY_SEPARATOR);
2321 $currentPath = substr($currentPath, 0, $separatorPosition);
2322 } while (!is_dir($currentPath) && $separatorPosition !== false);
2323 $result = @mkdir($fullDirectoryPath, $permissionMask, true);
2324 // Check existence of directory again to avoid race condition. Directory could have get created by another process between previous is_dir() and mkdir()
2325 if (!$result && !@is_dir($fullDirectoryPath)) {
2326 throw new \RuntimeException('Could not create directory "' . $fullDirectoryPath . '"!', 1170251401);
2327 }
2328 }
2329 return $firstCreatedPath;
2330 }
2331
2332 /**
2333 * Wrapper function for rmdir, allowing recursive deletion of folders and files
2334 *
2335 * @param string $path Absolute path to folder, see PHP rmdir() function. Removes trailing slash internally.
2336 * @param bool $removeNonEmpty Allow deletion of non-empty directories
2337 * @return bool TRUE if @rmdir went well!
2338 */
2339 public static function rmdir($path, $removeNonEmpty = false)
2340 {
2341 $OK = false;
2342 // Remove trailing slash
2343 $path = preg_replace('|/$|', '', $path);
2344 if (file_exists($path)) {
2345 $OK = true;
2346 if (!is_link($path) && is_dir($path)) {
2347 if ($removeNonEmpty == true && ($handle = @opendir($path))) {
2348 while ($OK && false !== ($file = readdir($handle))) {
2349 if ($file === '.' || $file === '..') {
2350 continue;
2351 }
2352 $OK = static::rmdir($path . '/' . $file, $removeNonEmpty);
2353 }
2354 closedir($handle);
2355 }
2356 if ($OK) {
2357 $OK = @rmdir($path);
2358 }
2359 } elseif (is_link($path) && is_dir($path) && TYPO3_OS === 'WIN') {
2360 $OK = @rmdir($path);
2361 } else {
2362 // If $path is a file, simply remove it
2363 $OK = @unlink($path);
2364 }
2365 clearstatcache();
2366 } elseif (is_link($path)) {
2367 $OK = @unlink($path);
2368 if (!$OK && TYPO3_OS === 'WIN') {
2369 // Try to delete dead folder links on Windows systems
2370 $OK = @rmdir($path);
2371 }
2372 clearstatcache();
2373 }
2374 return $OK;
2375 }
2376
2377 /**
2378 * Flushes a directory by first moving to a temporary resource, and then
2379 * triggering the remove process. This way directories can be flushed faster
2380 * to prevent race conditions on concurrent processes accessing the same directory.
2381 *
2382 * @param string $directory The directory to be renamed and flushed
2383 * @param bool $keepOriginalDirectory Whether to only empty the directory and not remove it
2384 * @param bool $flushOpcodeCache Also flush the opcode cache right after renaming the directory.
2385 * @return bool Whether the action was successful
2386 */
2387 public static function flushDirectory($directory, $keepOriginalDirectory = false, $flushOpcodeCache = false)
2388 {
2389 $result = false;
2390
2391 if (is_dir($directory)) {
2392 $temporaryDirectory = rtrim($directory, '/') . '.' . StringUtility::getUniqueId('remove') . '/';
2393 if (rename($directory, $temporaryDirectory)) {
2394 if ($flushOpcodeCache) {
2395 self::makeInstance(OpcodeCacheService::class)->clearAllActive($directory);
2396 }
2397 if ($keepOriginalDirectory) {
2398 static::mkdir($directory);
2399 }
2400 clearstatcache();
2401 $result = static::rmdir($temporaryDirectory, true);
2402 }
2403 }
2404
2405 return $result;
2406 }
2407
2408 /**
2409 * Returns an array with the names of folders in a specific path
2410 * Will return 'error' (string) if there were an error with reading directory content.
2411 *
2412 * @param string $path Path to list directories from
2413 * @return array Returns an array with the directory entries as values. If no path, the return value is nothing.
2414 */
2415 public static function get_dirs($path)
2416 {
2417 $dirs = null;
2418 if ($path) {
2419 if (is_dir($path)) {
2420 $dir = scandir($path);
2421 $dirs = [];
2422 foreach ($dir as $entry) {
2423 if (is_dir($path . '/' . $entry) && $entry !== '..' && $entry !== '.') {
2424 $dirs[] = $entry;
2425 }
2426 }
2427 } else {
2428 $dirs = 'error';
2429 }
2430 }
2431 return $dirs;
2432 }
2433
2434 /**
2435 * Finds all files in a given path and returns them as an array. Each
2436 * array key is a md5 hash of the full path to the file. This is done because
2437 * 'some' extensions like the import/export extension depend on this.
2438 *
2439 * @param string $path The path to retrieve the files from.
2440 * @param string $extensionList A comma-separated list of file extensions. Only files of the specified types will be retrieved. When left blank, files of any type will be retrieved.
2441 * @param bool $prependPath If TRUE, the full path to the file is returned. If FALSE only the file name is returned.
2442 * @param string $order The sorting order. The default sorting order is alphabetical. Setting $order to 'mtime' will sort the files by modification time.
2443 * @param string $excludePattern A regular expression pattern of file names to exclude. For example: 'clear.gif' or '(clear.gif|.htaccess)'. The pattern will be wrapped with: '/^' and '$/'.
2444 * @return array|string Array of the files found, or an error message in case the path could not be opened.
2445 */
2446 public static function getFilesInDir($path, $extensionList = '', $prependPath = false, $order = '', $excludePattern = '')
2447 {
2448 $excludePattern = (string)$excludePattern;
2449 $path = rtrim($path, '/');
2450 if (!@is_dir($path)) {
2451 return [];
2452 }
2453
2454 $rawFileList = scandir($path);
2455 if ($rawFileList === false) {
2456 return 'error opening path: "' . $path . '"';
2457 }
2458
2459 $pathPrefix = $path . '/';
2460 $extensionList = ',' . $extensionList . ',';
2461 $files = [];
2462 foreach ($rawFileList as $entry) {
2463 $completePathToEntry = $pathPrefix . $entry;
2464 if (!@is_file($completePathToEntry)) {
2465 continue;
2466 }
2467
2468 if (
2469 ($extensionList === ',,' || stripos($extensionList, ',' . pathinfo($entry, PATHINFO_EXTENSION) . ',') !== false)
2470 && ($excludePattern === '' || !preg_match(('/^' . $excludePattern . '$/'), $entry))
2471 ) {
2472 if ($order !== 'mtime') {
2473 $files[] = $entry;
2474 } else {
2475 // Store the value in the key so we can do a fast asort later.
2476 $files[$entry] = filemtime($completePathToEntry);
2477 }
2478 }
2479 }
2480
2481 $valueName = 'value';
2482 if ($order === 'mtime') {
2483 asort($files);
2484 $valueName = 'key';
2485 }
2486
2487 $valuePathPrefix = $prependPath ? $pathPrefix : '';
2488 $foundFiles = [];
2489 foreach ($files as $key => $value) {
2490 // Don't change this ever - extensions may depend on the fact that the hash is an md5 of the path! (import/export extension)
2491 $foundFiles[md5($pathPrefix . ${$valueName})] = $valuePathPrefix . ${$valueName};
2492 }
2493
2494 return $foundFiles;
2495 }
2496
2497 /**
2498 * Recursively gather all files and folders of a path.
2499 *
2500 * @param array $fileArr Empty input array (will have files added to it)
2501 * @param string $path The path to read recursively from (absolute) (include trailing slash!)
2502 * @param string $extList Comma list of file extensions: Only files with extensions in this list (if applicable) will be selected.
2503 * @param bool $regDirs If set, directories are also included in output.
2504 * @param int $recursivityLevels The number of levels to dig down...
2505 * @param string $excludePattern regex pattern of files/directories to exclude
2506 * @return array An array with the found files/directories.
2507 */
2508 public static function getAllFilesAndFoldersInPath(array $fileArr, $path, $extList = '', $regDirs = false, $recursivityLevels = 99, $excludePattern = '')
2509 {
2510 if ($regDirs) {
2511 $fileArr[md5($path)] = $path;
2512 }
2513 $fileArr = array_merge($fileArr, self::getFilesInDir($path, $extList, 1, 1, $excludePattern));
2514 $dirs = self::get_dirs($path);
2515 if ($recursivityLevels > 0 && is_array($dirs)) {
2516 foreach ($dirs as $subdirs) {
2517 if ((string)$subdirs !== '' && ($excludePattern === '' || !preg_match(('/^' . $excludePattern . '$/'), $subdirs))) {
2518 $fileArr = self::getAllFilesAndFoldersInPath($fileArr, $path . $subdirs . '/', $extList, $regDirs, $recursivityLevels - 1, $excludePattern);
2519 }
2520 }
2521 }
2522 return $fileArr;
2523 }
2524
2525 /**
2526 * Removes the absolute part of all files/folders in fileArr
2527 *
2528 * @param array $fileArr The file array to remove the prefix from
2529 * @param string $prefixToRemove The prefix path to remove (if found as first part of string!)
2530 * @return array The input $fileArr processed.
2531 */
2532 public static function removePrefixPathFromList(array $fileArr, $prefixToRemove)
2533 {
2534 foreach ($fileArr as $k => &$absFileRef) {
2535 if (self::isFirstPartOfStr($absFileRef, $prefixToRemove)) {
2536 $absFileRef = substr($absFileRef, strlen($prefixToRemove));
2537 } else {
2538 return 'ERROR: One or more of the files was NOT prefixed with the prefix-path!';
2539 }
2540 }
2541 unset($absFileRef);
2542 return $fileArr;
2543 }
2544
2545 /**
2546 * Fixes a path for windows-backslashes and reduces double-slashes to single slashes
2547 *
2548 * @param string $theFile File path to process
2549 * @return string
2550 */
2551 public static function fixWindowsFilePath($theFile)
2552 {
2553 return str_replace(['\\', '//'], '/', $theFile);
2554 }
2555
2556 /**
2557 * Resolves "../" sections in the input path string.
2558 * For example "fileadmin/directory/../other_directory/" will be resolved to "fileadmin/other_directory/"
2559 *
2560 * @param string $pathStr File path in which "/../" is resolved
2561 * @return string
2562 */
2563 public static function resolveBackPath($pathStr)
2564 {
2565 if (strpos($pathStr, '..') === false) {
2566 return $pathStr;
2567 }
2568 $parts = explode('/', $pathStr);
2569 $output = [];
2570 $c = 0;
2571 foreach ($parts as $part) {
2572 if ($part === '..') {
2573 if ($c) {
2574 array_pop($output);
2575 --$c;
2576 } else {
2577 $output[] = $part;
2578 }
2579 } else {
2580 ++$c;
2581 $output[] = $part;
2582 }
2583 }
2584 return implode('/', $output);
2585 }
2586
2587 /**
2588 * Prefixes a URL used with 'header-location' with 'http://...' depending on whether it has it already.
2589 * - If already having a scheme, nothing is prepended
2590 * - If having REQUEST_URI slash '/', then prefixing 'http://[host]' (relative to host)
2591 * - Otherwise prefixed with TYPO3_REQUEST_DIR (relative to current dir / TYPO3_REQUEST_DIR)
2592 *
2593 * @param string $path URL / path to prepend full URL addressing to.
2594 * @return string
2595 */
2596 public static function locationHeaderUrl($path)
2597 {
2598 $uI = parse_url($path);
2599 // relative to HOST
2600 if ($path[0] === '/') {
2601 $path = self::getIndpEnv('TYPO3_REQUEST_HOST') . $path;
2602 } elseif (!$uI['scheme']) {
2603 // No scheme either
2604 $path = self::getIndpEnv('TYPO3_REQUEST_DIR') . $path;
2605 }
2606 return $path;
2607 }
2608
2609 /**
2610 * Returns the maximum upload size for a file that is allowed. Measured in KB.
2611 * This might be handy to find out the real upload limit that is possible for this
2612 * TYPO3 installation.
2613 *
2614 * @return int The maximum size of uploads that are allowed (measured in kilobytes)
2615 */
2616 public static function getMaxUploadFileSize()
2617 {
2618 // Check for PHP restrictions of the maximum size of one of the $_FILES
2619 $phpUploadLimit = self::getBytesFromSizeMeasurement(ini_get('upload_max_filesize'));
2620 // Check for PHP restrictions of the maximum $_POST size
2621 $phpPostLimit = self::getBytesFromSizeMeasurement(ini_get('post_max_size'));
2622 // If the total amount of post data is smaller (!) than the upload_max_filesize directive,
2623 // then this is the real limit in PHP
2624 $phpUploadLimit = $phpPostLimit > 0 && $phpPostLimit < $phpUploadLimit ? $phpPostLimit : $phpUploadLimit;
2625 return floor(($phpUploadLimit)) / 1024;
2626 }
2627
2628 /**
2629 * Gets the bytes value from a measurement string like "100k".
2630 *
2631 * @param string $measurement The measurement (e.g. "100k")
2632 * @return int The bytes value (e.g. 102400)
2633 */
2634 public static function getBytesFromSizeMeasurement($measurement)
2635 {
2636 $bytes = (float)$measurement;
2637 if (stripos($measurement, 'G')) {
2638 $bytes *= 1024 * 1024 * 1024;
2639 } elseif (stripos($measurement, 'M')) {
2640 $bytes *= 1024 * 1024;
2641 } elseif (stripos($measurement, 'K')) {
2642 $bytes *= 1024;
2643 }
2644 return $bytes;
2645 }
2646
2647 /**
2648 * Retrieves the maximum path length that is valid in the current environment.
2649 *
2650 * @return int The maximum available path length
2651 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
2652 */
2653 public static function getMaximumPathLength()
2654 {
2655 static::logDeprecatedFunction();
2656 return PHP_MAXPATHLEN;
2657 }
2658
2659 /**
2660 * Function for static version numbers on files, based on the filemtime
2661 *
2662 * This will make the filename automatically change when a file is
2663 * changed, and by that re-cached by the browser. If the file does not
2664 * exist physically the original file passed to the function is
2665 * returned without the timestamp.
2666 *
2667 * Behaviour is influenced by the setting
2668 * TYPO3_CONF_VARS[TYPO3_MODE][versionNumberInFilename]
2669 * = TRUE (BE) / "embed" (FE) : modify filename
2670 * = FALSE (BE) / "querystring" (FE) : add timestamp as parameter
2671 *
2672 * @param string $file Relative path to file including all potential query parameters (not htmlspecialchared yet)
2673 * @return string Relative path with version filename including the timestamp
2674 */
2675 public static function createVersionNumberedFilename($file)
2676 {
2677 $lookupFile = explode('?', $file);
2678 $path = self::resolveBackPath(self::dirname(PATH_thisScript) . '/' . $lookupFile[0]);
2679
2680 $doNothing = false;
2681 if (TYPO3_MODE === 'FE') {
2682 $mode = strtolower($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['versionNumberInFilename']);
2683 if ($mode === 'embed') {
2684 $mode = true;
2685 } else {
2686 if ($mode === 'querystring') {
2687 $mode = false;
2688 } else {
2689 $doNothing = true;
2690 }
2691 }
2692 } else {
2693 $mode = $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['versionNumberInFilename'];
2694 }
2695 if (!file_exists($path) || $doNothing) {
2696 // File not found, return filename unaltered
2697 $fullName = $file;
2698 } else {
2699 if (!$mode) {
2700 // If use of .htaccess rule is not configured,
2701 // we use the default query-string method
2702 if ($lookupFile[1]) {
2703 $separator = '&';
2704 } else {
2705 $separator = '?';
2706 }
2707 $fullName = $file . $separator . filemtime($path);
2708 } else {
2709 // Change the filename
2710 $name = explode('.', $lookupFile[0]);
2711 $extension = array_pop($name);
2712 array_push($name, filemtime($path), $extension);
2713 $fullName = implode('.', $name);
2714 // Append potential query string
2715 $fullName .= $lookupFile[1] ? '?' . $lookupFile[1] : '';
2716 }
2717 }
2718 return $fullName;
2719 }
2720
2721 /*************************
2722 *
2723 * SYSTEM INFORMATION
2724 *
2725 *************************/
2726
2727 /**
2728 * Returns the link-url to the current script.
2729 * In $getParams you can set associative keys corresponding to the GET-vars you wish to add to the URL. If you set them empty, they will remove existing GET-vars from the current URL.
2730 * REMEMBER to always use htmlspecialchars() for content in href-properties to get ampersands converted to entities (XHTML requirement and XSS precaution)
2731 *
2732 * @param array $getParams Array of GET parameters to include
2733 * @return string
2734 */
2735 public static function linkThisScript(array $getParams = [])
2736 {
2737 $parts = self::getIndpEnv('SCRIPT_NAME');
2738 $params = self::_GET();
2739 foreach ($getParams as $key => $value) {
2740 if ($value !== '') {
2741 $params[$key] = $value;
2742 } else {
2743 unset($params[$key]);
2744 }
2745 }
2746 $pString = self::implodeArrayForUrl('', $params);
2747 return $pString ? $parts . '?' . ltrim($pString, '&') : $parts;
2748 }
2749
2750 /**
2751 * Takes a full URL, $url, possibly with a querystring and overlays the $getParams arrays values onto the quirystring, packs it all together and returns the URL again.
2752 * So basically it adds the parameters in $getParams to an existing URL, $url
2753 *
2754 * @param string $url URL string
2755 * @param array $getParams Array of key/value pairs for get parameters to add/overrule with. Can be multidimensional.
2756 * @return string Output URL with added getParams.
2757 */
2758 public static function linkThisUrl($url, array $getParams = [])
2759 {
2760 $parts = parse_url($url);
2761 $getP = [];
2762 if ($parts['query']) {
2763 parse_str($parts['query'], $getP);
2764 }
2765 ArrayUtility::mergeRecursiveWithOverrule($getP, $getParams);
2766 $uP = explode('?', $url);
2767 $params = self::implodeArrayForUrl('', $getP);
2768 $outurl = $uP[0] . ($params ? '?' . substr($params, 1) : '');
2769 return $outurl;
2770 }
2771
2772 /**
2773 * Abstraction method which returns System Environment Variables regardless of server OS, CGI/MODULE version etc. Basically this is SERVER variables for most of them.
2774 * This should be used instead of getEnv() and $_SERVER/ENV_VARS to get reliable values for all situations.
2775 *
2776 * @param string $getEnvName Name of the "environment variable"/"server variable" you wish to use. Valid values are SCRIPT_NAME, SCRIPT_FILENAME, REQUEST_URI, PATH_INFO, REMOTE_ADDR, REMOTE_HOST, HTTP_REFERER, HTTP_HOST, HTTP_USER_AGENT, HTTP_ACCEPT_LANGUAGE, QUERY_STRING, TYPO3_DOCUMENT_ROOT, TYPO3_HOST_ONLY, TYPO3_HOST_ONLY, TYPO3_REQUEST_HOST, TYPO3_REQUEST_URL, TYPO3_REQUEST_SCRIPT, TYPO3_REQUEST_DIR, TYPO3_SITE_URL, _ARRAY
2777 * @return string Value based on the input key, independent of server/os environment.
2778 * @throws \UnexpectedValueException
2779 */
2780 public static function getIndpEnv($getEnvName)
2781 {
2782 if (isset(self::$indpEnvCache[$getEnvName])) {
2783 return self::$indpEnvCache[$getEnvName];
2784 }
2785
2786 /*
2787 Conventions:
2788 output from parse_url():
2789 URL: http://username:password@192.168.1.4:8080/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value#link1
2790 [scheme] => 'http'
2791 [user] => 'username'
2792 [pass] => 'password'
2793 [host] => '192.168.1.4'
2794 [port] => '8080'
2795 [path] => '/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/'
2796 [query] => 'arg1,arg2,arg3&p1=parameter1&p2[key]=value'
2797 [fragment] => 'link1'Further definition: [path_script] = '/typo3/32/temp/phpcheck/index.php'
2798 [path_dir] = '/typo3/32/temp/phpcheck/'
2799 [path_info] = '/arg1/arg2/arg3/'
2800 [path] = [path_script/path_dir][path_info]Keys supported:URI______:
2801 REQUEST_URI = [path]?[query] = /typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value
2802 HTTP_HOST = [host][:[port]] = 192.168.1.4:8080
2803 SCRIPT_NAME = [path_script]++ = /typo3/32/temp/phpcheck/index.php // NOTICE THAT SCRIPT_NAME will return the php-script name ALSO. [path_script] may not do that (eg. '/somedir/' may result in SCRIPT_NAME '/somedir/index.php')!
2804 PATH_INFO = [path_info] = /arg1/arg2/arg3/
2805 QUERY_STRING = [query] = arg1,arg2,arg3&p1=parameter1&p2[key]=value
2806 HTTP_REFERER = [scheme]://[host][:[port]][path] = http://192.168.1.4:8080/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value
2807 (Notice: NO username/password + NO fragment)CLIENT____:
2808 REMOTE_ADDR = (client IP)
2809 REMOTE_HOST = (client host)
2810 HTTP_USER_AGENT = (client user agent)
2811 HTTP_ACCEPT_LANGUAGE = (client accept language)SERVER____:
2812 SCRIPT_FILENAME = Absolute filename of script (Differs between windows/unix). On windows 'C:\\blabla\\blabl\\' will be converted to 'C:/blabla/blabl/'Special extras:
2813 TYPO3_HOST_ONLY = [host] = 192.168.1.4
2814 TYPO3_PORT = [port] = 8080 (blank if 80, taken from host value)
2815 TYPO3_REQUEST_HOST = [scheme]://[host][:[port]]
2816 TYPO3_REQUEST_URL = [scheme]://[host][:[port]][path]?[query] (scheme will by default be "http" until we can detect something different)
2817 TYPO3_REQUEST_SCRIPT = [scheme]://[host][:[port]][path_script]
2818 TYPO3_REQUEST_DIR = [scheme]://[host][:[port]][path_dir]
2819 TYPO3_SITE_URL = [scheme]://[host][:[port]][path_dir] of the TYPO3 website frontend
2820 TYPO3_SITE_PATH = [path_dir] of the TYPO3 website frontend
2821 TYPO3_SITE_SCRIPT = [script / Speaking URL] of the TYPO3 website
2822 TYPO3_DOCUMENT_ROOT = Absolute path of root of documents: TYPO3_DOCUMENT_ROOT.SCRIPT_NAME = SCRIPT_FILENAME (typically)
2823 TYPO3_SSL = Returns TRUE if this session uses SSL/TLS (https)
2824 TYPO3_PROXY = Returns TRUE if this session runs over a well known proxyNotice: [fragment] is apparently NEVER available to the script!Testing suggestions:
2825 - Output all the values.
2826 - In the script, make a link to the script it self, maybe add some parameters and click the link a few times so HTTP_REFERER is seen
2827 - ALSO TRY the script from the ROOT of a site (like 'http://www.mytest.com/' and not 'http://www.mytest.com/test/' !!)
2828 */
2829 $retVal = '';
2830 switch ((string)$getEnvName) {
2831 case 'SCRIPT_NAME':
2832 $retVal = self::isRunningOnCgiServerApi()
2833 && ($_SERVER['ORIG_PATH_INFO'] ?: $_SERVER['PATH_INFO'])
2834 ? ($_SERVER['ORIG_PATH_INFO'] ?: $_SERVER['PATH_INFO'])
2835 : ($_SERVER['ORIG_SCRIPT_NAME'] ?: $_SERVER['SCRIPT_NAME']);
2836 // Add a prefix if TYPO3 is behind a proxy: ext-domain.com => int-server.com/prefix
2837 if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
2838 if (self::getIndpEnv('TYPO3_SSL') && $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL']) {
2839 $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL'] . $retVal;
2840 } elseif ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix']) {
2841 $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix'] . $retVal;
2842 }
2843 }
2844 break;
2845 case 'SCRIPT_FILENAME':
2846 $retVal = PATH_thisScript;
2847 break;
2848 case 'REQUEST_URI':
2849 // Typical application of REQUEST_URI is return urls, forms submitting to itself etc. Example: returnUrl='.rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI'))
2850 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['requestURIvar']) {
2851 // This is for URL rewriters that store the original URI in a server variable (eg ISAPI_Rewriter for IIS: HTTP_X_REWRITE_URL)
2852 list($v, $n) = explode('|', $GLOBALS['TYPO3_CONF_VARS']['SYS']['requestURIvar']);
2853 $retVal = $GLOBALS[$v][$n];
2854 } elseif (!$_SERVER['REQUEST_URI']) {
2855 // This is for ISS/CGI which does not have the REQUEST_URI available.
2856 $retVal = '/' . ltrim(self::getIndpEnv('SCRIPT_NAME'), '/') . ($_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '');
2857 } else {
2858 $retVal = '/' . ltrim($_SERVER['REQUEST_URI'], '/');
2859 }
2860 // Add a prefix if TYPO3 is behind a proxy: ext-domain.com => int-server.com/prefix
2861 if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
2862 if (self::getIndpEnv('TYPO3_SSL') && $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL']) {
2863 $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL'] . $retVal;
2864 } elseif ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix']) {
2865 $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix'] . $retVal;
2866 }
2867 }
2868 break;
2869 case 'PATH_INFO':
2870 // $_SERVER['PATH_INFO'] != $_SERVER['SCRIPT_NAME'] is necessary because some servers (Windows/CGI)
2871 // are seen to set PATH_INFO equal to script_name
2872 // Further, there must be at least one '/' in the path - else the PATH_INFO value does not make sense.
2873 // IF 'PATH_INFO' never works for our purpose in TYPO3 with CGI-servers,
2874 // then 'PHP_SAPI=='cgi'' might be a better check.
2875 // Right now strcmp($_SERVER['PATH_INFO'], GeneralUtility::getIndpEnv('SCRIPT_NAME')) will always
2876 // return FALSE for CGI-versions, but that is only as long as SCRIPT_NAME is set equal to PATH_INFO
2877 // because of PHP_SAPI=='cgi' (see above)
2878 if (!self::isRunningOnCgiServerApi()) {
2879 $retVal = $_SERVER['PATH_INFO'];
2880 }
2881 break;
2882 case 'TYPO3_REV_PROXY':
2883 $retVal = self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
2884 break;
2885 case 'REMOTE_ADDR':
2886 $retVal = $_SERVER['REMOTE_ADDR'];
2887 if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
2888 $ip = self::trimExplode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
2889 // Choose which IP in list to use
2890 if (!empty($ip)) {
2891 switch ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyHeaderMultiValue']) {
2892 case 'last':
2893 $ip = array_pop($ip);
2894 break;
2895 case 'first':
2896 $ip = array_shift($ip);
2897 break;
2898 case 'none':
2899
2900 default:
2901 $ip = '';
2902 }
2903 }
2904 if (self::validIP($ip)) {
2905 $retVal = $ip;
2906 }
2907 }
2908 break;
2909 case 'HTTP_HOST':
2910 // if it is not set we're most likely on the cli
2911 $retVal = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
2912 if (isset($_SERVER['REMOTE_ADDR']) && static::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
2913 $host = self::trimExplode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
2914 // Choose which host in list to use
2915 if (!empty($host)) {
2916 switch ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyHeaderMultiValue']) {
2917 case 'last':
2918 $host = array_pop($host);
2919 break;
2920 case 'first':
2921 $host = array_shift($host);
2922 break;
2923 case 'none':
2924
2925 default:
2926 $host = '';
2927 }
2928 }
2929 if ($host) {
2930 $retVal = $host;
2931 }
2932 }
2933 if (!static::isAllowedHostHeaderValue($retVal)) {
2934 throw new \UnexpectedValueException(
2935 'The current host header value does not match the configured trusted hosts pattern! Check the pattern defined in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'trustedHostsPattern\'] and adapt it, if you want to allow the current host header \'' . $retVal . '\' for your installation.',
2936 1396795884
2937 );
2938 }
2939 break;
2940 case 'HTTP_REFERER':
2941
2942 case 'HTTP_USER_AGENT':
2943
2944 case 'HTTP_ACCEPT_ENCODING':
2945
2946 case 'HTTP_ACCEPT_LANGUAGE':
2947
2948 case 'REMOTE_HOST':
2949
2950 case 'QUERY_STRING':
2951 if (isset($_SERVER[$getEnvName])) {
2952 $retVal = $_SERVER[$getEnvName];
2953 }
2954 break;
2955 case 'TYPO3_DOCUMENT_ROOT':
2956 // Get the web root (it is not the root of the TYPO3 installation)
2957 // The absolute path of the script can be calculated with TYPO3_DOCUMENT_ROOT + SCRIPT_FILENAME
2958 // Some CGI-versions (LA13CGI) and mod-rewrite rules on MODULE versions will deliver a 'wrong' DOCUMENT_ROOT (according to our description). Further various aliases/mod_rewrite rules can disturb this as well.
2959 // Therefore the DOCUMENT_ROOT is now always calculated as the SCRIPT_FILENAME minus the end part shared with SCRIPT_NAME.
2960 $SFN = self::getIndpEnv('SCRIPT_FILENAME');
2961 $SN_A = explode('/', strrev(self::getIndpEnv('SCRIPT_NAME')));
2962 $SFN_A = explode('/', strrev($SFN));
2963 $acc = [];
2964 foreach ($SN_A as $kk => $vv) {
2965 if ((string)$SFN_A[$kk] === (string)$vv) {
2966 $acc[] = $vv;
2967 } else {
2968 break;
2969 }
2970 }
2971 $commonEnd = strrev(implode('/', $acc));
2972 if ((string)$commonEnd !== '') {
2973 $retVal = substr($SFN, 0, -(strlen($commonEnd) + 1));
2974 }
2975 break;
2976 case 'TYPO3_HOST_ONLY':
2977 $httpHost = self::getIndpEnv('HTTP_HOST');
2978 $httpHostBracketPosition = strpos($httpHost, ']');
2979 $httpHostParts = explode(':', $httpHost);
2980 $retVal = $httpHostBracketPosition !== false ? substr($httpHost, 0, $httpHostBracketPosition + 1) : array_shift($httpHostParts);
2981 break;
2982 case 'TYPO3_PORT':
2983 $httpHost = self::getIndpEnv('HTTP_HOST');
2984 $httpHostOnly = self::getIndpEnv('TYPO3_HOST_ONLY');
2985 $retVal = strlen($httpHost) > strlen($httpHostOnly) ? substr($httpHost, strlen($httpHostOnly) + 1) : '';
2986 break;
2987 case 'TYPO3_REQUEST_HOST':
2988 $retVal = (self::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://') . self::getIndpEnv('HTTP_HOST');
2989 break;
2990 case 'TYPO3_REQUEST_URL':
2991 $retVal = self::getIndpEnv('TYPO3_REQUEST_HOST') . self::getIndpEnv('REQUEST_URI');
2992 break;
2993 case 'TYPO3_REQUEST_SCRIPT':
2994 $retVal = self::getIndpEnv('TYPO3_REQUEST_HOST') . self::getIndpEnv('SCRIPT_NAME');
2995 break;
2996 case 'TYPO3_REQUEST_DIR':
2997 $retVal = self::getIndpEnv('TYPO3_REQUEST_HOST') . self::dirname(self::getIndpEnv('SCRIPT_NAME')) . '/';
2998 break;
2999 case 'TYPO3_SITE_URL':
3000 $url = self::getIndpEnv('TYPO3_REQUEST_DIR');
3001 // This can only be set by external entry scripts
3002 if (defined('TYPO3_PATH_WEB')) {
3003 $retVal = $url;
3004 } elseif (defined('PATH_thisScript') && defined('PATH_site')