Passed
Push — master ( 00519f...366a1b )
by Andreas
17:30
created

net_nehmer_comments_spamchecker::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.comments
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Helper class for spam checks
11
 *
12
 * @package net.nehmer.comments
13
 */
14
class net_nehmer_comments_spamchecker
15
{
16
    const ERROR = -1;
17
    const SPAM = 0;
18
    const HAM = 1;
19
20
    /**
21
     * Check the post against possible spam filters.
22
     *
23
     * This will update post status on the background and log the information.
24
     */
25
    public static function check(net_nehmer_comments_comment $comment)
26
    {
27
        $ret = self::check_linksleeve($comment->title . ' ' . $comment->content . ' ' . $comment->author);
28
29
        if ($ret == self::HAM) {
30
            // Quality content
31
            debug_add("Linksleeve noted comment \"{$comment->title}\" ({$comment->guid}) as ham");
32
33
            $comment->moderate(net_nehmer_comments_comment::MODERATED, 'reported_not_junk', 'linksleeve');
34
        } elseif ($ret == self::SPAM) {
35
            // Spam
36
            debug_add("Linksleeve noted comment \"{$comment->title}\" ({$comment->guid}) as spam");
37
38
            $comment->moderate(net_nehmer_comments_comment::JUNK, 'confirmed_junk', 'linksleeve');
39
        }
40
    }
41
42
    private static function check_linksleeve($text) : int
43
    {
44
        $data = 'content=' . $text;
45
        $buf = "";
46
47
        $fp = fsockopen("www.linksleeve.org", 80, $errno, $errstr, 30);
48
        if ($fp === false) {
49
            debug_add('Connection failure: ' . $errstr, MIDCOM_LOG_WARN);
50
            return self::ERROR;
51
        }
52
        $header  = "POST /pslv.php HTTP/1.0\r\n";
53
        $header .= "Host: www.linksleeve.org\r\n";
54
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
55
        $header .= "Content-Length: " . strlen($data) . "\r\n";
56
        $header .= "User-Agent: Mozilla/4.0 (compatible: MSIE 7.0; Windows NT 6.0)\r\n";
57
        $header .= "Connection: close\r\n\r\n";
58
        $header .= $data;
59
60
        fwrite($fp, $header, strlen($header));
61
62
        while (!feof($fp)) {
63
            $buf .= fgets($fp, 128);
64
        }
65
66
        fclose($fp);
67
68
        if (!stristr($buf, "-slv-1-/slv-")) {
69
            return self::SPAM;
70
        }
71
        return self::HAM;
72
    }
73
}
74