Location: PHPKode > scripts > QXML > qxml/QWhoIsIp.class.php
<?php

class QWhoIsIP
{
	protected $ip = "255.255.255.255";
	protected $whoIs = "whois.ripe.net";
	protected $port = 43;
	protected $timeOut = 10;
	protected $buffer = "";
	protected $result = array();
	protected $array_return_mode;
	protected $total = 0;
	protected $cache = false;
	protected $fields = array(
							"inetNum" => "inetnum",
							"org" => "org",
							"netName" => "netname",
							"descr" => "descr",
							"country" => "country",
							"adminC" => "admin-c",
							"techC" => "tech-c",
							"status" => "status",
							"mntBy" => "mnt-by",
							"mntLower" => "mnt-lower",
							"mntRoutes" => "mnt-routes",
							"source" => "source",
							"organisation" => "organisation",
							"orgName" => "org-name",
							"orgType" => "org-type",
							"address" => "address",
							"phone" => "phone",
							"faxNo" => "fax-no",
							"eMail" => "e-mail",
							"mntRef" => "mnt-ref",
							"person" => "person",
							"nicHdl" => "nic-hdl",
							"route" => "route",
							"origin" => "origin"
	);

	public function __construct($ip_array, $array_return_mode = "array")
	{
		$this->array_return_mode = $array_return_mode;
		$ip_array = (array) $ip_array;
		$this->total = count($ip_array);
		$i = 0;
		foreach($ip_array as $key => $ip)
		{
			if($this->isValidIP($ip))
			{
				$this->ip = $ip;
				if($this->fetch())
				{
					$this->result["ip"][$key]["@attributes"]["addr"] = $ip;
					$this->result["ip"][$key]["@attributes"]["createdAt"] = strftime("%Y-%m-%d %H:%M:%S", time());
					$this->result["ip"][$key]["addresses"] = $this->extract();

					if($this->result["ip"][$key]["@textNode"]["inetnum"] == "0.0.0.0 - 255.255.255.255"){
						$this->result["ip"][$key]["@attributes"]["state"] = 102;
					}
				}
				else
				{
					$this->result["ip"][$key]["@attributes"]["state"] = 101; //could not connect
					$this->result["ip"][$key]["@textNode"] = "";
				}
			}
			else {
				$this->result["ip"][$key]["@attributes"]["state"] = 100; //incorrect IP format
				$this->result["ip"][$key]["@textNode"] = "";
			}
		}
	}
	protected function extract()
	{
		$ret = array();

		foreach($this->fields as $key => $get)
		{
			if(preg_match_all("/$get:\s*(.*+)/i", $this->buffer, $matches))
			{
				if(count($matches[1]) > 1)
				{
					if($this->array_return_mode == "array"){
						foreach ($matches[1] as $k => $m){
							$ret[$key][$k]["@attributes"] = "";
							$ret[$key][$k]["@textNode"] = $m;
						}
					} else {
						foreach($matches[1] as $ras){
							$ret[$key] .= $ras . "\n";
						}
					}
				}
				else{
					$ret["@attributes"][$key] = $matches[1][0];
				}
			}
			
		}
		return $ret;
	}

	protected function isValidIp($ip){
		return preg_match("/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/", $ip) ? true : false;
	}

	protected function fetch(){
		$write = false;
		if($this->cache==true && !is_file("apps/{$this->ip}.tmp")){
			$fp = fopen("apps/{$this->ip}.tmp","wb");
			$write = true;
		} else if($this->cache==true) {
			$this->buffer = file_get_contents("apps/{$this->ip}.tmp");
			return true;
		}

		if(!$sock = @fsockopen($this->whoIs, $this->port, &$errno, &$errstr, 10)) {
			return false;
		} else {
			fputs($sock, "{$this->ip}\n");
			while(!feof($sock)){
				$buffer .= fgets($sock, 10240);
			}
			fclose($sock);
			$this->buffer = $buffer;
				
			if($this->cache==true && $write==true){
				fwrite($fp, $buffer);
				fclose($fp);
			}
			return true;
		}
	}
	
	public function doCache(){
		$this->cache = true;
	}

	public function getResult(){
		$whois = array();
		$whois["@attributes"]["createdAt"] = strftime("%Y-%m-%d %H:%M:%S", time());
		return array("whois" => array_merge($whois, $this->result));
		
	}
}
?>
Return current item: QXML