Location: PHPKode > scripts > Google Pagerank Script > google-pagerank.php
<?PHP
/**
*
* @Script Google Pagerank
* @version 1.0.0
* @copyright (c) Agrizlive.com
* @license You are allowed to modify this script. But script should have the powered by agrizlive and copyrights
*
* Tested : PHP 5.2.11
*/

/**
*/
class pr
{
	protected $url = '';
	protected $method = '';
	protected $width = '';
	protected $image_location = '';
	public function __construct($url,$method,$width,$image_location)
	{
		$this->url = $url;
		$this->method = $method;
		$this->width = $width;
		$this->image_location = $image_location;
	}
	
	public function pagerank() 
	{
		
		//Check url has http://
		// If not add http:// in url
		if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $this->url)) { $this->url='http://'.$this->url;}
		
		
		//Function calculates Page Rank
		$page_rank = $this->getPageRank();
		
		$pagerank = "PageRank : $page_rank/10";

		//Image Method
		if ($this->method == 'image') {
			header("Cache-Control: no-cache, must-revalidate");
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			$pr = imagecreatefrompng($this->image_location."/pr/pr_$page_rank.png");
			imagepng($pr);
		}
		
		//CSS Method
		else if($this->method == "css") {		
				$prpercent = 100 * $page_rank / 10;
				$html='<a href="javascript: void(0)" title="'.$pagerank.'"><div style="position: relative; width: '.$this->width.'px; padding: 0; background: #D9D9D9;"><strong style="width: '.$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 10px; line-height: 10px;"><span></span></strong></div></a>';
				return $html;
		}
		else {
			$pr_positive = $this->width * $page_rank / 10;
			$pr_negative = $this->width - $pr_positive;
			$html='<a href="javascript: void(0)" title="'.$pagerank.'"><img src="'.$this->image_location.'/green.jpg" width='.$pr_positive.' height="15" border="0" alt="'.$pagerank.'"><img src="'.$this->image_location.'/gray.jpg" width='.$pr_negative.' height="15" border="0" alt="'.$pagerank.'"></a>';
			return $html;
		}
	}
	
	public function getPageRank() 
	{
		$ch = $this->getCheckSum();
		$google_toolbar = 'toolbarqueries.google.com';
		$referrer = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
		$fp = fsockopen($google_toolbar, 80, $errno, $errstr, 30);
		if ($fp) {
		   $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$this->url HTTP/1.1\r\n";
		   $out .= "User-Agent: $referrer\r\n";
		   $out .= "Host: $google_toolbar\r\n";
		   $out .= "Connection: Close\r\n\r\n";
		
		   fwrite($fp, $out);
		   
		   while (!feof($fp)) {
				$data = fgets($fp, 128);
				$pos = strpos($data, "Rank_");
				if($pos === false){} else{
					$pr=substr($data, $pos + 9);
					$pr=trim($pr);
					$pr=str_replace("\n",'',$pr);
					return $pr;
				}
		   }
		   fclose($fp);
		}
	}
	
	public function getCheckSum()
	{
		//return the pagerank checksum hash
		$hash = $this->hashURL();
		return $this->checkHash($hash);
	}	
		
	public function StrToNum($Str, $Check, $Magic) {
		$Int32Unit = 4294967296;  // 2^32

		$length = strlen($Str);
		for ($i = 0; $i < $length; $i++) {
			$Check *= $Magic; 	
			
			if ($Check >= $Int32Unit) {
				$Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
				//if the check less than -2^31
				$Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
			}
			$Check += ord($Str{$i}); 
		}
		return $Check;
	}

	//This function generates hash for the url
	function hashURL() {
		$Check1 = $this->StrToNum($this->url, 0x1505, 0x21);
		$Check2 = $this->StrToNum($this->url, 0, 0x1003F);

		$Check1 >>= 2; 	
		$Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
		$Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
		$Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);	
		
		$T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
		$T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
		
		return ($T1 | $T2);
	}

	//genearate a checksum for the hash string
	function checkHash($Hashnum) 
	{
		$CheckByte = 0;
		$Flag = 0;

		$HashStr = sprintf('%u', $Hashnum) ;
		$length = strlen($HashStr);
		
		for ($i = $length - 1;  $i >= 0;  $i --) {
			$Re = $HashStr{$i};
			if (1 === ($Flag % 2)) {              
				$Re += $Re;     
				$Re = (int)($Re / 10) + ($Re % 10);
			}
			$CheckByte += $Re;
			$Flag ++;	
		}

		$CheckByte %= 10;
		if (0 !== $CheckByte) {
			$CheckByte = 10 - $CheckByte;
			if (1 === ($Flag % 2) ) {
				if (1 === ($CheckByte % 2)) {
					$CheckByte += 9;
				}
				$CheckByte >>= 1;
			}
		}

		return '7'.$CheckByte.$HashStr;
	}
}

?>
Return current item: Google Pagerank Script