]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivitySpam/ActivitySpamPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / ActivitySpam / ActivitySpamPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011,2012, StatusNet, Inc.
5  *
6  * ActivitySpam Plugin
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Spam
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Check new notices with activity spam service.
39  * 
40  * @category  Spam
41  * @package   StatusNet
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/
46  */
47 class ActivitySpamPlugin extends Plugin
48 {
49     public $server = null;
50     public $hideSpam = false;
51
52     const REVIEWSPAM = 'ActivitySpamPlugin::REVIEWSPAM';
53     const TRAINSPAM = 'ActivitySpamPlugin::TRAINSPAM';
54
55     /**
56      * Initializer
57      *
58      * @return boolean hook value; true means continue processing, false means stop.
59      */
60     function initialize()
61     {
62         $this->filter = new SpamFilter(common_config('activityspam', 'server'),
63                                        common_config('activityspam', 'consumerkey'),
64                                        common_config('activityspam', 'secret'));
65
66         $this->hideSpam = common_config('activityspam', 'hidespam');
67
68         // Let DB_DataObject find Spam_score
69
70         common_config_set('db', 'class_location', 
71                           common_config('db', 'class_location') .':'.dirname(__FILE__));
72
73         return true;
74     }
75
76     /**
77      * Database schema setup
78      *
79      * @see Schema
80      * @see ColumnDef
81      *
82      * @return boolean hook value; true means continue processing, false means stop.
83      */
84
85     function onCheckSchema()
86     {
87         $schema = Schema::get();
88         $schema->ensureTable('spam_score', Spam_score::schemaDef());
89
90         Spam_score::upgrade();
91
92         return true;
93     }
94
95     /**
96      * When a notice is saved, check its spam score
97      * 
98      * @param Notice $notice Notice that was just saved
99      *
100      * @return boolean hook value; true means continue processing, false means stop.
101      */
102
103     function onEndNoticeSave($notice)
104     {
105         try {
106
107             $result = $this->filter->test($notice);
108
109             $score = Spam_score::saveNew($notice, $result);
110
111             $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
112
113         } catch (Exception $e) {
114             // Log but continue 
115             $this->log(LOG_ERR, $e->getMessage());
116         }
117
118         return true;
119     }
120
121     function onNoticeDeleteRelated(Notice $notice) {
122         $score = Spam_score::getKV('notice_id', $notice->id);
123         if (!empty($score)) {
124             $score->delete();
125         }
126         return true;
127     }
128
129     function onUserRightsCheck(Profile $profile, $right, &$result) {
130         switch ($right) {
131         case self::REVIEWSPAM:
132         case self::TRAINSPAM:
133             $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
134             return false;
135         default:
136             return true;
137         }
138     }
139
140     function onGetSpamFilter(&$filter) {
141         $filter = $this->filter;
142         return false;
143     }
144
145     function onEndShowNoticeOptionItems($nli)
146     {
147         $profile = Profile::current();
148
149         if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
150
151             $notice = $nli->getNotice();
152             $out = $nli->getOut();
153
154             if (!empty($notice)) {
155
156                 $score = Spam_score::getKV('notice_id', $notice->id);
157
158                 if (empty($score)) {
159                     // If it's empty, we can train it.
160                     $form = new TrainSpamForm($out, $notice);
161                     $form->show();
162                 } else if ($score->is_spam) {
163                     $form = new TrainHamForm($out, $notice);
164                     $form->show();
165                 } else if (!$score->is_spam) {
166                     $form = new TrainSpamForm($out, $notice);
167                     $form->show();
168                 }
169             }
170         }
171
172         return true;
173     }
174     
175     /**
176      * Map URLs to actions
177      *
178      * @param URLMapper $m path-to-action mapper
179      *
180      * @return boolean hook value; true means continue processing, false means stop.
181      */
182
183     public function onRouterInitialized(URLMapper $m)
184     {
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'));
191         return true;
192     }
193
194     function onEndShowStyles(Action $action)
195     {
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 } ');
199         return true;
200     }
201
202     function onEndPublicGroupNav($nav)
203     {
204         $user = common_current_user();
205
206         if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
207             $nav->out->menuItem(common_local_url('spam'),
208                                 _m('MENU','Spam'),
209                                 // TRANS: Menu item title in search group navigation panel.
210                                 _('Notices marked as spam'),
211                                 $nav->actionName == 'spam',
212                                 'nav_timeline_spam');
213         }
214
215         return true;
216     }
217
218     function onPluginVersion(array &$versions)
219     {
220         $versions[] = array('name' => 'ActivitySpam',
221                             'version' => GNUSOCIAL_VERSION,
222                             'author' => 'Evan Prodromou',
223                             'homepage' => 'http://status.net/wiki/Plugin:ActivitySpam',
224                             'description' =>
225                             _m('Test notices against the Activity Spam service.'));
226         return true;
227     }
228
229     function onEndNoticeInScope($notice, $profile, &$bResult)
230     {
231         if ($this->hideSpam) {
232             if ($bResult) {
233
234                 $score = Spam_score::getKV('notice_id', $notice->id);
235
236                 if (!empty($score) && $score->is_spam) {
237                     if (empty($profile) ||
238                         ($profile->id !== $notice->profile_id &&
239                          !$profile->hasRight(self::REVIEWSPAM))) {
240                         $bResult = false;
241                     }
242                 }
243             }
244         }
245
246         return true;
247     }
248
249     /**
250      * Pre-cache our spam scores if needed.
251      */
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);
255         }
256         return true;
257     }
258 }