tools-SURFmap.php

<?php

/*
 * Checks whether the specified IPv4 address belongs to the specified IP
 * address range (net).
 * Parameters:
 *    ip_address - IPv4 address in octet notation (e.g. '192.168.1.1')
 *    prefix - IPv4 subnet range, in nfdump filter notation
 */
function ip_address_in_net($ip_address, $prefix) {
	// http://pgregg.com/blog/2009/04/php-algorithms-determining-if-an-ip-is-within-a-specific-range/
	$ip_address_dec = ip2long($ip_address);

	// Since we use nfdump subnet notation, we need to make the subnet address complete
	$full_prefix = substr($prefix, 0, strpos($prefix, '/'));
	for ($i = 3 - substr_count($prefix, '.'); $i > 0; $i--) {
		$full_prefix .= '.0';
	}
	$full_prefix_dec = ip2long($full_prefix);

	$net_mask = intval(substr($prefix, strpos($prefix, '/') + 1));
	$net_mask_dec = bindec(str_pad('', $net_mask, '1') . str_pad('', 32 - $net_mask, '0'));

	return (($ip_address_dec & $net_mask_dec) == ($full_prefix_dec & $net_mask_dec));
}


/*
 * Recursive variant of the PHP's glob function.
 */
function glob_recursive($pattern, $flags = 0) {
	$files = glob($pattern, $flags);

	foreach(glob(dirname($pattern).'/*', GLOB_ONLYDIR/GLOB_NOSORT) as $dir) {
		$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
	}

	return $files;
}


?>