/** * @param string $url * @param string $key * @return bool|null */ function localSearchUrl($url, $key) { $safeBrowsing = new SafeBrowsingClient($key); //Creating hashes by url $hashes = $safeBrowsing->getHashesByUrl($url); $localDbFile = 'hosts_prefixes.json'; if (!is_file($localDbFile)) { exit('File "' . $localDbFile . '" not found'); } $data = file_get_contents($localDbFile); $localHashPrefixes = json_decode($data, true); foreach ($hashes as $hash) { foreach ($localHashPrefixes as $shavar) { foreach ($shavar as $chunkNum => $chunk) { foreach ($chunk as $hashPrefix) { if ($hash['prefix'] === $hashPrefix) { //Found prefix in local DB echo '<div class="alert alert-info"> Префикс хеша найден в локальной БД. Ищем в списке опасных сайтов... </div>'; //Check full hash if ($safeBrowsing->searchUrl($url)) { return true; } } } } } } return false; }
public function testGetHashesByUrlTooMuchSubDomains() { $apiKey = 'test'; $url = 'test.test.test.test.test.test.com'; $safeBrowsing = new SafeBrowsingClient($apiKey); $result = $safeBrowsing->getHashesByUrl($url); $this->assertNotEmpty($result); }