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');
72 * Database schema setup
77 * @return boolean hook value; true means continue processing, false means stop.
80 function onCheckSchema()
82 $schema = Schema::get();
83 $schema->ensureTable('spam_score', Spam_score::schemaDef());
85 Spam_score::upgrade();
91 * Load related modules when needed
93 * @param string $cls Name of the class to be loaded
95 * @return boolean hook value; true means continue processing, false means stop.
98 function onAutoload($cls)
100 $dir = dirname(__FILE__);
106 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
109 include_once $dir . '/'.$cls.'.php';
112 case 'SpamNoticeStream':
113 case 'TrainSpamForm':
115 include_once $dir . '/'.strtolower($cls).'.php';
123 * When a notice is saved, check its spam score
125 * @param Notice $notice Notice that was just saved
127 * @return boolean hook value; true means continue processing, false means stop.
130 function onEndNoticeSave($notice)
134 $result = $this->filter->test($notice);
136 $score = Spam_score::saveNew($notice, $result);
138 $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
140 } catch (Exception $e) {
142 $this->log(LOG_ERR, $e->getMessage());
148 function onNoticeDeleteRelated($notice) {
149 $score = Spam_score::staticGet('notice_id', $notice->id);
150 if (!empty($score)) {
156 function onUserRightsCheck($profile, $right, &$result) {
158 case self::REVIEWSPAM:
159 case self::TRAINSPAM:
160 $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
167 function onGetSpamFilter(&$filter) {
168 $filter = $this->filter;
172 function onEndShowNoticeOptionItems($nli)
174 $profile = Profile::current();
176 if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
178 $notice = $nli->getNotice();
179 $out = $nli->getOut();
181 if (!empty($notice)) {
183 $score = Spam_score::staticGet('notice_id', $notice->id);
186 $this->debug("No score for notice " . $notice->id);
187 // XXX: show a question-mark or something
188 } else if ($score->is_spam) {
189 $form = new TrainHamForm($out, $notice);
191 } else if (!$score->is_spam) {
192 $form = new TrainSpamForm($out, $notice);
202 * Map URLs to actions
204 * @param Net_URL_Mapper $m path-to-action mapper
206 * @return boolean hook value; true means continue processing, false means stop.
209 function onRouterInitialized($m)
211 $m->connect('main/train/spam',
212 array('action' => 'train', 'category' => 'spam'));
213 $m->connect('main/train/ham',
214 array('action' => 'train', 'category' => 'ham'));
215 $m->connect('main/spam',
216 array('action' => 'spam'));
220 function onEndShowStyles($action)
222 $action->element('style', null,
223 '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
224 '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
228 function onEndPublicGroupNav($nav)
230 $user = common_current_user();
232 if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
233 $nav->out->menuItem(common_local_url('spam'),
235 // TRANS: Menu item title in search group navigation panel.
236 _('Notices marked as spam'),
237 $nav->actionName == 'spam',
238 'nav_timeline_spam');
244 function onPluginVersion(&$versions)
246 $versions[] = array('name' => 'ActivitySpam',
247 'version' => STATUSNET_VERSION,
248 'author' => 'Evan Prodromou',
249 'homepage' => 'http://status.net/wiki/Plugin:ActivitySpam',
251 _m('Test notices against the Activity Spam service.'));
255 function onEndNoticeInScope($notice, $profile, &$bResult)
257 if ($this->hideSpam) {
260 $score = Spam_score::staticGet('notice_id', $notice->id);
262 if (!empty($score) && $score->is_spam) {
263 if (empty($profile) ||
264 ($profile->id !== $notice->profile_id &&
265 !$profile->hasRight(self::REVIEWSPAM))) {
276 * Pre-cache our spam scores if needed.
278 function onEndNoticeListPrefill(&$notices, &$profiles, $avatarSize) {
279 if ($this->hideSpam) {
280 foreach ($notices as $notice) {
281 $ids[] = $notice->id;
283 Memcached_DataObject::multiGet('Spam_score', 'notice_id', $ids);