123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class ActivitySpamPlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
- public $server = null;
- public $hideSpam = false;
- const REVIEWSPAM = 'ActivitySpamPlugin::REVIEWSPAM';
- const TRAINSPAM = 'ActivitySpamPlugin::TRAINSPAM';
-
- function initialize()
- {
- $this->filter = new SpamFilter(common_config('activityspam', 'server'),
- common_config('activityspam', 'consumerkey'),
- common_config('activityspam', 'secret'));
- $this->hideSpam = common_config('activityspam', 'hidespam');
-
- common_config_set('db', 'class_location',
- common_config('db', 'class_location') .':'.dirname(__FILE__));
- return true;
- }
-
- function onCheckSchema()
- {
- $schema = Schema::get();
- $schema->ensureTable('spam_score', Spam_score::schemaDef());
- Spam_score::upgrade();
- return true;
- }
-
- function onEndNoticeSave($notice)
- {
- try {
- $result = $this->filter->test($notice);
- $score = Spam_score::saveNew($notice, $result);
- $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
- } catch (Exception $e) {
-
- $this->log(LOG_ERR, $e->getMessage());
- }
- return true;
- }
- function onNoticeDeleteRelated($notice) {
- $score = Spam_score::getKV('notice_id', $notice->id);
- if (!empty($score)) {
- $score->delete();
- }
- return true;
- }
- function onUserRightsCheck($profile, $right, &$result) {
- switch ($right) {
- case self::REVIEWSPAM:
- case self::TRAINSPAM:
- $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
- return false;
- default:
- return true;
- }
- }
- function onGetSpamFilter(&$filter) {
- $filter = $this->filter;
- return false;
- }
- function onEndShowNoticeOptionItems($nli)
- {
- $profile = Profile::current();
- if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
- $notice = $nli->getNotice();
- $out = $nli->getOut();
- if (!empty($notice)) {
- $score = Spam_score::getKV('notice_id', $notice->id);
- if (empty($score)) {
-
- $form = new TrainSpamForm($out, $notice);
- $form->show();
- } else if ($score->is_spam) {
- $form = new TrainHamForm($out, $notice);
- $form->show();
- } else if (!$score->is_spam) {
- $form = new TrainSpamForm($out, $notice);
- $form->show();
- }
- }
- }
- return true;
- }
-
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect('main/train/spam',
- ['action' => 'train',
- 'category' => 'spam']);
- $m->connect('main/train/ham',
- ['action' => 'train',
- 'category' => 'ham']);
- $m->connect('main/spam',
- ['action' => 'spam']);
- return true;
- }
- function onEndShowStyles($action)
- {
- $action->element('style', null,
- '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
- '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
- return true;
- }
- function onEndPublicGroupNav($nav)
- {
- $user = common_current_user();
- if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
- $nav->out->menuItem(common_local_url('spam'),
- _m('MENU','Spam'),
-
- _('Notices marked as spam'),
- $nav->actionName == 'spam',
- 'nav_timeline_spam');
- }
- return true;
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = array('name' => 'ActivitySpam',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Evan Prodromou',
- 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ActivitySpam',
- 'description' =>
- _m('Test notices against the Activity Spam service.'));
- return true;
- }
- function onEndNoticeInScope($notice, $profile, &$bResult)
- {
- if ($this->hideSpam) {
- if ($bResult) {
- $score = Spam_score::getKV('notice_id', $notice->id);
- if (!empty($score) && $score->is_spam) {
- if (empty($profile) ||
- ($profile->id !== $notice->profile_id &&
- !$profile->hasRight(self::REVIEWSPAM))) {
- $bResult = false;
- }
- }
- }
- }
- return true;
- }
-
- function onEndNoticeListPrefill(array &$notices, array &$profiles, array $notice_ids, Profile $scoped=null) {
- if ($this->hideSpam) {
- Spam_score::multiGet('notice_id', $notice_ids);
- }
- return true;
- }
- }
|