<?php
#############################################################################
# A simple PHP/MySQL guest book
#  written by Jim Auldridge
#
# Obtained for free at http://www.christian-web-masters.com/
#
# Please read entire "readme" file, else do not continue!
#
#############################################################################
# BEGIN MySQL INTERACTION CODE
class MySQL_class {

  var $db, $id, $result, $rows, $data, $a_rows;
  var $user, $pass, $host;

# DONT TOUCH ABOVE THIS LINE
#############################################################################

/* Make sure you configure the "Connect" function (below)
     with your DB specific information */

  function Connect($db) {
    // ------------------------->Set below to your db username
    $this->user = "lazy";
    // ------------------------->Set below to your db password
    $this->pass = "pokolgep";
    // ------------------------->Set below to your default database host 
    //                           (if colocated on the same machine as the 
    //                           server executing this script, "localhost"
    //                           will work fine
    $this->host = "localhost";
    // ------------------------->Set below to your database where the table
    //                           "gBook" should reside
    $this->db = "gBook";

##############################################################################  
# DONT TOUCH BELOW THIS LINE
    $this->id = @mysql_pconnect($this->host, $this->user, $this->pass)
    or
    MySQL_ErrorMsg("Unable to connect to MySQL server: $this->host : '" . $_SERVER['SERVER_NAME'] . "'");
    $this->selectdb($this->db);
  }

  function SelectDB($db) {
    @mysql_select_db($db, $this->id)
    or
    MySQL_ErrorMsg("Unable to select database: $db");
  }
  
  function Query($query) {
    $this->result = @mysql_query($query, $this->id)
    or
    MySQL_ErrorMsg("Unable to perform query: $query");
    $this->rows = @mysql_num_rows($this->result);
    $this->a_rows = @mysql_affected_rows($this->id);
  }
  /* Use this function if the query will only return a
  single data element. */

  function Fetch($row) {
    @mysql_data_seek($this->result, $row)
    or
    MySQL_ErrorMsg("Unable to seek data row: $row");
    $this->data = @mysql_fetch_array($this->result)
    or
    MySQL_ErrorMsg("Unable to fetch row: $row");
  }

  function Insert($query) {
    $this->result = @mysql_query($query, $this->id)
    or
    MySQL_ErrorMsg("Unable to perform insert: $query");
    $this->a_rows = @mysql_affected_rows($this->id);
  }

  function Delete($query) {
    $this->result = @mysql_query($query, $this->id)
    or
    MySQL_ErrorMsg("Unable to perform Delete: $query");
    $this->a_rows = @mysql_affected_rows($this->id);
  }
}

/* MySQL_ErrorMsg
Print out an MySQL error message */
function MySQL_ErrorMsg($msg) {
  echo "<!-- Closing out a bunch of HTML constructs which might prevent\n"
  ."the HTML page from displaying the error text. -->\n"
  ."</ul></dl></ol></table></script>\n";
  // Display the error message
  $text  = "<p style=\"color: #ff0000;\">Error: $msg <br />\nMySQL said: \"" . mysql_error() . "\"</p>\n";
  die($text);
}
# END MySQL INTERACTION CODE
##############################################################################

function printEntry($name,$email,$date,$comment,$id,$admin) {
        echo "<table width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"
        ." <tr>\n"
        ."  <td colspan=\"2\" width=\"550\"><hr width=\"100%\"></td>\n"
        ." </tr>\n"
        ." <tr>\n"
        ."  <td width=\"300\"><b>$name</b></td>\n"
        ."  <td width=\"250\">\n"
        ."  <div align=\"right\">\n"
        ."  <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"
        ."   <tr>\n"
        ."    <td><a href=\"mailto:$email\">e-mail</a></td>\n"
        ."    <td width=\"15\"><img src=\"pix.gif\" alt=\"\" height=\"1\" width=\"15\" border=\"0\"></td>\n"
        ."    <td><div align=\"right\">$date</div></td>\n"
        ."   </tr>\n"
        ."  </table>\n"
        ."  </div></td>\n"
        ." </tr>\n"
        ." <tr>\n"
        ."  <td colspan=\"2\" width=\"550\"><img src=\"pix.gif\" alt=\"\" height=\"7\" width=\"1\" border=\"0\"></td>\n"
        ." </tr>\n"
        ." <tr>\n"
        ."  <td colspan=\"2\" width=\"550\">\n"
        ."  <table width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"
        ."   <tr>\n"
        ."    <td width=\"45\"><img src=\"dot_w.gif\" alt=\"\" height=\"18\" width=\"45\" border=\"0\"></td>\n"
        ."    <td width=\"505\">$comment</td>\n"
        ."   </tr>\n";
        if ($admin == "yes") {
                echo "   <tr>\n"
                ."    <td width=\"550\" colspan=\"2\">\n"
                ."    <script type=\"text/javascript\">\n"
                ."    function delCon(ENTRY) {\n"
                ."            if (window.confirm(\"Are you sure you want to delete record $id?\")) {\n"
                ."                    document.forms[ENTRY].submit();\n"
                ."            }\n"
                ."    }\n"
                ."    </script>\n"
                ."    <form name=\"d$id\" action=\"admin.php\" method=\"POST\">\n"
                ."    <input type=\"hidden\" name=\"action\" value=\"del\" />\n"
                ."    <input type=\"hidden\" name=\"id\" value=\"$id\" />\n"
                ."    </form><div align=\"right\"><small style=\"cursor: pointer;\" onclick=\"javascript:delCon('d$id')\">*delete</small></div></td>\n"
                ."   </tr>\n";
        }
        echo "  </table></td>\n"
        ." </tr>\n"
        ."</table>\n";
}

function viewAll($admin) {
        require_once("functions.php");
        $viewAll = new MySQL_class();
        $viewAll->Connect("");
        $viewAll->Query("SELECT * FROM gBook ORDER BY id DESC");
        for ($i="0"; $i<$viewAll->rows; $i++) {
                $viewAll->fetch($i);
                $id = $viewAll->data[0];
                $date = $viewAll->data[1];
                $name = $viewAll->data[2];
                $email = $viewAll->data[3];
                $comment = $viewAll->data[4];
                if ($admin == "yes") {
                        printEntry($name,$email,$date,$comment,$id,$admin);
                }
                else {
                        printEntry($name,$email,$date,$comment,"none",$admin);
                }
        }
}
?>
