‪TYPO3CMS  11.5
SourceHost.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 
30 {
42  {
43  return ‪JavaScriptModuleInstruction::forRequireJS('TYPO3/CMS/Redirects/FormEngineEvaluation', 'FormEngineEvaluation');
44  }
45 
52  public function ‪evaluateFieldValue(string $value): string
53  {
54  // 1) Special case: * means any domain
55  if ($value === '*') {
56  return $value;
57  }
58 
59  // 2) Check if value contains a protocol like http:// https:// etc...
61  $tmp = $this->‪parseUrl($value);
62  if (!empty($tmp)) {
63  return $tmp;
64  }
65  }
66 
67  // 3) Check domain name
68  // remove anything after the first "/"
69  $checkValue = $value;
70  if (str_contains($value, '/')) {
71  $checkValue = substr($value, 0, (int)strpos($value, '/'));
72  }
73  $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])$/';
74  if (preg_match_all($validHostnameRegex, $checkValue, $matches, PREG_SET_ORDER) !== false) {
75  if (!empty($matches)) {
76  return $checkValue;
77  }
78  }
79 
80  // 4) IPv4 or IPv6
81  $isIP = filter_var($value, FILTER_VALIDATE_IP) === $value;
82  if ($isIP) {
83  return $value;
84  }
85 
86  return '';
87  }
88 
89  protected function ‪parseUrl(string $value): string
90  {
91  $urlParts = parse_url($value);
92  if (!empty($urlParts['host'])) {
93  $value = $urlParts['host'];
94 
95  // Special case IPv6 with protocol: http://[2001:0db8:85a3:08d3::0370:7344]/
96  // $urlParts['host'] will be [2001:0db8:85a3:08d3::0370:7344]
97  $ipv6Pattern = '/\[([a-zA-Z0-9:]*)\]/';
98  preg_match_all($ipv6Pattern, $urlParts['host'], $ipv6Matches, PREG_SET_ORDER);
99  if (!empty($ipv6Matches[0][1])) {
100  $value = $ipv6Matches[0][1];
101  }
102  }
103  return $value;
104  }
105 }
‪TYPO3\CMS\Redirects\Evaluation\SourceHost
Definition: SourceHost.php:30
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\parseUrl
‪parseUrl(string $value)
Definition: SourceHost.php:89
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\forRequireJS
‪static self forRequireJS(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:49
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\returnFieldJS
‪JavaScriptModuleInstruction returnFieldJS()
Definition: SourceHost.php:41
‪TYPO3\CMS\Core\Utility\PathUtility\hasProtocolAndScheme
‪static bool hasProtocolAndScheme(string $path)
Definition: PathUtility.php:463
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\evaluateFieldValue
‪string evaluateFieldValue(string $value)
Definition: SourceHost.php:52
‪TYPO3\CMS\Redirects\Evaluation
Definition: SourceHost.php:18