Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
IpUtils.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpFoundation;
013
014 /**
015 * Http utility functions.
016 *
017 * @author Fabien Potencier <fabien@symfony.com>
018 */
019 class IpUtils
020 {
021 private static $checkedIps = [];
022
023 /**
024 * This class should not be instantiated.
025 */
026 private function __construct()
027 {
028 }
029
030 /**
031 * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
032 *
033 * @param string $requestIp IP to check
034 * @param string|array $ips List of IPs or subnets (can be a string if only a single one)
035 *
036 * @return bool Whether the IP is valid
037 */
038 public static function checkIp($requestIp, $ips)
039 {
040 if (!\is_array($ips)) {
041 $ips = [$ips];
042 }
043
044 $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';
045
046 foreach ($ips as $ip) {
047 if (self::$method($requestIp, $ip)) {
048 return true;
049 }
050 }
051
052 return false;
053 }
054
055 /**
056 * Compares two IPv4 addresses.
057 * In case a subnet is given, it checks if it contains the request IP.
058 *
059 * @param string $requestIp IPv4 address to check
060 * @param string $ip IPv4 address or subnet in CIDR notation
061 *
062 * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet
063 */
064 public static function checkIp4($requestIp, $ip)
065 {
066 $cacheKey = $requestIp.'-'.$ip;
067 if (isset(self::$checkedIps[$cacheKey])) {
068 return self::$checkedIps[$cacheKey];
069 }
070
071 if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
072 return self::$checkedIps[$cacheKey] = false;
073 }
074
075 if (false !== strpos($ip, '/')) {
076 list($address, $netmask) = explode('/', $ip, 2);
077
078 if ('0' === $netmask) {
079 return self::$checkedIps[$cacheKey] = filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4);
080 }
081
082 if ($netmask < 0 || $netmask > 32) {
083 return self::$checkedIps[$cacheKey] = false;
084 }
085 } else {
086 $address = $ip;
087 $netmask = 32;
088 }
089
090 if (false === ip2long($address)) {
091 return self::$checkedIps[$cacheKey] = false;
092 }
093
094 return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
095 }
096
097 /**
098 * Compares two IPv6 addresses.
099 * In case a subnet is given, it checks if it contains the request IP.
100 *
101 * @author David Soria Parra <dsp at php dot net>
102 *
103 * @see https://github.com/dsp/v6tools
104 *
105 * @param string $requestIp IPv6 address to check
106 * @param string $ip IPv6 address or subnet in CIDR notation
107 *
108 * @return bool Whether the IP is valid
109 *
110 * @throws \RuntimeException When IPV6 support is not enabled
111 */
112 public static function checkIp6($requestIp, $ip)
113 {
114 $cacheKey = $requestIp.'-'.$ip;
115 if (isset(self::$checkedIps[$cacheKey])) {
116 return self::$checkedIps[$cacheKey];
117 }
118
119 if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) {
120 throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
121 }
122
123 if (false !== strpos($ip, '/')) {
124 list($address, $netmask) = explode('/', $ip, 2);
125
126 if ('0' === $netmask) {
127 return (bool) unpack('n*', @inet_pton($address));
128 }
129
130 if ($netmask < 1 || $netmask > 128) {
131 return self::$checkedIps[$cacheKey] = false;
132 }
133 } else {
134 $address = $ip;
135 $netmask = 128;
136 }
137
138 $bytesAddr = unpack('n*', @inet_pton($address));
139 $bytesTest = unpack('n*', @inet_pton($requestIp));
140
141 if (!$bytesAddr || !$bytesTest) {
142 return self::$checkedIps[$cacheKey] = false;
143 }
144
145 for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
146 $left = $netmask - 16 * ($i - 1);
147 $left = ($left <= 16) ? $left : 16;
148 $mask = ~(0xffff >> $left) & 0xffff;
149 if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
150 return self::$checkedIps[$cacheKey] = false;
151 }
152 }
153
154 return self::$checkedIps[$cacheKey] = true;
155 }
156 }
157