strtotime("-604800 seconds"))) { // close database connection mysql_close($dbLink); // return false to indicate an update wasn't performed return false; } // read the latest iplists version $version_url = 'http://www.iplists.com/nw/version.php'; $latest_version = mysql_escape_string(file_get_contents($version_url)); // if no updated version information was retrieved, abort if (!$latest_version) { // return false to indicate an update wasn't performed return false; } // save the update data $q = "DELETE FROM cloak_update"; mysql_query($q); $q = "INSERT INTO cloak_update (version, updated_on) " . "VALUES('$latest_version', NOW())"; mysql_query($q); // if we already have the current data, don't attempt an update if ($latest_version == $db_version) { // close database connection mysql_close($dbLink); // return false to indicate an update wasn't performed return false; } // update the database SimpleCloak::_updateCloakingDB('google', 'http://www.iplists.com/nw/google.txt'); SimpleCloak::_updateCloakingDB('yahoo', 'http://www.iplists.com/nw/inktomi.txt'); SimpleCloak::_updateCloakingDB('msn', 'http://www.iplists.com/nw/msn.txt'); SimpleCloak::_updateCloakingDB('ask', 'http://www.iplists.com/nw/askjeeves.txt'); SimpleCloak::_updateCloakingDB('altavista', 'http://www.iplists.com/nw/altavista.txt'); SimpleCloak::_updateCloakingDB('lycos', 'http://www.iplists.com/nw/lycos.txt'); SimpleCloak::_updateCloakingDB('wisenut', 'http://www.iplists.com/nw/wisenut.txt'); // close connection mysql_close($dbLink); // return true to indicate a successful update return true; } // update the database for the mentioned spider, by reading the provided URL function _updateCloakingDB($spider_name, $url, $ua_regex = '/^# UA "(.*)"$/m', $ip_regex = '/^([0-9.]+)$/m') { // use cURL to read the data from $url // NOTE: additional settings are required when accessing the web through a proxy $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HEADER, 1); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_TIMEOUT, 60); $result = curl_exec($ch); curl_close($ch); // use _parseListURL to parse the list of IPs and user agents $lists = SimpleCloak::_parseListURL($result, $ua_regex, $ip_regex); // if the user agents and IPs weren't retrieved, we cancel the update if (!$lists['ua_list'] || !$lists['ip_list']) return; // lock the cloack_data table to avoid concurrency problems mysql_query('LOCK TABLES cloak_data WRITE'); // delete all the existing data for $spider_name SimpleCloak::_deleteSpiderData($spider_name); // insert the list of user agents for the spider foreach ($lists['ua_list'] as $ua) { SimpleCloak::_insertSpiderData($spider_name, 'UA', $ua); } // insert the list of IPs for the spider foreach ($lists['ip_list'] as $ip) { SimpleCloak::_insertSpiderData($spider_name, 'IP', $ip); } // release the table lock mysql_query('UNLOCK TABLES'); } // helper function used to parse lists of user agents and IPs function _parseListURL($data, $ua_regex, $ip_regex) { $ua_list_ret = preg_match_all($ua_regex, $data, $ua_list); $ip_list_ret = preg_match_all($ip_regex, $data, $ip_list); return array('ua_list' => $ua_list[1], 'ip_list' => $ip_list[1]); } // inserts a new row of data to the cloaking table function _insertSpiderData($spider_name, $record_type, $value) { // escape input data $spider_name = mysql_escape_string($spider_name); $record_type = mysql_escape_string($record_type); $value = mysql_escape_string($value); // build and execute the INSERT query $q = "INSERT INTO cloak_data (spider_name, record_type, value) " . "VALUES ('$spider_name', '$record_type', '$value')"; mysql_query($q); } // delete the cloaking data for the mentioned spider function _deleteSpiderData($spider_name) { // escape input data $spider_name = mysql_escape_string($spider_name); // build and execute the DELETE query $q = "DELETE FROM cloak_data WHERE spider_name='$spider_name'"; mysql_query($q); } } ?>