/usr/share/php/Horde/Mail/Autoconfig/Driver/Srv.php is in php-horde-mail-autoconfig 1.0.2-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | <?php
/**
* Copyright 2014-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2014-2015 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mail_Autoconfig
*/
/**
* Perform RFC 6186 DNS SRV record lookups to determine mail configuration.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2014-2015 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mail_Autoconfig
*/
class Horde_Mail_Autoconfig_Driver_Srv extends Horde_Mail_Autoconfig_Driver
{
/**
* DNS resolver.
*
* @var Net_DNS2_Resolver
*/
public $dns;
/**
* High priority: this is a standardized (RFC) method of determining
* configuration values.
*/
public $priority = 10;
/**
*/
public function msaSearch($domains, array $opts = array())
{
$queries = array('_submission');
return $this->_srvSearch($domains, $queries);
}
/**
*/
public function mailSearch($domains, array $opts = array())
{
$queries = array();
if (empty($opts['no_imap'])) {
$queries[] = '_imap';
$queries[] = '_imaps';
}
if (empty($opts['no_pop3'])) {
$queries[] = '_pop3';
$queries[] = '_pop3s';
}
return $this->_srvSearch($domains, $queries);
}
/**
* Perform the SRV search.
*
* @param array $domains List of domains to search.
* @param array $queries The SRV queries to run.
*
* @return mixed False if no servers found, or a list of server objects
* in order of decreasing priority.
*/
protected function _srvSearch($domains, $queries)
{
$obs = $out = array();
if (is_null($this->dns)) {
$this->dns = new Net_DNS2_Resolver();
}
foreach ($domains as $val) {
foreach ($queries as $val2) {
try {
$res = $this->dns->query($val2 . '._tcp.' . $val, 'SRV');
foreach ($res->answer as $val3) {
if (strlen($val3->target)) {
$val3->query = $val2;
$obs[$val3->priority][] = $val3;
}
}
} catch (Net_DNS2_Exception $e) {
// Not found; ignore.
}
}
}
if (empty($obs)) {
return false;
}
/* Sort via priority ranking. Lower value is higher priority. */
ksort($obs, SORT_NUMERIC);
foreach ($obs as $val) {
/* Do weight determination if a multiple servers have identical
* priorities. */
if (count($val) > 1) {
/* Weight determination algorithm is defined in RFC 2782.
* First, move all entries with weight 0 to beginning of
* list. */
$tmp = array();
foreach ($val as $key2 => $val2) {
if (empty($val2->weight)) {
$tmp[] = $val2;
unset($val[$key2]);
}
}
$tmp = array_merge($tmp, $val);
$val = array();
while (count($tmp) > 1) {
$i = 0;
/* Next, iterate over list and update the "running
* sum": the incremental value of each entry's weight. */
foreach ($tmp as $val2) {
$i += $val2->weight;
$val2->running = $i;
}
/* Finally, select a random number in the range of 0->$i.
* The first entry in the list (sequentially) that has a
* running total >= to this random number is the next
* server in the priority list. */
$rand = mt_rand(0, $i);
foreach ($tmp as $key2 => $val2) {
if ($val2->running >= $rand) {
$val[] = $val2;
/* Remove this server from the list. */
unset($tmp[$key2]);
break;
}
}
/* Repeat until we have a single entry left in $tmp. */
}
/* One entry left in $tmp, so add to $val. */
$val[] = reset($tmp);
}
foreach ($val as $val2) {
switch ($val2->query) {
case '_imap':
$tmp = new Horde_Mail_Autoconfig_Server_Imap();
break;
case '_imaps':
$tmp = new Horde_Mail_Autoconfig_Server_Imap();
$tmp->tls = 'tls';
break;
case '_pop3':
$tmp = new Horde_Mail_Autoconfig_Server_Pop3();
break;
case '_pop3s':
$tmp = new Horde_Mail_Autoconfig_Server_Pop3();
$tmp->tls = 'tls';
break;
case '_submission':
$tmp = new Horde_Mail_Autoconfig_Server_Msa();
break;
}
$tmp->host = strval($val2->target);
$tmp->port = intval($val2->port);
$out[] = $tmp;
}
}
return $out;
}
}
|