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 public $server = null;
50 public $hideSpam = false;
52 const REVIEWSPAM = 'ActivitySpamPlugin::REVIEWSPAM';
53 const TRAINSPAM = 'ActivitySpamPlugin::TRAINSPAM';
58 * @return boolean hook value; true means continue processing, false means stop.
62 $this->filter = new SpamFilter(common_config('activityspam', 'server'),
63 common_config('activityspam', 'consumerkey'),
64 common_config('activityspam', 'secret'));
66 $this->hideSpam = common_config('activityspam', 'hidespam');
68 // Let DB_DataObject find Spam_score
70 common_config_set('db', 'class_location',
71 common_config('db', 'class_location') .':'.dirname(__FILE__));
77 * Database schema setup
82 * @return boolean hook value; true means continue processing, false means stop.
85 function onCheckSchema()
87 $schema = Schema::get();
88 $schema->ensureTable('spam_score', Spam_score::schemaDef());
90 Spam_score::upgrade();
96 * When a notice is saved, check its spam score
98 * @param Notice $notice Notice that was just saved
100 * @return boolean hook value; true means continue processing, false means stop.
103 function onEndNoticeSave($notice)
107 $result = $this->filter->test($notice);
109 $score = Spam_score::saveNew($notice, $result);
111 $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
113 } catch (Exception $e) {
115 $this->log(LOG_ERR, $e->getMessage());
121 function onNoticeDeleteRelated(Notice $notice) {
122 $score = Spam_score::getKV('notice_id', $notice->id);
123 if (!empty($score)) {
129 function onUserRightsCheck(Profile $profile, $right, &$result) {
131 case self::REVIEWSPAM:
132 case self::TRAINSPAM:
133 $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
140 function onGetSpamFilter(&$filter) {
141 $filter = $this->filter;
145 function onEndShowNoticeOptionItems($nli)
147 $profile = Profile::current();
149 if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
151 $notice = $nli->getNotice();
152 $out = $nli->getOut();
154 if (!empty($notice)) {
156 $score = Spam_score::getKV('notice_id', $notice->id);
159 // If it's empty, we can train it.
160 $form = new TrainSpamForm($out, $notice);
162 } else if ($score->is_spam) {
163 $form = new TrainHamForm($out, $notice);
165 } else if (!$score->is_spam) {
166 $form = new TrainSpamForm($out, $notice);
176 * Map URLs to actions
178 * @param URLMapper $m path-to-action mapper
180 * @return boolean hook value; true means continue processing, false means stop.
183 public function onRouterInitialized(URLMapper $m)
185 $m->connect('main/train/spam',
186 array('action' => 'train', 'category' => 'spam'));
187 $m->connect('main/train/ham',
188 array('action' => 'train', 'category' => 'ham'));
189 $m->connect('main/spam',
190 array('action' => 'spam'));
194 function onEndShowStyles($action)
196 $action->element('style', null,
197 '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
198 '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
202 function onEndPublicGroupNav($nav)
204 $user = common_current_user();
206 if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
207 $nav->out->menuItem(common_local_url('spam'),
209 // TRANS: Menu item title in search group navigation panel.
210 _('Notices marked as spam'),
211 $nav->actionName == 'spam',
212 'nav_timeline_spam');
218 function onPluginVersion(array &$versions)
220 $versions[] = array('name' => 'ActivitySpam',
221 'version' => GNUSOCIAL_VERSION,
222 'author' => 'Evan Prodromou',
223 'homepage' => 'http://status.net/wiki/Plugin:ActivitySpam',
225 _m('Test notices against the Activity Spam service.'));
229 function onEndNoticeInScope($notice, $profile, &$bResult)
231 if ($this->hideSpam) {
234 $score = Spam_score::getKV('notice_id', $notice->id);
236 if (!empty($score) && $score->is_spam) {
237 if (empty($profile) ||
238 ($profile->id !== $notice->profile_id &&
239 !$profile->hasRight(self::REVIEWSPAM))) {
250 * Pre-cache our spam scores if needed.
252 function onEndNoticeListPrefill(array &$notices, array &$profiles, array $notice_ids, Profile $scoped=null) {
253 if ($this->hideSpam) {
254 Spam_score::multiGet('notice_id', $notice_ids);