Ajax software
Free javascripts
↑
Main Page
{
// 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);
229
Chapter 11: Cloaking, Geo-Targeting, and IP Delivery
c11.qxd:c11 11:01 229
Ajax software
Free javascripts
→