‪TYPO3CMS  10.4
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 
26 {
33  public function ‪returnFieldJS(): string
34  {
35  $jsCode = [];
36  $jsCode[] = 'if (value === \'*\') {return value;}';
37  $jsCode[] = 'var parser = document.createElement(\'a\');';
38  $jsCode[] = 'parser.href = value.indexOf(\'://\') != -1 ? value : \'http://\' + value;';
39  $jsCode[] = 'return parser.host;';
40  return implode(' ', $jsCode);
41  }
42 
49  public function ‪evaluateFieldValue(string $value): string
50  {
51  // 1) Special case: * means any domain
52  if ($value === '*') {
53  return $value;
54  }
55 
56  // 2) Check if value contains a protocol like http:// https:// etc...
57  if (strpos($value, '://') !== false) {
58  $tmp = $this->‪parseUrl($value);
59  if (!empty($tmp)) {
60  return $tmp;
61  }
62  }
63 
64  // 3) Check domain name
65  // remove anything after the first "/"
66  $checkValue = $value;
67  if (strpos($value, '/') !== false) {
68  $checkValue = substr($value, 0, strpos($value, '/'));
69  }
70  $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])$/';
71  if (preg_match_all($validHostnameRegex, $checkValue, $matches, PREG_SET_ORDER) !== false) {
72  if (!empty($matches)) {
73  return $checkValue;
74  }
75  }
76 
77  // 4) IPv4 or IPv6
78  $isIP = filter_var($value, FILTER_VALIDATE_IP) === $value;
79  if ($isIP) {
80  return $value;
81  }
82 
83  return '';
84  }
85 
90  protected function ‪parseUrl(string $value): string
91  {
92  $urlParts = parse_url($value);
93  if (!empty($urlParts['host'])) {
94  $value = $urlParts['host'];
95 
96  // Special case IPv6 with protocol: http://[2001:0db8:85a3:08d3::0370:7344]/
97  // $urlParts['host'] will be [2001:0db8:85a3:08d3::0370:7344]
98  $ipv6Pattern = '/\[([a-zA-Z0-9:]*)\]/';
99  preg_match_all($ipv6Pattern, $urlParts['host'], $ipv6Matches, PREG_SET_ORDER);
100  if (!empty($ipv6Matches[0][1])) {
101  $value = $ipv6Matches[0][1];
102  }
103  }
104  return $value;
105  }
106 }
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\parseUrl
‪string parseUrl(string $value)
Definition: SourceHost.php:90
‪TYPO3\CMS\Redirects\Evaluation\SourceHost
Definition: SourceHost.php:26
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\evaluateFieldValue
‪string evaluateFieldValue(string $value)
Definition: SourceHost.php:49
‪TYPO3\CMS\Redirects\Evaluation\SourceHost\returnFieldJS
‪string returnFieldJS()
Definition: SourceHost.php:33
‪TYPO3\CMS\Redirects\Evaluation
Definition: SourceHost.php:18