aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Woody <a_jelly_doughnut@phpbb.com>2010-08-19 10:53:10 -0500
committerJosh Woody <a_jelly_doughnut@phpbb.com>2010-08-19 10:53:10 -0500
commitf0aaecd6cc5f530b70d31a69b8d7305c98121d12 (patch)
tree590671d51c42d595f7fb2e96b111f1c2d3222b27
parent6343e67fab431ac15d62cff7d014e990a00c69f1 (diff)
parent24e23755cfe57afcf3125ba9c684b32942425696 (diff)
downloadforums-f0aaecd6cc5f530b70d31a69b8d7305c98121d12.tar
forums-f0aaecd6cc5f530b70d31a69b8d7305c98121d12.tar.gz
forums-f0aaecd6cc5f530b70d31a69b8d7305c98121d12.tar.bz2
forums-f0aaecd6cc5f530b70d31a69b8d7305c98121d12.tar.xz
forums-f0aaecd6cc5f530b70d31a69b8d7305c98121d12.zip
Merge branch 'ticket/bantu/9599' into develop-olympus
* ticket/bantu/9599: [ticket/9599] Reimplement phpbb_checkdnsrr() function. [ticket/9599] Adding tests for phpbb_checkdnsrr(). [ticket/9599] Adding network function tests.
-rw-r--r--phpBB/includes/functions.php169
-rw-r--r--tests/all_tests.php2
-rw-r--r--tests/network/all_tests.php40
-rw-r--r--tests/network/checkdnsrr.php63
4 files changed, 257 insertions, 17 deletions
diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php
index 6d2a6e685c..980a26d525 100644
--- a/phpBB/includes/functions.php
+++ b/phpBB/includes/functions.php
@@ -3462,26 +3462,110 @@ function short_ipv6($ip, $length)
/**
* Wrapper for php's checkdnsrr function.
*
-* The windows failover is from the php manual
-* Please make sure to check the return value for === true and === false, since NULL could
-* be returned too.
+* @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 true if entry found, false if not, NULL if this function is not supported by this environment
+* @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,
+*
+* @author bantu
*/
-function phpbb_checkdnsrr($host, $type = '')
+function phpbb_checkdnsrr($host, $type = 'MX')
{
- $type = (!$type) ? 'MX' : $type;
+ // 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.
- // Call checkdnsrr() if available. This is also the case on Windows with PHP 5.3 or later.
- if (function_exists('checkdnsrr'))
+ // If we're looking for an A record we can use gethostbyname()
+ if ($type == 'A' && function_exists('gethostbyname'))
{
- // The dot indicates to search the DNS root (helps those having DNS prefixes on the same domain)
- return checkdnsrr($host . '.', $type);
+ return (@gethostbyname($host_fqdn) == $host_fqdn) ? false : true;
}
- else if (DIRECTORY_SEPARATOR == '\\' && function_exists('exec'))
+
+ // checkdnsrr() is available on Windows since PHP 5.3,
+ // but until 5.3.3 it only works for MX records
+ // See: http://bugs.php.net/bug.php?id=51844
+
+ // Call checkdnsrr() if
+ // we're looking for an MX record or
+ // we're not on Windows or
+ // we're running a PHP version where #51844 has been fixed
+
+ // checkdnsrr() supports AAAA since 5.0.0
+ // checkdnsrr() supports TXT since 5.2.4
+ if (
+ ($type == 'MX' || DIRECTORY_SEPARATOR != '\\' || version_compare(PHP_VERSION, '5.3.3', '>=')) &&
+ ($type != 'AAAA' || version_compare(PHP_VERSION, '5.0.0', '>=')) &&
+ ($type != 'TXT' || version_compare(PHP_VERSION, '5.2.4', '>=')) &&
+ function_exists('checkdnsrr')
+ )
+ {
+ return checkdnsrr($host_fqdn, $type);
+ }
+
+ // dns_get_record() is available since PHP 5; since PHP 5.3 also on Windows,
+ // but on Windows it does not work reliable for AAAA records before PHP 5.3.1
+
+ // Call dns_get_record() if
+ // we're not looking for an AAAA record or
+ // we're not on Windows or
+ // we're running a PHP version where AAAA lookups work reliable
+ if (
+ ($type != 'AAAA' || DIRECTORY_SEPARATOR != '\\' || version_compare(PHP_VERSION, '5.3.1', '>=')) &&
+ function_exists('dns_get_record')
+ )
{
- // @exec('nslookup -retry=1 -timout=1 -type=' . escapeshellarg($type) . ' ' . escapeshellarg($host), $output);
- @exec('nslookup -type=' . escapeshellarg($type) . ' ' . escapeshellarg($host) . '.', $output);
+ // 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))
@@ -3491,15 +3575,66 @@ function phpbb_checkdnsrr($host, $type = '')
foreach ($output as $line)
{
- if (!trim($line))
+ $line = trim($line);
+
+ if (empty($line))
{
continue;
}
- // Valid records begin with host name:
- if (strpos($line, $host) === 0)
+ // Squash tabs and multiple whitespaces to a single whitespace.
+ $line = preg_replace('/\s+/', ' ', $line);
+
+ switch ($type)
{
- return true;
+ 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;
+ }
+
+ default:
+ case 'A':
+ case 'AAAA':
+ 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;
}
}
diff --git a/tests/all_tests.php b/tests/all_tests.php
index 7894d688ee..8dc07872e8 100644
--- a/tests/all_tests.php
+++ b/tests/all_tests.php
@@ -24,6 +24,7 @@ require_once 'template/all_tests.php';
require_once 'text_processing/all_tests.php';
require_once 'dbal/all_tests.php';
require_once 'regex/all_tests.php';
+require_once 'network/all_tests.php';
// exclude the test directory from code coverage reports
PHPUnit_Util_Filter::addDirectoryToFilter('./');
@@ -46,6 +47,7 @@ class phpbb_all_tests
$suite->addTest(phpbb_text_processing_all_tests::suite());
$suite->addTest(phpbb_dbal_all_tests::suite());
$suite->addTest(phpbb_regex_all_tests::suite());
+ $suite->addTest(phpbb_network_all_tests::suite());
return $suite;
}
diff --git a/tests/network/all_tests.php b/tests/network/all_tests.php
new file mode 100644
index 0000000000..b500647f81
--- /dev/null
+++ b/tests/network/all_tests.php
@@ -0,0 +1,40 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2010 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+if (!defined('PHPUnit_MAIN_METHOD'))
+{
+ define('PHPUnit_MAIN_METHOD', 'phpbb_network_all_tests::main');
+}
+
+require_once 'test_framework/framework.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+
+require_once 'network/checkdnsrr.php';
+
+class phpbb_network_all_tests
+{
+ public static function main()
+ {
+ PHPUnit_TextUI_TestRunner::run(self::suite());
+ }
+
+ public static function suite()
+ {
+ $suite = new PHPUnit_Framework_TestSuite('phpBB Network Functions');
+
+ $suite->addTestSuite('phpbb_network_checkdnsrr_test');
+
+ return $suite;
+ }
+}
+
+if (PHPUnit_MAIN_METHOD == 'phpbb_network_all_tests::main')
+{
+ phpbb_network_all_tests::main();
+}
diff --git a/tests/network/checkdnsrr.php b/tests/network/checkdnsrr.php
new file mode 100644
index 0000000000..57fe2761cc
--- /dev/null
+++ b/tests/network/checkdnsrr.php
@@ -0,0 +1,63 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2010 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+require_once 'test_framework/framework.php';
+require_once '../phpBB/includes/functions.php';
+
+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('news.cnet.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));
+ }
+}