Subversion Repositories php_utils

Rev

Rev 13 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * IP resolution functions
  5.  * Copyright 2012 Daniel Marschall, ViaThinkSoft
  6.  * Version 2012-02-02
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. /* -- Testcases --
  22. print_r(gethostbynamel6('example.com'));
  23. print_r(gethostbynamel6('ipv6.google.com'));
  24. print_r(gethostbynamel6('google.de'));
  25. print_r(gethostbynamel6('ipv6.google.de'));
  26. print_r(gethostbynamel6('abc'));
  27. print_r(gethostbynamel6('111.111.111.111'));
  28. print_r(gethostbynamel6('2620::2d0:200:0:0:0:10'));
  29. */
  30.  
  31. function resolveip($host) {
  32.         return gethostbynamel6($host);
  33. }
  34.  
  35. # http://www.php.net/manual/en/function.gethostbyname.php#70936
  36. # Modified by ViaThinkSoft
  37.  
  38. # VTS-Modified: try_a default false -> true
  39. function gethostbyname6($host, $try_a = /* false */ true) {
  40.         // get AAAA record for $host
  41.         // if $try_a is true, if AAAA fails, it tries for A
  42.         // the first match found is returned
  43.         // otherwise returns false
  44.  
  45.         $dns = gethostbynamel6($host, $try_a);
  46.         if ($dns == false) {
  47.                 return false;
  48.         } else {
  49.                 return $dns[0];
  50.         }
  51. }
  52.  
  53. # VTS-Modified: try_a default false -> true
  54. function gethostbynamel6($host, $try_a = /* false */ true) {
  55.         # Added by VTS
  56.         $ipfilter = filter_var($host,FILTER_VALIDATE_IP);
  57.         if ($ipfilter != '') return array($ipfilter);
  58.  
  59.         // get AAAA records for $host,
  60.         // if $try_a is true, if AAAA fails, it tries for A
  61.         // results are returned in an array of ips found matching type
  62.         // otherwise returns false
  63.  
  64.         $dns6 = dns_get_record($host, DNS_AAAA);
  65.         if ($try_a == true) {
  66.                 $dns4 = dns_get_record($host, DNS_A);
  67.                 $dns = array_merge($dns4, $dns6);
  68.         } else {
  69.                 $dns = $dns6;
  70.         }
  71.         $ip6 = array();
  72.         $ip4 = array();
  73.         foreach ($dns as $record) {
  74.                 if ($record["type"] == "A") {
  75.                         $ip4[] = $record["ip"];
  76.                 }
  77.                 if ($record["type"] == "AAAA") {
  78.                         $ip6[] = $record["ipv6"];
  79.                 }
  80.         }
  81.  
  82.         # VTS-Modified: Output IP4+IP6 merged instead of giving only IPv6 or IPv4
  83.         $merged = array_merge($ip4, $ip6);
  84.         if (count($merged) < 1) {
  85.                 return false;
  86.         } else {
  87.                 return $merged;
  88.         }
  89.         /*
  90.         if (count($ip6) < 1) {
  91.                 if ($try_a == true) {
  92.                         if (count($ip4) < 1) {
  93.                                 return false;
  94.                         } else {
  95.                                 return $ip4;
  96.                         }
  97.                 } else {
  98.                         return false;
  99.                 }
  100.         } else {
  101.                 return $ip6;
  102.         }
  103.         */
  104. }
  105.