<!-- Guestbook.php
	 CET4583 - Lesley Peterson
	 Modified from the script at Hardcoder.com
	 Last Modified: 01-25-2009
-->

<?php
$dbHost = "localhost";
$dbUser = "lesleyp_ucf";
$dbPass = "eris23";
$dbDatabase = "lesleyp_guestbook";

// Connect to DB

$li = mysql_connect($dbHost, $dbUser, $dbPass) or die("Could not connect");
mysql_select_db($dbDatabase, $li) or die ("could not select DB");

?>

<?php
// initiate some vars

$gb_str = ""; 	// $gb_str is the string we'll append entries to
$pgeTitle = "View and Sign Guestbook";
$ipaddress = $_SERVER["REMOTE_ADDR"];

// If form is submitted, then insert into DB
if (!empty($HTTP_POST_VARS["submit"])) {
	$name = $HTTP_POST_VARS["frmName"];
	$email = $HTTP_POST_VARS["frmEmail"];
	$comment = $HTTP_POST_VARS["frmComment"];
	$date = Date("Y-m-d h:i:s");

//	$gb_query = 	"insert into guestbook
//			values(0, '$name', '$comment', '$email', '$ipaddress', '$date')";

	$gb_query = 	"call spInsertGuestBookEntry('$name', '$comment', '$email', '$ipaddress')";

	mysql_query($gb_query);
	$res = mysql_affected_rows();

	// See if insert was successful or not
	if($res > 0) {
		$ret_str="Thank you! Your guestbook entry was successfully added.";
	} else {
		$ret_str = "Your guestbook entry was NOT successfully added.";
	}

	// Append success/failure message
	$gb_str .= "<span class=\"ret\">$ret_str</span><BR>";
}
?>

<?php
// The querystring
$get_query = "select GuestBookName, GuestBookEmail, GuestBookEntry, GuestBookIP, DATE_FORMAT(GuestBookDate, '%m-%d-%y %H:%i') GuestBookDate
		from guestbook";

$get_rs = mysql_query($get_query);
$gb_str .= "<hr size=\"1\">";

// While there are still results
while($get_row = mysql_fetch_array($get_rs)) {
	$name = $get_row["GuestBookName"];
	$email = $get_row["GuestBookEmail"];
	$comment = $get_row["GuestBookEntry"];
	$ipaddress = $get_row["GuestBookIP"];
	$date = $get_row["GuestBookDate"];

	if(!empty($name)) {
		// If name exists and email exists, link name to email
		if(!empty($email)) {
			$name="by <a href=\"mailto:$email\">$name</a>";
		}
	// If name does exist and email exists, link email to email
	} elseif (!empty($email)) {
		$name = "by <a href=\"mailto:$email\">$email</a>";
	// Else make name blank
	} else {
		$name = "";
	}

	// Append to string we'll print later on
	$gb_str .= "<br>$comment<p class=\"small\">< posted on $date $name from $ipaddress><hr size=\"1\">";
}

// Free Result Memory
mysql_free_result($get_rs);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Guestbook</TITLE>
<SCRIPT language="javascript">
<!--

/* This function is pulled from a generic validation file from
some other site (probably developer.netscape.com) and strips out
characters you don't want */

function stripCharsInBag (s, bag) {
	var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// This function just makes sure the comment field is not empty

function valForm(frm) {
	badChars = "<[]>{}";
	if(frm.frmComment.value == "" || frm.frmName== "" || frm.frmEmail == "") {
		alert("Please fill in all of the fields for the guestbook.");
		return false;
	} else {
		frm.frmComment.value = stripCharsInBag(frm.frmComment.value, badChars);
		// These values may be empty, but strip chars in case they're not
		frm.frmName.value = stripCharsInBag(frm.frmName.value, badChars);
		frm.frmEmail.value = stripCharsInBag(frm.frmEmail.value, badChars);
		return true;
	}
}

-->
</SCRIPT>
</HEAD>

<BODY bgcolor="#FFFFFF">

<h1>Guestbook for Lesley Peterson - CET4584</h1>

<p>Here is a link to the code for this assignment, in .txt format: <a href="guestbook.txt">guestbook.txt</a></p>

<form name="gb" action="<? echo $PHP_SELF;?>" method="post">
<table cellpadding="3" cellspacing="0" border="0">
  <tr>
    <td class="tdhead" valign="top" align="right">Name</td>
    <td valign="top"><input type="text" name="frmName" value="" size="30" maxlength="50"></td>
  </tr>
  <tr>
    <td class="tdhead" valign="top" align="right">Email</td>
    <td valign="top"><input type="text" name="frmEmail" value="" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td class="tdhead" valign="top" align="right">Comment</td>
    <td valign="top"><textarea name="frmComment" rows="5" cols="30"></textarea></td>
  </tr>
  <tr>
    <td> </td>
    <td><input type="submit" name="submit" value="Submit" onClick="return valForm(document.gb)">
    	<input type="reset" name="reset" value="Reset"></td>
  </tr>
</table>
</form>

<? echo $gb_str; ?>

</BODY>
</HTML>

<?php
// Close MySQL Connection
mysql_close($li);
?>

