<?php
/**
* Socket functions -  reading and writing for using the PHP 4 database object.
* Important! The following files from the PEAR are needed:
*
*  /DB.php, /DB/*.php
*
* You must put the files into your PHP include directory.     
*
* @author	Thomas Weinert	<subjective@subjective.de>
* @version	1.4.5
* @package  Netzbrett
* @modulegroup    Netzbrett_Socket
* @module   Netzbrett_Socket_PEAR
*/

/**
*  Include PEAR database object
*/
require_once("DB.php");

/**
* Read entries list without TEXT. 
*@param string $data_dir data location (Host;Database;Table;User;Password) 
*@return array entries list or false
*@access public
*/
function pear_read_entries($data_dir) {
  $result = false;
  list($uri,$tblname) = explode(";",$data_dir);
  
  $dbcon = DB::connect($uri);
  if (!DB::isError($dbcon)) {
    $res = $dbcon->query("SELECT * FROM $tblname ORDER BY file DESC");
    if (!DB::isError($res)) {
      while (!($code = $res->fetchInto($row,DB_FETCHMODE_ASSOC))) {
        if (!isset($code)) {
          break;
        }
        unset($row["TEXT"]);
        $result[$row["FILE"]] = $row;
      }
    } else {
      echo "Can not read entries from database.<p>".$res->message."</p>";
    }
    unset($dbcon);
  } else {
    echo "Can not connect database server.<p>".$dbcon->message."</p>";
  } 
  return $result;
}

/**
* Read one entry 
*@param string $data_dir data location (Host;Database;Table;User;Password) 
*@param integer $entrynr entry number 
*@return array loaded entry
*@access public
*/
function pear_read_entry($data_dir,$entrynr=0) {
  $entry = false;
  list($uri,$tblname) = explode(";",$data_dir);
  
  $dbcon = DB::connect($uri);
  if (!DB::isError($dbcon)) {
    $res = $dbcon->query("SELECT * FROM $tblname WHERE file = '$entrynr'");
    if (!DB::isError($res)) {
      while (!($code = $res->fetchInto($row,DB_FETCHMODE_ASSOC))) {
        if (!isset($code)) {
          break;
        }
        $entry = $row;
      }
    } else {
      echo "Can not read entries from database.<p>".$res->message."</p>";
    }
    unset($dbcon);
  } else {
    echo "Can not connect database server.<p>".$dbcon->message."</p>";
  } 
  return $entry;
}

/**
* Save/Update entry 
*@param string $data_dir data location (Host;Database;Table;User;Password) 
*@param boolean $new save as new entry
*@param array $entry_data entry data
*@param integer $entryno number of entry ($new==true) or of previous entry
*@return integer entry number or false
*@access public
*/
function pear_save_entry($data_dir,$new,$entry_data,$entryno) {

  function asl($str) {
    return addslashes($str);
  }

  $result = false;
  list($uri,$tblname) = explode(";",$data_dir);
  
  $dbcon = DB::connect($uri);
  if (!DB::isError($dbcon)) {
    if ($new) { 
      $res = $dbcon->query("INSERT INTO $tblname (DATE,HTTP,MAIL,NAME,PATH,PREV,SUBJ,TEXT) VALUES 
        ('".asl($entry_data["DATE"])."','".asl($entry_data["HTTP"])."','".asl($entry_data["MAIL"])."','".
            asl($entry_data["NAME"])."','".asl($entry_data["PATH"])."','".asl($entry_data["PREV"])."','".
            asl($entry_data["SUBJ"])."','".asl($entry_data["TEXT"])."')");
    } else {
      $res = $dbcon->query("UPDATE $tblname SET
                HTTP = '".asl($entry_data["HTTP"])."', MAIL = '".asl($entry_data["MAIL"])."',
                SUBJ = '".asl($entry_data["SUBJ"])."', TEXT = '".asl($entry_data["TEXT"])."'
                WHERE FILE = '$entryno'");
    }
    if (!DB::isError($res)) {
      if ($new) {
        $result = $entryno;
        $res = $dbcon->query("SELECT * FROM $tblname ORDER BY date DESC, file DESC"); 
        if (!DB::isError($res)) {
          if (!($code = $res->fetchInto($row,DB_FETCHMODE_ASSOC))) {
            $result = $row["FILE"];
          }
        } else {
          echo "Can not save to database.<p>".$res->message."</p>";        
        }
      } else {
        $result = $entryno;
      }
    } else {
      echo "Can not save to database.<p>".$res->message."</p>";
    }
    unset($dbcon);
  } else {
    echo "Can not connect database server.<p>".$dbcon->message."</p>";
  } 
  
  return $result;	
}

/**
* Delete entry and itīs answers
*@param string $data_dir data location (Host;Database;Table;User;Password) 
*@param integer $entry_nr number of entry
*@return integer true or false
*@access public
*/
function pear_delete_entries($data_dir="./data/",$entry_nr) {  
  $result = false;
  list($uri,$tblname) = explode(";",$data_dir);
  
  $dbcon = DB::connect($uri);
  if (!DB::isError($dbcon)) {
    $query = "DELETE FROM $tblname 
              WHERE (file = '$entry_nr') 
              OR (prev = '$entry_nr')
              OR (path LIKE '%;$entry_nr;%')";
    $res = $dbcon->query($query);
    if (!DB::isError($res)) {
      $result = true;
    } else {
      echo "Can not read entries from database.<p>".$res->message."</p>";
    }
    unset($dbcon);
  } else {
    echo "Can not connect database server.<p>".$dbcon->message."</p>";
  }
}

/**
* Load last entry 
*@param string $data_dir data location (Host;Database;Table;User;Password) 
*@return array last entry
*@access public
*/
function pear_read_lastentry($data_dir) {
  $entry = false;
  list($uri,$tblname) = explode(";",$data_dir);
  
  $dbcon = DB::connect($uri);
  if (!DB::isError($dbcon)) {
    $res = $dbcon->query("SELECT * FROM $tblname ORDER BY date DESC, file DESC");
    if (!DB::isError($res)) {
      if (!($code = $res->fetchInto($row,DB_FETCHMODE_ASSOC))) {
        $entry = $row;
      }
    } else {
      echo "Can not read entries from database.<p>".$res->message."</p>";
    }
    unset($dbcon);
  } else {
    echo "Can not connect database server.<p>".$dbcon->message."</p>";
  } 
  return $entry;
}

?>