xref: /plugin/captcha/action.php (revision 7218f96ceebeadf8d318824c7833a4bda6f325c9)
142a27035SAndreas Gohr<?php
242a27035SAndreas Gohr/**
342a27035SAndreas Gohr * CAPTCHA antispam plugin
442a27035SAndreas Gohr *
542a27035SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
642a27035SAndreas Gohr * @author     Andreas Gohr <gohr@cosmocode.de>
742a27035SAndreas Gohr */
842a27035SAndreas Gohr
942a27035SAndreas Gohr// must be run within Dokuwiki
1042a27035SAndreas Gohrif(!defined('DOKU_INC')) die();
1142a27035SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12*7218f96cSAndreas Gohr
1342a27035SAndreas Gohr
1442a27035SAndreas Gohrclass action_plugin_captcha extends DokuWiki_Action_Plugin {
1542a27035SAndreas Gohr
1642a27035SAndreas Gohr    /**
1742a27035SAndreas Gohr     * register the eventhandlers
1842a27035SAndreas Gohr     */
19*7218f96cSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
20*7218f96cSAndreas Gohr        // check CAPTCHA success
21c2695b40SAndreas Gohr        $controller->register_hook(
22c2695b40SAndreas Gohr            'ACTION_ACT_PREPROCESS',
2342a27035SAndreas Gohr            'BEFORE',
2442a27035SAndreas Gohr            $this,
25*7218f96cSAndreas Gohr            'handle_captcha_input',
26c2695b40SAndreas Gohr            array()
27c2695b40SAndreas Gohr        );
2842a27035SAndreas Gohr
29*7218f96cSAndreas Gohr        // inject in edit form
30c2695b40SAndreas Gohr        $controller->register_hook(
31c2695b40SAndreas Gohr            'HTML_EDITFORM_OUTPUT',
3247afabe6SAndreas Gohr            'BEFORE',
3347afabe6SAndreas Gohr            $this,
34*7218f96cSAndreas Gohr            'handle_form_output',
35*7218f96cSAndreas Gohr            array()
36c2695b40SAndreas Gohr        );
3742a27035SAndreas Gohr
38*7218f96cSAndreas Gohr        // inject in user registration
3942a27035SAndreas Gohr        if($this->getConf('regprotect')) {
40c2695b40SAndreas Gohr            $controller->register_hook(
41c2695b40SAndreas Gohr                'HTML_REGISTERFORM_OUTPUT',
4247afabe6SAndreas Gohr                'BEFORE',
4347afabe6SAndreas Gohr                $this,
44*7218f96cSAndreas Gohr                'handle_form_output',
45*7218f96cSAndreas Gohr                array()
46c2695b40SAndreas Gohr            );
4742a27035SAndreas Gohr        }
4842a27035SAndreas Gohr    }
4942a27035SAndreas Gohr
5042a27035SAndreas Gohr    /**
5142a27035SAndreas Gohr     * Will intercept the 'save' action and check for CAPTCHA first.
5242a27035SAndreas Gohr     */
53*7218f96cSAndreas Gohr    public function handle_captcha_input(Doku_Event $event, $param) {
54*7218f96cSAndreas Gohr        $act = act_clean($event->data);
5593f66506SAndreas Gohr        if(!('save' == $act || ($this->getConf('regprotect') &&
5693f66506SAndreas Gohr                'register' == $act &&
57c2695b40SAndreas Gohr                $_POST['save']))
58c2695b40SAndreas Gohr        ) {
5993f66506SAndreas Gohr            return; // nothing to do for us
6093f66506SAndreas Gohr        }
6193f66506SAndreas Gohr
6242a27035SAndreas Gohr        // do nothing if logged in user and no CAPTCHA required
6342a27035SAndreas Gohr        if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']) {
6442a27035SAndreas Gohr            return;
6542a27035SAndreas Gohr        }
6642a27035SAndreas Gohr
6777e00bf9SAndreas Gohr        // check captcha
68*7218f96cSAndreas Gohr        /** @var helper_plugin_captcha $helper */
6977e00bf9SAndreas Gohr        $helper = plugin_load('helper', 'captcha');
7077e00bf9SAndreas Gohr        if(!$helper->check()) {
7193f66506SAndreas Gohr            if($act == 'save') {
7293f66506SAndreas Gohr                // stay in preview mode
7342a27035SAndreas Gohr                $event->data = 'preview';
7493f66506SAndreas Gohr            } else {
7593f66506SAndreas Gohr                // stay in register mode, but disable the save parameter
7693f66506SAndreas Gohr                $_POST['save'] = false;
7742a27035SAndreas Gohr            }
7842a27035SAndreas Gohr        }
7942a27035SAndreas Gohr    }
8042a27035SAndreas Gohr
8142a27035SAndreas Gohr    /**
82*7218f96cSAndreas Gohr     * Inject the CAPTCHA in a DokuForm
8342a27035SAndreas Gohr     */
84*7218f96cSAndreas Gohr    public function handle_form_output(Doku_Event $event, $param) {
8547afabe6SAndreas Gohr        // get position of submit button
8647afabe6SAndreas Gohr        $pos = $event->data->findElementByAttribute('type', 'submit');
8747afabe6SAndreas Gohr        if(!$pos) return; // no button -> source view mode
8847afabe6SAndreas Gohr
8942a27035SAndreas Gohr        // do nothing if logged in user and no CAPTCHA required
9042a27035SAndreas Gohr        if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']) {
9142a27035SAndreas Gohr            return;
9242a27035SAndreas Gohr        }
9342a27035SAndreas Gohr
9477e00bf9SAndreas Gohr        // get the CAPTCHA
95*7218f96cSAndreas Gohr        /** @var helper_plugin_captcha $helper */
9677e00bf9SAndreas Gohr        $helper = plugin_load('helper', 'captcha');
9777e00bf9SAndreas Gohr        $out = $helper->getHTML();
9847afabe6SAndreas Gohr
99*7218f96cSAndreas Gohr        // new wiki - insert after the submit button
100*7218f96cSAndreas Gohr        $event->data->insertElement($pos + 1, $out);
10142a27035SAndreas Gohr    }
10242a27035SAndreas Gohr
10342a27035SAndreas Gohr}
10442a27035SAndreas Gohr
105