]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/BlogspamNetPlugin.php
602ced6614148c063caa4123b5e8c54b7654e293
[quix0rs-gnu-social.git] / plugins / 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  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 define('BLOGSPAMNETPLUGIN_VERSION', '0.1');
35
36 /**
37  * Plugin to check submitted notices with blogspam.net
38  *
39  * When new notices are saved, we check their text with blogspam.net (or
40  * a compatible service).
41  *
42  * Blogspam.net is supposed to catch blog comment spam, and I found that
43  * some of its tests (min/max size, bayesian match) gave a lot of false positives.
44  * So, I've turned those tests off by default. This may not get as many
45  * hits, but it's better than nothing.
46  *
47  * @category Plugin
48  * @package  StatusNet
49  * @author   Evan Prodromou <evan@status.net>
50  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51  * @link     http://status.net/
52  *
53  * @see      Event
54  */
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         $request = xmlrpc_encode_request('testComment', array($args));
73         $context = stream_context_create(array('http' => array('method' => "POST",
74                                                                'header' =>
75                                                                "Content-Type: text/xml\r\n".
76                                                                "User-Agent: " . $this->userAgent(),
77                                                                'content' => $request)));
78         $file = file_get_contents($this->baseUrl, false, $context);
79         $response = xmlrpc_decode($file);
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                 throw new ServerException(sprintf(_("Error from %s: %s"), $this->baseUrl, $match[2]), 500);
86             } else if (preg_match('/^SPAM(:(.*))?$/', $response, $match)) {
87                 throw new ClientException(sprintf(_("Spam checker results: %s"), $match[2]), 400);
88             } else if (preg_match('/^OK$/', $response)) {
89                 // don't do anything
90             } else {
91                 throw new ServerException(sprintf(_("Unexpected response from %s: %s"), $this->baseUrl, $response), 500);
92             }
93         }
94         return true;
95     }
96
97     function testArgs($notice)
98     {
99         $args = array();
100         $args['comment'] = $notice->content;
101         $args['ip'] = $this->getClientIP();
102
103         if (isset($_SERVER) && array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
104             $args['agent'] = $_SERVER['HTTP_USER_AGENT'];
105         }
106
107         $profile = $notice->getProfile();
108
109         if ($profile && $profile->homepage) {
110             $args['link'] = $profile->homepage;
111         }
112
113         if ($profile && $profile->fullname) {
114             $args['name'] = $profile->fullname;
115         } else {
116             $args['name'] = $profile->nickname;
117         }
118
119         $args['site'] = common_root_url();
120         $args['version'] = $this->userAgent();
121
122         $args['options'] = "max-size=140,min-size=0,min-words=0,exclude=bayasian";
123
124         return $args;
125     }
126
127     function getClientIP()
128     {
129         if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
130             // Note: order matters here; use proxy-forwarded stuff first
131             foreach (array('HTTP_X_FORWARDED_FOR', 'CLIENT-IP', 'REMOTE_ADDR') as $k) {
132                 if (isset($_SERVER[$k])) {
133                     return $_SERVER[$k];
134                 }
135             }
136         }
137         return '127.0.0.1';
138     }
139
140     function userAgent()
141     {
142         return 'BlogspamNetPlugin/'.BLOGSPAMNETPLUGIN_VERSION . ' StatusNet/' . LACONICA_VERSION;
143     }
144 }