]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivitySpam/train.php
Merge ActivitySpam plugin
[quix0rs-gnu-social.git] / plugins / ActivitySpam / train.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2012, StatusNet, Inc.
5  *
6  * Train a notice as spam
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 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  * Train a notice as spam
39  *
40  * @category  Spam
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 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
48 class TrainAction extends Action
49 {
50     protected $notice = null;
51     protected $filter = null;
52     protected $category = null;
53
54     /**
55      * For initializing members of the class.
56      *
57      * @param array $argarray misc. arguments
58      *
59      * @return boolean true
60      */
61
62     function prepare($argarray)
63     {
64         parent::prepare($argarray);
65
66         // User must be logged in.
67
68         $user = common_current_user();
69
70         if (empty($user)) {
71             throw new ClientException(_("You must be logged in to train spam."), 403);
72         }
73
74         // User must have the right to review spam
75
76         if (!$user->hasRight(ActivitySpamPlugin::TRAINSPAM)) {
77             throw new ClientException(_('You cannot review spam on this site.'), 403);
78         }
79
80         $id = $this->trimmed('notice');
81
82         $this->notice = Notice::staticGet('id', $id);
83
84         if (empty($this->notice)) {
85             throw new ClientException(_("No such notice."));
86         }
87
88         $this->checkSessionToken();
89
90         $filter = null;
91
92         Event::handle('GetSpamFilter', array(&$filter));
93
94         if (empty($filter)) {
95             throw new ServerException(_("No spam filter configured."));
96         }
97
98         $this->filter = $filter;
99
100         $this->category = $this->trimmed('category');
101
102         if ($this->category !== SpamFilter::SPAM &&
103             $this->category !== SpamFilter::HAM)
104         {
105             throw new ClientException(_("No such category."));
106         }
107
108         return true;
109     }
110
111     /**
112      * Handler method
113      *
114      * @param array $argarray is ignored since it's now passed in in prepare()
115      *
116      * @return void
117      */
118
119     function handle($argarray=null)
120     {
121         // Train
122
123         $this->filter->trainOnError($this->notice, $this->category);
124
125         // Re-test
126
127         $result = $this->filter->test($this->notice);
128
129         // Update or insert
130
131         $score = Spam_score::save($this->notice, $result);
132
133         // Show new toggle form
134
135         if ($this->category === SpamFilter::SPAM) {
136             $form = new TrainHamForm($this, $this->notice);
137         } else {
138             $form = new TrainSpamForm($this, $this->notice);
139         }
140
141         if ($this->boolean('ajax')) {
142             $this->startHTML('text/xml;charset=utf-8');
143             $this->elementStart('head');
144             // TRANS: Page title for page on which favorite notices can be unfavourited.
145             $this->element('title', null, _('Disfavor favorite.'));
146             $this->elementEnd('head');
147             $this->elementStart('body');
148             $form->show();
149             $this->elementEnd('body');
150             $this->elementEnd('html');
151         } else {
152             common_redirect(common_local_url('spam'), 303);
153         }
154     }
155 }