<?php
  
/*
   * A simple guestbook.
   *
   * ------------------------------------------------------------------------------
   * "THE BEER-WARE LICENSE" (Revision 42):
   * <giversen@netadmins.dk> wrote this file. As long as you retain this notice you
   * can do whatever you want with this stuff. If we meet some day, and you think
   * this stuff is worth it, you can buy me a beer in return. Anders Giversen
   * ------------------------------------------------------------------------------
   */
  
  /*
    This guestbook requires PHP4, MySQL (and my MySQL class). Modify it to fit your needs.
    
    The associated MySQL table:
    CREATE TABLE guestbook (
    id int(11) NOT NULL auto_increment,
    name varchar(55) NOT NULL default '',
    email varchar(55) NOT NULL default '',
    message text NOT NULL,
    date varchar(15) NOT NULL default '',
    time varchar(10) NOT NULL default '',
    PRIMARY KEY (id) );
   */
  
  
require("header.php");
  
  require(
"MySQL_class.php");
  
$sql = new MySQL_class("localhost""db""user""pass");
  
  if (empty(
$_GET['action'])) {
    
$action "None";
  } else {
    
$action addslashes($_GET['action']);
  }
  
$error 0;
  
  if (!empty(
$_POST['form']) && ($_POST['form'] == 'submitted')) {
    
$name addslashes(strip_tags(trim($_POST['name'])));
    
$email addslashes(strip_tags(trim($_POST['email'])));
    
$message addslashes(strip_tags(trim($_POST['message'])));
    
    
$message preg_replace("/(\r\n|\n|\r)/""<br />"$message);
    
$message preg_replace("/(<br \/>){2,}/""<br /><br />"$message);
    
    
$date date("M j, Y");
    
$time date("H:i");
    
    if (empty(
$name) || empty($message)) {
      
$error 1;
    } elseif (!empty(
$email) && !preg_match("/^(.+)@(.+)\.(.+)$/"$email)) {
      
$error 2;
    } else {
      
$sql->Insert("insert into guestbook values ('', '$name', '$email', '$message', '$date', '$time')");
      
      
header("Location: " $_SERVER['PHP_SELF'] . "?action=read");
      exit;
    }
  } else {
    
$name "";
    
$email "";
    
$message "";
  }
?>
<h2>Guestbook</h2>

<div class="sub-menu"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=read">Read the
guestbook</a> &middot; <a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=write">Write in
the guestbook</a></div>

<div class="centered">
<?php
  
if ($action == 'read') { // Read the guestbook
    
if (empty($_GET['offset'])) {
      
$offset 0;
    } else {
      
$offset addslashes($_GET['offset']);
    }
    
$limit 15;
    
    if (!
preg_match("/^([0-9]+)$/"$offset)) {
      exit(
"An error occurred while performing your request (expected integer value)!");
    }
    
    
$sql->Query("select * from guestbook");
    
$numrows $sql->getNumRows();
    
$pages intval($numrows $limit);
    if (
$numrows $limit) { $pages++; }
    
    
$sql->Query("select * from guestbook order by id desc limit $offset, $limit");
    while (
$row $sql->getNext()) {
      
printf("<div class=\"guestbook-entry\"><div class=\"guestbook-header\">%s wrote the %s at %s the following:</div>"
            
."%s</div>\n", empty($row['email']) ? $row['name'] : "<a href=\"mailto:".$row['email']."\">".$row['name']."</a>"
            
$row['date'], $row['time'], $row['message']);
    }
    
    print(
"<div class=\"navigation\">Page [");
    for (
$i 1$i <= $pages$i++) {
      
$newoffset $limit * ($i-1);
      
printf("%s", ($offset == $newoffset) ? "&nbsp;<b>$i</b>" :
                   
"&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?action=read&amp;offset=$newoffset\">$i</a>");
    }
    print(
"&nbsp;]</div>\n</div>\n\n");
  } else { 
// Write in the guestbook
    
$name stripslashes($name);
    
$email stripslashes($email);
    
$message stripslashes($message);
    
    
$message preg_replace("/<br \/>/""\n"$message);
?>
<form class="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table class="centered-form" summary="Guestbook form">
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="55" maxlength="50" value="<?php echo $name?>" />
<?php if ($error == 1) { print("<span class=\"smallred\">(*Required)</span>"); } ?></td>
</tr>

<tr>
<td>Email:</td>
<td><input type="text" name="email" size="55" maxlength="50" value="<?php echo $email?>" />
<?php if ($error == 2) { print("<span class=\"smallred\">(*Invalid)</span>"); } ?></td>
</tr>

<tr valign="top">
<td>Message: </td>
<td><textarea name="message" cols="50" rows="10"><?php echo $message?></textarea>
<?php if ($error == 1) { print("<span class=\"smallred\">(*Required)</span>"); } ?></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type="hidden" name="form" value="submitted" />
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>
</div>

<?php
  
}
  
  require(
"footer.php");
?>