<?php
/* (c) 2003 Lonnie Chrisman, all rights reserved. */
$dir = "./GuestBook";

$verbose = FALSE;
$admin = FALSE;
$all = FALSE;
$checks = FALSE;

foreach ($HTTP_SERVER_VARS["argv"] as $args) {
	foreach (explode("&",$args) as $arg) {
		if ($arg=="verbose") { $verbose=TRUE; }
		if ($arg=="admin")   { $admin=TRUE; }
		if ($arg=="all")     { $all=TRUE; }
		if ($arg=="checkboxes") { $checks=TRUE; }
		echo "<!--ARG: $arg-->\n";
	}
}
if ($HTTP_POST_VARS["verbose"]=="on") { $verbose=TRUE; }
if ($HTTP_POST_VARS["admin"]=="on")   { $admin=TRUE; }
if ($HTTP_POST_VARS["all"]=="on")     { $all=TRUE; }

$emailId = $HTTP_POST_VARS["email"];
if ($emailId!="") {
  setcookie("email",$emailId,time()+60*60*24*30);
} else {
  $emailId = $HTTP_COOKIE_VARS["email"];
}

$sName = $HTTP_POST_VARS["name"];
$isspam = false;
if (false && $sName != "") {		/* LDC 1/10/2014 Disabled form submissions */
  $comments = $HTTP_POST_VARS["comments"];
  /* Spam problem -- people attempting to post links to their web sites.
   * Solution: Don't accept submissions containing hyperlinks
   */
  if (preg_match("/<|(&lt;)a\s+href/",$comments)) {
  	$spam = true;
  } elseif (preg_match("/http:/",$comments)) {
  	$spam = true;
  } else {

	  /* It appears that a form was submitted.  Store the data. */
	  $filename = tempnam($dir,"entry");
	  if ($entry = fopen($filename,"w")) {
		fwrite($entry,"$sName\n");
		fwrite($entry,$HTTP_POST_VARS["relation"] . "\n");
		if ($HTTP_POST_VARS["showEmail"]=="on") {
			fwrite($entry,"*");
		} else {
			fwrite($entry,"-");
		}
		fwrite($entry,$HTTP_POST_VARS["email"] . "\n");
		fwrite($entry,"\n\n\n\n"); /* reserved*/
		fwrite($entry,$comments);
		fclose($entry);
	  }
   }
}

$deleteEntry = $HTTP_POST_VARS["delete"];
if ($deleteEntry != "") {
	if (substr($deleteEntry,0,5)=="entry") {
		$newname = "deleted" . substr($deleteEntry,5);
		rename($dir . "/" . $deleteEntry, $dir . "/" . $newname);
	} else {
		unlink($dir . "/" . $deleteEntry);
	}
}

$undelete = $HTTP_POST_VARS["undelete"];
if ($undelete!="") {
    $newname = "entry" . substr($undelete,7);
    rename($dir . "/" . $undelete,$dir . "/" . $newname);
}

function PrintHiddenFields()
{
	global $verbose,$admin,$all;
	if ($verbose) { ?> <input type="hidden" name="verbose" value="on"> <? }
	if ($admin)   { ?> <input type="hidden" name="admin"   value="on"> <? }
	if ($all)     { ?> <input type="hidden" name="all"     value="on"> <? }
}
?>
<html>
<head>
<title>Memorial Guest Book for Lyle Dale Chrisman (1910-2003)</title>
<style>
<!--
h1 { text-align : center ; font };
.h1s1 { color : green ; font-size : 80%}
.h1s2 {  }
.h1s3 { font-size : 80% }
.h1s4 { font-size : 60% }
-->
</style>
</head>
<body>
<h1><span class="h1s1">Guest Book For</span><br>
<span class="h1s2">Lyle Dale Chrisman</span><br>
<span class="h1s3">5 Nov 1910 - 19 Jul 2003</span><br>
<span class="h1s4">"Tough Old Bird"</span></h1>


<p>
Return to <a href="http://chrisman.org/LyleChrisman">Memorial Page</a>.
</p>

<?

if (false && $sName != "") {		/* LDC 1/10/2014 Disabled form processing */
  if ($spam) {
    ?>
    	<p><big>Submission rejected as likely spam.  URLs are not
    	accepted in message.</big></b>
    <?
  } else {
	?>
		<p>
		<big>Thank you signing our guest book!
		Your posting should now appear below.
		</big>
		</p>
		<p>
		If you'd like to post again, click <a href="guestBook.php">here</a>.
		</p>
	<?
  }
} elseif ($deleteEntry!="") {
?>
	<p>
	<big>Entry <?=$deleteEntry?> DELETED</big>
	</p>
<?
} else {
?>
<!-- Disabled form
<h2>Sign the Guest Book</h2>
<p>
If you knew Lyle, please sign the guest book and, if you feel
up to it, leave your thoughts, a memorable anecdote, or
other information about Lyle for other's to read.
Feel free to return and sign multiple times if you think of
something more to write.
</p>

<form name="signit" action="<?=$PHP_SELF?>" method="POST">
<table border="0">
<tr valign="top"><td>Your name (req):</td>
<td><input type="text" name="name" size="30"></td></tr>
<tr><td>Your email:</td><td><input type="text" name="email">&nbsp;&nbsp;
<input type="checkbox" name="showEmail">Show email on posting</td></tr>
<tr valign="top"><td>Relationship to Lyle (if any):</td>
<td><input type="text" name="relation"></td></tr>
<tr valign="top"><td>Comments,anecdotes,<br>stories, etc.:</td><td>
<textarea name="comments" cols="60" rows="10" >
</textarea></td></tr>
<tr><td>&nbsp;</td><td>
<input type="submit" value="Post">
</td></tr>
</table>
<? PrintHiddenFields() ?>
</form>
-->
<?
}
?>

<p>(The guestbook form for adding new entries has been disabled, due to excessive spam)</p>

<h2>What Others Have Written:</h2>

<?

function GetLine($fd)
{
	$l = str_replace("\\","",rtrim(fgets($fd,16000)));
	return htmlspecialchars($l);

}

$fdDir = opendir($dir);
$allfiles = array();
while ($f = readdir($fdDir)) {
  if (!is_dir($dir . "/" . $f)) {
  	if ($all or substr($f,0,5)=="entry") {
    	$allfiles{$f} = filemtime($dir . "/" . $f);
    }
  }
}
arsort($allfiles);
foreach ($allfiles as $f => $date) {
    if ($fd = fopen($dir . "/" . $f,"r")) {
    	?>
    	<hr>
    	<!--<?=$f?>-->
    	<? if ($verbose) { echo "$f<br>\n"; } ?>
    	<b>From:</b> <?=GetLine($fd,1024)?><br>
    	<b>Relation:</b> <?=GetLine($fd,1024)?><br>
    	<b>Date:</b> <?=date("dS F Y",$date)?><br>
    	<? $showEmail = fgetc($fd);
    	   $email = trim(GetLine($fd,1024));
    	   if ($verbose or $showEmail=="*") {
    	     ?><b>Email:</b> <a href="mailto:<?=$email?>"><?=$email?></a><br> <?
    	   }
    	/* reserved fields */
    	fgets($fd,1024);
    	fgets($fd,1024);
    	fgets($fd,1024);
    	fgets($fd,1024);
    	echo "<p>";
    	while (!feof($fd)) {
    		$l = GetLine($fd);
    		echo "$l<br>\n";
    	}
    	echo "</p>";
    	fclose($fd);
    	if ($admin) {
    		?>
    		  <form name="<?=$f?>" action="<?=$PHP_SELF?>" method="POST">
    		  <input type="submit" value="DELETE">
    		  <input type="hidden" name="delete" value="<?=$f?>">
    		  <? PrintHiddenFields(); ?>
    		  </form>
    		<?
    		if (substr($f,0,5)!="entry") { ?>
    		  <form name="<?=$f?>" action="<?=$PHP_SELF?>" method="POST">
    		  <input type="submit" value="UNDELETE">
    		  <input type="hidden" name="undelete" value="<?=$f?>">
    		  <? PrintHiddenFields(); ?>
    		  </form> <?
    		}
    	}
    	if (0 && $emailId != "" && strcasecmp($emailId,strtolower($email))==0) {
    		?><form name="edit" action="<?=$PHP_SELF?>" method="POST">
    		   <input type="submit" value="EDIT">
    		   <input type="hidden" value="edit" value="<?=$f?>">
    		   <? PrintHiddenFields(); ?>
    		   </form>
    		<?
    	}
    }
}
closedir($fdDir);
?>


<hr>
Page maintained by <a href="http://chrisman.org/Lonnie">Lonnie Chrisman</a><br>

<?
if ($verbose) { phpinfo(); }
?>

</body>
</html>

