<?

    
require_once "include/header.php";

    
define("GBOOKFILE""../guestbook.txt");

function 
format_link($link$name)
{
    
$val=str_replace("::"":"$link);

    if (
trim($link) == '')
    {
        return 
$name;
    }
    if (
preg_match('/[A-Za-z0-9.-_]+@[A-Za-z0-9\\-.]+\\.[A-Za-z]+/'$val))
    {
        return 
"<A HREF=\"mailto:$val\">$name</A>";
    }
    elseif (
preg_match('/(http|ftp)\\:\\/\\/[A-Za-z0-9._\\-#%\\/]+/'$val))
    {
        return 
"<A HREF=\"$val\">$name</A>";
    }
    elseif (
preg_match('/www.[A-Za-z0-9._\\-#%\/]+/'$val))
    {
        return 
"<A HREF=\"http://".urlencode($val)."\">$name</A>";
    }
    else
    {
        return 
"<A HREF=\"javascript:alert('".str_replace('\'''\\\''str_replace('"''\'\''$link))."');\">$name</A>";
    }

}

if (
$post != '' || $name != '' || $link != '')
{
    
$name=trim($name);
    
$post=trim($post);
    
$link=trim($link);

    
$accept=true;

    if (
$name == '')
    {
        print 
"<FONT COLOR=\"red\"><B>You failed to enter a name</B></FONT><BR>\n";
        
$accept=false;
    }
    if (
strlen($name) > 25)
    {
        print 
"<FONT COLOR=\"red\"><B>Your name was excessively long.</B></FONT><BR>\n";
        
$accept=false;
    }
    if (
$post == '')
    {
        print 
"<FONT COLOR=\"red\"><B>You must enter a post.</B></FONT><BR>\n";
        
$accept=false;
    }
    if (
strlen($post) > 5000)
    {
        print 
"<FONT COLOR=\"red\"><B>Your post was excessively long.</B></FONT><BR>\n";
        
$accept=false;
    }
    if (
strlen($post) > 200)
    {
        print 
"<FONT COLOR=\"red\"><B>Your link was excessively long.</B></FONT><BR>\n";
        
$accept=false;
    }

    if (
$accept == true)
    {
        
$str=htmlspecialchars(stripslashes($name)).":".str_replace(":""::"$link).":".time().":".str_replace("\n""<BR>"str_replace("\r""<BR>"str_replace("\r\n""<BR>"htmlspecialchars(stripslashes($post)))))."\n";

        
$f=fopen(GBOOKFILE"a");

        if (
$f)
        {
            if (!(
fwrite($f$str) and fflush($f) and fclose($f)))
            {
                print 
"<FONT COLOR=\"red\"><B>Couldn't write post.</B></FONT><BR>\n";
            }
            else
            {
                
$post='';
                
$name='';
                
$link='';
            }
        }
        else print 
"<FONT COLOR=\"red\"><B>Could not open guestbook for appending.</B></FONT><BR>\n";

    }


}

$gbook=array();    //preinit
$gbook=file(GBOOKFILE);

?>
<TABLE BORDER=0 WIDTH=100% CELLSPACING=0 CELLPADDING=0>
<TR BGCOLOR="black"><TD COLSPAN=2 HEIGHT=1></TD></TR>
<TR><TD>
<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=5>
<?

$color
=false;

foreach (
$gbook as $p)
{
    if (
preg_match("/([^:]+):((?:\:\:|[^:])*):([0-9]+):(.+)/"$p$matches))
    {

        
$color=!$color;

        
$href=format_link($matches[2], $matches[1]);
?>
<TR BGCOLOR="<?=(($color) ? "#F4E9FD" "#FAF5FF")?>"><TD WIDTH=150 VALIGN="TOP"><B><?=$href?></B><BR><FONT SIZE=1><?=strftime("%I:%M%p %d %B %Y"intval($matches[3]))?></FONT></TD><TD><?=$matches[4]?></TD></TR>
<?
    
}
}

?>
</TABLE></TD></TR>
<TR BGCOLOR="black"><TD COLSPAN=2 HEIGHT=1></TD></TR>
</TABLE>

<BR><BR>
<H2 ALIGN="CENTER">Sign this guestbook</H2>
<P ALIGN="CENTER">
<TABLE WIDTH=500>
<FORM ACTION="gbook.php" METHOD="POST">
<TR ALIGN="LEFT"><TD>Name</TD><TD><INPUT NAME="name" VALUE="<?=$name?>"></TD></TR>
<TR ALIGN="LEFT"><TD>Website or Email</TD><TD><INPUT NAME="link" VALUE="<?=$link?>"></TD></TR>
<TR ALIGN="LEFT"><TD COLSPAN=2>Post:<BR><TEXTAREA NAME="post" ROWS=3 COLS=50><?=$post?></TEXTAREA></TD></TR>
<TR ALIGN="LEFT"><TD><TD ALIGN="RIGHT"><INPUT TYPE="submit" VALUe="Sign"></TD></TR>
</FORM>
</TABLE>
</P>
<?

    
require_once "include/footer.php";

?>