api.ophanimnetlib.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Represents a basic network address subroutines library.
  4. */
  5. class OphanimNetLib {
  6. /**
  7. * Contains all available networks array
  8. *
  9. * @var array
  10. */
  11. protected $allNets = array();
  12. public function __construct($loadNets = true) {
  13. if ($loadNets) {
  14. $this->loadNets();
  15. }
  16. }
  17. /**
  18. * Loads the networks by retrieving all networks from the OphanimMgr class.
  19. */
  20. protected function loadNets() {
  21. $settings = new OphanimMgr();
  22. $this->allNets = $settings->getAllNetworks();
  23. }
  24. /**
  25. * Returns array with range start and end IP from IP address with CIDR notation
  26. *
  27. * @param string $ipcidr
  28. * @param bool $excludeNetworkAddr
  29. * @param bool $excludeBroadcastAddr
  30. *
  31. * @return array startip/endip
  32. */
  33. public function ipcidrToStartEndIP($ipcidr, $excludeNetworkAddr = false, $excludeBroadcastAddr = false) {
  34. $range = array();
  35. $ipcidr = explode('/', $ipcidr);
  36. $startip = (ip2long($ipcidr[0])) & ((-1 << (32 - (int) $ipcidr[1])));
  37. $endip = $startip + pow(2, (32 - (int) $ipcidr[1])) - 1;
  38. $startip = ($excludeNetworkAddr ? $startip + 1 : $startip);
  39. $endip = ($excludeBroadcastAddr ? $endip - 1 : $endip);
  40. $range['startip'] = long2ip($startip);
  41. $range['endip'] = long2ip($endip);
  42. return ($range);
  43. }
  44. /**
  45. * Converts CIDR mask into decimal like 24 => 255.255.255.0
  46. *
  47. * @param int $mask_bits
  48. *
  49. * @return string
  50. */
  51. public function cidr2mask($mask_bits) {
  52. if ($mask_bits > 31 || $mask_bits < 0)
  53. return ("0.0.0.0");
  54. $host_bits = 32 - $mask_bits;
  55. $num_hosts = pow(2, $host_bits) - 1;
  56. $netmask = ip2long("255.255.255.255") - $num_hosts;
  57. return long2ip($netmask);
  58. }
  59. /**
  60. * Checks is some IP between another two
  61. *
  62. * @param string $user_ip
  63. * @param string $ip_begin
  64. * @param string $ip_end
  65. *
  66. * @return bool
  67. */
  68. public function isIpBetween($ip, $ip_begin, $ip_end) {
  69. return (ip2int($ip) >= ip2int($ip_begin) && ip2int($ip) <= ip2int($ip_end));
  70. }
  71. /**
  72. * Checks if an IP is within a CIDR network
  73. *
  74. * @param string $ip
  75. * @param string $cidr
  76. *
  77. * @return bool
  78. */
  79. public function isIpInCidr($ip, $cidr) {
  80. list($network, $mask) = explode('/', $cidr);
  81. $networkStart = ip2long($network) & ~((1 << (32 - $mask)) - 1);
  82. $networkEnd = $networkStart + pow(2, (32 - $mask)) - 1;
  83. $ipLong = ip2long($ip);
  84. return ($ipLong >= $networkStart && $ipLong <= $networkEnd);
  85. }
  86. /**
  87. * Retrieves the network description for a given IP address.
  88. *
  89. * @param string $ip The IP address to retrieve the network description for.
  90. *
  91. * @return string
  92. */
  93. public function getIpNetDescription($ip) {
  94. $result = '';
  95. if (!empty($this->allNets)) {
  96. foreach ($this->allNets as $netId => $eachNetData) {
  97. if ($this->isIpInCidr($ip, $eachNetData['network'])) {
  98. $netDesc = $eachNetData['network'];
  99. if (isset($eachNetData['descr'])) {
  100. if (!empty($eachNetData['descr'])) {
  101. $netDesc = $eachNetData['descr'];
  102. }
  103. }
  104. $result = $netDesc;
  105. break;
  106. }
  107. }
  108. }
  109. return ($result);
  110. }
  111. }