<?php
  /*******************************************************************************
  **  File        :  hippo_sql_query.php                                       **
  **  Class name  :  hippo_sql_query                                           **
  **  Author      :  DavidLanz                                                 **
  **                                                                           **
  **  Description :  This class is database access wrapper that can access     **
  **                 MySQL or PosgresSQL or MsSql databases.                   **
  **                                                                           **
  **  Date          :  25/07/2005                                              **
  **  Last Modified :  25/07/2005                                              **
  **                                                                           **
  **  PHP Version   :  5.0.4                                                   **
  *******************************************************************************/
    
  class hippo_sql_query
  {
    var $database_type;
    var $hostname;
    var $db_user='';
    var $db_pass='';
    var $db_name;
    var $db_port='';
    var $db_link;
    var $query_str;
    var $qry_flag;
    var $err_mail_title;
    var $applydate;
    var $client_ip;
    var $admin_email;
    var $err_msg='';
    
    /* Constructor */
    function hippo_sql_query($database_type, $hostname, $db_name, $db_user, $db_pass, $db_port)
    {
      $this->database_type = $database_type;  /* $database_type: MySQL, MSSQL or PgSQL */
      $this->hostname = $hostname;
      $this->db_name = $db_name;
      $this->db_user = $db_user;
      $this->db_pass = $db_pass;
      $this->db_port = $db_port;
      $this->admin_email = array();
      
      // DBVar.php should place with the file that called hippo_sql_query class or it'll get errors
      require('DBVar.php');
      
      $this->err_mail_title = $err_mail_title;
      $this->applydate = $applydate;
      $this->client_ip = $client_ip;
      for($i=0;$i<count($admin_email);$i++)
      {
        $this->admin_email[$i]=$admin_email[$i];
      }
    }
    
    function connect_to_mysql()
    {
      $this->db_link  =  @mysql_connect($this->hostname,$this->db_user,$this->db_pass)or die(mysql_error());
      if(!@mysql_select_db($this->db_name, $this->db_link))
      {
        $err_msg = mysql_error($this->db_link).'\n';
        $this->err_msg .= $err_msg;
        
        for($i=0;$i<count($this->admin_email);$i++)
        {
          mail($this->admin_email[$i],$this->err_mail_title.$this->applydate, $this->client_ip."_".$err_msg);
        }
      }
    }

    function connect_to_pgsql()
    {
      $this->pg_link =  pg_connect("host=$this->hostname port=$this->db_port dbname=$this->db_name user=$this->db_user password=root");
      if(!$this->pg_link)  pg_errormessage();
    }

    function connect_to_mssql()
    {
      $this->db_link  =  @mssql_connect($this->hostname,$this->db_user,$this->db_pass)or die(mysql_error());
      
      if(!@mssql_select_db($this->db_name, $this->db_link))
      {
        $err_msg = 'Unable to select database:'.$this->db_name.'\n';
        $this->err_msg .= $err_msg;
        for($i=0;$i<count($this->admin_email);$i++)
        {
          mail($this->admin_email[$i],$this->err_mail_title.$this->applydate, $this->client_ip."_".$err_msg);
        }
      }
    }

    /*
    $query: sql query string
    $flag: numeric, ex: 0,1,2,3,4 and 5, default is 0
          0: Single value
          1: Single Dimensional indexed array
          2: Two Dimensional indexed Array
          3: Single Dimensional Associative array
          4: Two Dimensional Associative array
          5: Execution of Insert,Update,Delete Queries
    */
    function query_exec($query_str, $qry_flag, $res="0")
    {
      $this->query_str  =  $query_str;
      $this->qry_flag   =  $qry_flag;

      if(strtolower($this->database_type)=='mysql')
      {
        $this->connect_to_mysql();
        return $this->mysql_commands($query_str, $qry_flag);
      }
      else if(strtolower($this->database_type)=='pgsql')
      {
        $this->connect_to_pgsql();
        return $this->pgsql_commands($query_str, $qry_flag);
      }
      else if(strtolower($this->database_type)=='mssql')
      {
        $this->connect_to_mssql();
        return $this->mssql_commands($query_str, $qry_flag);
      }
      else
      {
        $err_msg = 'Undefined to select database type:'.$this->database_type.'\n';
        $this->err_msg .= $err_msg;
        for($i=0;$i<count($this->admin_email);$i++)
        {
          mail($this->admin_email[$i],$this->err_mail_title.$this->applydate, $this->client_ip."_".$err_msg);
        }
      }
    }

    function mysql_commands($query_str, $qry_flag)
    {
      /* Checking whether the Query is Valid or not */
      if(!($result=@mysql_query($query_str, $this->db_link)))
      {
        $err_msg = mysql_error($this->db_link).'\n';
        $this->err_msg .= $err_msg;
        for($i=0;$i<count($this->admin_email);$i++)
        {
          mail($this->admin_email[$i],$this->err_mail_title.$this->applydate, $this->client_ip."_".$err_msg."_".$query_str);
        }
        return $this->err_msg;
      }
      else
      {
        switch($qry_flag)
        {
          case 0:
                 return @mysql_result($result,0);
                 break;
          case 1:  
                 $row = @mysql_fetch_row($result);
                 return $row;
                 break;
          case 2:
                 while($row = @mysql_fetch_row($result))
                 {
                   $ret_array[] = $row;
                 }
                 return $ret_array;
                 break;
          case 3:
                 $row = @mysql_fetch_array(($result),MYSQL_ASSOC);
                 return $row;
                 break;
          case 4:
                 while($row = mysql_fetch_array(($result),MYSQL_ASSOC))
                 {
                   $ret_array[] = $row;
                 }
                 return $ret_array;
                 break;
          case 5:
                 if($result)
                 {
                   return true;
                 }
                 else
                 {
                   return false;
                 }
                 break;
          default:
                 $this->err_msg .= 'Invalid Query Flag\n';
                 return $this->err_msg;
                 break;

        } // End: switch
      } // Else: if(!$result)
    } // End:  function mysql_commands($query_str, $qry_flag)

    function pgsql_commands($query_str, $qry_flag)
    {
      if(!($result=@pg_query($this->pg_link,$query_str)))
      {
        $err_msg = pg_errormessage().'\n';
        $this->err_msg .= $err_msg;
        for($i=0;$i<count($this->admin_email);$i++)
        {
          mail($this->admin_email[$i],$this->err_mail_title.$this->applydate, $this->client_ip."_".$err_msg."_".$query_str);
        }
        return $this->err_msg;
      }
      else
      {
        switch($qry_flag)
        {
          case 0:
                 return @pg_result($result,0);
                 break;
          case 1: 
                 while ($row = @pg_fetch_row($result))
                 {
                   $ret_array  = $row;
                 }
                 return $ret_array;
                 break;
          case 2:
                 while ($row = @pg_fetch_row($result))
                 {
                   $ret_array[] = $row;
                 }
                 return $ret_array;
                 break;
          case 3:
                 while($row = @pg_fetch_assoc($result))
                 {
                   $ret_array   = $row;
                 }
                 return $ret_array;
                 break;
          case 4:
                 while($row = @pg_fetch_assoc($result))
                 {
                   $ret_array[]  = $row;
                 }
                 return $ret_array;
                 break;
          case 5:
                 if($result)
                 {
                   return true;
                 }
                 else
                 {
                   return false;
                 }
                 break;
          default:
                 $this->err_msg .= 'Invalid Query Flag\n';
                 return $this->err_msg;
                 break;
        } // End : switch
      } // Else : if(!$result)
    } // End : function pgsql_commands($query_str, $qry_flag)

    function mssql_commands($query_str, $qry_flag)
    {
      if(!($result=@mssql_query($query_str, $this->db_link)))
      {
        $err_msg = 'Unable exec sql command\n';
        $this->err_msg .= $err_msg;
        for($i=0;$i<count($this->admin_email);$i++)
        {
          mail($this->admin_email[$i],$this->err_mail_title.$this->applydate, $this->client_ip."_".$err_msg."_".$query_str);
        }
        return $this->err_msg;
      }
      else
      {
        switch($qry_flag)
        {
          case 0:
                 return @mssql_result($result,0);
                 break;
          case 1:
                 $row = @mssql_fetch_row($result);
                 return $row;
                 break;
          case 2:
                 while($row = @mssql_fetch_row($result))
                 {
                   $ret_array[] = $row;
                 }
                 return $ret_array;
                 break;
          case 3:
                 $row = @mssql_fetch_array($result);
                 return $row;
                 break;
          case 4:
                 while($row = mssql_fetch_array($result))
                 {
                   $ret_array[] = $row;
                 }
                 return $ret_array;
                 break;
          case 5:
                 if($result)
                 {
                   return true;
                 }
                 else
                 {
                   return false;
                 }
                 break;
          default:
                 $this->err_msg .= 'Invalid Query Flag\n';
                 return $this->err_msg;
                 break;
        } // End : switch($qry_flag)
      } // Else: if(!$result)
    } // End : function mssql_commands($query_str, $qry_flag)
  } // End: Class

?>