Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

IpUtils.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 3.44 KiB


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 = false !== strpos($requestIp, ':') ? '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 IP is valid
061       */
062      public static function checkIp4($requestIp, $ip)
063      {
064          if (false !== strpos($ip, '/')) {
065              list($address, $netmask) = explode('/', $ip, 2);
066   
067              if ($netmask < 1 || $netmask > 32) {
068                  return false;
069              }
070          } else {
071              $address = $ip;
072              $netmask = 32;
073          }
074   
075          return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
076      }
077   
078      /**
079       * Compares two IPv6 addresses.
080       * In case a subnet is given, it checks if it contains the request IP.
081       *
082       * @author David Soria Parra <dsp at php dot net>
083       * @see https://github.com/dsp/v6tools
084       *
085       * @param string $requestIp IPv6 address to check
086       * @param string $ip        IPv6 address or subnet in CIDR notation
087       *
088       * @return bool    Whether the IP is valid
089       *
090       * @throws \RuntimeException When IPV6 support is not enabled
091       */
092      public static function checkIp6($requestIp, $ip)
093      {
094          if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
095              throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
096          }
097   
098          if (false !== strpos($ip, '/')) {
099              list($address, $netmask) = explode('/', $ip, 2);
100   
101              if ($netmask < 1 || $netmask > 128) {
102                  return false;
103              }
104          } else {
105              $address = $ip;
106              $netmask = 128;
107          }
108   
109          $bytesAddr = unpack("n*", inet_pton($address));
110          $bytesTest = unpack("n*", inet_pton($requestIp));
111   
112          for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
113              $left = $netmask - 16 * ($i-1);
114              $left = ($left <= 16) ? $left : 16;
115              $mask = ~(0xffff >> $left) & 0xffff;
116              if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
117                  return false;
118              }
119          }
120   
121          return true;
122      }
123  }
124