1  <html> 
2  <head> 
3  </head> 
4  <body> 
5  <?php 
6  //Incluir aquí los datos de conexión a MySQL. 
7  $hostname=localhost; //Host MySQL 
8  $username='usuario'; //User MySQL 
9  $password=''; //Password 
10  $databsename='dbname'; //Base de datos MySQL 
11   
12  function update_pagerank($pagerank,$id,$dblink){ 
13   
14      $sql = "UPDATE 
15                          `enlacesexternos` SET 
16                           `pagerank` = $pagerank 
17                  WHERE  
18                      `id`= $id"; 
19      //echo $sql; 
20      if ( isset( $sql ) && !empty( $sql )){ 
21            //echo "<!--".$sql."-->"; 
22            $result = mysql_query($sql); 
23            return $result; 
24      } 
25    } 
26   
27  class GooglePageRank { 
28   
29    var $_GOOGLE_MAGIC = 0xE6359A60; 
30    var $_url = ''; 
31    var $_checksum = ''; 
32   
33    function GooglePageRank($url) 
34    { 
35      $this->_url = $url; 
36    } 
37   
38    function _strToNum($Str, $Check, $Magic) 
39    { 
40      $Int32Unit = 4294967296; 
41   
42      $length = strlen($Str); 
43      for ($i = 0; $i < $length; $i++) { 
44        $Check *= $Magic; 
45   
46        if ($Check >= $Int32Unit) { 
47          $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit)); 
48          $Check = ($Check < -2147483647) ? ($Check + $Int32Unit) : $Check; 
49        } 
50        $Check += ord($Str{$i}); 
51      } 
52      return $Check; 
53    } 
54   
55    function _hashURL($String) 
56    { 
57      $Check1 = $this->_strToNum($String, 0x1505, 0x21); 
58      $Check2 = $this->_strToNum($String, 0, 0x1003F); 
59   
60      $Check1 >>= 2; 
61      $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F); 
62      $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF); 
63      $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF); 
64   
65      $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F ); 
66      $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 ); 
67   
68      return ($T1 | $T2); 
69    } 
70   
71    function checksum() 
72    { 
73      if($this->_checksum != '') return $this->_checksum; 
74   
75      $Hashnum = $this->_hashURL($this->_url); 
76   
77      $CheckByte = 0; 
78      $Flag = 0; 
79   
80      $HashStr = sprintf('%u', $Hashnum) ; 
81      $length = strlen($HashStr); 
82   
83      for ($i = $length - 1;  $i >= 0;  $i --) { 
84        $Re = $HashStr{$i}; 
85        if (1 == ($Flag % 2)) { 
86          $Re += $Re; 
87          $Re = (int)($Re / 10) + ($Re % 10); 
88        } 
89        $CheckByte += $Re; 
90        $Flag ++; 
91      } 
92   
93      $CheckByte %= 10; 
94      if (0 !== $CheckByte) { 
95        $CheckByte = 10 - $CheckByte; 
96        if (1 === ($Flag%2) ) { 
97          if (1 === ($CheckByte % 2)) { 
98            $CheckByte += 9; 
99          } 
100          $CheckByte >>= 1; 
101        } 
102      } 
103   
104      $this->_checksum = '7'.$CheckByte.$HashStr; 
105      return $this->_checksum; 
106    } 
107   
108    function pageRankUrl($dcchosen) 
109    { 
110      return $dcchosen . '/search?client=navclient-auto&features=Rank:&q=info:'.$this->_url.'&ch='.$this->checksum(); 
111    } 
112   
113    function getPageRank($dcchosen) 
114    { 
115      $fh = @fopen($this->pageRankUrl($dcchosen), "r"); 
116      if($fh) 
117      { 
118        $contenido = ''; 
119        while (!feof($fh)) { 
120          $contenido .= fread($fh, 8192); 
121        } 
122        fclose($fh); 
123        ltrim($contenido); 
124        rtrim($contenido); 
125        $contenido=str_replace("Rank_1:1:","",$contenido); 
126        $contenido=str_replace("Rank_1:2:","",$contenido); 
127        return $contenido; 
128      } 
129      return -1; 
130    } 
131   
132  } 
133   
134  $dc = "http://toolbarqueries.google.com"; 
135  $link = mysql_connect($hostname,$username,$password) 
136          or die("Could not connect: " . mysql_error()); 
137         mysql_select_db($databasename, $link) or die ( mysql_error()); 
138  if(isset($_GET["tipo"])){ 
139    switch ($_GET["tipo"]){ 
140      case "todos": 
141        $sql= "SELECT * FROM enlaces;"; 
142        break; 
143      case "nulos": 
144        $sql= "SELECT * FROM enlaces WHERE pagerank is NULL;"; 
145        break; 
146      default: 
147        $sql= "SELECT * FROM enlaces WHERE 0;"; 
148        break; 
149    } 
150    $res=mysql_query($sql,$link); 
151    while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ 
152      extract ($row,EXTR_OVERWRITE); 
153      $gpr =& new GooglePageRank($link); 
154      $pagerank = $gpr->getPageRank($dc); 
155      $status=update_pagerank($pagerank,$id,$link); 
156      echo "intentando...id:$id - $link pagerank:$pagerank status:$status <br>"; 
157    } 
158  } 
159  ?> 
160   
161   
162  <br><br><a href='#'>Volver al menu principal.</a> 
163  </body> 
164  </html>