Categorización | General, PHP

Calcular PageRank en mi sitio Web

En una de mis tantas búsquedas por la red encontré la solicitud que un usuario hacia en el sitio Yahoo! Respuestas , y su cuestionamiento hacia referencia a la posibilidad de insertar un calculador de PageRank en su sitio Web, pero sin tener que depender de servicios como CalcularPageRank.com, MiPageRank.com, entre otros. En aquel entonces desafortunadamente no tuve tiempo de prestar la ayuda necesaria, pero en esta ocasión vengo a dejarles un script PHP que nos permitirá implementar esta función.

Antes que nada quiero aclarar que esta clase PHP no es de mi autoría, y realmente tampoco me acuerdo desde donde la descargue, simplemente modifique algunas líneas y suprimí otras, con el propósito de que fuera mas comprensible.

Bien, dicho lo anterior aquí les dejo los archivos completos del script para que los descarguen:

index.hml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
	<head>
		<title>Calculador de PageRank&trade;</title>
	</head>
 
	<body>
 
		Ejemplo 1:
 
		<br />
		<br />
 
		<a href="http://misitioweb.com/" target="_blank">
 
        	<img alt="PageRank" border="0" src="http://misitioweb/directorio/de/mi/calculador/display.php?a=getCode">
 
		</a>
 
		<br />
		<br />
 
		Ejemplo 2:
 
		<br />
		<br />
 
		<a href="http://misitioweb.com/" target="_blank">
 
        	<img alt="PageRank" border="0" src="http://misitioweb/directorio/de/mi/calculador/display.php?a=getCode&amp;s=Green">
 
		</a>
 
	</body>
 
</html>

display.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
 
define( 'URL_BASE', 	'http://misitioweb/directorio/de/mi/calculador/' );
define( 'PATH_BASE', 	'/ruta/absoluta/de/misitioweb/' );
 
require_once( 'class.pageRank.php' );
 
$pr = new pageRank( PATH_BASE . 'img/' , URL_BASE . 'img/' );
 
if( ( !empty( $_REQUEST['a'] ) ) && ( $_REQUEST['a'] == 'getCode' ) ) {
 
	$style = ( !empty( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : 'Labs';
 
	if( !empty( $_SERVER['HTTP_REFERER'] ) ) {
 
		$pagerank = $pr->getPageRank( $_SERVER['HTTP_REFERER'] , $pr->hosts[1] );
		$im = $pr->getImage( $pagerank, $style );
 
		if( $im ) {
 
			header( 'Cache-Control: no-cache, must-revalidate' );
			header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
			header( 'Content-type: image/gif' );
 
			imagegif( $im );
 
		} else {
 
			$error = true;
 
		}
 
	} else {
 
		$error = true;
 
	}
 
	if( $error ) {
 
		header( 'Location:' . $pr->url_images . $style . '/pr0.gif' );
 
	}
 
}
 
?>

class.pageRank.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
 
define( 'GOOGLE_MAGIC' , 0xE6359A60 );
 
class pageRank {
 
	var $pr;
	var $hosts = array ( 'www.google.com' , 'toolbarqueries.google.com' , '64.233.161.104' , '216.239.59.99' , '64.233.161.99' , '66.102.9.104' , '216.239.39.104' , '66.102.7.99' , '216.239.57.104' , '66.102.9.99' , '216.239.53.104' , '66.102.11.99' , '216.239.57.99' , '66.102.11.104' , '216.239.59.104' , '216.239.39.99' , '216.239.37.104' , '66.249.93.104' , '64.233.179.104' , '216.239.51.104' , '66.102.7.104' );
 
	function pageRank( $path_images , $url_images ) {
 
		$this->path_images = $path_images;
		$this->url_images = $url_images;
 
		return true;
	}
 
	function getImage( $pr = '0' , $style ) {
 
		$imagefile = $this->path_images . $style . '/pr' . $pr . '.gif';
		$im = @imagecreatefromgif( $imagefile );
 
		if( $im ) {
 
			return $im;
 
		} else {
 
			return false;
 
		}
	}
 
	function getPageRank( $url , $host = 'www.google.com' ) {
 
		$fp = fsockopen( $host , 80 , $errno , $errstr , 30 );
 
		if( $fp ) {
 
			$hash = $this->getHash( $url );
			$ch = $this->getCh( $hash );
 
			$out = "GET /search?client=navclient-auto&ch=" . $ch . "&features=Rank&q=info:" . $url . " HTTP/1.1rn" ;
			$out .= "Host: " . $host . "rn";
			$out .= "Connection: Closernrn";
 
			fwrite( $fp , $out );
 
			while( !feof( $fp ) ) {
 
				$data = fgets( $fp , 128 );
				$pos = strpos ( $data , "Rank_" );
 
				if( $pos !== false ) {
 
					$pagerank = intval( substr( $data , $pos + 9 ) );
					return $pagerank;
 
				}
 
			}
 
			fclose( $fp );
 
		} else {
 
			$this->errors[] = $errstr. ' (' . $errno . ')';
			return false;
 
		}
 
	}
 
	function strToInt( $string , $check , $gmagic ) {
 
		$integer32 = 4294967296;
 
		$length = strlen( $string );
 
		for( $i=0; $i < $length; $i++ ) {
 
			$check *= $gmagic;
 
			if( $check >= $integer32 ) {
 
				$check = ( $check - $integer32 * (int) ( $check / $integer32 ) );
				$check = ( $check < -2147483648) ? ( $check + $integer32 ) : $check;
 
			}
 
			$check += ord( $string {$i} );
		}
 
		return $check;
 
	}
 
	function getHash( $url ) {
 
		$check1 = $this->strToInt( $url , 0x1505 , 0x21 );
		$check2 = $this->strToInt( $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 );
 
	}
 
	function getCh( $hash ) {
 
		$checkByte = 0;
		$flag = 0;
 
		$string = sprintf( '%u' , $hash );
		$length = strlen( $string );
 
		for( $i = $length-1; $i >= 0; $i-- ) {
 
			$Re = $string {$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 . $string;
 
	}
 
}
 
?>

Ahora solo deben modificar un par de líneas en el archivo display.php por la URL y la ruta absoluta del script:

3
4
define( 'URL_BASE', 	'http://misitioweb/directorio/de/mi/calculador/' );
define( 'PATH_BASE', 	'/ruta/absoluta/de/misitioweb/' );

Luego en el archivo index.html debemos cambiar la URL de la imagen por la que corresponda:

15
src="http://misitioweb/directorio/de/mi/calculador/display.php?a=getCode"
29
src="http://misitioweb/directorio/de/mi/calculador/display.php?a=getCode&amp;s=Green"

Por ultimo, las imágenes estarán guardadas en el directorio de nombre img/Labs/, e img/Green/ .

Imágenes del directorio Labs (Ejemplo 1: por defecto. Espero les guste mi diseño)

Imágenes del directorio Green (Ejemplo 2. Ya venian con el script)

Listo, espero que a muchos les sea útil este aporte.

¿Te fue útil este artículo? Invitame una cerveza para calmar la sed y escribir muchos más.

Deja una respuesta

Obtén una cuenta de
@memoriasdeunprogramador.com
gratuita
Quien lo hubiera imaginado, Microsoft ayudándome a pasar un rato de ocio... aunque es solo una más de sus estrategias publicitarias estuvo divertido, y no esta de más conocer un poco sobre sus productos.

Lastima que para poder participar tenga que ser por medio de Facebook... bueno, lastima para mi porque soy uno de los pocos que aun no se dejan atrapar por la red social, así que me toco crearme una cuenta solo para esto :s

Si lo juegan espero que les vaya bien con la maquinita de dulces... casi que no encuentro como solucionarlo :)
8:46 PM Jun 30, 2010, comment