Location: PHPKode > projects > Joomla SEF / SEO - extending OpenSEF > includes/sef.googlepr.php
<?php

/**********************************************************************
GooglePR -- Calculates the Google PageRank of a specified URL
Authors : Raistlin Majere (euclide at email dot it) (google_pagerank()
Emre Odabas (eodabas at msn dot com)
Version : 1.2
**********************************************************************/

class GooglePR {

  // Settings
  var $googleDomains  = Array("toolbarqueries.google.com");
  var $userAgent      = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5";
  var $days_cache     = 2;
  var $processResult  = Array();

  // Private Vars
  var $GOOGLE_MAGIC   = 0xE6359A60;

  // ----------------------------------------------------------------------
  // Get Graphical Image
  function getRankImage( $pr=null, $width=40, $method='style' ) {
    global $LPHP_SETTINGS;
    if( $pr === null )
      $pr = $this->getProcResult('rank');
    $pagerank = "PageRank: $pr/10";
    // Google Image
    if ($method == 'image') {
      $prpos=$width*$pr/10;
      $prneg=$width-$prpos;
      $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.'">';
    }
    // DIV Style
    if ($method == 'style') {
      $prpercent=100*$pr/10;
      $html='<div style="position: relative; width: '.$width.'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>';
    }
    return $html;
  }

  // ----------------------------------------------------------------------
  // Check Google for a URL Value

  function checkUrl($url = null) {

    // Start Counter
    $time_start = $this->_microTimeFloat();

    // Build Query
    if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $url)) $url='http://'.$url;
    $GoogleHOST = $this->googleDomains[mt_rand(0,count($this->googleDomains)-1)];
    $GoogleCH   = $this->_getChannel($url);
    $GoogleURL  = '/search?client=navclient-auto&ch='.$GoogleCH.'&features=Rank&q=info:'.urlencode($url);
    $contents   = null;
    $PageRank   = -1;

    // Request from Google
    if (@function_exists("curl_init")) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://".$GoogleHOST.$GoogleURL);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
      curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
      $contents = trim(@curl_exec($ch));
      curl_close ($ch);
    } else if ($socket  = @fsockopen($GoogleHOST, "80", $errno, $errstr, 30)) {
      $request  = "GET $GoogleURL HTTP/1.0\r\n";
      $request .= "Host: $GoogleHOST\r\n";
      $request .= "User-Agent: ".$this->userAgent."\r\n";
      $request .= "Accept-Language: en-us, en;q=0.50\r\n";
      $request .= "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66\r\n";
      $request .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
      $request .= "Connection: close\r\n";
      $request .= "Cache-Control: max-age=0\r\n\r\n";
      stream_set_timeout ( $socket,10);
      fwrite( $socket, $request );
      $ret = '';
      while (!feof($socket))
        $ret .= fread($socket,4096);
      fclose($socket);
      $contents = trim(substr($ret,strpos($ret,"\r\n\r\n") + 4));
    } else {
      $contents = trim(@file_get_contents("http://".$GoogleHOST.$GoogleURL));
    }

    if( preg_match('/^Rank_/',$contents) )
      $PageRank = preg_replace('/^Rank_.*\:(\d+)$/','$1',$contents);
    $PageRank = $PageRank < 0 ? 0 : $PageRank > 10 ? 10 : $PageRank;

    // Debugging
    $this->processResult['exec_time'] = $this->_microTimeFloat() - $time_start;
    $this->processResult['rank']      = $PageRank;
    $this->processResult['url']       = $url;
    $this->processResult['result']    = $contents;

    return $PageRank;

  } // checkUrl

  // ----------------------------------------------------------------------
  // Get Process Result Values

  function getProcResult($key){
    if( array_key_exists($key,$this->processResult) )
      return $this->processResult[$key];
    else
      return null;
  }

  // ----------------------------------------------------------------------
  // Internal Operations

  //convert a string to a 32-bit integer
  function _StrToNum($Str, $Check, $Magic) {
    $Int32Unit = 4294967296; // 2^32
    $length = strlen($Str);
    for ($i = 0; $i < $length; $i++) {
      $Check *= $Magic;
      // If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
      //  the result of converting to integer is undefined
      //  refer to http://www.php.net/manual/en/language.types.integer.php
      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;
  }

  //genearate a hash for a url
  function _HashURL($String) {
    $Check1 = $this->_StrToNum($String, 0x1505, 0x21);
    $Check2 = $this->_StrToNum($String, 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 _getChannel( $url ) {
    $Hashnum = $this->_HashURL($url);
    $Flag = 0;
    $CheckByte = 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 time
  function _microTimeFloat() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
  }

}
Return current item: Joomla SEF / SEO - extending OpenSEF