<?php
/**
* Socket functions -  reading and writing for using MySQL.    
*
* @author	Thomas Weinert	<subjective@subjective.de>
* @version	1.4.5
* @package  Netzbrett
* @modulegroup    Netzbrett_Socket
* @module   Netzbrett_Socket_MySQL
*/

/**
* 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 mysql_read_entries($data_dir) {
  $result = false;
  list($host,$dbname,$tblname,$user,$pass) = explode(";",$data_dir);
  
  $dbcon = mysql_connect($host,$user,$pass);
  if ($dbcon) {
    if (mysql_select_db($dbname,$dbcon)) {
      $query = "SELECT * FROM $tblname ORDER BY file DESC";
      $res = mysql_query($query);
      if ($res) {
        while ($row = mysql_fetch_array($res)) {
          //unset($row["TEXT"]); 
          if (!is_array($result)) {
            $result = array($row["FILE"]=>$row);
          } else {
            $result[$row["FILE"]] = $row;
          }
        }
        mysql_free_result($res);
      } else {
        echo "Can not read entries from database.<p>".mysql_error()."</p>";
      }
    } else {
      echo "Can not select database.<p>".mysql_error()."</p>";
    }
    mysql_close($dbcon);
  } else {
    echo "Can not connect mySQL server.<p>".mysql_error()."</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 mysql_read_entry($data_dir,$entrynr=0) {
  $entry = false;
  list($host,$dbname,$tblname,$user,$pass) = explode(";",$data_dir);
  
  $dbcon = mysql_connect($host,$user,$pass);
  if ($dbcon) {
    if (mysql_select_db($dbname,$dbcon)) {
      $query = "SELECT * FROM $tblname WHERE file = '$entrynr'";
      $res = mysql_query($query);
      if ($res) {
        if (!is_array($row)) { 
          settype($row,"array"); 
        }
        if ($row = mysql_fetch_array($res)) {
          $entry = $row;
        }
        mysql_free_result($res);
      } else {
        echo "Can not read entries from database.<p>".$query."</p><p>".mysql_error()."</p>";
      }
    } else {
      echo "Can not select database.<p>".mysql_error()."</p>";
    }
    mysql_close($dbcon);
  } else {
    echo "Can not connect mySQL server.<p>".mysql_error()."</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 mysql_save_entry($data_dir,$new,$entry_data,$entryno) {

  $result = false;
  list($host,$dbname,$tblname,$user,$pass) = explode(";",$data_dir);
  
  $dbcon = mysql_connect($host,$user,$pass);
  if ($dbcon) {
    if (mysql_select_db($dbname,$dbcon)) {
      if ($new) { 
        $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 {
        $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'";
      }
      $res = mysql_query($query);
      if ($res) {
        if ($new) {
          $result = mysql_insert_id($dbcon);
        } else {
          $result = $entryno;
        }
      } else {
        echo "Can not read entries from database.<p>".mysql_error()."</p>";
      }
    } else {
      echo "Can not select database.<p>".mysql_error()."</p>";
    }
    mysql_close($dbcon);
  } else {
    echo "Can not connect mySQL server.<p>".mysql_error()."</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 mysql_delete_entries($data_dir="./data/",$entry_nr) {  
  $result = false;
  list($host,$dbname,$tblname,$user,$pass) = explode(";",$data_dir);
  
  $dbcon = mysql_connect($host,$user,$pass);
  if ($dbcon) {
    if (mysql_select_db($dbname,$dbcon)) {
      $query = "DELETE FROM $tblname 
                WHERE (file = '$entry_nr') 
                OR (prev = '$entry_nr')
                OR (path LIKE '%;$entry_nr;%')";
      $res = mysql_query($query);
      if ($res) {
        $result = true;
      } else {
        echo "Can not read entries from database.<p>".mysql_error()."</p>";
      }
    } else {
      echo "Can not select database.<p>".mysql_error()."</p>";
    }
    mysql_close($dbcon);
  } else {
    echo "Can not connect mySQL server.<p>".mysql_error()."</p>";
  }
}

/**
* Load last entry 
*@param string $data_dir data location (Host;Database;Table;User;Password) 
*@return array last entry
*@access public
*/
function mysql_read_lastentry($data_dir) {
  $entry = false;
  list($host,$dbname,$tblname,$user,$pass) = explode(";",$data_dir);
  
  $dbcon = mysql_connect($host,$user,$pass);
  if ($dbcon) {
    if (mysql_select_db($dbname,$dbcon)) {
      $query = "SELECT * FROM $tblname ORDER BY date DESC, file DESC";
      $res = mysql_query($query);
      if ($res) {
        if (!is_array($row)) { 
          settype($row,"array"); 
        }
        if ($row = mysql_fetch_array($res)) {
          $entry = $row;
        }
        mysql_free_result($res);
      } else {
        echo "Can not read entries from database.<p>".$query."</p><p>".mysql_error()."</p>";
      }
    } else {
      echo "Can not select database.<p>".mysql_error()."</p>";
    }
    mysql_close($dbcon);
  } else {
    echo "Can not connect mySQL server.<p>".mysql_error()."</p>";
  } 
  return $entry;
}

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

?>
