1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-24 09:30:34 +01:00

Merge pull request #5724 from rubencm/ticket/16190

[ticket/16190] Deprecate phpbb's checkdnsrr wrapper
This commit is contained in:
Marc Alexander 2019-11-01 08:16:10 +01:00
commit 9887a9d76c
No known key found for this signature in database
GPG Key ID: 50E0D2423696F995
7 changed files with 27 additions and 238 deletions

View File

@ -3141,172 +3141,6 @@ function phpbb_inet_pton($address)
return false;
}
/**
* Wrapper for php's checkdnsrr function.
*
* @param string $host Fully-Qualified Domain Name
* @param string $type Resource record type to lookup
* Supported types are: MX (default), A, AAAA, NS, TXT, CNAME
* Other types may work or may not work
*
* @return mixed true if entry found,
* false if entry not found,
* null if this function is not supported by this environment
*
* Since null can also be returned, you probably want to compare the result
* with === true or === false,
*/
function phpbb_checkdnsrr($host, $type = 'MX')
{
// The dot indicates to search the DNS root (helps those having DNS prefixes on the same domain)
if (substr($host, -1) == '.')
{
$host_fqdn = $host;
$host = substr($host, 0, -1);
}
else
{
$host_fqdn = $host . '.';
}
// $host has format some.host.example.com
// $host_fqdn has format some.host.example.com.
// If we're looking for an A record we can use gethostbyname()
if ($type == 'A' && function_exists('gethostbyname'))
{
return (@gethostbyname($host_fqdn) == $host_fqdn) ? false : true;
}
if (function_exists('checkdnsrr'))
{
return checkdnsrr($host_fqdn, $type);
}
if (function_exists('dns_get_record'))
{
// dns_get_record() expects an integer as second parameter
// We have to convert the string $type to the corresponding integer constant.
$type_constant = 'DNS_' . $type;
$type_param = (defined($type_constant)) ? constant($type_constant) : DNS_ANY;
// dns_get_record() might throw E_WARNING and return false for records that do not exist
$resultset = @dns_get_record($host_fqdn, $type_param);
if (empty($resultset) || !is_array($resultset))
{
return false;
}
else if ($type_param == DNS_ANY)
{
// $resultset is a non-empty array
return true;
}
foreach ($resultset as $result)
{
if (
isset($result['host']) && $result['host'] == $host &&
isset($result['type']) && $result['type'] == $type
)
{
return true;
}
}
return false;
}
// If we're on Windows we can still try to call nslookup via exec() as a last resort
if (DIRECTORY_SEPARATOR == '\\' && function_exists('exec'))
{
@exec('nslookup -type=' . escapeshellarg($type) . ' ' . escapeshellarg($host_fqdn), $output);
// If output is empty, the nslookup failed
if (empty($output))
{
return NULL;
}
foreach ($output as $line)
{
$line = trim($line);
if (empty($line))
{
continue;
}
// Squash tabs and multiple whitespaces to a single whitespace.
$line = preg_replace('/\s+/', ' ', $line);
switch ($type)
{
case 'MX':
if (stripos($line, "$host MX") === 0)
{
return true;
}
break;
case 'NS':
if (stripos($line, "$host nameserver") === 0)
{
return true;
}
break;
case 'TXT':
if (stripos($line, "$host text") === 0)
{
return true;
}
break;
case 'CNAME':
if (stripos($line, "$host canonical name") === 0)
{
return true;
}
break;
default:
case 'AAAA':
// AAAA records returned by nslookup on Windows XP/2003 have this format.
// Later Windows versions use the A record format below for AAAA records.
if (stripos($line, "$host AAAA IPv6 address") === 0)
{
return true;
}
// No break
case 'A':
if (!empty($host_matches))
{
// Second line
if (stripos($line, "Address: ") === 0)
{
return true;
}
else
{
$host_matches = false;
}
}
else if (stripos($line, "Name: $host") === 0)
{
// First line
$host_matches = true;
}
break;
}
}
return false;
}
return NULL;
}
// Handler, header and footer
/**

View File

@ -601,3 +601,25 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage
return $file;
}
/**
* Wrapper for php's checkdnsrr function.
*
* @param string $host Fully-Qualified Domain Name
* @param string $type Resource record type to lookup
* Supported types are: MX (default), A, AAAA, NS, TXT, CNAME
* Other types may work or may not work
*
* @return mixed true if entry found,
* false if entry not found,
* null if this function is not supported by this environment
*
* Since null can also be returned, you probably want to compare the result
* with === true or === false,
*
* @deprecated 3.3.0-b2 (To be removed: 4.0.0)
*/
function phpbb_checkdnsrr($host, $type = 'MX')
{
return checkdnsrr($host, $type);
}

View File

@ -1910,7 +1910,7 @@ function phpbb_validate_email($email, $config = null)
{
list(, $domain) = explode('@', $email);
if (phpbb_checkdnsrr($domain, 'A') === false && phpbb_checkdnsrr($domain, 'MX') === false)
if (checkdnsrr($domain, 'A') === false && checkdnsrr($domain, 'MX') === false)
{
return 'DOMAIN_NO_MX_RECORD';
}

View File

@ -1400,7 +1400,7 @@ class session
foreach ($dnsbl_check as $dnsbl => $lookup)
{
if (phpbb_checkdnsrr($reverse_ip . '.' . $dnsbl . '.', 'A') === true)
if (checkdnsrr($reverse_ip . '.' . $dnsbl . '.', 'A') === true)
{
$info = array($dnsbl, $lookup . $ip);
}
@ -1444,7 +1444,7 @@ class session
{
// One problem here... the return parameter for the "windows" method is different from what
// we expect... this may render this check useless...
if (phpbb_checkdnsrr($uri . '.multi.uribl.com.', 'A') === true)
if (checkdnsrr($uri . '.multi.uribl.com.', 'A') === true)
{
return true;
}

View File

@ -24,7 +24,7 @@ class phpbb_functions_get_remote_file extends phpbb_test_case
$hostname = 'version.phpbb.com';
if (!phpbb_checkdnsrr($hostname, 'A'))
if (!checkdnsrr($hostname, 'A'))
{
$this->markTestSkipped(sprintf(
'Could not find a DNS record for hostname %s. ' .

View File

@ -1,67 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/**
* @group slow
*/
class phpbb_network_checkdnsrr_test extends phpbb_test_case
{
public function data_provider()
{
return array(
// Existing MX record
array('phpbb.com', 'MX', true),
// Non-existing MX record
array('does-not-exist.phpbb.com', 'MX', false),
// Existing A record
array('www.phpbb.com', 'A', true),
// Non-existing A record
array('does-not-exist.phpbb.com', 'A', false),
// Existing AAAA record
array('www.six.heise.de', 'AAAA', true),
// Non-existing AAAA record
array('does-not-exist.phpbb.com', 'AAAA', false),
// Existing CNAME record
array('area51.phpbb.com', 'CNAME', true),
// Non-existing CNAME record
array('does-not-exist.phpbb.com', 'CNAME', false),
// Existing NS record
array('phpbb.com', 'NS', true),
// Non-existing NS record
array('does-not-exist', 'NS', false),
// Existing TXT record
array('phpbb.com', 'TXT', true),
// Non-existing TXT record
array('does-not-exist', 'TXT', false),
);
}
/**
* @dataProvider data_provider
*/
public function test_checkdnsrr($host, $type, $expected)
{
$this->assertEquals($expected, phpbb_checkdnsrr($host, $type));
}
}

View File

@ -42,7 +42,7 @@ class phpbb_version_helper_fetch_test extends phpbb_test_case
global $phpbb_root_path, $phpEx;
include_once($phpbb_root_path . 'includes/functions.' . $phpEx);
if (!phpbb_checkdnsrr('version.phpbb.com', 'A'))
if (!checkdnsrr('version.phpbb.com', 'A'))
{
$this->markTestSkipped(sprintf(
'Could not find a DNS record for hostname %s. ' .