]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/BlogspamNet/BlogspamNetPlugin.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / BlogspamNet / BlogspamNetPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to check submitted notices with blogspam.net
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Brion Vibber <brion@status.net>
26  * @copyright 2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 define('BLOGSPAMNETPLUGIN_VERSION', '0.1');
36
37 /**
38  * Plugin to check submitted notices with blogspam.net
39  *
40  * When new notices are saved, we check their text with blogspam.net (or
41  * a compatible service).
42  *
43  * Blogspam.net is supposed to catch blog comment spam, and I found that
44  * some of its tests (min/max size, bayesian match) gave a lot of false positives.
45  * So, I've turned those tests off by default. This may not get as many
46  * hits, but it's better than nothing.
47  *
48  * @category Plugin
49  * @package  StatusNet
50  * @author   Evan Prodromou <evan@status.net>
51  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52  * @link     http://status.net/
53  *
54  * @see      Event
55  */
56 class BlogspamNetPlugin extends Plugin
57 {
58     var $baseUrl = 'http://test.blogspam.net:8888/';
59
60     function __construct($url=null)
61     {
62         parent::__construct();
63         if ($url) {
64             $this->baseUrl = $url;
65         }
66     }
67
68     function onStartNoticeSave($notice)
69     {
70         $args = $this->testArgs($notice);
71         common_debug("Blogspamnet args = " . print_r($args, TRUE));
72         $requestBody = xmlrpc_encode_request('testComment', array($args));
73
74         $request = new HTTPClient($this->baseUrl, HTTPClient::METHOD_POST);
75         $request->setHeader('Content-Type', 'text/xml');
76         $request->setBody($requestBody);
77         $httpResponse = $request->send();
78
79         $response = xmlrpc_decode($httpResponse->getBody());
80         if (xmlrpc_is_fault($response)) {
81             throw new ServerException("$response[faultString] ($response[faultCode])", 500);
82         } else {
83             common_debug("Blogspamnet results = " . $response);
84             if (preg_match('/^ERROR(:(.*))?$/', $response, $match)) {
85                 // TRANS: Server exception thrown when blogspam.net returns error status.
86                 // TRANS: %1$s is the base URL, %2$s is the error (unknown contents; no period).
87                 throw new ServerException(sprintf(_m('Error from %1$s: %2$s'), $this->baseUrl, $match[2]), 500);
88             } else if (preg_match('/^SPAM(:(.*))?$/', $response, $match)) {
89                 // TRANS: Server exception thrown when blogspam.net returns spam status.
90                 // TRANS: Does not end with period because of unknown contents for %s (spam match).
91                 throw new ClientException(sprintf(_m('Spam checker results: %s'), $match[2]), 400);
92             } else if (preg_match('/^OK$/', $response)) {
93                 // don't do anything
94             } else {
95                 // TRANS: Server exception thrown when blogspam.net returns an unexpected status.
96                 // TRANS: %1$s is the base URL, %2$s is the response (unknown contents; no period).
97                 throw new ServerException(sprintf(_m('Unexpected response from %1$s: %2$s'), $this->baseUrl, $response), 500);
98             }
99         }
100         return true;
101     }
102
103     function testArgs($notice)
104     {
105         $args = array();
106         $args['comment'] = $notice->content;
107         $args['ip'] = $this->getClientIP();
108
109         if (isset($_SERVER) && array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
110             $args['agent'] = $_SERVER['HTTP_USER_AGENT'];
111         }
112
113         $profile = $notice->getProfile();
114
115         if ($profile && $profile->homepage) {
116             $args['link'] = $profile->homepage;
117         }
118
119         if ($profile && $profile->fullname) {
120             $args['name'] = $profile->fullname;
121         } else {
122             $args['name'] = $profile->nickname;
123         }
124
125         $args['site'] = common_root_url();
126         $args['version'] = $this->userAgent();
127
128         $args['options'] = "max-size=" . common_config('site','textlimit') . ",min-size=0,min-words=0,exclude=bayasian";
129
130         return $args;
131     }
132
133     function getClientIP()
134     {
135         if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
136             // Note: order matters here; use proxy-forwarded stuff first
137             foreach (array('HTTP_X_FORWARDED_FOR', 'CLIENT-IP', 'REMOTE_ADDR') as $k) {
138                 if (isset($_SERVER[$k])) {
139                     return $_SERVER[$k];
140                 }
141             }
142         }
143         return '127.0.0.1';
144     }
145
146     function version()
147     {
148         return BLOGSPAMNETPLUGIN_VERSION;
149     }
150
151     function onPluginVersion(array &$versions)
152     {
153         $versions[] = array('name' => 'BlogspamNet',
154                             'version' => BLOGSPAMNETPLUGIN_VERSION,
155                             'author' => 'Evan Prodromou, Brion Vibber',
156                             'homepage' => 'http://status.net/wiki/Plugin:BlogspamNet',
157                             'rawdescription' =>
158                             // TRANS: Plugin description.
159                             _m('Plugin to check submitted notices with blogspam.net.'));
160         return true;
161     }
162 }