<?

// ***************************************************************
// *                                                             *
// *    BIG SAM   :   Built-In Guestbook / Stand-Alone Module    *
// *             Version 1.0.04 - March, 23th 2000               *
// *                                                             *
// * Copyleft 2000 Georges Zadrozynski & Rémi Lecoupé-Grainville *
// *        Gezzed@atilla.eisti.fr & RLG@atilla.eisti.fr         *
// *                                                             *
// *     This source is under GNU/GPL General Public License     *
// *                                                             *
// ***************************************************************

// $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).
//#ADMINPASSWD
$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;


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


// Self regeneration of the script


if (strpos($myself"_temporary")!=false
    {
    
$mainscript=sprintf("%s.php3"substr($myself0strpos($myself"_temporary")));
    
$mainscript2=sprintf("%s.php3"substr($myself20strpos($myself2"_temporary")));
    
$tempscript=$myself;
    
$tempscript2=$myself2;
    
unlink($mainscript);
    
copy($tempscript,$mainscript);
    
chmod"$mainscript"0666 ); // Diabolic !
    
header("Location: http://$SERVER_NAME$mainscript2");
    }
$mainscript=$myself;
$mainscript2=$myself2;
$tempscript=sprintf("%s_temporary.php3"substr($myself0strpos($myself".php3")));
$tempscript2=sprintf("%s_temporary.php3"substr($myself20strpos($myself2".php3")));
if (
file_exists($tempscript)) 
    {
    if (
filemtime($tempscript)>filemtime($mainscript)) header("Location: http://$SERVER_NAME$tempscript2");
    else 
unlink($tempscript);
    }



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

// This place rules ! It's 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] = "Gezzed";
$archive_mail[  $index] = "gezzed@atilla.eisti.fr";
$archive_date[  $index] = "Wednesday 22nd of March 2000 02:31:59 PM";
$archive_text[  $index] = "Ceci est un ultime test !";
//#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;

// Choosing colors (The Following is the ATILLA-type based model)
$formbordercolor "#FFFFFF";
$formheadcolor   "#42426F";
$formbodycolor   "#FFF8DC";
$formtailcolor   "#EFE8CC";
$formheadtext    "#FFFFFF";
$formbodytext    "#000000";
$borderthickness 3;


/*
// Choosing colors (The Following is the standard-type based model)
$formbordercolor = "#FFFFFF";
$formheadcolor   = "#FFFFFF";
$formbodycolor   = "#FFFFFF";
$formtailcolor   = "#FFFFFF";
$formheadtext    = "#000000";
$formbodytext    = "#000000";
$borderthickness = 3;
*/


// Writing HTML Code
echo ("<CENTER>\n");
echo (
"<FORM METHOD=POST ACTION=\"$mainscript2\"><TABLE border=0 cellspacing=0 cellpadding=3>\n");
echo (
"<TR bgcolor=$formbordercolor><TD colspan=5 height=$borderthickness><font size=1>&nbsp;</font></TD></TR>\n");
echo (
"<TR bgcolor=$formheadcolor>\n");
echo (
"<TD rowspan=5 bgcolor=$formbordercolor width=$borderthickness>&nbsp;</TD>\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 (
"<TD rowspan=5 bgcolor=$formbordercolor width=$borderthickness>&nbsp;</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 (
"<TR bgcolor=$formbordercolor><TD colspan=5 height=$borderthickness><font size=1>&nbsp;</font></TD></TR>\n");
echo (
"</TABLE>\n");
echo (
"</FORM>\n");
echo (
"</CENTER>\n");
}

function 
displayerror($errormessage)
{
echo (
"<table width=100% height=100%><tr><td width=100% height=100% valign=middle align=center>");

// Setting up colors to display errors.
$errorheadcolor   "#42426F";
$errorbodycolor1  "#EFE8CC";
$errorbodycolor2  "#FFF8DC";
$errorheadtext    "#FFFFFF";
$errorbodytext1   "#000000";
$errorbodytext2   "#42426F";

    echo (
"<table cellspacing=0 border=0 width=250><tr><td bgcolor=$errorheadcolor>");
    echo (
"<font Color=$errorheadtext size=-3>BIG SAM Guestbook</font><br>");
    echo (
"</td></tr><tr><td bgcolor=$errorbodycolor1><Font Color=$errorbodytext>");
    echo (
"<i>Administrator Error</i>");
    echo (
"</Font>");
    echo (
"</td></tr><tr><td bgcolor=$errorbodycolor2>");
    echo (
"<Font Color=$errorbodytext2>");
    echo (
"<br><center><B>$errormessage</B></center><BR>");
    echo (
"</Font></td></tr></table><P>&nbsp;</P>\n");

echo (
"</td></tr></table>");
exit;
}

function 
displayheader()
{
echo (
"<HTML>\n");
echo (
"<BODY bgcolor=#ffffff>\n");
}


function 
standardize_text($str)
{


$str strip_tags($str);
$str str_replace("\'""'"$str);
$str str_replace("\r\n""<br>"$str);

//test
//$str=htmlentities($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();

// Message arrays are displayed here :

echo "<center>\n";

// Setting up colors to display entries.
$entryheadcolor   "#42426F";
$entrybodycolor1  "#EFE8CC";
$entrybodycolor2  "#FFF8DC";
$entryheadtext    "#FFFFFF";
$entrybodytext1   "#000000";
$entrybodytext2   "#42426F";

// Displaying all the entries.
for ($i=$i<=$index $i++)
    {
    
$msgnumber $index $i 1;
    echo (
"<table cellspacing=0 border=0><tr><td bgcolor=$entryheadcolor>");
    echo (
"<font Color=$entryheadtext size=-3>$archive_date[$i]</font><br>");
    echo (
"</td></tr><tr><td bgcolor=$entrybodycolor1><Font Color=$entrybodytext><B>");
    if (
$archive_mail[$i]!="") echo ("$msgnumber : <A HREF=\"mailto:$archive_mail[$i]\">$archive_name[$i]</A>");
    else  echo (
"$msgnumber : $archive_name[$i]");
    echo (
"</Font>");
    echo (
"</td></tr><tr><td bgcolor=$entrybodycolor2>");
    echo (
"<Font Color=$entrybodytext2>");
    echo (
"<B>$archive_text[$i]</B><BR>");
    echo (
"</Font></td></tr></table><P>&nbsp;</P>\n");
    }

echo 
"</center>\n";

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

} 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

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

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

$insert_msgdate date"l dS of 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: http://$SERVER_NAME$tempscript2");

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

}} 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.
//                            (Good luck Remi !)

echo("this doesnt work yet<br>\n");
echo(
"<center><table cellspacing=0 border=0>\n");
echo(
"<form>\n");
echo(
"<tr><td bgcolor=#654321 colspan=2><font color=#ffffff>\n");
echo(
"&nbsp;<b>Change <i>Big Sam</i> Password</b>.</font></td></tr>\n");
echo(
"<tr><td align=right bgcolor=#FEDCBA>&nbsp; New Password :</td>\n");
echo(
"<td align=left bgcolor=#FEDCBA><input type=password name=newpassword1>&nbsp;</td></tr>\n");
echo(
"<tr><td align=right bgcolor=#FEDCBA>&nbsp; Retype new Password :</td>\n");
echo(
"<td align=left bgcolor=#FEDCBA><input type=password name=newpassword2>&nbsp;</td></tr>\n");
echo(
"<tr><td bgcolor=#FEDCCA colspan=2 align=center>\n");
echo(
"<input type=hidden name=admin value=$admin>\n");
echo(
"<input type=hidden name=command value=change_password_value>\n");
echo(
"<input type=submit value=\"Change Password\"></td></tr>\n");
echo(
"</form>\n");
echo(
"</tr></table></center>\n");


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

echo ("<FORM>\n");

for (
$i=$i<=$index $i++)
    {
    
$str str_replace("<br>""\r\n"$archive_text[$i]);

    
$msgnumber $index $i 1;
    
//echo ("$msgnumber : $archive_date[$i] - $archive_name[$i] (<a href=\"mailto:$archive_mail[$i]\">$archive_mail[$i]</a>) <br>$archive_text[$i]<br><br>\n");
        
echo ("<B>$msgnumber</B>&nbsp;<input type=checkbox name=delete$i>\n"); 
    echo (
"<input name=name$i value=\"$archive_name[$i]\">\n");
    echo (
"<input name=email$i value=\"$archive_mail[$i]\"><br>\n");
    echo (
"<textarea cols=60 rows=2 name=message$i>$str</textarea><br>\n");
    echo (
"<br>");
    }

echo (
"</FORM>\n");


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

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


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



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

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


// Use Case no 2-C
// Admin mode (The administrator is deleting entries).
//    $admin   = $adminpassword
//    $deleteXXX = 1
//         Check if $admin matches $adminpassword.
//         Loop XXX from 0 to maximum, and delete when $deleteXXX = 1



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

// Use Case no 2-D
// Admin mode (The administrator is updating entries).
//    $admin      = $adminpassword
//    $update     = true
//    $nameXXX    = "Modified name (no XXX)"
//    $emailXXX   = "Modified email (no XXX)"
//    $messageXXX = "Modified message (no XXX)"
//         Check if $admin matches $adminpassword.
//         Rewrites ALL the fields.


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

}} else {


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

displayheader();
displayerror("Wrong Admin Password");


//******************************************************************************
}}


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


?>
</body>
</html>