...

Easy User Comment

Phil Jackson
10-23-2009, 05:49 PM
<?php

//www.actwebdesigns.co.uk
//luke@actwebdesigns.co.uk
//
// <style type="text/css">
// div.commentWrapper { margin:20px auto; border:1px solid #404040; background-color:#F2F2F2; }
// div.commentWrapper p.comment, div.commentWrapper p.email, div.commentWrapper p.name { padding: 0 10px; }
// #userCommentName { border: 1px solid #404040; padding:4px; font-size:16px; }
// #commentInput label { font-size:16px; }
// #userCommentEmail { border: 1px solid #404040; padding:4px; font-size:16px; }
// #userComment { border: 1px solid #404040; width:530px; padding:4px; font-size:16px; }
// #commentInput { margin-top:40px; }
// p.inputP { text-align:right; width:230px; }
// #ACTpostComment { padding: 6px 10px; border:1px solid #404040; background-color:#669669; color:#FFF; }
// .Emessage { color:#F00; }
// .message { color:#060; }
// </style>

function userComment($name, $message, $email)
{
$result = "";
if(!file_exists("ACTuserComments/".md5($name)."/comments.txt"))
{
if(!file_exists("ACTuserComments/"))
{
mkdir("ACTuserComments/", 0700);
}
if(!file_exists("ACTuserComments/".md5($name)."/"))
{
mkdir("ACTuserComments/".md5($name)."/", 0700);
}
$fp = fopen("ACTuserComments/".md5($name)."/comments.txt", 'w');
fwrite($fp, '');
fclose($fp);
}

if(isset($_POST['ACTpostComment']))
{
if((isset($_POST['foo']) || isset($_POST['bar']) || isset($_POST['foobar'])) && (@empty($_POST['foo']) && @empty($_POST['bar']) && @empty($_POST['foobar'])))
{
if(isset($_POST['userCommentName']) && !empty($_POST['userCommentName']))
{
if(isset($_POST['userCommentEmail']) && !empty($_POST['userCommentEmail']))
{
if(preg_match("#^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$#is", $_POST['userCommentEmail']))
{
if(isset($_POST['userComment']) && !empty($_POST['userComment']))
{
$namePost = strip_tags($_POST['userCommentName']);
$emailPost = strip_tags($_POST['userCommentEmail']);
$commentPost = strip_tags($_POST['userComment']);

$contents = file_get_contents("ACTuserComments/".md5($name)."/comments.txt");
if($fp = @fopen("ACTuserComments/".md5($name)."/comments.txt", 'w'))
{

$input = "<div class=\"commentWrapper\">\n";
$input .= " <p class=\"comment\">".$commentPost."</p>\n";
$input .= " <p class=\"name\">".$namePost."</p>\n";
$input .= " <p class=\"email\"><small>".str_replace(".", " [dot] ", str_replace("@", " [at] ", $emailPost))."</small></p>\n";
$input .= "</div>\n";
$input .= $contents;

if(@fwrite($fp, $input))
{
fclose($fp);

$to = $email;
$subject = 'Comment has been made on your site!';
$headers = "From: do-not-reply@yoursite.co.uk\nContent-Type: text/html; charset=iso-8859-1";

$Emessage = "<h1 style=\"color:#FF9933; font-size:16px; margin-left: 10px;\">A comment has been left on your thread, &quot;".$name."&quot;</h1>";

$Emessage .= "<p style=\"margin-left:10px; color:#404040;\">".$namePost."</p>";
$Emessage .= "<p style=\"margin-left:10px; color:#404040;\">".$emailPost."</p>";
$Emessage .= "<p style=\"margin-left:10px; color:#404040;\">".$commentPost."</p>";

@mail( $to, $subject, $Emessage, $headers );
$result = "<p class=\"message\">".$message."</p>\n";
}
else
{
$result = "<p class=\"Emessage\">Could not write to document, please contact support.</p>\n";
}
}
else
{
$result = "<p class=\"Emessage\">Could not open document, please contact support</p>\n";
}
}
else
{
$result = "<p class=\"Emessage\">Please enter a comment.</p>\n";
}
}
else
{
$result = "<p class=\"Emessage\">Please enter a valid email address.</p>\n";
}
}
else
{
$result = "<p class=\"Emessage\">Please enter your email address.</p>\n";
}
}
else
{
$result = "<p class=\"Emessage\">Please enter your name or alias.</p>\n";
}
}
}

if(file_exists("ACTuserComments/".md5($name)."/comments.txt"))
{
$result .= file_get_contents("ACTuserComments/".md5($name)."/comments.txt");
}

$fake = array("please do not enter data here, this is a test", "ple@se do not enter d@t@ here, this is @ test", "please d0 n0t enter data here, this is a test");
$fake2 = array("foo", "bar", "foobar");
$fake = $fake[rand(0, 2)];
$fake2 = $fake2[rand(0, 2)];

$result .= "<div id=\"commentInput\">\n";
$result .= " <form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">\n";
$result .= " <p class=\"inputP\"><label for=\"userCommentName\">name: </label><input type=\"text\" name=\"userCommentName\" id=\"userCommentName\" /></p>\n";
$result .= " <p class=\"inputP\"><label for=\"userCommentEmail\">email: </label><input type=\"text\" name=\"userCommentEmail\" id=\"userCommentEmail\" /></p>\n";
$result .= " <p style=\"display:none;\"><label for=\"".$fake2."\">".$fake.": </label><input type=\"text\" name=\"".$fake2."\" id=\"".$fake2."\" /></p>\n";
$result .= " <p><label for=\"userComment\">Comment: </label><textarea cols=\"66\" rows=\"4\" name=\"userComment\" id=\"userComment\"></textarea></p>\n";
$result .= " <p><input type=\"submit\" name=\"ACTpostComment\" id=\"ACTpostComment\" value=\"Post Comment\" /></p>\n";
$result .= " </form>\n";
$result .= "</div>\n";

return $result;
}

//echo userComment("thread1", "thank you for your comment.");
//Place in document where you want coments to be left

?>



For more functions and demo about this one:

http://www.actwebdesigns.co.uk/web-design-mansfield/php-functions/user-comment-function.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum