#!/usr/bin/env php
<?php
	$root = substr( __DIR__, 0, strrpos( __DIR__, '/' ) );
	require_once( $root . '/dronebl.config.php' );
	require_once( $frameworks_path . '/DroneBL/Database.framework' );
	require_once( $frameworks_path . '/DroneBL/Validation.framework' );
	require_once( $frameworks_path . '/DroneBL/Cron.framework' );

	function _test_remove($entry) {
		switch (intval($entry['type']))
		{
			case 12: // Open DNS Resolver
				$test = exec('/usr/bin/env dig +short test.openresolver.com TXT @'.$entry['ip']);
				if (preg_match('/open-resolver-detected/', $test))
					return false;
				break;
			case 18: // DNS/MX type hostname detected on IRC
				$test = strtolower(gethostbyaddr($entry['ip']));
				if ($test == strtolower($entry['ip'])) // No PTR's associated
					return true;
				$matches = array('[^\.]+mail','mail','correo','smtp','mx','mta','news','ns','dns','ftp','www','web','imap');
				foreach ($matches AS $match)
				{
					if (preg_match('/^'.$match.'/', $test))
						return false;
				}
				echo $entry['ip']." -> ".$test.PHP_EOL;
				break;
		}
		return true;
	}

	$test = get_db_rows("SELECT DISTINCT(ip) FROM blacklist WHERE type=12 AND listed=1");
	while ($entry = get_db_tuple($test))
		_test_remove(array('ip' => $entry['ip'],'type' => 12));
?>
