private function validateHost($host, &$isWildcard) { global $config; /* dnsmasq allows wildcard entries by prefixing a host with a period. This constructor recognizes that and appends something in front of the host during validation so that it passes. */ if (strpos($host, '.') === 0) { $host = "a{$host}"; $isWildcard = TRUE; } else { $isWildcard = FALSE; } /* This is the most lazy solution yet to validate a host, but who wants to deal with regular expression? */ if (filter_var("http://{$host}/index.htnl?q=1", FILTER_VALIDATE_URL) === FALSE) { return FALSE; } /* Maybe exclude host */ if ($isWildcard === FALSE && $config['exclude_unresolved_hosts']) { if (HostEntry::hostHasResolvableAddress($host, 'A') === FALSE && HostEntry::hostHasResolvableAddress($host, 'AAAA') === FALSE && HostEntry::hostHasResolvableAddress($host, 'CNAME') === FALSE) { return FALSE; } } return TRUE; }
function domains_from_list($listUrl) { /* Perform request for contents. */ $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $listUrl); curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curlHandle, CURLOPT_TIMEOUT, 15); curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0'); $listContents = curl_exec($curlHandle); curl_close($curlHandle); if ($listContents === FALSE) { return FALSE; } /* Process returned result */ $final_result = array(); /* Have to use regular expression for splitting into an array because of many different line endings. */ $lines = preg_split("/(\r\n|\n|\r)/", $listContents); foreach ($lines as $line) { $line = trim($line); /* If string is empty or starts with # (comment), then ignore this entry. */ if (strlen($line) === 0 || strpos($line, '#') === 0) { continue; } /* Attempt to parse dnsmasq address= lists */ $isWildcardHost = FALSE; if (strpos($line, 'address=/') === 0) { $lineItems = explode('/', $line); if ($lineItems && count($lineItems) === 3) { $line = $lineItems[1]; goto validate_address; } } /* Attempt to split up line using combinations of tab and space characters. */ $lineItems = preg_split("/([\\s\t]+)/", $line); /* If our string split up and we have 2 entries, then the file was probably a hosts file. We validate the first item to ensure that it was an IP address to back this assumption. */ if ($lineItems && count($lineItems) === 2) { if (filter_var($lineItems[0], FILTER_VALIDATE_IP)) { $line = $lineItems[1]; } } validate_address: /* Check wether the remainder is a host */ $hostEntry = HostEntry::newEntry($line); if ($hostEntry !== NULL) { $final_result[] = $hostEntry; } } return $final_result; }