/
Service.php
162 lines (150 loc) · 5 KB
/
Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Concrete\Core\Antispam;
use Concrete\Core\Logging\Channels;
use Config;
use Core;
use Group;
use Loader;
use Log;
use Page;
use Concrete\Core\User\User;
use UserInfo;
class Service
{
/**
* @var bool|mixed
*/
protected $controller = false;
public function __construct()
{
$library = Library::getActive();
if (is_object($library)) {
$this->controller = $library->getController();
}
}
/**
* @return Group|null
*/
public function getAllowlistGroup()
{
return Group::getByID(Config::get('concrete.spam.allowist_group', Config::get('concrete.spam.whitelist_group')));
}
/**
* Report some content with the poster's information to the AntiSpam service.
*
* @param string $content
* @param UserInfo $ui
* @param string $ip
* @param string $ua
* @param array $additionalArgs
*/
public function report($content, $author, $email, $ip, $ua, $additionalArgs = array())
{
$args['content'] = $content;
$args['author'] = $author;
$args['author_email'] = $email;
$args['ip_address'] = $ip;
$args['user_agent'] = $ua;
foreach ($additionalArgs as $key => $value) {
$args[$key] = $value;
}
if ($this->controller && method_exists($this->controller, 'report')) {
$this->controller->report($args);
}
$u = Core::make(User::class);
\Log::info(t('Content %s (author %s, %s) flagged as spam by user %s',
$content,
$author,
$email,
$u->getUserName()
));
}
/**
* @param string $content
* @param string $type
* @param array $additionalArgs
* @param bool $user
*
* @return bool
*
* @throws \Exception
*/
public function check($content, $type, $additionalArgs = array(), $user = false)
{
if ($this->controller) {
if (!$user) {
$user = Core::make(User::class);
}
$wlg = $this->getAllowlistGroup();
if ($wlg instanceof Group && $user->inGroup($wlg)) {
// Never spam if user is in the allowlist
return true;
}
/** @var \Concrete\Core\Permission\IPService $iph */
$iph = Core::make('helper/validation/ip');
$ip = $iph->getRequestIP();
$args['ip_address'] = ($ip === false) ? ('') : ($ip->getIp($ip::FORMAT_IP_STRING));
$args['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$args['content'] = $content;
foreach ($additionalArgs as $key => $value) {
$args[$key] = $value;
}
if (isset($args['user']) && is_object($args['user'])) {
$u = $args['user'];
} else {
$u = Core::make(User::class);
}
if (!isset($args['email']) && $u->isRegistered()) {
$ui = UserInfo::getByID($u->getUserID());
$args['email'] = $ui->getUserEmail();
}
$r = $this->controller->check($args);
if ($r) {
return true;
} else {
$logText = '';
$c = Page::getCurrentPage();
if (is_object($c)) {
$logText .= t('URL: %s', Loader::helper('navigation')->getLinkToCollection($c, true));
$logText .= "\n";
}
if ($u->isRegistered()) {
$logText .= t('User: %s (ID %s)', $u->getUserName(), $u->getUserID());
$logText .= "\n";
}
$logText .= t('Type: %s', Loader::helper('text')->unhandle($type));
$logText .= "\n";
foreach ($args as $key => $value) {
$logText .= Loader::helper('text')->unhandle($key) . ': ' . $value . "\n";
}
if (Config::get('concrete.log.spam')) {
$logger = \Core::make('log/factory')->createLogger(Channels::CHANNEL_SPAM);
$logger->warning($logText);
}
if (Config::get('concrete.spam.notify_email') != '') {
$mh = Loader::helper('mail');
$mh->to(Config::get('concrete.spam.notify_email'));
$mh->addParameter('content', $logText);
$mh->addParameter('siteName', tc('SiteName', \Core::make('site')->getSite()->getSiteName()));
$mh->load('spam_detected');
$mh->sendMail();
}
return false;
}
} else {
return true; // return true if it passes the test
}
}
/**
* @param $nm
* @param $args
*
* @return mixed
*/
public function __call($nm, $args)
{
if (method_exists($this->controller, $nm)) {
return call_user_func_array(array($this->controller, $nm), $args);
}
}
}