<?
session_start();
$filename = "vietboy.csv";
$messages = array();
//first do the read
if (isset($_GET['admin'])):
loginform();
exit;
elseif(isset($_POST['login'])):
$name="user";
$pwd = "password";
if (($_POST['name'] === $name) && ($_POST['pwd'] === $pwd)):
$_SESSION['loggedin'] = TRUE;
adminpage();
endif;
exit;
elseif (isset($_SESSION['loggedin'])):
if (isset($_POST['archive'])):
archive();
endif;
unset($_SESSION['loggedin']);
unset($_POST);
header("Location:".$_SERVER['PHP_SELF']);
elseif (isset($_GET['logout'])):
unset($_SESSION['loggedin']);
header("Location:".$_SERVER['PHP_SELF']);
exit;
elseif(isset($_POST['submit'])):
writeshout();
unset($_POST);
header("Location:".$_SERVER['PHP_SELF']);
else:
shoutbox();
endif;
function doctype() { ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Vietboy's Shout Box</title>
<style type="text/css">
/*General styles*/
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
color:#999999;
}
/*layout styles */
div.formcontainer {
width: 460px;
padding: 5px;
margin: 0px auto;
background-color:#CCFFCC;
}
/* styles for the form */
div.row {
clear: both;
padding-top: 10px;
}
div.row span.label {
float: left;
width: 100px;
text-align: right;
}
div.row span.formw {
float: right;
width: 335px;
text-align: left;
}
div.row textarea {
width:95%;
height: 10ex;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
color:#006699;
}
div.row .textbox {
width:95%;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
color:#006699;
}
form .shoutboxfieldset {
border:1px dashed #CCCCCC;
}
form .formbutton {
background-color:#ccffcc;
border: 1px dashed #CCCCCC;
color: #FF0000;
cursor:pointer;
}
form legend {
font-size:larger;
font-weight:bold;
color:#FF0000;
}
/* end formstyles */
/*content*/
.contentwrapper {
width: 460px;
padding: 5px;
margin: 0px auto;
background-color:#CCFFCC;
margin-top: 50px;
}
.messagesectiontitle {
font-size:larger;
font-weight:bold;
color:#FF0000;
}
.freshmessage, .inthelastweek, .intehlastmonth, .veryold {
margin-top:5px;
border-width: 1px;
border-style:dashed;
}
.freshmessage {
}
.inthelastweek {
background-color:#99CCCC;
}
.inthelastmonth {
background-color:#99CCFF;
}
.veryold {
background-color:#33CCFF;
}
.incontentmessagetitle {
font-weight:bold;
}
.message {
}
</style>
</head>
<? }
function shoutbox () {
global $filename;
$fh = fopen($filename, "rt") or die("cannot open the file for reading");
while (($messages[] = fgetcsv($fh, 2048)) !== FALSE):
//do nothing
endwhile;
doctype();
?>
<body>
<div class="formcontainer">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="application/x-[URL unfurl="true"]www-form-urlencoded">[/URL]
<fieldset class="shoutboxfieldset">
<legend>Shout Box!</legend>
<div class="row">
<span class="label">
Your Name:
</span>
<span class="formw">
<input class="textbox" type="text" value="" name="name" />
</span>
</div>
<div class="row">
<span class="label">
Your Message:
</span>
<span class="formw">
<textarea name="message"></textarea>
</span>
</div>
<div class="row">
<span class="formw">
<input class="formbutton" type="submit" name="submit" value="Shout!" />
<input class="formbutton" type="reset" name="reset" value="Clear!" />
</span>
</div>
</fieldset>
</form>
</div>
<div class="contentwrapper">
<div class="messagesectiontitle">Messages:</div>
<? $cnt = count($messages);
for ($i=$cnt; $i>0; $i--):
$message = $messages[$i];
if ($message[0] > strtotime("yesterday")):
$class="freshmessage";
elseif ($message[0] > strtotime("one week ago")):
$class="inthelastweek";
elseif ($message[0] > strtotime("one month ago")):
$class="inthelastmonth";
else:
$class="veryold";
endif;
if (!empty($message[2])):
?>
<div class="<?=$class?>">
<div class="incontentmessagetitle">
Message left by <?=$message[1]?> on <?=date("d-m-Y h:m:s",$message[0])?>
</div>
<div class="message">
<?=nl2br($message[2])?>
</div>
</div>
<?
endif;
endfor;
?>
</div>
<div class="contentwrapper">
<div class="formcontainer">
<form methd="get" action="<?=$_SERVER['PHP_SELF']?>">
<div class="row">
<span class="formw">
<input class="formbutton" type="submit" name="admin" value="Admin Site!" />
</span>
</div>
<div class="row"> </div>
</form>
</div>
</div>
</body>
</html>
<? }
function loginform() {
doctype();?>
?>
<body>
<div class="formcontainer">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="application/x-[URL unfurl="true"]www-form-urlencoded">[/URL]
<fieldset class="shoutboxfieldset">
<legend>Login to the Admin Site</legend>
<div class="row">
<span class="label">
Your Name:
</span>
<span class="formw">
<input class="textbox" type="text" value="" name="name" />
</span>
</div>
<div class="row">
<span class="label">
Your Password:
</span>
<span class="formw">
<input class="textbox" type="password" value="" name="pwd" />
</span>
</div>
<div class="row">
<span class="formw">
<input class="formbutton" type="submit" name="login" value="Login!" />
<input class="formbutton" type="reset" name="reset" value="Clear!" />
</span>
</div>
</fieldset>
</form>
</div>
</body>
</html>
<? exit; }
function adminpage() {
session_start();
doctype();
?>
<body>
<div class="formcontainer">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="application/x-[URL unfurl="true"]www-form-urlencoded">[/URL]
<fieldset class="shoutboxfieldset">
<legend>Archive the Shout Box!</legend>
<div class="row">
<span class="label">
Archive From:
</span>
<span class="formw">
<select name="day">
<? for($i=1; $i<=31; $i++): ?>
<option value=<?=$i?>><?=$i?></option>
<? endfor; ?>
</select>
<select name="month">
<? for($i=1; $i<=12; $i++): ?>
<option value=<?=$i?>><?=date("M",strtotime("2000-$i-1"))?></option>
<? endfor; ?>
</select>
<select name="year">
<? for($i=2006; $i<=2010; $i++): ?>
<option value=<?=$i?>><?=$i?></option>
<? endfor; ?>
</select>
</span>
</div>
<div class="row">
<span class="formw">
<input class="formbutton" type="submit" name="archive" value="Archive!" />
<input class="formbutton" type="reset" name="reset" value="Clear!" />
</span>
</div>
</fieldset>
</form>
</div>
</body>
</html>
<?
}
function archive() {
global $archive;
global $filename;
$archivefilename = "archive_".$filename;
$archivetime = strtotime($_POST['year']."-".$_POST['month']."-".$_POST['day']);
$fh = fopen($filename, "rt") or die("cannot open the file for reading");
while (($messages[] = fgetcsv($fh, 2048)) !== FALSE):
//do nothing
endwhile;
fclose;
$fcurrent = fopen($filename, "wt") or die("cannot open the file for writing");
$fold = fopen($archivefilename, "at") or die("cannot open the file for writing");
foreach ($messages as $message):
if($message[0] > $archivetime):
fwrite($fcurrent, "\"".$message[0]."\",\"".$message[1]."\",\"".$message[2]."\"\n");
else:
fwrite($fold, "\"".$message[0]."\",\"".$message[1]."\",\"".$message[2]."\"\n");
endif;
endforeach;
fclose($fcurrent);
fclose($fold);
}
function writeshout() {
if (!get_magic_quotes_gpc()):
$time = strtotime("now");
$name = trim($_POST['name']);
$message = trim($_POST['message']);
else:
$time = strtotime("now");
$name = stripslashes(trim($_POST['name']));
$message = stripslashes(trim($_POST['message']));
endif;
//now do the write
$fh = fopen($filename, "at") or die("cannot open the file for writing");
fwrite($fh, "\"$time\",\"$name\",\"$message\"\n");
fclose;
}
?>