<?php
/* ########################## INFO ##########################
Advanced shoutbox
Version 1.0
Free script from WB - Webdesign for beginners.
Visit http://plohni.com/wb for more scripts.
Feel free to modify this script.
lgp
/* ########################## INFO ########################## */
/* ###################### INSTALLATION ###################### */
// a) Adjust the configuration variables to your needs
$file = "shouts.txt"; // Name of the file which
// contains the shouts
$shouts = 4; // Number of shouts to be displayed
$maxlength_name = "20"; // Maximum length of name
$maxlength_text = "150"; // Maximum length of text
$break_name = "15"; // Break name after characters
// without space
$break_text = "15"; // Break text after characters
// without space
// b) Copy this code to your PHP file
// c) Copy your PHP file and the shouts file defined in
// variable $file to your server using ASCII mode
// d) Make the shouts file writable (Windows: adjust
// security, Unix: chmod 777)
/* ###################### INSTALLATION ###################### */
/* ############# SCRIPT (EDIT AT YOUR OWN RISK) ############# */
?>
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="text" value="Your name" name="input_name" maxlength="<?php echo $maxlength_name; ?>" onfocus="if(this.value=='Your name'){this.value='';}" onblur="if(this.value==''){this.value='Your name';}" /><br />
<input type="text" value="Your text" name="input_text" maxlength="<?php echo $maxlength_text; ?>" onfocus="if(this.value=='Your text'){this.value='';}" onblur="if(this.value==''){this.value='Your text';}" /><br />
<input type="submit" value="Shout!" />
</form>
</p>
<hr />
<p>
<?php
//function to break text after x characters
function str_break($str,$maxlen){
$nobr = 0;
$len = strlen($str);
for($i=0;$i<$len;$i++){
if(($str[$i]!=' ') && ($str[$i]!='-') && ($str[$i]!="\n")){
$nobr++;
}else{
$nobr = 0;
if($maxlen+$i>$len){
$str_br .= substr($str,$i);
break;
}
}
if($nobr>$maxlen){
$str_br .= ' '.$str[$i];
$nobr = 1;
}else{
$str_br .= $str[$i];
}
}
return $str_br;
}
//number of shouts to be displayed
$display = (isset($_GET["show"]) ? "all" : $shouts);
//print links to either show all or specific number of shouts
if($display == "all"){
?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>">View small shoutbox</a><?php
}else{
?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show=all">View all shouts</a><?php
}
?>
</p><p>
<?php
//insert new shout
$input_name = $_POST["input_name"];
$input_text = $_POST["input_text"];
//check if form has been submitted
if(isset($input_name) && isset($input_text) && $input_name!="Your name" && $input_text!="Your text" && strlen($input_name)>0 && strlen($input_text)>0){
//get last name and comment
$handle = fopen($file,"r");
while(!feof($handle)){
$row = fgets($handle,999999);
list($tmp_name,$tmp_text) = split("\|\|\|\|\|",$row);
if($tmp_name != "" && $tmp_text != ""){
$last_name = $tmp_name;
$last_text = str_replace("\n","",$tmp_text);
}
}
fclose($handle);
$input_name = str_break($input_name,$break_name); //break name
$input_name = str_replace("<","<",$input_name); //prevent html input
$input_name = str_replace(">",">",$input_name); //prevent html input
$input_name = stripslashes($input_name); //strip slashes
$input_text = str_break($input_text,$break_text); //break text
$input_text = str_replace("<","<",$input_text); //prevent html input
$input_text = str_replace(">",">",$input_text); //prevent html input
$input_text = stripslashes($input_text); //strip slashes
if($last_name != $input_name || $last_text != $input_text){
$handle = fopen($file,"a"); //open shouts file to write (append)
fputs($handle,"$input_name|||||$input_text\n"); //insert name and shout
fclose($handle); //close file handle
}
}
//read shouts file
$names = array(); //array to store names
$shouts = array(); //array to store shouts
$handle = fopen($file,"r"); //open shouts file to read
while(!feof($handle)){ //read row by row
$row = fgets($handle,999999);
list($name,$shout) = split("\|\|\|\|\|",$row);
if($name){
array_push($names,$name);
array_push($shouts,$shout);
}
}
fclose($handle); //close file handle
//reverse arrays so that new lines are first
$names = array_reverse($names);
$shouts = array_reverse($shouts);
//number of shouts to really print
$max = ($display == "all" ? count($names) : $display);
//print shouts
for($i=0;$i<$max && $i<count($names);$i++){
?><p><strong><?php echo $names[$i]; ?>:</strong> <?php echo $shouts[$i]; ?></p>
<?php } ?>
</p>