session_start();
$_SESSION['section_url']='interact_guestbook.php';
$isLogged = false;
if((trim($_SESSION['username']) != '') && (time() - $_SESSION['timestamp'] < 1800)){
$isLogged=true;
}
require_once('common.php');
$page_type = strtolower($_REQUEST['p']);
//if($page_type=="signin"){
$padding = "";
$return_success_message = "";
$return_error_message = "";
$msg_display = "none";
$form_display = "block";
$banned = false;
$ip=$_SERVER['REMOTE_ADDR'];
//for captcha images
$str = "";
for($i=0;$i<5;$i++){
$var[$i] = rand(1,26);
if($var[$i]==1){
$letter[$i] = "a";
}
if($var[$i]==1){
$letter[$i] = "a";
}
if($var[$i]==2){
$letter[$i] = "b";
}
if($var[$i]==3){
$letter[$i] = "c";
}
if($var[$i]==4){
$letter[$i] = "d";
}
if($var[$i]==5){
$letter[$i] = "e";
}
if($var[$i]==6){
$letter[$i] = "f";
}
if($var[$i]==7){
$letter[$i] = "g";
}
if($var[$i]==8){
$letter[$i] = "h";
}
if($var[$i]==9){
$letter[$i] = "i";
}
if($var[$i]==10){
$letter[$i] = "j";
}
if($var[$i]==11){
$letter[$i] = "k";
}
if($var[$i]==12){
$letter[$i] = "l";
}
if($var[$i]==13){
$letter[$i] = "m";
}
if($var[$i]==14){
$letter[$i] = "n";
}
if($var[$i]==15){
$letter[$i] = "o";
}
if($var[$i]==16){
$letter[$i] = "p";
}
if($var[$i]==17){
$letter[$i] = "q";
}
if($var[$i]==18){
$letter[$i] = "r";
}
if($var[$i]==19){
$letter[$i] = "s";
}
if($var[$i]==20){
$letter[$i] = "t";
}
if($var[$i]==21){
$letter[$i] = "u";
}
if($var[$i]==22){
$letter[$i] = "v";
}
if($var[$i]==23){
$letter[$i] = "w";
}
if($var[$i]==24){
$letter[$i] = "x";
}
if($var[$i]==25){
$letter[$i] = "y";
}
if($var[$i]==26){
$letter[$i] = "z";
}
$str .= $letter[$i];
}
$valid_key = md5($str);
//}else{
//check if has pending posts
$query_pending = "SELECT guestbook.* from guestbook INNER JOIN users ON(guestbook.name=users.name AND users.username='".$_SESSION['username']."') WHERE guestbook.status='pending'";
$result_pending = $db->query($query_pending);
$num_pending = $db->num_rows($result_pending);
$row_pending = mysql_fetch_assoc($result_pending);
if($num_pending>0){
$error_message = "Your message has been sent and is waiting approval.";
$color = "green";
}else{
if(!$isLogged){
$error_message = "Please login to sign our guestbook!";
}else{
$error_message = "Please sign our guestbook!";
}
$color = "#99CCFF";
}
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}
//check if IP is banned
$query_ban = "SELECT ip_address from banned_IPs where ip_address='$ip'";
$result_ban = $db->query($query_ban);
$num_ban = $db->num_rows($result_ban);
if($num_ban>0){
$padding = "14px";
$error_message = "You have been banned from posting on the Guestbook!";
$color = "orange";
$msg_display = "block";
$form_display = "none";
$banned = true;
}
$current_page = $page;
$page_count = ($page-1)*10;
$query_posts = " SELECT * from guestbook where status='approved' order by date desc LIMIT $page_count, 10";
$result = $db->query($query_posts);
$num_posts = $db->num_rows($result);
$query_total = "SELECT * from guestbook where status='approved'";
$result_total = $db->query($query_total);
$num_posts_total = $db->num_rows($result_total);
$total_pages = ceil($num_posts_total/10);
if(isset($_POST['submit']) && $banned == false){
$validation_code = $_POST['validation_code'];
$valid_key = $_POST['valid_key'];
$error_message='';
if(md5($validation_code) == $valid_key){
if($isLogged){
$username = $_SESSION['username'];
$query_email = " SELECT email,name FROM users where username='$username'";
$result_email = $db->query($query_email);
$row = mysql_fetch_assoc($result_email);
$user_name = $row['name'];
$user_email = $row['email'];
}else{
$user_name = $_POST['name'];
$user_email = $_POST['email'];
if(empty($user_name)){
$color = "red";
$error_message.= "Username field is required.
";
}
if(empty($user_email)){
$color = "red";
$error_message.= "Email field is required.
";
}
}
$message = htmlentities(trim($_POST['message'], ENT_QUOTES));
if(empty($message)){
$color = "red";
$error_message.= "Please enter your message.
";
}
if(empty($error_message)){
$query_post = " INSERT INTO guestbook VALUES ('', '$user_name', '$user_email', NOW() , '$message', 'pending','$ip');";
$result = $db->query($query_post);
if($result){
$error_message = "Your message has been sent and is waiting approval.";
$color = "green";
$msg_display = "block";
$form_display = "none";
header("location: interact_guestbook.php?p=waiting_app");
}
}
}else{
$error_message .= "You have entered an invalid code.";
$color = "#FFCC00";
$msg_display = "none";
$form_display = "block";
}
}
//}
?>
Welcome to the Greg Lake Guestbook
if($page_type=="signin" && $isLogged && !($num_pending>0)){ ?> }else{ ?> =$error_message?>