示例#1
0
文件: Dns.php 项目: jubinpatel/horde
 public function create(Horde_Injector $injector)
 {
     if (!class_exists('Net_DNS2_Resolver')) {
         return null;
     }
     if ($tmpdir = Horde::getTempDir()) {
         $config = array('cache_file' => $tmpdir . '/horde_dns.cache', 'cache_size' => 100000, 'cache_type' => 'file');
     } else {
         $config = array();
     }
     $resolver = new Net_DNS2_Resolver($config);
     if (is_readable('/etc/resolv.conf')) {
         try {
             $resolver->setServers('/etc/resolv.conf');
         } catch (Net_DNS2_Exception $e) {
         }
     }
     return $resolver;
 }
 function autodiscover($domain, $dns = array())
 {
     require_once PEAR_DIR . 'Net/DNS2.php';
     // TODO: Lookup DNS server from hosts file if not set
     $q = new Net_DNS2_Resolver();
     if ($dns) {
         $q->setServers($dns);
     }
     $servers = array();
     try {
         $r = $q->query('_ldap._tcp.' . $domain, 'SRV');
     } catch (Net_DNS2_Exception $e) {
         // TODO: Log warning or something
         return $servers;
     }
     foreach ($r->answer as $srv) {
         // TODO: Get the actual IP of the server (?)
         $servers[] = array('host' => "{$srv->target}:{$srv->port}", 'priority' => $srv->priority, 'weight' => $srv->weight);
     }
     // Sort servers by priority ASC, then weight DESC
     usort($servers, function ($a, $b) {
         return ($a['priority'] << 15) - $a['weight'] - ($b['priority'] << 15) + $b['weight'];
     });
     return $servers;
 }