‪TYPO3CMS  ‪main
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 {
40  {
41  return ‪JavaScriptModuleInstruction::create('@typo3/redirects/form-engine-evaluation.js', 'FormEngineEvaluation');
42  }
43 
50  public function ‪evaluateFieldValue(string $value): string
51  {
52  // 1) Special case: * means any domain
53  if ($value === '*') {
54  return $value;
55  }
56 
57  // 2) Check if value contains a protocol like http:// https:// etc...
59  $tmp = $this->‪parseUrl($value);
60  if (!empty($tmp)) {
61  return $tmp;
62  }
63  }
64 
65  // 3) Check domain name
66  // remove anything after the first "/"
67  $checkValue = $value;
68  if (str_contains($value, '/')) {
69  $checkValue = substr($value, 0, (int)strpos($value, '/'));
70  }
71  $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])$/';
72  if (preg_match_all($validHostnameRegex, $checkValue, $matches, PREG_SET_ORDER) !== false) {
73  if (!empty($matches)) {
74  return $checkValue;
75  }
76  }
77 
78  // 4) IPv4 or IPv6
79  $isIP = filter_var($value, FILTER_VALIDATE_IP) === $value;
80  if ($isIP) {
81  return $value;
82  }
83 
84  return '';
85  }
86 
87  protected function ‪parseUrl(string $value): string
88  {
89  $urlParts = parse_url($value);
90  if (!empty($urlParts['host'])) {
91  $value = $urlParts['host'];
92 
93  // Special case IPv6 with protocol: http://[2001:0db8:85a3:08d3::0370:7344]/
94  // $urlParts['host'] will be [2001:0db8:85a3:08d3::0370:7344]
95  $ipv6Pattern = '/\[([a-zA-Z0-9:]*)\]/';
96  preg_match_all($ipv6Pattern, $urlParts['host'], $ipv6Matches, PREG_SET_ORDER);
97  if (!empty($ipv6Matches[0][1])) {
98  $value = $ipv6Matches[0][1];
99  }
100  }
101  return $value;
102  }
103 }
‪TYPO3\CMS\Redirects\Evaluation\SourceHost
Definition: SourceHost.php:30
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\parseUrl
‪parseUrl(string $value)
Definition: SourceHost.php:87
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\returnFieldJS
‪returnFieldJS()
Definition: SourceHost.php:39
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\evaluateFieldValue
‪string evaluateFieldValue(string $value)
Definition: SourceHost.php:50
‪TYPO3\CMS\Core\Utility\PathUtility\hasProtocolAndScheme
‪static hasProtocolAndScheme(string $path)
Definition: PathUtility.php:445
‪TYPO3\CMS\Redirects\Evaluation
Definition: SourceHost.php:18