Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace GeoIp2;
   6  
   7  class Util
   8  {
   9      /**
  10       * This returns the network in CIDR notation for the given IP and prefix
  11       * length. This is for internal use only.
  12       *
  13       * @internal
  14       * @ignore
  15       */
  16      public static function cidr(string $ipAddress, int $prefixLen): string
  17      {
  18          $ipBytes = inet_pton($ipAddress);
  19          $networkBytes = str_repeat("\0", \strlen($ipBytes));
  20  
  21          $curPrefix = $prefixLen;
  22          for ($i = 0; $i < \strlen($ipBytes) && $curPrefix > 0; $i++) {
  23              $b = $ipBytes[$i];
  24              if ($curPrefix < 8) {
  25                  $shiftN = 8 - $curPrefix;
  26                  $b = \chr(0xFF & (\ord($b) >> $shiftN) << $shiftN);
  27              }
  28              $networkBytes[$i] = $b;
  29              $curPrefix -= 8;
  30          }
  31  
  32          $network = inet_ntop($networkBytes);
  33  
  34          return "$network/$prefixLen";
  35      }
  36  }