‪TYPO3CMS  9.5
SourceHost.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
25 {
31  public function ‪returnFieldJS(): string
32  {
33  $jsCode = [];
34  $jsCode[] = 'if (value === \'*\') {return value;}';
35  $jsCode[] = 'var parser = document.createElement(\'a\');';
36  $jsCode[] = 'parser.href = value.indexOf(\'://\') != -1 ? value : \'http://\' + value;';
37  $jsCode[] = 'return parser.host;';
38  return implode(' ', $jsCode);
39  }
40 
47  public function ‪evaluateFieldValue(string $value): string
48  {
49  // 1) Special case: * means any domain
50  if ($value === '*') {
51  return $value;
52  }
53 
54  // 2) Check if value contains a protocol like http:// https:// etc...
55  if (strpos($value, '://') !== false) {
56  $tmp = $this->‪parseUrl($value);
57  if (!empty($tmp)) {
58  return $tmp;
59  }
60  }
61 
62  // 3) Check domain name
63  // remove anything after the first "/"
64  $checkValue = $value;
65  if (strpos($value, '/') !== false) {
66  $checkValue = substr($value, 0, strpos($value, '/'));
67  }
68  $validHostnameRegex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/';
69  if (preg_match_all($validHostnameRegex, $checkValue, $matches, PREG_SET_ORDER) !== false) {
70  if (!empty($matches)) {
71  return $checkValue;
72  }
73  }
74 
75  // 4) IPv4 or IPv6
76  $isIP = filter_var($value, FILTER_VALIDATE_IP) === $value;
77  if ($isIP) {
78  return $value;
79  }
80 
81  return '';
82  }
83 
88  protected function ‪parseUrl(string $value): string
89  {
90  $urlParts = parse_url($value);
91  if (!empty($urlParts['host'])) {
92  $value = $urlParts['host'];
93 
94  // Special case IPv6 with protocol: http://[2001:0db8:85a3:08d3::0370:7344]/
95  // $urlParts['host'] will be [2001:0db8:85a3:08d3::0370:7344]
96  $ipv6Pattern = '/\[([a-zA-Z0-9:]*)\]/';
97  preg_match_all($ipv6Pattern, $urlParts['host'], $ipv6Matches, PREG_SET_ORDER);
98  if (!empty($ipv6Matches[0][1])) {
99  $value = $ipv6Matches[0][1];
100  }
101  }
102  return $value;
103  }
104 }
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\parseUrl
‪string parseUrl(string $value)
Definition: SourceHost.php:88
‪TYPO3\CMS\Redirects\Evaluation\SourceHost
Definition: SourceHost.php:25
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\evaluateFieldValue
‪string evaluateFieldValue(string $value)
Definition: SourceHost.php:47
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\returnFieldJS
‪string returnFieldJS()
Definition: SourceHost.php:31
‪TYPO3\CMS\Redirects\Evaluation
Definition: SourceHost.php:4