Bij deze de source, ik kan me niet meer herinneren waar ik dit script vandaan heb, let iedergeval niet op de enorme troep en overbodige codes enz. ik heb me nog niet erg verdiept in php, vandaar
<?php
$emailEntryTo = "";
$emailEntryFrom = "guestbook@" . $SERVER_NAME;
$entriesPerPage = 35;
$datafile = "gb_entries.txt";
$hrImage = "";
require("header.inc.php");
require("footer.inc.php");
if (isset($HTTP_GET_VARS['add'])) {
if (isset($HTTP_POST_VARS['submitForm']))
addEntry();
else
displayForm();
}
else
displayEntries();
function displayForm($errorMessage = "") {
if ($errorMessage)
global $HTTP_POST_VARS;
echoHeader("Nieuw Bericht Plaatsen");
?>
<p class="navbarTop">
[<a href="guestbook.php">Bekijk berichten</a>]
<strong>Nieuw Bericht Plaatsen</strong>
</p>
Nieuw Bericht Plaatsen
<?php
// end of HTML
echo "<p>Vul in iedergeval de met <b>bold</b> aangegeven velden in ; de rest moet je zelf weten.\n";
echo "HTML codes worden uit de berichten gefilterd.</p>\n\n";
if ($errorMessage)
echo "<p class=\"new\"><strong>Error:</strong> " . $errorMessage . "</p>\n\n";
echo "<form action=\"guestbook.php?add=1\" method=\"post\">\n";
echo "<table>\n";
echoFormInputTag("Naam", "name", $HTTP_POST_VARS['name'], 30, true);
echoFormInputTag("E-mail", "email", $HTTP_POST_VARS['email'], 30);
echo "<tr>\n";
echo "\t<td></td>\n";
echo "\t<td>\n";
echo "\t\t(<input type=\"checkbox\" name=\"hideEmail\"";
if (!$errorMessage || isset($HTTP_POST_VARS['hideEmail']))
echo " checked=\"checked\"";
echo " /> e-mail adres is alleen te zien voor de beheerder van het gastenboek)\n";
echo "\t</td>\n";
echo "</tr>\n";
echoFormInputTag("Homepage", "homepage", $HTTP_POST_VARS['homepage'], 50);
echoFormInputTag("Stad", "city", $HTTP_POST_VARS['city'], 30);
echoFormInputTag("Land", "country", $HTTP_POST_VARS['country'], 30);
echo "<tr>\n";
echo "\t<td class=\"label\" style=\"vertical-align: top\">\n";
echo "\t\t<strong>Bericht:</strong>\n";
echo "\t</td>\n";
echo "\t<td>\n";
echo "\t\t<textarea name=\"message\" rows=\"6\" cols=\"45\">";
if (isset($HTTP_POST_VARS['message']))
echo $HTTP_POST_VARS['message'];
echo "</textarea>\n";
echo "\t</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "\t<td></td>\n";
echo "\t<td>\n";
echo "\t\t<input type=\"submit\" name=\"submitForm\" value=\"Verzenden\" />\n";
echo "\t\t<input type=\"submit\" name=\"resetForm\" value=\"Wissen\" />\n";
echo "\t</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "</form>\n\n";
echo "<p class=\"navbarBottom\">\n";
echo "[<a href=\"guestbook.php\">Bekijk berichten</a>]\n";
echo "<strong>Sign guestbook</strong>\n</p>\n";
echoFooter();
if ($errorMessage)
exit;
}
function echoFormInputTag($label, $name, &$value, $size, $required = false) {
echo "<tr>\n";
echo "\t<td class=\"label\">\n";
if ($required)
echo "\t\t<strong>" . $label . ":</strong>\n";
else
echo "\t\t" . $label . ":\n";
echo "\t</td>\n";
echo "\t<td>\n";
echo "\t\t<input type=\"text\" name=\"" . $name . "\" value=\"";
if (isset($value))
echo $value;
echo "\" size=\"" . $size . "\" maxlength=\"" . $size * 4 . "\" />\n";
echo "\t</td>\n";
echo "</tr>\n";
}
function addEntry() {
// get config. and form vars from global scope
global $datafile, $emailEntryTo, $emailEntryFrom, $HTTP_POST_VARS;
// remove whitespace and slashes
while (list($key, $value) = each($HTTP_POST_VARS)) {
$HTTP_POST_VARS[$key] = stripSlashes(trim($value));
}
// check that required fields aren't empty
if ($HTTP_POST_VARS['name'] == "")
displayForm("<var>Name</var> field is empty.");
if ($HTTP_POST_VARS['message'] == "")
displayForm("<var>Message</var> field is empty.");
// check for basic errors in e-mail address
if ($HTTP_POST_VARS['email'] != ""
&& !validEmailAddr($HTTP_POST_VARS['email']))
displayForm("<var>" . $HTTP_POST_VARS['email']
. "</var> is not a valid e-mail address.");
// remove "http://" from homepage address
$HTTP_POST_VARS['homepage'] = str_replace("http://", "", $HTTP_POST_VARS['homepage']);
// get host name
$host = gethostbyaddr(getenv("REMOTE_ADDR"));
// get time and date added (in GMT)
$date = gmdate("Y-m-d @ H:i:s", time());
// open datafile for appending
@ $fp = fopen($datafile, "a");
if (!$fp)
displayForm("Failed to open datafile." . getFileIOErrorMsg());
// lock datafile
if (!flock($fp, LOCK_EX))
displayForm("Failed to lock datafile." . getFileIOErrorMsg());
// append new entry to datafile
fwrite($fp, $date . "|" . $host . "|"
. removeHTML($HTTP_POST_VARS['name']) . "|"
. removeHTML($HTTP_POST_VARS['email']) . "|"
. $HTTP_POST_VARS['hideEmail'] . "|"
. removeHTML($HTTP_POST_VARS['homepage']) . "|"
. removeHTML($HTTP_POST_VARS['city']) . "|"
. removeHTML($HTTP_POST_VARS['country']) . "|"
. removeHTML($HTTP_POST_VARS['message'], true) . "\n");
flock($fp, LOCK_UN);
fclose($fp);
if ($emailEntryTo != "") {
$subject = "New Entry: " . $HTTP_POST_VARS['name'];
$content = "At " . $date . " (GMT), " . $HTTP_POST_VARS['name']
. " (" . $host . ") wrote the following in your guestbook:\n\n"
. $HTTP_POST_VARS['message'] . "\n";
if ($HTTP_POST_VARS['city'] != "" || $HTTP_POST_VARS['country'] != ""
|| $HTTP_POST_VARS['homepage'] != "") {
$content .= "\n--\n";
if ($HTTP_POST_VARS['homepage'] != "")
$content .= "Homepage: http://" . $HTTP_POST_VARS['homepage'] . "\n";
if ($HTTP_POST_VARS['city'] != "" || $HTTP_POST_VARS['country'] != "") {
$content .= "From: ";
if ($HTTP_POST_VARS['city'] != "") {
$content .= $HTTP_POST_VARS['city'];
if ($HTTP_POST_VARS['country'] != "")
$content .= ", " . $HTTP_POST_VARS['country'];
}
else
$content .= $HTTP_POST_VARS['country'];
$content .= "\n";
}
}
$extraHeaders = "From: " . $emailEntryFrom;
// add Reply-To field if entry contains an e-mail address
if ($HTTP_POST_VARS['email'] != "")
$extraHeaders .= "\nReply-To: " . $HTTP_POST_VARS['email'];
@ mail($emailEntryTo, $subject, $content, $extraHeaders);
}
header("Location: guestbook.php");
}
function removeHTML($var, $allowNewline = false) {
$var = str_replace(chr(13), "", $var);
if ($allowNewline)
// replace linefeeds with HTML breaks
$var = str_replace(chr(10), "<br />", $var);
else
// remove linefeeds
$var = str_replace(chr(10), "", $var);
return $var;
}
function validEmailAddr($addr) {
$addr = strtolower($addr);
// split address into user/domain name and check that they aren't empty
$addr = explode("@", $addr);
if (sizeof($addr) != 2 || $addr[0] == "")
return false;
// split domain name into levels and check that there's at least two
$levels = explode(".", $addr[1]);
$sLevels = sizeof($levels);
if ($sLevels < 2)
return false;
$tld = $levels[$sLevels - 1];
if (strlen($tld) < 2 || (strlen($tld) > 3 && $tld != "arpa"
&& $tld != "info" && $tld != "name" && $tld != "aero"
&& $tld != "coop" && $tld != "museum"))
return false;
return true;
}
function displayEntries() {
global $emailEntryTo, $datafile, $entriesPerPage, $hrImage,
$HTTP_GET_VARS;
@ $entries = file($datafile);
if ($entries === false && file_exists($datafile))
$errorMessage = "<strong>Error: </strong>Failed to open datafile."
. getFileIOErrorMsg();
else if (empty($entries))
$errorMessage = "Guestbook is empty.";
if (!isset($errorMessage)) {
$sEntries = sizeof($entries);
// determine first entry on last page
if ($sEntries > $entriesPerPage) {
$startLastPage = $sEntries % $entriesPerPage;
if ($startLastPage)
$startLastPage = $sEntries - $startLastPage;
else
$startLastPage = $sEntries - $entriesPerPage;
}
else
$startLastPage = 0;
// display last page if $HTTP_GET_VARS['start'] not set
if (!isset($HTTP_GET_VARS['start']) || $HTTP_GET_VARS['start'] >= $sEntries)
$HTTP_GET_VARS['start'] = $startLastPage;
else if ($HTTP_GET_VARS['start'] < 0)
$HTTP_GET_VARS['start'] = 0;
// get entries to display
$entries = array_slice($entries, $HTTP_GET_VARS['start'], $entriesPerPage);
// reverse list to display newest on top
$entries = array_reverse($entries);
}
$pageTitle = 'Guestbook Entries';
if (!isset($errorMessage)) {
$pageTitle .= ' ' . ($HTTP_GET_VARS['start'] + 1) . ' - ';
if ($HTTP_GET_VARS['start'] + $entriesPerPage < $sEntries)
$pageTitle .= $HTTP_GET_VARS['start'] + $entriesPerPage;
else
$pageTitle .= $sEntries;
}
echoHeader($pageTitle);
echo "<p class=\"navbarTop\">\n";
echo "\n";
echo "[<a href=\"guestbook.php?add=1\">Nieuw Bericht Plaatsen</a>]\n</p>\n\n";
if (!isset($errorMessage) && $sEntries > $entriesPerPage)
printDisplayEntriesMenu($HTTP_GET_VARS['start'], $startLastPage, 0);
if (isset($errorMessage))
echo "Guestbook entries\n\n";
else {
}
if (isset($errorMessage))
echo "<p class=\"new\">" . $errorMessage . "</p>\n\n";
else {
$numEntriesToDisplay = sizeof($entries);
for ($entry = current($entries); $entry; $entry = next($entries)) {
list($date, $host, $name, $email, $hideEmail, $homepage, $city,
$country, $message) = explode("|", $entry);
echo "<span class=guestbookname>" . $name;
if ($city != "") {
echo " - " . $city;
if ($country != "")
echo ", " . $country;
}
else if ($country != "")
echo " - " . $country;
echo "</span>\n";
echo "<span class=guestbookdate>" . $date . " (GMT)";
if ($homepage != "")
echo "\n| <span class=guestbookhomepage><a href=\"http://" . $homepage . "\">homepage</a></span>";
if ($email != "" && !$hideEmail)
echo "\n| <a href=\"mailto:" . $email . "\">e-mail</a>";
echo "</span><br>\n";
$numEntriesToDisplay--;
echo "<span class=guestbookmessage>";
echo str_replace("<br />", "<br />\n", chop($message));
echo "</span><br>";
if ($numEntriesToDisplay) {
echo "";
if ($hrImage != "")
echo "<table width=100% border=0 cellpadding=0 cellspacing=0><td height=1 background=images/dotline.jpg></td></table>";
else
echo "<br><table width=100% border=0 cellpadding=0 cellspacing=0><td height=1 background=images/dotline.jpg></td></table><br>";
echo "\n\n";
}
}
}
if (!isset($errorMessage) && $sEntries > $entriesPerPage)
printDisplayEntriesMenu($HTTP_GET_VARS['start'], $startLastPage, 1);
echo "<p class=\"navbarBottom\">\n";
echo "\n";
echoFooter();
}
function printDisplayEntriesMenu($start, $startLastPage, $navbarPos) {
// get config. variable from global scope
global $entriesPerPage;
if ($navbarPos)
echo "<p class=\"navbarBottom\">\n";
else
echo "<p class=\"navbarTop\">\n";
if ($start > 0)
echo "[<a href=\"guestbook.php?start=0\">First</a>]\n";
else
echo "<strong>First</strong>\n";
if ($start - $entriesPerPage > 0)
echo "[<a href=\"guestbook.php?start="
. ($start - $entriesPerPage) . "\">Previous</a>]\n";
if ($start + $entriesPerPage < $startLastPage)
echo "[<a href=\"guestbook.php?start="
. ($start + $entriesPerPage) . "\">Next</a>]\n";
if ($start != $startLastPage)
echo "[<a href=\"guestbook.php?start="
. $startLastPage . "\">Last</a>]\n";
else
echo "<strong>Last</strong>\n";
echo "</p>\n\n";
}
function getFileIOErrorMsg() {
global $emailEntryTo;
$msg = "\nThis could be a result of multiple users trying to access"
. " the file simultaneously.\nPlease try again in a few seconds";
if ($emailEntryTo != "")
$msg .= ", and <a href=\"mailto:"
. $emailEntryTo . "\">notify me</a> if it still doesn't work.";
else
$msg .= ".";
return $msg;
}
?>