Location: PHPKode > scripts > user-agent.org > data/class.user-agents.org.php
<?php
// user-agents.org connection class
// author: lee johnstone : <hide@address.com> 
// Site : http://freakcms.com
// license: http://freakcms.com
// Creative commons share alike license.
// connects to the site and can list, save in mysql or xml format
// good for large websites wanting to stay uptodate with the latest user agents
// that are being naughty.
// 
//start of class
class User_Agents_dot_org {

/* user agents connecting and storage */


// name of the robots.xml file save
private $robots_xml = 'robots.xml';
// user agents array varible.
public $User_Agents_XML = array ();
// public save function
// used to define which type of save the user wants.
public function Save($type, $data){
	switch($type){
		case 'sql':
		$this->Save_User_Agent_MYSQL($data);
		break;
		case 'xml':
		$this->Save_User_Agent_XML();
		break;
	}
	return;
}
// sort user agents
// rearranges the user agents into a readable xml array
private function User_Agents($xml){
	$obj = new SimpleXMLElement($xml);
	$this->User_Agents_XML = array();
	$type = 'user-agent';
foreach($obj as $str ){
	$this->User_Agents_XML[] = $str;
	}
	return $this->User_Agents_XML;
}
// save user agents to mysql
// save user agents to a mysql database if u have one
private function Save_User_Agent_MYSQL($str){
	$res = mysql_query("INSERT INTO `user_agents_org` (`id`, `string`, `description`, `type`, `comment`, `link1`, `link2`) 
													   VALUES('{$str[id]}','$str[string]}','{$str[descritpion]}','{$str[type]}','{$str[comment]}','{$str[link1]}','{$str[link2]}')") or die('Error : ' . mysql_error());
	return;
}
// save user agents in xml form
// saves all user agents to a xml like on user-agents.org
private function Save_User_Agent_XML(){
if(file_exists($this->robots_xml)){
	$this->KillRobotsXml();
	}
	$file = fopen($this->robots_xml,"w+");
	fwrite($file,'<user-agents>');
	$this->User_Agents_Dot_Org();
	$type = 'user-agents';	
foreach ($this->User_Agents_XML as $i =>  $k) {
	$excludes = '
	<user-agent>
	<ID>'.$k->ID.'</ID>
	<String>'.$k->String.'</String>
	<Description>'.$k->Descritpion.'</Description>
	<Type>'.$k->Type.'</Type>
	<Comment>'.$k->Comment.'</Comment>
	<Link1>'.$k->Link1.'</Link1>
	<Link1>'.$k->Link2.'</Link1>
	</user-agent>';
	fwrite($file,$excludes);
	}
	fwrite($file,'</user-agents>');
	fclose($file);
	return;
}
// Kill the local xml file
// Removes the lcoal xml file before another is created
private function KillRobotsXml(){
	return unlink($this->robots_xml);
}
// user-agents.org connection
// connection to user-agents.org and obtain the latest list 
public function User_Agents_Dot_Org(){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://www.user-agents.org/allagents.xml');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$res = curl_exec($ch);
	$data = strstr($res, '<?');
	curl_close($ch);
	$this->User_Agents($data);
	return;
}
// end of class
}
?>
Return current item: user-agent.org