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