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 $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 // FIXME: Cannot use type-hint NoticeListItem as NoticeListItemAdapter exists, too!
148 assert(is_object($nli));
150 $profile = Profile::current();
152 if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
154 $notice = $nli->getNotice();
155 $out = $nli->getOut();
157 if (!empty($notice)) {
159 $score = Spam_score::getKV('notice_id', $notice->id);
162 // If it's empty, we can train it.
163 $form = new TrainSpamForm($out, $notice);
165 } else if ($score->is_spam) {
166 $form = new TrainHamForm($out, $notice);
168 } else if (!$score->is_spam) {
169 $form = new TrainSpamForm($out, $notice);
179 * Map URLs to actions
181 * @param URLMapper $m path-to-action mapper
183 * @return boolean hook value; true means continue processing, false means stop.
186 public function onRouterInitialized(URLMapper $m)
188 $m->connect('main/train/spam',
189 array('action' => 'train', 'category' => 'spam'));
190 $m->connect('main/train/ham',
191 array('action' => 'train', 'category' => 'ham'));
192 $m->connect('main/spam',
193 array('action' => 'spam'));
197 function onEndShowStyles(Action $action)
199 $action->element('style', null,
200 '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
201 '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
205 function onEndPublicGroupNav(Menu $nav)
207 $user = common_current_user();
209 if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
210 $nav->out->menuItem(common_local_url('spam'),
212 // TRANS: Menu item title in search group navigation panel.
213 _('Notices marked as spam'),
214 $nav->actionName == 'spam',
215 'nav_timeline_spam');
221 function onPluginVersion(array &$versions)
223 $versions[] = array('name' => 'ActivitySpam',
224 'version' => GNUSOCIAL_VERSION,
225 'author' => 'Evan Prodromou',
226 'homepage' => 'http://status.net/wiki/Plugin:ActivitySpam',
228 _m('Test notices against the Activity Spam service.'));
232 function onEndNoticeInScope(Notice $notice, Profile $profile, &$bResult)
234 if ($this->hideSpam) {
237 $score = Spam_score::getKV('notice_id', $notice->id);
239 if (!empty($score) && $score->is_spam) {
240 if (empty($profile) ||
241 ($profile->id !== $notice->profile_id &&
242 !$profile->hasRight(self::REVIEWSPAM))) {
253 * Pre-cache our spam scores if needed.
255 function onEndNoticeListPrefill(array &$notices, array &$profiles, array $notice_ids, Profile $scoped=null) {
256 if ($this->hideSpam) {
257 Spam_score::multiGet('notice_id', $notice_ids);