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