xref: /plugin/captcha/action.php (revision 6ee10a68403244a06038486cb81bd0fa5c955c73)
1<?php
2/**
3 * CAPTCHA antispam plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12
13class action_plugin_captcha extends DokuWiki_Action_Plugin {
14
15    /**
16     * register the eventhandlers
17     */
18    public function register(Doku_Event_Handler $controller) {
19        // check CAPTCHA success
20        $controller->register_hook(
21            'ACTION_ACT_PREPROCESS',
22            'BEFORE',
23            $this,
24            'handle_captcha_input',
25            array()
26        );
27
28        // inject in edit form
29        $controller->register_hook(
30            'HTML_EDITFORM_OUTPUT',
31            'BEFORE',
32            $this,
33            'handle_form_output',
34            array()
35        );
36
37        // inject in user registration
38        $controller->register_hook(
39            'HTML_REGISTERFORM_OUTPUT',
40            'BEFORE',
41            $this,
42            'handle_form_output',
43            array()
44        );
45    }
46
47    /**
48     * Will intercept the 'save' action and check for CAPTCHA first.
49     */
50    public function handle_captcha_input(Doku_Event $event, $param) {
51        $act = act_clean($event->data);
52        if(!(
53            'save' == $act ||
54            ('register' == $act && $_POST['save'])
55        )
56        ) {
57            return; // nothing to do for us
58        }
59
60        // do nothing if logged in user and no CAPTCHA required
61        if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']) {
62            return;
63        }
64
65        // check captcha
66        /** @var helper_plugin_captcha $helper */
67        $helper = plugin_load('helper', 'captcha');
68        if(!$helper->check()) {
69            if($act == 'save') {
70                // stay in preview mode
71                $event->data = 'preview';
72            } else {
73                // stay in register mode, but disable the save parameter
74                $_POST['save'] = false;
75            }
76        }
77    }
78
79    /**
80     * Inject the CAPTCHA in a DokuForm
81     */
82    public function handle_form_output(Doku_Event $event, $param) {
83        // get position of submit button
84        $pos = $event->data->findElementByAttribute('type', 'submit');
85        if(!$pos) return; // no button -> source view mode
86
87        // do nothing if logged in user and no CAPTCHA required
88        if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']) {
89            return;
90        }
91
92        // get the CAPTCHA
93        /** @var helper_plugin_captcha $helper */
94        $helper = plugin_load('helper', 'captcha');
95        $out = $helper->getHTML();
96
97        // new wiki - insert after the submit button
98        $event->data->insertElement($pos + 1, $out);
99    }
100
101}
102
103