

<?php


function db_connect()
{
require("./total.inc");

if(0 == strcmp($database, "POSTGRES")){
   $conn = Pg_connect($pgsql_data);
   return $conn;
   }
else if(0 == strcmp($database, "MYSQL")){
   $conn = mysql_connect($mysql_ip, $mysql_user, $mysql_pass);
   if(!$conn){
      return -1;
      }
   mysql_select_db($mysql_db, $conn);
   return $conn;
   }
return $conn;
}





function db_querym($conn, $sql)
{
require("./total.inc");

if(0 == strcmp($database, "POSTGRES")){
   $result = pg_query($conn, $sql);
   return $result;
   }
else if(0 == strcmp($database, "MYSQL")){
   $result = mysql_query($sql, $conn);

   return $result;
   }
return $result;
}









function db_query($conn, $sql)
{
require("./total.inc");

// もしテーブルに変化のある問い合わせの場合はqulogテーブルに書き込む
if(eregi("update ", $sql) || eregi("insert into ", $sql) || eregi("DELETE FROM ", $sql)){

   $htco = $_SERVER["HTTP_COOKIE"];
   $htac = $_SERVER['HTTP_ACCEPT'];
   $htcon = $_SERVER['HTTP_CONNECTION'];
   $htho = $_SERVER['HTTP_HOST'];
   $read = $_SERVER["REMOTE_ADDR"];
   $reho = $_SERVER['REMOTE_HOST'];
   $htre = $_SERVER["HTTP_REFERER"];
   $qust = $_SERVER['QUERY_STRING'];
   $htus = $_SERVER['HTTP_USER_AGENT'];

   if(0 >= strlen($htco)){ $htco = "NOT"; }
   if(0 >= strlen($htac)){ $htac = "NOT"; }
   if(0 >= strlen($htcon)){ $htcon = "NOT"; }
   if(0 >= strlen($htho)){ $htho = "NOT"; }
   if(0 >= strlen($read)){ $read = "NOT"; }
   if(0 >= strlen($reho)){ $reho = "NOT"; }
   if(0 >= strlen($htre)){ $htre = "NOT"; }
   if(0 >= strlen($qust)){ $qust = "NOT"; }
   if(0 >= strlen($htus)){ $htus = "NOT"; }

   $udate = date("U");
   $ydate = date("Y");
   $mdate = date("m");
   $ddate = date("d");
   $hdate = date("H");
   $idate = date("i");
   $sdate = date("s");

   $qsql = $sql;
   $qsql = strtr($qsql, "/'", "/!");

   $resultlog = db_querym($conn, "insert into qulog
     (
     udate,
     ydate,
     mdate,
     ddate,
     hdate,
     idate,
     sdate,
     htco,
     htac,
     htcon,
     htho,
     read,
     reho,
     htre,
     qust,
     htus,
     qsql) values (
     '$udate',
     '$ydate',
     '$mdate',
     '$ddate',
     '$hdate',
     '$idate',
     '$sdate',
     '$htco',
     '$htac',
     '$htcon',
     '$htho',
     '$read',
     '$reho',
     '$htre',
     '$qust',
     '$htus',
     '$qsql')");


   // 記録がqulogに残せない場合はファイルに書く
   if(!$resultlog){
      print("bud update query set qulog\n");

      if(0 >= strlen($db_logfile)){
         $db_logfile = "/tmp/apo.log";
         }
      if(($fp = fopen($db_logfile, "a+"))==NULL){
         print("db_logfile not open: sql = $sql\n");
         }

      $wdata = $udate . ":" . $htco . ":" . $htac . ":" . $htcon . ":" . $htho . ":" . $read . ":" . $reho . ":" . $htre . ":" . $qust . ":" . $htus . ":" . $sql . "\n";

      fwrite($fp, $wdata);
      fclose($fp);
      }

   db_free_result($resultlog);
   }


if(0 == strcmp($database, "POSTGRES")){
   $result = pg_query($conn, $sql);
   return $result;
   }
else if(0 == strcmp($database, "MYSQL")){
   $result = mysql_query($sql, $conn);

   return $result;
   }
return $result;
}




function db_num_rows($result)
{
require("./total.inc");

if(0 == strcmp($database, "POSTGRES")){
   $num = pg_num_rows($result);
   return $num;
   }
else if(0 == strcmp($database, "MYSQL")){
   $num = mysql_num_rows($result);
   return $num;
   }

return 0;
}




function db_fetch_Result($result, $row, $field)
{
require("./total.inc");

if(0 == strcmp($database, "POSTGRES")){
   $reso = pg_fetch_Result($result, $row, $field);
   return $reso;
   }
else if(0 == strcmp($database, "MYSQL")){
   if(mysql_data_seek($result, $row)){
      $item = mysql_fetch_object($result);
      $reso = $item->$field;

      }
   else{
      echo "not seek data! \n";
      }
   return $reso;
   }

}




function db_free_result($result)
{
require("./total.inc");

if(0 == strcmp($database, "POSTGRES")){
   pg_free_result($result);
   return 0;
   }
else if(0 == strcmp($database, "MYSQL")){
   mysql_free_result($result);
   return 0;
   }

}




function db_Close($conn)
{
require("./total.inc");

if(0 == strcmp($database, "POSTGRES")){
   pg_Close($conn);
   return 0;
   }
else if(0 == strcmp($database, "MYSQL")){
   mysql_close($conn);
   return 0;
   }

}




?>




