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