ReverseDNSHelper.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\utils;
  3. use MGModule\DNSManager2\mgLibs\custom\dns\record\Record;
  4. use MGModule\DNSManager2\models\custom\reverse\Reverse;
  5. use MGModule\DNSManager2\mgLibs\custom\dns\exceptions\DNSSubmoduleException;
  6. class ReverseDNSHelper
  7. {
  8. private static function getIPObject($ip)
  9. {
  10. if($ip instanceof IP)
  11. return $ip;
  12. return new IP($ip);
  13. }
  14. public static function reverseZoneName($ip)
  15. {
  16. $ip = self::getIPObject($ip);
  17. if($ip->isIPv4()) {
  18. $ip->reverse();
  19. return $ip->slice(1) . '.in-addr.arpa';
  20. } else {
  21. //return self::IPv6ToDotNotation($ip->slice(0)) . '.ip6.arpa';
  22. return self::IPv6ToDotNotation($ip->slice(0, IP::HALF)) . '.ip6.arpa';
  23. }
  24. }
  25. public static function reverseRecordName($ip)
  26. {
  27. $ip = self::getIPObject($ip);
  28. if($ip->isIPv4()) {
  29. $ip->reverse();
  30. return $ip->slice(0, 1);
  31. } else {
  32. return self::IPv6ToDotNotation($ip->slice(IP::HALF));
  33. }
  34. }
  35. public static function createPTRRecord($ip, $ttl, $value)
  36. {
  37. $record = new Record();
  38. $record->name = self::reverseRecordName($ip);
  39. $record->ttl = $ttl;
  40. $record->type = 'PTR';
  41. $record->createRDATAObject('PTR');
  42. $record->rdata->setFirstProperty($value);
  43. return $record;
  44. }
  45. private static function IPv6ToDotNotation($ip) {
  46. $ip = strrev(str_replace(':', '', $ip));
  47. return trim(chunk_split($ip, 1, '.'),'.');
  48. }
  49. public static function buildReverseFromARPA(Record $record,$domain)
  50. {
  51. $out = array();
  52. $out['from'] = $record->rdata->ptrdname;
  53. $num = explode('.',$record->name);
  54. $domain = explode('in',$domain);
  55. $firstIpPart = substr($domain[0],0,-1);
  56. $explodeFirstPart = explode('.',$firstIpPart);
  57. $reverseIp = array_reverse($explodeFirstPart);
  58. $out['ip'] = implode('.',$reverseIp) . '.' . $num[0];
  59. $out['ttl'] = $record->ttl;
  60. return $out;
  61. }
  62. public static function fromIP6Arpa($ip,$ptr)
  63. {
  64. $secondPart = str_replace('.'.$ip.'.', '', $ptr);
  65. $ip = str_replace('ip6.arpa', '' , $ip);
  66. $ip .= $secondPart;
  67. if(strlen($ip) !== 63)
  68. {
  69. throw new DNSSubmoduleException('Incorrect ip6.arpa form.');
  70. }
  71. $colon = 7;
  72. $split = explode('.', $ip);
  73. $adress = array_reverse($split);
  74. for($i = $colon; $i > 0; $i--)
  75. {
  76. $index = $i * 4;
  77. array_splice($adress,$index,0,':');
  78. }
  79. $ipv6 = implode('', $adress);
  80. return $ipv6;
  81. }
  82. }