1<?php
2/**
3 *
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Michiel Dethmers <hello@botbouncer.org>
6 */
7
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_botbouncer extends DokuWiki_Action_Plugin {
15
16    /**
17     * return some info
18     */
19    function getInfo(){
20        return confToHash(dirname(__FILE__).'/plugin.info.txt');
21    }
22
23    /**
24     * register the eventhandlers and initialize some options
25     */
26    function register(Doku_Event_Handler $controller){
27        $controller->register_hook('DOKUWIKI_STARTED',
28                                   'BEFORE',
29                                   $this,
30                                   'handle_start',
31                                   array());
32    }
33
34    function handle_start(&$event, $param) {
35        ## handle whitelist
36        $whitelist_ips = explode(',',$this->getConf('whitelist'));
37        $whitelist_ips = array_map('trim', $whitelist_ips);
38        if (in_array($_SERVER['REMOTE_ADDR'],$whitelist_ips)) {
39        return;
40        }
41        $ips = array();
42        if (isset($_SERVER['REMOTE_ADDR'])) {
43          $ips[] = $_SERVER['REMOTE_ADDR'];
44          if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
45            $ips[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
46          }
47        }
48
49        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
50            $isRegister = 0;
51            if ($_REQUEST['do'] == 'register' && $_POST['save'] == 1) {
52                $email = $_POST['email'];
53                $username = $_POST['login'];
54                $content = $_POST['fullname'];
55                $isRegister = 1;
56            } else {
57                $username = $_SESSION[DOKU_COOKIE]['auth']['info']['name'];
58                $email = $_SESSION[DOKU_COOKIE]['auth']['info']['mail'];
59                $content = ''; ##@@TODO find some field to pass on here
60            }
61
62           // error_reporting(E_ALL);
63           // ini_set('display_errors',true);
64            $honeypotApiKey = $this->getConf('honeypotapikey');
65            $akismetApiKey = $this->getConf('akismetapikey');
66            $akismetUrl = $this->getConf('akismetblogurl');
67            $mollomPublicKey = $this->getConf('mollompublickey');
68            $mollomPrivateKey = $this->getConf('mollomprivatekey');
69            $continue = $this->getConf('continue');
70            $spamError = $this->getConf('spamerror');
71            include dirname(__FILE__).'/lib/botbouncer.php';
72            $fsc = new botBouncer($honeypotApiKey,$akismetApiKey,$akismetUrl,$mollomPrivateKey,$mollomPublicKey);
73            $fsc->setLogRoot($GLOBALS['conf']['cachedir']);
74            if ($fsc->isSpam(
75              array(
76        #         'test' => 'spam',
77        #         'test' => 'ham',
78                'username' => $username,
79                'email' => $email,
80                'content' => $content,
81                'ips' => $ips,
82              ),
83              !empty($continue)
84            )) {
85
86            $logLine = time()."\t".$fsc->matchedBy. "\t".$fsc->matchedOn;
87            unset($_POST['save']);
88            if (!$isRegister) {
89              ## @@TODO return a "nice error" ie in the page
90              ## whilst blocking any further action
91                print $spamError;exit;
92            }
93        } else {
94            $logLine = time().' no match';
95            //print "This is ham";
96        }
97        file_put_contents($GLOBALS['conf']['cachedir'].'/botbouncer.log',$logLine."\n",FILE_APPEND);
98      }
99    }
100
101}
102
103
104