Description: This package is a plug and play application for any website. It does not require any database connectivity. All the entries are saved in a text file. The filename starts with .ht which is protected by Apache server. For further addition to the file, it is advisable to save it below the web root.
/*
* GuestBook Light
* ====================================================================
* Description: This package is a plug and play for any website. It
* does not require any database connectivity. All the entries are
* saved in a text file. The filename starts with .ht which is
* protected by Apache server. For further addition to the file, it is
* advisable to save it below the web root.
* ====================================================================
* Author : Ehsan Haque
* Web : http://ehsan.bdwebwork.com
* Created : 4th June 2006
* Demo : http://resource.bdwebwork.com/GuestBookLite/
*
* Filename : guestBook.php
* Purpose : Displays Guest Book entries and a form for users to sign
*/
require_once("functions.php");
/*
* Comment is added in this Block
*/
if ((!empty($_POST['cmd'])) && ($_POST['cmd'] == "add"))
{
// Save comment
$addComment = addComment();
if (!$addComment)
{
// Message to show when save process fails
$msg = "Could not add your comment.\nPlease make sure you entered required fileds and try again.";
if (!empty($_POST))
{
// If save process fails the values for each input fields are set here
foreach ($_POST as $key => $value)
{
$$key = stripslashes(strip_tags(trim($value)));
}
}
}
else
{
$successMsg = "Thank you for Signing my Guest Book";
}
}
/*
* Get Guest Book Entries
*/
$gBEntries = getGBEntries();
/*
* Filename : functions.php
* Purpose : Contains functions to add comments, return list of comments
*/
session_start();
/*
* Defining Constants
* -- GB_FILENAME : The filename that contains the Guest Book entries. This file is named
* starting with .ht, which by default in Apache Configuration is protected.
* To ensure further protection it is advisable to keep the file outside
* web root directory.
* -- PAGE_LIMIT : Number of Entries to show per page
*/
define('GB_FILENAME', ".htguestbook");
define('PAGE_LIMIT', 5);
$gBFilename = GB_FILENAME;
// Creating the file if file does note exists and placing the pointer at the end of the file
$gBFile = fopen($gBFilename, "a+");
fclose($gBFile);
/*
* This function adds comment to the above mentioned protected text file
* return boolean
*/
function addComment()
{
/*
* Checking if $_POST is empty or not. If not, it creates variables with the named
* of input fields passed from Guest Book form and assigns the respective values to them.
*/
if (!empty($_POST))
{
foreach ($_POST as $key => $value)
{
// Taking out all the HTML tags and trimming the values
$$key = strip_tags(trim($value));
}
}
// Guest's name cannot be left blank, so returns false if found blank
if ($yourname == "")
{
return false;
}
// Guest's comment cannot be left blank, so returns false if found blank
if ($yourcomment == "")
{
return false;
}
/*
* Create a MD5 of entry made
* -- This will be used to make sure refreshing a page does not add duplicate entries
*/
$entryMD5 = MD5($yourname . $youremail . $yourweb . $yourcomment);
if ((!empty($_SESSION['entryMD5'])) && ($_SESSION['entryMD5'] == $entryMD5))
{
return true;
}
/*
* Preparing the Guest Book entry
* -- Every entry is seperated by ==
* -- Every field is seperated by ||||
* -- Every key and value of a field is seperated by ^^^^
*/
$gBEntry = "==";
$gBEntry .= "Name^^^^$yourname||||";
$gBEntry .= "Email^^^^$youremail||||";
$gBEntry .= "Web^^^^$yourweb||||";
$gBEntry .= "Time^^^^$datetime||||";
$gBEntry .= "Comment^^^^";
$gBEntry .= $yourcomment;
// Writes the entry to protected text file
$signGB = fwrite($gBFile, $gBEntry);
// MD5 of entry is saved in session
$_SESSION['entryMD5'] = $entryMD5;
fclose($gBFile);
return true;
}
/*
* This function fetches the Entries from the protected text file
* and creates an array to return. By default array_reverse() is used
* to sort the list in Descending order.
* return array
*/
function getGBEntries()
{
$list = array();
$item = array();
// Reads the file if the filesize is greater than 0
if (filesize($gBFilename) > 0)
{
// Assigns the content to a variable
$gBFileContent = fread($gBFile, filesize($gBFilename));
fclose($gBFile);
}
if (!empty($gBFileContent))
{
// Seperating the entries
$entriesArr = explode("==", trim($gBFileContent));
if (!empty($entriesArr))
{
foreach ($entriesArr as $key => $value)
{
if ($value != "")
{
// Seperating fields
$lineArr = explode("||||", $value);
foreach ($lineArr as $lineKey => $lineValue)
{
// Seperating key and value of each field
$itemArr = explode("^^^^", $lineValue);
$item[$itemArr[0]] = $itemArr[1];
}
/*
* Creating the array of entries
* -- $list[index] = $item[fieldKey] = fieldValue
* -- e.g. $list[2] = $item['Name'] = 'Ehsan Haque'
*/
$list[] = $item;
}
}
// Reversing the array to sort entry in Descending order
$list = array_reverse($list);
}
}
return $list;
}
/*
* This function dumps a variable
*/
function dump($var)
{
echo "<pre>";
print_r($var);
echo "</pre>";
}