1<?php 2/** 3 * Plugin: Google Page Rank 4 * 5 * based on an alogoritham on PageRank Lookup v1.1 by HM2K (update: 31/01/07) found here: http://www.hm2k.com/projects/pagerank 6 * 7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 8 * @author Marcel Steinger <sourcecode@steinger.ch> 9 */ 10 11// must be run within DokuWiki 12if(!defined('DOKU_INC')) die(); 13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 14require_once DOKU_PLUGIN.'syntax.php'; 15 16/** 17 * All DokuWiki plugins to extend the parser/rendering mechanism 18 * need to inherit from this class 19 */ 20class syntax_plugin_googlepagerank extends DokuWiki_Syntax_Plugin { 21 22 function getType() { return 'substition'; } 23 function getSort() { return 314; } 24 /** 25 * settings - host and user agent 26 */ 27 public $googlehost = 'toolbarqueries.google.com'; 28 public $googleua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5'; 29 30 function connectTo($mode) { 31 $this->Lexer->addSpecialPattern('\{\{pagerank>[^}]*\}\}',$mode,'plugin_googlepagerank'); 32 } 33 34 /** 35 * Handle the match 36 */ 37 function handle($match, $state, $pos, &$handler) { 38 39 $match = substr($match,11,-2); 40 list($url,$width,$method) = explode(',',$match); 41 if ( empty($url)) { $url = "none";} 42 if ( empty($width)) { $width = 40;} 43 if ( empty($method)) {$method = "style";} 44 return array($url,$width,$method); 45 } 46 /** 47 * Create output 48 */ 49 function render($mode, &$renderer, $data) { 50 if($mode == 'xhtml'){ 51 if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $data[0])) { $data[0]='http://'.$data[0]; } 52 $pr=$this->getpr($data[0]); 53 if ($pr == '') {$pr = 0;} 54 $pagerank="PageRank: $pr/10"; 55 //The (old) image method 56 if ($data[2] == 'image') { 57 $prpos=$data[1]*$pr/10; 58 $prneg=$data[1]-$prpos; 59 $html='<img src="http://www.google.com/images/pos.gif" width='.$prpos.' height=4 border=0 alt="'.$pagerank.'"><img src="http://www.google.com/images/neg.gif" width='.$prneg.' height=4 border=0 alt="'.$pagerank.'">'; 60 } 61 //The pre-styled method 62 if ($data[2] == 'style') { 63 $prpercent=100*$pr/10; 64 $html='<div style="position: relative; width: '.$data[1].'px; padding: 0; background: #D9D9D9;"><strong style="width: '.$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 4px; line-height: 4px;"><span></span></strong></div>'; 65 } 66 $renderer->doc .= '<a href="'.$data[0].'" title="'.$pagerank.'">'.$html.'</a>'; 67 return true; 68 } 69 return false; 70 } 71 /** 72 * convert a string to a 32-bit integer 73 */ 74 function StrToNum($Str, $Check, $Magic) { 75 $Int32Unit = 4294967296; // 2^32 76 77 $length = strlen($Str); 78 for ($i = 0; $i < $length; $i++) { 79 $Check *= $Magic; 80 //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), 81 // the result of converting to integer is undefined 82 // refer to http://www.php.net/manual/en/language.types.integer.php 83 if ($Check >= $Int32Unit) { 84 $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit)); 85 //if the check less than -2^31 86 $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check; 87 } 88 $Check += ord($Str{$i}); 89 } 90 return $Check; 91 } 92 /** 93 * genearate a hash for a url 94 */ 95 function HashURL($String) { 96 $Check1 = $this->StrToNum($String, 0x1505, 0x21); 97 $Check2 = $this->StrToNum($String, 0, 0x1003F); 98 99 $Check1 >>= 2; 100 $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F); 101 $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF); 102 $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF); 103 104 $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F ); 105 $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 ); 106 107 return ($T1 | $T2); 108 } 109 /** 110 *genearate a checksum for the hash string 111 */ 112 function CheckHash($Hashnum) { 113 $CheckByte = 0; 114 $Flag = 0; 115 116 $HashStr = sprintf('%u', $Hashnum) ; 117 $length = strlen($HashStr); 118 119 for ($i = $length - 1; $i >= 0; $i --) { 120 $Re = $HashStr{$i}; 121 if (1 === ($Flag % 2)) { 122 $Re += $Re; 123 $Re = (int)($Re / 10) + ($Re % 10); 124 } 125 $CheckByte += $Re; 126 $Flag ++; 127 } 128 $CheckByte %= 10; 129 if (0 !== $CheckByte) { 130 $CheckByte = 10 - $CheckByte; 131 if (1 === ($Flag % 2) ) { 132 if (1 === ($CheckByte % 2)) { 133 $CheckByte += 9; 134 } 135 $CheckByte >>= 1; 136 } 137 } 138 139 return '7'.$CheckByte.$HashStr; 140 } 141 /** 142 * return the pagerank checksum hash 143 */ 144 function getch($url) { return $this->CheckHash($this->HashURL($url)); } 145 /** 146 * return the pagerank figure 147 */ 148 function getpr($url) { 149 //global $this->googlehost,$this->googleua; 150 $ch = $this->getch($url); 151 $fp = fsockopen($this->googlehost, 80, $errno, $errstr, 30); 152 if ($fp) { 153 $out = "GET /tbr?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n"; 154 //echo "<pre>$out</pre>\n"; //debug only 155 $out .= "User-Agent: $this->googleua\r\n"; 156 $out .= "Host: $this->googlehost\r\n"; 157 $out .= "Connection: Close\r\n\r\n"; 158 fwrite($fp, $out); 159 //$pagerank = substr(fgets($fp, 128), 4); //debug only 160 //echo $pagerank; //debug only 161 while (!feof($fp)) { 162 $data = fgets($fp, 128); 163 //echo $data; 164 $pos = strpos($data, "Rank_"); 165 if($pos === false){} else{ 166 $pr=substr($data, $pos + 9); 167 $pr=trim($pr); 168 $pr=str_replace("\n",'',$pr); 169 return $pr; 170 } 171 } 172 //else { echo "$errstr ($errno)<br />\n"; } //debug only 173 fclose($fp); 174 } 175 } 176} 177?>