<?


/*****************************************************************
*                                                                *
*  This is an OLD version of Big Sam.                            *
*  Please got to http://bigsam.gezzed.net to get a newer one.    *
*                                                                *
*                The Big Sam Team, bigsam@gezzed.net             *
*                                                                *
*****************************************************************/


// ***************************************************************
// *                                                             *
// *    BIG SAM   :   Built-In Guestbook / Stand-Alone Module    *
// *               Version 1.0.13 - July, 17th 2000              *
// *                                                             *
// * Copyleft 2000 Georges Zadrozynski & Rémi Lecoupé-Grainville *
// *             gezzed@atilla.org & rlg@atilla.org              *
// *                                                             *
// *         Homepage : http://www.atilla.org/bigsam             *
// *                Mail : bigsam@atilla.org                     *
// *                                                             *
// *     This source is under GNU/GPL General Public License     *
// *                                                             *
// ***************************************************************

$version "1.0.13";

// $adminpassword is the password which will be asked for any administration
// actions. This password could be changed only if in admin mode (old 
// $adminpassword matches $admin).
// Take care : The beginning of the line should look like "$adminpassword ="
// or "$adminpassword=" (No spaces, no tabs, etc...)
$adminpassword "bigsam";
// $myself is the name of the current script (this file) and which is going to
// be modified by itself. This means that (theoretically) this file can be 
// placed anywhere on the system, with any filename.
$myself  $SCRIPT_FILENAME;
$myself2 $SCRIPT_NAME;

if (
$SERVER_PORT=="80"$BASEURL="http://$SERVER_NAME";
else 
$BASEURL="http://$SERVER_NAME:$SERVER_PORT";

// Compatibility for Win32 -> not tested
if (strpos($SERVER_SOFTWARE"Win32")) 
    {
    
$myself$PATH_TRANSLATED;
    
$myself2 $PATH_INFO;
    }



// Number of fields to be displayed simultaneously.
// To display everything, set $display_step to 0.
$display_step 20;
// Beginning of the data to be displayed, default = 1
if ($display_begin=="") { $display_begin 1; }


// Aspect of the script : Colors
// Choosing colors 
/*
    // Standard (Greys, bright background)
    $color_white    = "\"#FFFFFF\"";
    $color_black    = "\"#000000\"";
    $color_heads    = "\"#666666\"";
    $color_body1    = "\"#EEEEEE\"";
    $color_body2    = "\"#DDDDDD\"";
*/
// The Following is the ATILLA-type based model
$color_white    "\"#FFFFFF\"";
$color_black    "\"#000000\"";
$color_heads    "\"#42426F\"";
$color_body1    "\"#FFF8DC\"";
$color_body2    "\"#EFE8CC\"";

// Data submission form colors
$formheadcolor    $color_heads;
$formbodycolor    $color_body1;
$formtailcolor    $color_body2;
$formheadtext     $color_white;
$formbodytext     $color_black;
// Guestbook entries colors
$entryheadcolor   $color_heads;
$entrybodycolor1  $color_body2;
$entrybodycolor2  $color_body1;
$entryheadtext    $color_white;
$entrybodytext1   $color_black;
$entrybodytext2   $color_heads;
// Messages colors
$adminheadcolor   $color_heads;
$adminheadtext    $color_white;
$adminbodycolor1  $color_body2;
$adminbodytext1   $color_black;
$adminbodycolor2  $color_body1;
$adminbodytext2   $color_heads;


// Self regeneration of the script (The Rémi's ultime bricolage !)

$extension=substr($myselfstrrpos($myself".")+1);


if (
strpos($myself"_temporary")!=false
    {
    
$thename=substr($myself0strrpos($myself"_temporary"));
    
$thename2=substr($myself20strrpos($myself2"_temporary"));

    
$mainscript="${thename}.$extension";
    
$mainscript2="${thename2}.$extension";
    
$tempscript="${thename}_temporary.$extension";
    
$tempscript2="${thename2}_temporary.$extension";

    if (!
strpos($SERVER_SOFTWARE"Win32")) unlink($mainscript);
    
copy($tempscript,$mainscript);
    
chmod"$mainscript"0666 ); // Diabolic !

    // deleting the inuse file
    
if (!strpos($SERVER_SOFTWARE"Win32")) unlink("${thename}_inuse.$extension");
    
    
header("Location: $BASEURL$mainscript2");
    exit;
    }

$thename=substr($myself0strrpos($myself"."));
$thename2=substr($myself20strrpos($myself2"."));

$mainscript="${thename}.$extension";
$mainscript2="${thename2}.$extension";
$tempscript="${thename}_temporary.$extension";
$tempscript2="${thename2}_temporary.$extension";


if (
file_exists($tempscript) && !file_exists("${thename}_inuse.$extension")) 
{
    if (
filemtime($tempscript)>filemtime($mainscript))
    {
        
header("Location: $BASEURL$tempscript2");
        exit;
    }
    else if (!
strpos($SERVER_SOFTWARE"Win32"))
    {
        
unlink($tempscript);
    }
}


//******************************************************************************

// This place is where all the data are archived, before being used by the 
// miscellaneous cases.

$index 0;

// Here are automatically filled data arrays :

//#DATABEGIN
$archive_name[++$index] = "The Big Sam Team";
$archive_mail[  $index] = "bigsam@atilla.org";
$archive_date[  $index] = "17th July 2000 - 12:00:00 AM";
$archive_text[  $index] = "Thank you for using Big Sam v.$version !";
//#DATAEND

// From now, $index will indicate the number of data entries.

//******************************************************************************
// function displayform : Display the form which enables the client to type a
// message.

function displayform()
{
    global 
$mainscript2;
    
$borderthickness 3;

    global 
$formheadcolor;
    global 
$formbodycolor;
    global 
$formtailcolor;
    global 
$formheadtext;
    global 
$formbodytext;

    
// Writing HTML Code
    
echo ("<center>\n");
    echo (
"<form method=\"POST\" action=\"$mainscript2\"><table border=0 cellspacing=0 cellpadding=3>\n");
    echo (
"<tr bgcolor=$formheadcolor>\n");
    echo (
"<td><font color=$formheadtext><b>Name :</b></font><input name=\"name\" size=20></td>\n");
    echo (
"<td>&nbsp;</td>\n");
    echo (
"<td align=right><font color=$formheadtext><b>E-mail :</b></font><input name=\"email\" size=20></td>\n");
    echo (
"</tr>\n");
    echo (
"<tr bgcolor=$formbodycolor><td colspan=3>\n");
    echo (
"<font color=$formbodytext><b>Message :</b></font><br>\n");
    echo (
"<textarea name=\"message\" cols=60 rows=7></textarea>\n");
    echo (
"</td></tr>\n");
    echo (
"<tr bgcolor=$formtailcolor><td colspan=3 align=\"right\">\n");
    echo (
"<input type=\"submit\" value=\"Send the message\">");
    echo (
"</td></tr>\n");
    echo (
"</table>\n");
    echo (
"</form>\n");
    echo (
"</center>\n");
}

//******************************************************************************
// function displayerror displays the $errormessage error message.

function displayerror($errormessage,$msgtype)
{
    global 
$adminheadcolor;
    global 
$adminheadtext;
    global 
$adminbodycolor1;
    global 
$adminbodytext1;
    global 
$adminbodycolor2;
    global 
$adminbodytext2;

    
displayheader();
    echo (
"<table width=\"100%\" height=\"100%\"><tr><td width=\"100%\" height=\"100%\" valign=\"middle\" align=\"center\">");
    echo (
"<table cellspacing=0 border=0 width=250><tr><td bgcolor=$adminheadcolor>");
    echo (
"<font Color=$adminheadtext size=-3>&nbsp; BIG SAM Guestbook</font><br>");
    echo (
"</td></tr><tr><td bgcolor=$adminbodycolor1><font color=$adminbodytext>");
    if (
$msgtype=="1"// Admin Message
        
echo ("<i>&nbsp; Administrator Error</i>");
    if (
$msgtype=="2"// System Message
        
echo ("<i>&nbsp; System Error</i>");
    echo (
"</font>");
    echo (
"</td></tr><tr><td bgcolor=$adminbodycolor2>");
    echo (
"<font color=$adminbodytext2>");
    echo (
"<br><center><b>$errormessage</b></center><br>");
    echo (
"</font></td></tr></table><p>&nbsp;</p>\n");
    echo (
"</td></tr></table>");
    
displayfooter();
    exit;
}

//******************************************************************************
// function displayheader and displayfooter displays the header/footer for your page

function displayheader()
{
    global 
$version;
    echo (
"<!-- BIG SAM v.$version - Built-In Guestbook / Stand-Alone Module - http://www.atilla.org/bigsam -->\n");
    
// You migh use and include instead of all this :
    // include ("head.inc");
    
echo ("<html>\n");
    echo (
"<body bgcolor=\"#FFFFFF\">\n");
}

function 
displayfooter()
{
    echo (
"</body></html>");
}

//******************************************************************************
// function standardize_text filters ambiguous chains in $str

function standardize_text($str)
{
    
$str str_replace("<""&lt;"$str);
    
$str str_replace(">""&gt;"$str);
    
$str strip_tags($str);
    
$str str_replace("\'""'"$str);
    
$str str_replace("$""&#036;"$str);
    
$str str_replace("\r\n""<br>"$str);
    return 
$str;
}

//******************************************************************************

if ($admin=="") {
if (
$name=="" || $message=="") {

displayheader();

// Use Case no 1-A
// This case is encountered when a client simply wants to add a message to the
// guestbook. (Every variable is empty);

// Displays the entry form to enable the client to type a message.
displayform();
echo (
"<p>&nbsp;</p>\n");

// Message arrays are displayed here :

echo "<center>\n";

// Displaying all the entries.

if ($display_step==0)
    {
    
$display_begin=1;
    
$display_step=$index;
    }

for (
$i=$display_begin ; ($i<=($display_begin+$display_step-1)&&($i<=$index)) ; $i++)
    {
    
$msgnumber $index $i 1;
    echo (
"<table cellspacing=0 border=0 width=600><tr><td bgcolor=$entryheadcolor>");
    echo (
"<font color=$entryheadtext size=-3>&nbsp; &nbsp; $archive_date[$i] &nbsp; &nbsp;</font>");
    echo (
"</td></tr><tr><td bgcolor=$entrybodycolor1>");
    echo (
"<font Color=$entrybodytext1><b>");
    if (
$archive_mail[$i]!="")
        {
        echo (
"$msgnumber : <a href=\"mailto:$archive_mail[$i]\">$archive_name[$i]</a>");
        }
    else 
        {
        echo (
"$msgnumber : $archive_name[$i]");
        }
    echo (
"</b></font>");
    echo (
"</td></tr><tr><td bgcolor=$entrybodycolor2>");
    echo (
"<font color=$entrybodytext2>");
    echo (
"<b>$archive_text[$i]</b>");
    echo (
"</font></td></tr></table><br><br>\n");
    }


echo (
"<p>&nbsp;</p>\n");

$nextindex $display_begin $display_step;
$previndex $display_begin $display_step;
$prevEnd   $index $previndex 1;
$prevBegin $index $previndex $display_step +2;
$nextEnd   $index $nextindex 1;
$nextBegin $index $nextindex $display_step +2;
if (
$nextBegin<1$nextBegin 1;

if (
$nextindex<$index)     echo ("<a href=\"${myself2}?display_begin=$nextindex\"><tt>[</tt>$nextBegin-$nextEnd<tt>]</tt></a>\n");
if (
$previndex>0)     echo ("<a href=\"${myself2}?display_begin=$previndex\"><tt>[</tt>$prevBegin-$prevEnd<tt>]</tt></a>\n");

echo 
"</center>\n";
displayfooter();
exit;

//******************************************************************************

} else {

// Use Case no 1-B
// Case is encountered when a client has just filled in the appropriate fields
// (name, mail, message).
//    $name    = "Some Name"
//    $email   = "Some@Email"
//    $message = "Some Message"
//        Simply add message in the top of all previous messages.

// to do : adding message to this file, then redirecting to myself


// If the inuse file exists, someone else is modifying this guestbook
// You can't continue unless it's in use for more than 5 sec
if (file_exists("${thename}_inuse.$extension"))
{
    if (
time()<filemtime("${thename}_inuse.$extension")+5)
    {
        
displayerror("This GuestBook is in use !<br>Please try again...",2);
    }
    else if (!
strpos($SERVER_SOFTWARE"Win32"))
    {
        
unlink("${thename}_inuse.$extension");
        if (
file_exists($tempscript)) unlink($tempscript);
    }
}


$fout fopen("${thename}_inuse.$extension""w");fclose($fout);


$fin  fopen($mainscript"r");
$fout fopen($tempscript"w");
if (
$fout<|| $fin<0
    {
    
displayerror("Error opening files !<br>Be sure writing is allowed in this directory.",2);
    }

$line "";
for(
$i=$i<100000 && strpos($line"#DATABEGIN")==false $i++)
    {
    
$line=fgets($fin10000);
    
fputs($fout$line);
    }

$insert_msgdate date"dS F Y - h:i:s A" );

$name=standardize_text($name);
$email=standardize_text($email);
$message=standardize_text($message);

fputs ($fout,"\$archive_name[++\$index] = \"$name\";\n");
fputs ($fout,"\$archive_mail[  \$index] = \"$email\";\n");
fputs ($fout,"\$archive_date[  \$index] = \"$insert_msgdate\";\n");
fputs ($fout,"\$archive_text[  \$index] = \"$message\";\n");

for(
$i=$i<10000 && !feof($fin) ; $i++) 
    {
    
$line=fgets($fin10000);
    
fputs($fout$line);
    }
fclose($fin);
fclose($fout);

header("Location: $BASEURL$tempscript2");
exit;

//******************************************************************************

}} else {

if (
$admin==$adminpassword) {
if (
$command=="") {

displayheader();

// Use Case no 2-A
// Admin mode (The administrator can modify password, fields, or remove
// messages).
//    $admin   = $adminpassword
//        Shows a password modification form.
//        Shows all messages and enables to modify/delete them.

echo("<center><h2>BIG SAM</h2><h4>Guestbook Administration</h4></center>\n");
echo(
"<br>\n");


// Display the form which enables the admin to change his password
echo ("<center><table cellspacing=0 border=0>\n");
echo (
"<form method=\"POST\" action=\"$mainscript2\">\n");
echo (
"<tr><td bgcolor=$adminheadcolor colspan=2><font color=$adminheadtext>\n");
echo (
"&nbsp;<b>Change <i>Big Sam</i> Password</b>.</font></td></tr>\n");
echo (
"<tr><td align=right bgcolor=$adminbodycolor2>");
echo (
"<font color=$adminbodytext2>&nbsp; New Password :</font></td>\n");
echo (
"<td align=left bgcolor=$adminbodycolor2>");
echo (
"<input type=\"password\" name=\"newpassword1\">&nbsp;</td></tr>\n");
echo (
"<tr><td align=\"right\" bgcolor=$adminbodycolor2>");
echo (
"<font color=$adminbodytext2>&nbsp; Retype new Password :</font></td>\n");
echo (
"<td align=left bgcolor=$adminbodycolor2>");
echo (
"<input type=\"password\" name=\"newpassword2\">&nbsp;</td></tr>\n");
echo (
"<tr><td bgcolor=$adminbodycolor1 colspan=2 align=\"center\">\n");
echo (
"<input type=\"hidden\" name=\"admin\" value=\"$admin\">\n");
echo (
"<input type=\"hidden\" name=\"command\" value=\"passwd\">\n");
echo (
"<input type=\"submit\" value=\"Change Password\"></td></tr>\n");
echo (
"</form>\n");
echo (
"</tr></table></center>\n");

echo(
"<br><hr><br>\n");

// Message arrays are displayed here in forms in order to modify them :

echo ("<form method=\"POST\" action=\"$mainscript2\"><center>\n");

for (
$i=$i<=$index $i++)
    {
    
$str str_replace("<br>""\r\n"$archive_text[$i]);
    
$msgnumber $index $i 1;
    echo (
"<table cellspacing=0 cellpadding=3><tr><td colspan=4 bgcolor=$adminheadcolor>\n");
    echo (
"<font color=$adminheadtext><b>&nbsp;");
    echo (
"Entry #$msgnumber</b>.</font>&nbsp;</td></tr>\n");
    echo (
"<tr bgcolor=$adminbodycolor2>\n");
        echo (
"<td>&nbsp;<input type=\"checkbox\" name=\"keep$i\" checked>&nbsp;</td>\n"); 
    echo (
"<td><input name=\"name$i\" value=\"$archive_name[$i]\"></td>\n");
    echo (
"<td><input name=\"email$i\" value=\"$archive_mail[$i]\"></td>\n");
    echo (
"<td><input name=\"date$i\" value=\"$archive_date[$i]\"></td>\n");
    echo (
"<tr bgcolor=$adminbodycolor1>\n");
    echo (
"<td colspan=4><textarea cols=60 rows=2 name=\"message$i\">$str");
    echo (
"</textarea></td>\n</tr></table>\n<br>\n");
    }
echo (
"<input type=\"hidden\" name=\"admin\" value=\"$admin\">\n");
echo (
"<input type=\"hidden\" name=\"index\" value=\"$index\">\n");
echo (
"<input type=\"hidden\" name=\"command\" value=\"modify\">\n");
echo (
"<input type=\"submit\" value=\"Submit Modifications\">\n");
echo (
"</form></center>\n");
displayfooter();
exit;


//******************************************************************************


else if (
$command=="passwd")
{
// Use Case no 2-B
// Admin mode (The administrator is modifying his password.).
//    $admin        = $adminpassword
//    $newpassword1 = "New Password 1"
//    $newpassword2 = "New Password 2"
//         Check if $newpassword1 matches $newpassword2
//         Set the $adminpassword line to :
//         $adminpassword = "$newpassword1" 

if($newpassword1!=$newpassword2)
    {
    
// The passwords doesn't match
    
displayerror("The new passwords don't match !",1);
    }
else
    {

    
// If the inuse file exists, someone else is modifying this guestbook
    // You can't continue
    
if (file_exists("${thename}_inuse.$extension")) displayerror("This GuestBook is in use !<br>Please try again...",2);
    
$fout fopen("${thename}_inuse.$extension""w");fclose($fout);

    
$fin  fopen($mainscript"r");
    
$fout fopen($tempscript"w");
    if (
$fout<|| $fin<0
        {
        
displayerror("Error opening files !<br>Be sure writing is allowed in this directory.",2);
        }
    
$line "";
    for(
$i=$i<10000 && !feof($fin); $i++)
            {
        
$line=fgets($fin10000);
        
// Seek the "$adminpassword =" line
        
if ((substr($line016)=="\$adminpassword =")||(substr($line015)=="\$adminpassword="))
            {
            
// Replace by the new password
            
$newpassword1 standardize_text($newpassword1);
            
fputs ($fout,"\$adminpassword = \"$newpassword1\";\n");
            }
        else
            {
            
fputs($fout$line);
            }
        }
    
fclose($fin);
    
fclose($fout);
    
header("Location: $BASEURL$tempscript2");
    exit;
    }
    
//******************************************************************************

} else if ($command=="modify") {

// Use Case no 2-C

// Admin mode (The administrator is updating entries).
//    $admin      = $adminpassword
//    $nameXXX    = "Modified name (no XXX)"
//    $emailXXX   = "Modified email (no XXX)"
//    $messageXXX = "Modified message (no XXX)"
//    $keep       = "on" or ""
//         Check if $admin matches $adminpassword.
//         Rewrites ALL the fields except when $keepXXX = ""

// If the inuse file exists, someone else is modifying this guestbook
// You can't continue
if (file_exists("${thename}_inuse.$extension")) displayerror("This GuestBook is in use !<br>Please try again...",2);
$fout fopen("${thename}_inuse.$extension""w");fclose($fout);

$fin  fopen($mainscript"r");
$fout fopen($tempscript"w");
if (
$fout<|| $fin<0)
    {
    
displayerror("Error opening files !<br>Be sure writing is allowed in this directory.",2);
    }
    
// Writes all the script lines until "#DATABEGIN"
$line "";
for(
$i=$i<100000 && strpos($line"#DATABEGIN")==false $i++)
    {
    
$line=fgets($fin10000);
    
fputs($fout$line);
    }

for (
$i=$i<=$index $i++)
    {
    
// Get values from the form, and standardize them
    
$variable="name$i";
    
$name=standardize_text("${$variable}");
    
$variable="email$i";
    
$email=standardize_text("${$variable}");
    
$variable="date$i";
    
$insert_msgdate=standardize_text("${$variable}");
    
$variable="message$i";
    
$message=standardize_text("${$variable}");
    
    
$variable="keep$i";
    if (${
$variable}!="")
        {
        
fputs ($fout,"\$archive_name[++\$index] = \"$name\";\n");
        
fputs ($fout,"\$archive_mail[  \$index] = \"$email\";\n");
        
fputs ($fout,"\$archive_date[  \$index] = \"$insert_msgdate\";\n");
        
fputs ($fout,"\$archive_text[  \$index] = \"$message\";\n");
        }
    }

// Seeks for the "#DATAEND" line, and skips all the data
$line "";
for(
$i=$i<1000 && strpos($line"#DATAEND")==false $i++)
    {
    
$line=fgets($fin10000);    
    }
    
fputs($fout$line);
    
// Writes the rest of the script  
for($i=$i<10000 && !feof($fin) ; $i++) 
    {
    
$line=fgets($fin10000);
    
fputs($fout$line);
    }
fclose($fin);
fclose($fout);

header("Location: $BASEURL$tempscript2");
exit;
}

//******************************************************************************

} else {

// Use Case no 2-Z
// If $admin does NOT match $adminpassword.
//    $admin   != $adminpassword
//        Error message : "Wrong Admin Password"

displayerror("Wrong Admin Password",1);

}}

//******************************************************************************

?>