1<?php
2/**
3 *
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Andreas Gohr <gohr@cosmocode.de>
6 */
7
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10define('BB2_CWD', dirname(__FILE__));
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'action.php');
14
15class action_plugin_badbehaviour extends DokuWiki_Action_Plugin {
16
17    /**
18     * return some info
19     */
20    function getInfo(){
21        return confToHash(dirname(__FILE__).'/plugin.info.txt');
22    }
23
24    /**
25     * register the eventhandlers and initialize some options
26     */
27    function register(Doku_Event_Handler $controller){
28
29        $controller->register_hook('DOKUWIKI_STARTED',
30                                   'BEFORE',
31                                   $this,
32                                   'handle_start',
33                                   array());
34
35        $controller->register_hook('TPL_METAHEADER_OUTPUT',
36                                   'BEFORE',
37                                   $this,
38                                   'handle_metaheaders',
39                                   array());
40    }
41
42    /**
43     * Do the magic
44     */
45    function handle_start(&$event, $param){
46        require_once(BB2_CWD . "/bad-behavior/version.inc.php");
47        require_once(BB2_CWD . "/bad-behavior/core.inc.php");
48
49        bb2_start(array( 'log_table'     => 'badbehaviour',
50                         'display_stats' => true,
51                         'strict'        => false,
52                         'verbose'       => false,
53                         'skipblackhole' => $this->getConf('skipblackhole')
54                 ));
55    }
56
57    /**
58     * Extend the meta headers
59     */
60    function handle_metaheaders(&$event, $param){
61        global $bb2_javascript;
62        if (!$bb2_javascript) return;
63
64        // we just use some values from the bb2 script but build our own (better) version
65
66        preg_match('/(bb2_addLoadEvent\(function\(\) \{)(.*)(\}\);)/s',$bb2_javascript,$match);
67        $data = 'jQuery(function(){'.$match[2].'});';
68
69        $event->data['script'][] = array( 'type'=>'text/javascript', 'charset'=>'utf-8', '_data'=>$data);
70    }
71
72}
73
74/*
75 * Bad Behavior expects certain global functions. Mostly related to the DB logging,
76 * which is not used in DokuWiki
77 */
78
79function bb2_relative_path() { return DOKU_BASE; }
80function bb2_db_date() { return gmdate('Y-m-d H:i:s'); }
81function bb2_db_affected_rows() { return false; }
82function bb2_db_escape($string) { return $string; }
83function bb2_db_num_rows($result) { return ($result === FALSE) ? 0 : count($result); }
84function bb2_db_query($query) { return false; }
85function bb2_db_rows($result) { return $result; }
86function bb2_email() {
87    $bb2 =& plugin_load('action','badbehaviour');
88    return $bb2->getConf('email');
89}
90
91/**
92 * This is an extension hook provided by BB2, we use it to do our
93 * own logging.
94 */
95function bb2_banned_callback($settings, $package, $key){
96    global $conf;
97
98    $data = array();
99    $data[] = time();
100    $data[] = stripctl($package['ip']);
101    $data[] = stripctl($package['request_method']);
102    $data[] = stripctl($package['request_uri']);
103    $data[] = stripctl($package['server_protocol']);
104    $data[] = stripctl($package['user_agent']);
105    $data[] = stripctl($key);
106
107    io_saveFile($conf['cachedir'].'/badbehaviour.log',join("\t",$data)."\n",true);
108}
109
110