<?php
/* getpub
 * Author : Frédéric MAIRE
 * Parameters :
 *   idx : the pub index. if not define then 1. 
 * Return :
 *   the reference pub page.
 */
require("inc_base.php3");

  if ($idx==0) { $idx=1; }

  // open database to find image ref and add a view
  mysql_Connect($hostname,$username,$password) or die("Could not connect on database server.");
  mysql_Select_Db($dbname) or die("erreur acces BD");

  $query = "SELECT link_url,click FROM pub WHERE idx=".$idx;
  $query_res = mysql_query($query); 
  if ($query_res) {
    $link_url = mysql_result($query_res,0,"link_url");
    $click =  mysql_result($query_res,0,"click");

    // update the trace of all hits
    $addr_ip = getenv("REMOTE_ADDR");
    $host = gethostbyaddr($addr_ip);
    $dtime = date("Y-m-d H:i") .":00"; // remove seconds
    //$query = "INSERT INTO pub_hits VALUES (concat(current_date(),' ',current_time()),\"".$addr_ip."\",\"".$host."\",".$idx.")";
    $query = "INSERT INTO pub_hits VALUES (\"".$dtime."\",\"".$addr_ip."\",\"".$host."\",".$idx.")";
    $query_res = mysql_query($query); 

    if ($query_res) {
      // update the click counter
      $click = $click + 1;
      $query = "UPDATE pub SET click=$click WHERE idx=".$idx;
      $query_res = mysql_query($query);
    }
  }
  mysql_Close();

  // go to the link
  header ("Location: $link_url");
  print "<html><body></body></html>";

?>