]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivitySpam/ActivitySpamPlugin.php
e9c481e3bc7487adb3f3f72e002165c5fd1e1bb5
[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      * Load related modules when needed
97      *
98      * @param string $cls Name of the class to be loaded
99      *
100      * @return boolean hook value; true means continue processing, false means stop.
101      */
102
103     function onAutoload($cls)
104     {
105         $dir = dirname(__FILE__);
106
107         switch ($cls)
108         {
109         case 'TrainAction':
110         case 'SpamAction':
111             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
112             return false;
113         case 'Spam_score':
114             include_once $dir . '/'.$cls.'.php';
115             return false;
116         case 'SpamFilter':
117         case 'SpamNoticeStream':
118         case 'TrainSpamForm':
119         case 'TrainHamForm':
120             include_once $dir . '/'.strtolower($cls).'.php';
121             return false;
122         default:
123             return true;
124         }
125     }
126
127     /**
128      * When a notice is saved, check its spam score
129      * 
130      * @param Notice $notice Notice that was just saved
131      *
132      * @return boolean hook value; true means continue processing, false means stop.
133      */
134
135     function onEndNoticeSave($notice)
136     {
137         try {
138
139             $result = $this->filter->test($notice);
140
141             $score = Spam_score::saveNew($notice, $result);
142
143             $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
144
145         } catch (Exception $e) {
146             // Log but continue 
147             $this->log(LOG_ERR, $e->getMessage());
148         }
149
150         return true;
151     }
152
153     function onNoticeDeleteRelated($notice) {
154         $score = Spam_score::staticGet('notice_id', $notice->id);
155         if (!empty($score)) {
156             $score->delete();
157         }
158         return true;
159     }
160
161     function onUserRightsCheck($profile, $right, &$result) {
162         switch ($right) {
163         case self::REVIEWSPAM:
164         case self::TRAINSPAM:
165             $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
166             return false;
167         default:
168             return true;
169         }
170     }
171
172     function onGetSpamFilter(&$filter) {
173         $filter = $this->filter;
174         return false;
175     }
176
177     function onEndShowNoticeOptionItems($nli)
178     {
179         $profile = Profile::current();
180
181         if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
182
183             $notice = $nli->getNotice();
184             $out = $nli->getOut();
185
186             if (!empty($notice)) {
187
188                 $score = Spam_score::staticGet('notice_id', $notice->id);
189
190                 if (empty($score)) {
191                     $this->debug("No score for notice " . $notice->id);
192                     // XXX: show a question-mark or something
193                 } else if ($score->is_spam) {
194                     $form = new TrainHamForm($out, $notice);
195                     $form->show();
196                 } else if (!$score->is_spam) {
197                     $form = new TrainSpamForm($out, $notice);
198                     $form->show();
199                 }
200             }
201         }
202
203         return true;
204     }
205     
206     /**
207      * Map URLs to actions
208      *
209      * @param Net_URL_Mapper $m path-to-action mapper
210      *
211      * @return boolean hook value; true means continue processing, false means stop.
212      */
213
214     function onRouterInitialized($m)
215     {
216         $m->connect('main/train/spam',
217                     array('action' => 'train', 'category' => 'spam'));
218         $m->connect('main/train/ham',
219                     array('action' => 'train', 'category' => 'ham'));
220         $m->connect('main/spam',
221                     array('action' => 'spam'));
222         return true;
223     }
224
225     function onEndShowStyles($action)
226     {
227         $action->element('style', null,
228                          '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
229                          '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
230         return true;
231     }
232
233     function onEndPublicGroupNav($nav)
234     {
235         $user = common_current_user();
236
237         if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
238             $nav->out->menuItem(common_local_url('spam'),
239                                 _m('MENU','Spam'),
240                                 // TRANS: Menu item title in search group navigation panel.
241                                 _('Notices marked as spam'),
242                                 $nav->actionName == 'spam',
243                                 'nav_timeline_spam');
244         }
245
246         return true;
247     }
248
249     function onPluginVersion(&$versions)
250     {
251         $versions[] = array('name' => 'ActivitySpam',
252                             'version' => STATUSNET_VERSION,
253                             'author' => 'Evan Prodromou',
254                             'homepage' => 'http://status.net/wiki/Plugin:ActivitySpam',
255                             'description' =>
256                             _m('Test notices against the Activity Spam service.'));
257         return true;
258     }
259
260     function onEndNoticeInScope($notice, $profile, &$bResult)
261     {
262         if ($this->hideSpam) {
263             if ($bResult) {
264
265                 $score = Spam_score::staticGet('notice_id', $notice->id);
266
267                 if (!empty($score) && $score->is_spam) {
268                     if (empty($profile) ||
269                         ($profile->id !== $notice->profile_id &&
270                          !$profile->hasRight(self::REVIEWSPAM))) {
271                         $bResult = false;
272                     }
273                 }
274             }
275         }
276
277         return true;
278     }
279
280     /**
281      * Pre-cache our spam scores if needed.
282      */
283     function onEndNoticeListPrefill(&$notices, &$profiles, $avatarSize) {
284         if ($this->hideSpam) {
285             foreach ($notices as $notice) {
286                 $ids[] = $notice->id;
287             }
288             Memcached_DataObject::multiGet('Spam_score', 'notice_id', $ids);
289         }
290         return true;
291     }
292 }