1
|
|
|
<?php |
2
|
|
|
namespace RbComment\Controller; |
3
|
|
|
|
4
|
|
|
use RbComment\Model\Comment; |
5
|
|
|
use RbComment\Form\CommentForm; |
6
|
|
|
use RbComment\Model\CommentTable; |
7
|
|
|
use Zend\Http\PhpEnvironment\RemoteAddress; |
8
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
9
|
|
|
use ZendService\Akismet\Akismet; |
10
|
|
|
|
11
|
|
|
class CommentController extends AbstractActionController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var CommentTable |
15
|
|
|
*/ |
16
|
|
|
private $commentTable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Akismet |
20
|
|
|
*/ |
21
|
|
|
private $akismetService; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $configService; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
array $configService, |
30
|
|
|
CommentTable $commentTable, |
31
|
|
|
Akismet $akismetService |
32
|
|
|
) { |
33
|
|
|
$this->configService = $configService; |
34
|
|
|
$this->commentTable = $commentTable; |
35
|
|
|
$this->akismetService = $akismetService; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function addAction() |
39
|
|
|
{ |
40
|
|
|
$rbCommentConfig = (object) $this->configService['rb_comment']; |
41
|
|
|
|
42
|
|
|
$form = new CommentForm($rbCommentConfig->strings); |
43
|
|
|
|
44
|
|
|
$request = $this->getRequest(); |
45
|
|
|
if ($request->isPost()) { |
46
|
|
|
$comment = new Comment(); |
47
|
|
|
$form->setInputFilter($comment->getInputFilter()); |
48
|
|
|
$form->setData($request->getPost()); |
49
|
|
|
|
50
|
|
|
if ($form->isValid()) { |
51
|
|
|
$comment->exchangeArray($form->getData()); |
52
|
|
|
|
53
|
|
|
// Set default visibility from config |
54
|
|
|
$comment->visible = $rbCommentConfig->default_visibility; |
55
|
|
|
|
56
|
|
|
// If akismet is enabled check for spam |
57
|
|
|
if (($rbCommentConfig->akismet['enabled'] === true) && |
58
|
|
|
$this->isSpam($comment, $rbCommentConfig)) { |
59
|
|
|
$comment->spam = 1; |
|
|
|
|
60
|
|
|
$comment->visible = 0; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// We need the id for the mailer |
64
|
|
|
$comment->id = $this->commentTable->saveComment($comment); |
65
|
|
|
|
66
|
|
|
// Send email if active and not spam |
67
|
|
|
if (($rbCommentConfig->email['notify'] === true) && |
68
|
|
|
($comment->spam === 0)) { |
69
|
|
|
$this->rbMailer($comment); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->redirect()->toUrl($form->get('uri')->getValue()); |
73
|
|
|
} else { |
74
|
|
|
$this->flashMessenger()->setNamespace('RbComment'); |
|
|
|
|
75
|
|
|
$this->flashMessenger()->addMessage(json_encode($form->getMessages())); |
|
|
|
|
76
|
|
|
|
77
|
|
|
return $this->redirect()->toUrl($form->get('uri')->getValue() . '#rbcomment'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Checks if a comment is spam using the akismet service. |
84
|
|
|
* |
85
|
|
|
* @param Comment $comment |
86
|
|
|
* @param mixed $rbCommentConfig |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
|
protected function isSpam($comment, $rbCommentConfig) |
90
|
|
|
{ |
91
|
|
|
$remote = new RemoteAddress(); |
92
|
|
|
$remote->setUseProxy($rbCommentConfig->akismet['proxy']['use']); |
93
|
|
|
$remote->setTrustedProxies($rbCommentConfig->akismet['proxy']['trusted']); |
94
|
|
|
$remote->setProxyHeader($rbCommentConfig->akismet['proxy']['header']); |
95
|
|
|
|
96
|
|
|
$content = [ |
97
|
|
|
'user_ip' => $remote->getIpAddress(), |
98
|
|
|
'user_agent' => filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), |
99
|
|
|
'comment_type' => 'comment', |
100
|
|
|
'comment_author' => $comment->author, |
101
|
|
|
'comment_author_email' => $comment->contact, |
102
|
|
|
'comment_content' => $comment->content, |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
return $this->akismetService->isSpam($content); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.