1<?php
2
3/**
4 * Akismet plugin for use with the Linkback Plugin for DokuWiki
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Gina Haeussge <osd@foosel.net>
8 * @author     Andreas Gohr <gohr@cosmocode.de>
9 */
10
11class action_plugin_linkback_akismet extends DokuWiki_Action_Plugin {
12
13    /**
14     * register the eventhandlers
15     */
16    function register(Doku_Event_Handler $controller) {
17        $controller->register_hook('ACTION_LINKBACK_RECEIVED', 'BEFORE', $this, 'handle_linkback_received', array ());
18        $controller->register_hook('ACTION_LINKBACK_HAM', 'AFTER', $this, 'handle_linkback_ham', array ());
19        $controller->register_hook('ACTION_LINKBACK_SPAM', 'AFTER', $this, 'handle_linkback_spam', array ());
20    }
21
22    /**
23     * Handler for the ACTION_LINKBACK_RECEIVED event
24     */
25    function handle_linkback_received(Doku_Event $event, $param) {
26        $linkback = $event->data['linkback'];
27
28        $tools = plugin_load('tools', 'linkback');
29
30        if (!$this->getConf('akismet_enable') || !$this->getConf('akismet_apikey'))
31            return;
32
33        $data = $this->_prepareData($linkback);
34        if ($this->_checkForSpam($data)) {
35            $event->data['log'][] = "\tAkismet marked linkback as spam";
36            $event->data['show'] = false;
37            if (!$this->getConf('akismet_moderate'))
38                $event->preventDefault();
39            else
40            	$event->data['log'][] = "\t -> moderated";
41        } else {
42        	$event->data['log'][] = "\tAkismet marked linkback as ham";
43        }
44    }
45
46    /**
47     * Handler for the ACTION_LINKBACK_HAM event
48     */
49    function handle_linkback_ham(Doku_Event $event, $params) {
50        $linkback = $event->data['linkback'];
51
52        if (!$this->getConf('akismet_enabled') || !$this->getConf('akismet_apikey'))
53            return;
54
55        $data = $this->_prepareData($linkback);
56        $this->_reportHam($data);
57    }
58
59    /**
60     * Handler for the ACTION_LINKBACK_SPAM event
61     */
62    function handle_linkback_spam(Doku_Event $event, $params) {
63        $linkback = $event->data['linkback'];
64
65        if (!$this->getConf('akismet_enabled') || !$this->getConf('akismet_apikey'))
66            return;
67
68        $data = $this->_prepareData($linkback);
69        $this->_reportSpam($data);
70    }
71
72    /**
73     * Submit the data to the Akismet comment-check webservice and return whether it was
74     * classified as spam (true) or ham (false)
75     */
76    function _checkForSpam($data) {
77        $resp = $this->_submitData('comment-check', $data);
78        if ($resp == 'true')
79            return true;
80        return false;
81    }
82
83    /**
84     * Submit the data to the Akismet submit-ham webservice
85     */
86    function _reportHam($data) {
87        $this->_submitData('submit-ham', $data);
88    }
89
90    /**
91     * Submit the data to the Akismet submit-spam webservice
92     */
93    function _reportSpam($data) {
94        $this->_submitData('submit-spam', $data);
95    }
96
97    /**
98     * Prepares the data to send to Akismet
99     */
100    function _prepareData($linkback) {
101        return array (
102            'blog' => DOKU_URL,
103            'user_ip' => $linkback['submitter_ip'],
104            'user_agent' => $linkback['submitter_useragent'],
105            'referrer' => $linkback['submitter_referer'],
106            'comment_author_url' => $linkback['url'],
107            'comment_type' => $linkback['type'],
108            'comment_content' => $linkback['raw_excerpt'],
109        );
110    }
111
112    /**
113     * Submits the given data to the given Akismet service.
114     *
115     * @param string $function Akismet service to use. Can be
116     *                          'comment-check', 'submit-ham' or
117     *                          'submit-spam'
118     * @param array $data Linkback data to submit
119     * @return          string  The response of Akismet
120     */
121    function _submitData($function, $data) {
122        $info = $this->getInfo();
123        $http = new dokuwiki\HTTP\DokuHTTPClient();
124        // The Aksimet guys ask for a verbose UserAgent:
125        $http->agent = 'DokuWiki/' . getVersion() . ' | ' . $info['name'] . '/' . $info['date'];
126        $http->timeout = 5;
127        return $http->post('https://' . $this->getConf('akismet_apikey') . '.rest.akismet.com/1.1/comment-check', $data);
128    }
129
130}
131