]> 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 $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         // FIXME: Cannot use type-hint NoticeListItem as NoticeListItemAdapter exists, too!
148         assert(is_object($nli));
149
150         $profile = Profile::current();
151
152         if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
153
154             $notice = $nli->getNotice();
155             $out = $nli->getOut();
156
157             if (!empty($notice)) {
158
159                 $score = Spam_score::getKV('notice_id', $notice->id);
160
161                 if (empty($score)) {
162                     // If it's empty, we can train it.
163                     $form = new TrainSpamForm($out, $notice);
164                     $form->show();
165                 } else if ($score->is_spam) {
166                     $form = new TrainHamForm($out, $notice);
167                     $form->show();
168                 } else if (!$score->is_spam) {
169                     $form = new TrainSpamForm($out, $notice);
170                     $form->show();
171                 }
172             }
173         }
174
175         return true;
176     }
177     
178     /**
179      * Map URLs to actions
180      *
181      * @param URLMapper $m path-to-action mapper
182      *
183      * @return boolean hook value; true means continue processing, false means stop.
184      */
185
186     public function onRouterInitialized(URLMapper $m)
187     {
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'));
194         return true;
195     }
196
197     function onEndShowStyles(Action $action)
198     {
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 } ');
202         return true;
203     }
204
205     function onEndPublicGroupNav(Menu $nav)
206     {
207         $user = common_current_user();
208
209         if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
210             $nav->out->menuItem(common_local_url('spam'),
211                                 _m('MENU','Spam'),
212                                 // TRANS: Menu item title in search group navigation panel.
213                                 _('Notices marked as spam'),
214                                 $nav->actionName == 'spam',
215                                 'nav_timeline_spam');
216         }
217
218         return true;
219     }
220
221     function onPluginVersion(array &$versions)
222     {
223         $versions[] = array('name' => 'ActivitySpam',
224                             'version' => GNUSOCIAL_VERSION,
225                             'author' => 'Evan Prodromou',
226                             'homepage' => 'http://status.net/wiki/Plugin:ActivitySpam',
227                             'description' =>
228                             _m('Test notices against the Activity Spam service.'));
229         return true;
230     }
231
232     function onEndNoticeInScope(Notice $notice, Profile $profile, &$bResult)
233     {
234         if ($this->hideSpam) {
235             if ($bResult) {
236
237                 $score = Spam_score::getKV('notice_id', $notice->id);
238
239                 if (!empty($score) && $score->is_spam) {
240                     if (empty($profile) ||
241                         ($profile->id !== $notice->profile_id &&
242                          !$profile->hasRight(self::REVIEWSPAM))) {
243                         $bResult = false;
244                     }
245                 }
246             }
247         }
248
249         return true;
250     }
251
252     /**
253      * Pre-cache our spam scores if needed.
254      */
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);
258         }
259         return true;
260     }
261 }