1<?php
2/**
3 * StopForumSpam Plugin - Action Section
4 *
5 * A part of the system is inspired by IPBan plugin (https://github.com/splitbrain/dokuwiki-plugin-ipban)
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     HokkaidoPerson <dosankomali@yahoo.co.jp>
9 */
10
11// must be run within Dokuwiki
12if(!defined('DOKU_INC')) die();
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14require_once(DOKU_PLUGIN.'action.php');
15
16class action_plugin_stopforumspam2 extends DokuWiki_Action_Plugin {
17
18    function register(Doku_Event_Handler $controller){
19        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'accessdenial', array());
20        $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'ipprevent', array());
21        $controller->register_hook('AUTH_USER_CHANGE', 'BEFORE', $this, 'elementcheck', array());
22    }
23
24    function accessdenial(&$event, $param){
25        $helper = plugin_load('helper','stopforumspam2');
26
27        if ($helper->quickipcheck(null, $this->getConf('accessRefusalFreq'), $this->getConf('accessRefusalConf'))) {
28            $text = $this->locale_xhtml('banned');
29            $title = $this->getLang('denied');
30            header("HTTP/1.0 403 Forbidden");
31            echo<<<EOT
32<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
33"http://www.w3.org/TR/html4/loose.dtd">
34<html>
35<head><title>$title</title></head>
36<meta name="viewport" content="width=device-width,initial-scale=1">
37<body style="font-family: Arial, sans-serif">
38  <div style="width:60%; margin: auto; background-color: #fcc;
39              border: 1px solid #faa; padding: 0.5em 1em;">
40  $text
41  </div>
42</body>
43</html>
44EOT;
45        exit;
46        }
47    }
48
49    function ipprevent(&$event, $param) {
50        global $ACT;
51        $helper = plugin_load('helper','stopforumspam2');
52        if ($ACT == 'edit' and $helper->quickipcheck(null, $this->getConf('protectEditFreq'), $this->getConf('protectEditConf'))) {
53            echo $this->locale_xhtml('bannededit');
54            $event->preventDefault();
55        }
56        if ($ACT == 'register' and $helper->quickipcheck(null, $this->getConf('protectRegFreq'), $this->getConf('protectRegConf'))) {
57            echo $this->locale_xhtml('bannedreg');
58            $event->preventDefault();
59        }
60    }
61
62    function elementcheck(&$event, $param) {
63        global $conf;
64        $helper = plugin_load('helper','stopforumspam2');
65        $iplogname = $conf['cachedir'] . '/stopforumspam2_' . $_SERVER['REMOTE_ADDR'] . '.txt';
66        $expiremin = $this->getConf('preventNuisanceReg');
67
68        if ($event->data['type'] == 'create') {
69            if ($expiremin != 0) {
70                if (file_exists($iplogname)) {
71                    $logdate = file_get_contents($iplogname);
72                    $current = time();
73                    $expire = $expiremin * 60;
74                    if ($current - $logdate > $expire) {
75                        unlink($iplogname);
76                    } else {
77                        $string = sprintf($this->getLang('beinglisted'), round(($logdate + $expire - $current) / 60, 1));
78                        msg($string, -1);
79                        $event->preventDefault();
80                        return;
81                    }
82                }
83            }
84
85            $username = $event->data['params'][2];
86            $email = $event->data['params'][3];
87            $result1 = $helper->freqcheck(null, $email, $username, FALSE, $this->getConf('protectRegFreq'));
88            $result2 = $helper->confcheck(null, $email, $username, FALSE, $this->getConf('protectRegConf'));
89
90            if ($result1 or $result2) {
91                msg($this->getLang('spammyelementreg'), -1);
92                if ($expiremin != 0) {
93                    $handle = @fopen($iplogname, 'w');
94                    if ($handle) {
95                        $string = sprintf($this->getLang('listed'), $expiremin);
96                        if (fwrite($handle, time()) !== FALSE) msg($string, -1);
97                        fclose($handle);
98                    }
99                }
100                $event->preventDefault();
101            }
102        }
103    }
104
105}
106