Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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