/[drupal]/contributions/modules/google_pr/PageRankXor32.php
ViewVC logotype

Contents of /contributions/modules/google_pr/PageRankXor32.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed Sep 20 17:13:38 2006 UTC (3 years, 2 months ago) by introfini
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, DRUPAL-6--1-1, DRUPAL-5--1-1, DRUPAL-5--1-0, DRUPAL-5--1-3, DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5, DRUPAL-4-7, DRUPAL-6--1
File MIME type: text/x-php
first commit
1 <?php
2 /******************
3 * PageRankXor32 class created by MagicBeanDip
4 * PageRank class was created by others
5 * Look for updates at http://v1.magicbeandip.com/mbd-file/PageRankReport.php
6 * This code is released into the Public Domain
7 *
8 * Sample use:
9 * include('PageRankXor32.php');
10 * $oPR=new PageRankXor32();
11 * echo $oPR->getRank('http://www.amazon.com');
12 *
13 ******************/
14
15 define('GOOGLE_MAGIC', 0xE6359A60);
16
17 // Use this class if your server is having problems with bitwise operations
18 class PageRankXor32 extends PageRank {
19 function xor32($a, $b) {
20 return $this->int32($a) ^ $this->int32($b);
21 }
22 //return least significant 32 bits
23 //works by telling unserialize to create an integer even though we provide a double value
24 function int32($x) {
25 return unserialize("i:$x;");
26 //return intval($x); // This line doesn't work on all servers.
27 }
28
29 function mix($a,$b,$c) {
30 $a -= $b; $a -= $c; $a = $this->xor32($a,$this->zeroFill($c,13));
31 $b -= $c; $b -= $a; $b = $this->xor32($b,$a<<8);
32 $c -= $a; $c -= $b; $c = $this->xor32($c,$this->zeroFill($b,13));
33 $a -= $b; $a -= $c; $a = $this->xor32($a,$this->zeroFill($c,12));
34 $b -= $c; $b -= $a; $b = $this->xor32($b,$a<<16);
35 $c -= $a; $c -= $b; $c = $this->xor32($c,$this->zeroFill($b,5));
36 $a -= $b; $a -= $c; $a = $this->xor32($a,$this->zeroFill($c,3));
37 $b -= $c; $b -= $a; $b = $this->xor32($b,$a<<10);
38 $c -= $a; $c -= $b; $c = $this->xor32($c,$this->zeroFill($b,15));
39 return array($a,$b,$c);
40 }
41 }
42
43 //This class should work on most servers
44 class PageRank {
45 function zeroFill($a, $b){
46 $z = hexdec(80000000);
47 if ($z & $a){
48 $a = ($a>>1);
49 $a &= (~$z);
50 $a |= 0x40000000;
51 $a = ($a>>($b-1));
52 }else{
53 $a = ($a>>$b);
54 }
55 return $a;
56 }
57
58 function mix($a,$b,$c) {
59 $a -= $b; $a -= $c; $a ^= ($this->zeroFill($c,13));
60 $b -= $c; $b -= $a; $b ^= ($a<<8);
61 $c -= $a; $c -= $b; $c ^= ($this->zeroFill($b,13));
62 $a -= $b; $a -= $c; $a ^= ($this->zeroFill($c,12));
63 $b -= $c; $b -= $a; $b ^= ($a<<16);
64 $c -= $a; $c -= $b; $c ^= ($this->zeroFill($b,5));
65 $a -= $b; $a -= $c; $a ^= ($this->zeroFill($c,3));
66 $b -= $c; $b -= $a; $b ^= ($a<<10);
67 $c -= $a; $c -= $b; $c ^= ($this->zeroFill($b,15));
68 return array($a,$b,$c);
69 }
70
71 function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
72 if(is_null($length)) {
73 $length = sizeof($url);
74 }
75 $a = $b = 0x9E3779B9;
76 $c = $init;
77 $k = 0;
78 $len = $length;
79 while($len >= 12) {
80 $a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
81 $b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
82 $c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
83 $mix = $this->mix($a,$b,$c);
84 $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
85 $k += 12;
86 $len -= 12;
87 }
88 $c += $length;
89 switch($len){
90 case 11: $c+=($url[$k+10]<<24);
91 case 10: $c+=($url[$k+9]<<16);
92 case 9 : $c+=($url[$k+8]<<8);
93 /* the first byte of c is reserved for the length */
94 case 8 : $b+=($url[$k+7]<<24);
95 case 7 : $b+=($url[$k+6]<<16);
96 case 6 : $b+=($url[$k+5]<<8);
97 case 5 : $b+=($url[$k+4]);
98 case 4 : $a+=($url[$k+3]<<24);
99 case 3 : $a+=($url[$k+2]<<16);
100 case 2 : $a+=($url[$k+1]<<8);
101 case 1 : $a+=($url[$k+0]);
102 }
103 $mix = $this->mix($a,$b,$c);
104 /* report the result */
105 return $mix[2];
106 }
107
108 //converts a string into an array of integers containing the numeric value of the char
109 function strord($string) {
110 for($i=0;$i<strlen($string);$i++) {
111 $result[$i] = ord($string{$i});
112 }
113 return $result;
114 }
115
116 //returns -1 if no page rank was found
117 function getRank($url){
118 $ch = "6".$this->GoogleCH($this->strord("info:" . $url));
119
120 $pagerank=-1;
121 $fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
122 if (!$fp) {
123 echo "$errstr ($errno)<br />\n";
124 } else {
125 $out = "GET /search?client=navclient-auto&ch=" . $ch . "&features=Rank&q=info:" . $url . " HTTP/1.1\r\n" ;
126 $out .= "Host: www.google.com\r\n" ;
127 $out .= "Connection: Close\r\n\r\n" ;
128 fwrite($fp, $out);
129 while (!feof($fp)) {
130 $data = fgets($fp, 128);
131 $pos = strpos($data, "Rank_");
132 if($pos === false){
133 }else{
134 $pagerank = trim(substr($data, $pos + 9));
135 }
136 }
137 fclose($fp);
138 }
139 return $pagerank;
140 }
141 }

  ViewVC Help
Powered by ViewVC 1.1.2