Skip to content

Instantly share code, notes, and snippets.

@worenga
Created January 20, 2013 01:34
Show Gist options
  • Save worenga/4576187 to your computer and use it in GitHub Desktop.
Save worenga/4576187 to your computer and use it in GitHub Desktop.
renderWith('ArticleCommentInterface');
}
function PostCommentForm()
{
Requirements::javascript('jsparty/behaviour.js');
Requirements::javascript('jsparty/prototype.js');
Requirements::javascript('jsparty/scriptaculous/effects.js');
Requirements::javascript('mysite/javascript/ArticleCommentInterface.js');
$hidden = new HiddenField("ParentID", "ParentID", $this->page->ID);
$name = new TextField("Name", "Your name");
$name->setExtraClass("required");
$email = new EmailField("Email", "Your email");
$email->setExtraClass("required");
$website = new TextField("Website", "Your website", "http://");
$comment = new TextareaField("Comment", "Your Comment");
$comment->setExtraClass("required");
$fields = new FieldSet($hidden, $name, $email, $website, $comment);
if(MathSpamProtection::isEnabled()){
$fields->push(new TextField("Math","Spam protection question: ".MathSpamProtection::getMathQuestion()));
}
$validator = new RequiredFields("Name", "Email", "Comment");
$form = new ArticleCommentInterface_Form($this->controller, $this->methodName . ".PostCommentForm",$fields, new FieldSet(
new FormAction("postcomment", "Post")
), $validator);
$form->loadDataFrom(array(
"Name" => Cookie::get("ArticleCommentInterface_Name"),
"Email" => Cookie::get("ArticleCommentInterface_Email"),
"Website" => Cookie::get("ArticleCommentInterface_Website")
));
return $form;
}
function Comments()
{
// Comment limits
if(isset($_GET['commentStart'])) {
$limit = (int)$_GET['commentStart'].",".ArticleComment::$comments_per_page;
} else {
$limit = "0,".ArticleComment::$comments_per_page;
}
$spamfilter = isset($_GET['showspam']) ? '' : 'AND IsSpam=0';
$unmoderatedfilter = Permission::check('ADMIN') ? '' : 'AND NeedsModeration = 0';
$comments = DataObject::get("ArticleComment", "ParentID = '" . Convert::raw2sql($this->page->ID) . "' $spamfilter $unmoderatedfilter", "Created ASC", "", $limit);
if(is_null($comments)) {
return;
}
// This allows us to use the normal 'start' GET variables as well (In the weird circumstance where you have paginated comments AND something else paginated)
$comments->setPaginationGetVar('commentStart');
return $comments;
}
}
class ArticleCommentInterface_Form extends PageCommentInterface_Form
{
function postcomment($data)
{
$parsed = $this->parseContent($data['Comment']);
// Spam filtering
if(SSAkismet::isEnabled())
{
try
{
$akismet = new SSAkismet();
$akismet->setCommentAuthor($data['Name']);
$akismet->setCommentAuthorEmail($data['Email']);
$akismet->setCommentAuthorURL($data['Website']);
$akismet->setCommentContent($parsed);
if($akismet->isCommentSpam())
{
if(SSAkismet::getSaveSpam())
{
$comment = Object::create('ArticleComment');
$this->saveInto($comment);
$comment->Comment = $parsed;
$comment->ArticleID = $data['ParentID'];
$comment->setField("IsSpam", true);
$comment->write();
}
echo "Spam detected!!
";
echo "If you believe this was in error, please email ";
echo ereg_replace("@", " _(at)_", Email::getAdminEmail());
echo ".
The message you posted was:
";
echo $parsed;
return;
}
}
catch (Exception $e)
{
// Akismet didn't work, continue without spam check
}
}
//check if spam question was right.
if(MathSpamProtection::isEnabled())
{
if(!MathSpamProtection::correctAnswer($data['Math']))
{
if(!Director::is_ajax())
{
Director::redirectBack();
}
return "spamprotectionfalied"; //used by javascript for checking if the spam question was wrong
}
}
Cookie::set("ArticleCommentInterface_Name", $data['Name']);
Cookie::set("ArticleCommentInterface_Email", $data['Email']);
Cookie::set("ArticleCommentInterface_Website", $data['Website']);
$comment = Object::create('ArticleComment');
$this->saveInto($comment);
$comment->Comment = $parsed;
$comment->ArticleID = $data['ParentID'];
$comment->IsSpam = false;
$comment->NeedsModeration = PageComment::moderationEnabled();
$comment->write();
if(Director::is_ajax())
{
if($comment->NeedsModeration)
{
echo "Your comment has been submitted and is now awaiting moderation.";
}
else
{
echo $comment->renderWith('ArticleCommentInterface_singlecomment');
}
}
else
{
Director::redirectBack();
}
}
function parseContent($content)
{
require_once($_SERVER['DOCUMENT_ROOT']."/htmlpurifier/library/HTMLPurifier.auto.php");
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML', 'Allowed', 'a[href|title],p,strong,em');
$config->set('AutoFormat', 'AutoParagraph', true);
$config->set('AutoFormat', 'Linkify', true);
$purifier = new HTMLPurifier($config);
$clean = $purifier->purify($content);
return $clean;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment