Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
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 /**
022 * This class should not be instantiated.
023 */
024 private function __construct()
025 {
026 }
027
028 /**
029 * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
030 *
031 * @param string $requestIp IP to check
032 * @param string|array $ips List of IPs or subnets (can be a string if only a single one)
033 *
034 * @return bool Whether the IP is valid
035 */
036 public static function checkIp($requestIp, $ips)
037 {
038 if (!is_array($ips)) {
039 $ips = array($ips);
040 }
041
042 $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';
043
044 foreach ($ips as $ip) {
045 if (self::$method($requestIp, $ip)) {
046 return true;
047 }
048 }
049
050 return false;
051 }
052
053 /**
054 * Compares two IPv4 addresses.
055 * In case a subnet is given, it checks if it contains the request IP.
056 *
057 * @param string $requestIp IPv4 address to check
058 * @param string $ip IPv4 address or subnet in CIDR notation
059 *
060 * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet
061 */
062 public static function checkIp4($requestIp, $ip)
063 {
064 if (!filter_var($requestIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
065 return false;
066 }
067
068 if (false !== strpos($ip, '/')) {
069 list($address, $netmask) = explode('/', $ip, 2);
070
071 if ($netmask === '0') {
072 return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
073 }
074
075 if ($netmask < 0 || $netmask > 32) {
076 return false;
077 }
078 } else {
079 $address = $ip;
080 $netmask = 32;
081 }
082
083 return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
084 }
085
086 /**
087 * Compares two IPv6 addresses.
088 * In case a subnet is given, it checks if it contains the request IP.
089 *
090 * @author David Soria Parra <dsp at php dot net>
091 *
092 * @see https://github.com/dsp/v6tools
093 *
094 * @param string $requestIp IPv6 address to check
095 * @param string $ip IPv6 address or subnet in CIDR notation
096 *
097 * @return bool Whether the IP is valid
098 *
099 * @throws \RuntimeException When IPV6 support is not enabled
100 */
101 public static function checkIp6($requestIp, $ip)
102 {
103 if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
104 throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
105 }
106
107 if (false !== strpos($ip, '/')) {
108 list($address, $netmask) = explode('/', $ip, 2);
109
110 if ($netmask < 1 || $netmask > 128) {
111 return false;
112 }
113 } else {
114 $address = $ip;
115 $netmask = 128;
116 }
117
118 $bytesAddr = unpack('n*', @inet_pton($address));
119 $bytesTest = unpack('n*', @inet_pton($requestIp));
120
121 if (!$bytesAddr || !$bytesTest) {
122 return false;
123 }
124
125 for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
126 $left = $netmask - 16 * ($i - 1);
127 $left = ($left <= 16) ? $left : 16;
128 $mask = ~(0xffff >> $left) & 0xffff;
129 if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
130 return false;
131 }
132 }
133
134 return true;
135 }
136 }
137