<?php

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */

  /*===================================================
    main
  ===================================================*/

  include "application.php";
  include "lib/pagerank.class.php";
  
  RemoveFilter("gzip_compression");
  RemoveFilter("runtime");
  RemoveFilter("start_session");
  RunPreFilter(__FILE__);

  $rank = 0;
  $expired = date('Y-m-d', strtotime("now + $pagerank_cache day"));
  $query = "select rank from idx_pagerank 
            where link_id = '$link_id' 
                  and engine = 'g'
                  and last_update < '$expired'
            limit 1";
  $result = $dbConn->Execute($query);
  if ($result->RecordCount()) {
    $rank = $result->Fields('rank');
  }
  else {
    $query = "select url from idx_link where link_id = '$link_id' limit 1";
    $result = $dbConn->Execute($query);
    if ($result->RecordCount()) {
      $url = $result->Fields('url');
      $engine = new googlerank();
      $rank = $engine->FetchPR($url);
      $query = "replace into idx_pagerank
                (link_id, engine, rank, last_update) values
                ('$link_id', 'g', '$rank', now())";
      $result = $dbConn->Execute($query);
    }
  }
  
  $rankimage = 'themes/' . $active_theme . '/images/pr/' . $rank . '.gif';

  header('Content-Type: image/gif');
  readfile($rankimage);

  RunPostFilter(__FILE__);

?>