root / branches / 2.1.x / actions / AddCommentAndRating.php @ 1111
View | Annotate | Download (3.7 KB)
| 1 | <?php
|
|---|---|
| 2 | |
| 3 | /*
|
| 4 | * @copyright Copyright (C) 2005-2010 Keyboard Monkeys Ltd. http://www.kb-m.com |
| 5 | * @license http://creativecommons.org/licenses/BSD/ BSD License |
| 6 | * @author Keyboard Monkeys Ltd. |
| 7 | * @since Sciret 1.2 |
| 8 | * @package Sciret |
| 9 | * @packager Keyboard Monkeys |
| 10 | */ |
| 11 | |
| 12 | require 'actions/Action.php'; |
| 13 | |
| 14 | class AddCommentAndRating extends Action |
| 15 | {
|
| 16 | |
| 17 | function dispatch()
|
| 18 | {
|
| 19 | if ($this->configuration->getConfigValue('badBehaviorEnabled')) { |
| 20 | require 'libs/bad-behavior/bad-behavior-generic.php'; |
| 21 | } |
| 22 | |
| 23 | if (!$this->user->isAnonymous()) { |
| 24 | $_POST['commentUserName'] = $this->user->getFullName(); |
| 25 | } |
| 26 | |
| 27 | $artId = isset($_GET['artId'])? (int)$_GET['artId'] : 0; |
| 28 | if ($_POST['comment_box'] != '' && $_POST['commentUserName'] == '') { |
| 29 | exit;
|
| 30 | } |
| 31 | |
| 32 | $message = '';
|
| 33 | |
| 34 | if ($_POST['comment_box'] != '') { |
| 35 | |
| 36 | if ($this->configuration->getConfigValue('akismetEnabled')) { |
| 37 | $antiSpam = new AntiSpam($this->configuration, AntiSpam::TYPE_AKISMET); |
| 38 | $this->_checkForSpam($antiSpam, $artId);
|
| 39 | } |
| 40 | |
| 41 | if ($this->configuration->getConfigValue('typePadAntiSpamEnabled')) { |
| 42 | $antiSpam = new AntiSpam($this->configuration, AntiSpam::TYPE_TYPEPAD); |
| 43 | $this->_checkForSpam($antiSpam, $artId);
|
| 44 | } |
| 45 | |
| 46 | $comment = new Kb_Model_Comment;
|
| 47 | $comment->setUserName($_POST['commentUserName']);
|
| 48 | $comment->setEntered(date('Y-m-d h:i:s'));
|
| 49 | $comment->setContents($_POST['comment_box']);
|
| 50 | $comment->setArticleId($artId); |
| 51 | $comment->setPublished($this->configuration->getConfigValue('publishCommentsAuto')); |
| 52 | $comment->save(); |
| 53 | $this->addHistoryEntry($artId, translate('Added comment'), false, $_POST['commentUserName']); |
| 54 | $message = $this->translate('Comment saved successfully'); |
| 55 | } |
| 56 | |
| 57 | if (isset($_POST['Rate']) && $_POST['Rate'] > 0) { |
| 58 | $article = new Kb_Model_Article($artId);
|
| 59 | $article->addVote($_POST['Rate']);
|
| 60 | $article->save(false);
|
| 61 | $this->addHistoryEntry($artId, translate('Article rated'), false, $_POST['commentUserName']); |
| 62 | $message = $this->translate('Article has been rated successfully'); |
| 63 | setcookie('voted_article_'.$artId, true); |
| 64 | } |
| 65 | |
| 66 | if ($_POST['comment_box'] != '' && isset($_POST['Rate'])) { |
| 67 | $message = $this->translate('Article has been commented and rated successfully'); |
| 68 | } |
| 69 | |
| 70 | Library::redirect(Library::getLink(array('view' => 'ViewComments', 'artId' => $artId, 'commentsMessage' => urlencode($message)))); |
| 71 | } |
| 72 | |
| 73 | private function _checkForSpam($antiSpam, $artId) |
| 74 | {
|
| 75 | $data = array(
|
| 76 | 'user_ip' => $_SERVER['REMOTE_ADDR'], |
| 77 | 'user_agent' => $_SERVER['HTTP_USER_AGENT'], |
| 78 | 'referer' => isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER'] : '', |
| 79 | 'permalink' => Library::getBaseURL() . "/view=ViewArticle&id=$artId", |
| 80 | 'comment_type' => 'comment', |
| 81 | 'comment_author' => $_POST['commentUserName'], |
| 82 | 'comment_content' => $_POST['comment_box'], |
| 83 | ); |
| 84 | |
| 85 | try {
|
| 86 | if ($antiSpam->isSpam($data)) {
|
| 87 | $message = $this->translate('Comment detected as spam. ** DISCARDED **'); |
| 88 | Library::redirect(Library::getLink(array(
|
| 89 | 'view' => 'ViewComments', |
| 90 | 'artId' => $artId,
|
| 91 | 'commentsMessage' => urlencode($message)
|
| 92 | ))); |
| 93 | } |
| 94 | } catch (Exception $e) {
|
| 95 | // TypePad AntiSpam API Key is wrong, so nothing to do in this case :(
|
| 96 | } |
| 97 | } |
| 98 | } |