3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011,2012, StatusNet, Inc.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011,2012 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Check new notices with activity spam service.
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011,2012 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class ActivitySpamPlugin extends Plugin
49 const PLUGIN_VERSION = '2.0.0';
51 public $server = null;
52 public $hideSpam = false;
54 const REVIEWSPAM = 'ActivitySpamPlugin::REVIEWSPAM';
55 const TRAINSPAM = 'ActivitySpamPlugin::TRAINSPAM';
60 * @return boolean hook value; true means continue processing, false means stop.
64 $this->filter = new SpamFilter(common_config('activityspam', 'server'),
65 common_config('activityspam', 'consumerkey'),
66 common_config('activityspam', 'secret'));
68 $this->hideSpam = common_config('activityspam', 'hidespam');
70 // Let DB_DataObject find Spam_score
72 common_config_set('db', 'class_location',
73 common_config('db', 'class_location') .':'.dirname(__FILE__));
79 * Database schema setup
84 * @return boolean hook value; true means continue processing, false means stop.
87 function onCheckSchema()
89 $schema = Schema::get();
90 $schema->ensureTable('spam_score', Spam_score::schemaDef());
92 Spam_score::upgrade();
98 * When a notice is saved, check its spam score
100 * @param Notice $notice Notice that was just saved
102 * @return boolean hook value; true means continue processing, false means stop.
105 function onEndNoticeSave($notice)
109 $result = $this->filter->test($notice);
111 $score = Spam_score::saveNew($notice, $result);
113 $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
115 } catch (Exception $e) {
117 $this->log(LOG_ERR, $e->getMessage());
123 function onNoticeDeleteRelated($notice) {
124 $score = Spam_score::getKV('notice_id', $notice->id);
125 if (!empty($score)) {
131 function onUserRightsCheck($profile, $right, &$result) {
133 case self::REVIEWSPAM:
134 case self::TRAINSPAM:
135 $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
142 function onGetSpamFilter(&$filter) {
143 $filter = $this->filter;
147 function onEndShowNoticeOptionItems($nli)
149 $profile = Profile::current();
151 if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
153 $notice = $nli->getNotice();
154 $out = $nli->getOut();
156 if (!empty($notice)) {
158 $score = Spam_score::getKV('notice_id', $notice->id);
161 // If it's empty, we can train it.
162 $form = new TrainSpamForm($out, $notice);
164 } else if ($score->is_spam) {
165 $form = new TrainHamForm($out, $notice);
167 } else if (!$score->is_spam) {
168 $form = new TrainSpamForm($out, $notice);
178 * Map URLs to actions
180 * @param URLMapper $m path-to-action mapper
182 * @return boolean hook value; true means continue processing, false means stop.
185 public function onRouterInitialized(URLMapper $m)
187 $m->connect('main/train/spam',
188 array('action' => 'train', 'category' => 'spam'));
189 $m->connect('main/train/ham',
190 array('action' => 'train', 'category' => 'ham'));
191 $m->connect('main/spam',
192 array('action' => 'spam'));
196 function onEndShowStyles($action)
198 $action->element('style', null,
199 '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
200 '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
204 function onEndPublicGroupNav($nav)
206 $user = common_current_user();
208 if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
209 $nav->out->menuItem(common_local_url('spam'),
211 // TRANS: Menu item title in search group navigation panel.
212 _('Notices marked as spam'),
213 $nav->actionName == 'spam',
214 'nav_timeline_spam');
220 function onPluginVersion(array &$versions)
222 $versions[] = array('name' => 'ActivitySpam',
223 'version' => self::PLUGIN_VERSION,
224 'author' => 'Evan Prodromou',
225 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ActivitySpam',
227 _m('Test notices against the Activity Spam service.'));
231 function onEndNoticeInScope($notice, $profile, &$bResult)
233 if ($this->hideSpam) {
236 $score = Spam_score::getKV('notice_id', $notice->id);
238 if (!empty($score) && $score->is_spam) {
239 if (empty($profile) ||
240 ($profile->id !== $notice->profile_id &&
241 !$profile->hasRight(self::REVIEWSPAM))) {
252 * Pre-cache our spam scores if needed.
254 function onEndNoticeListPrefill(array &$notices, array &$profiles, array $notice_ids, Profile $scoped=null) {
255 if ($this->hideSpam) {
256 Spam_score::multiGet('notice_id', $notice_ids);