*/ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'action.php'); require_once(DOKU_INC.'inc/blowfish.php'); class action_plugin_captcha extends DokuWiki_Action_Plugin { /** * return some info */ function getInfo(){ return array( 'author' => 'Andreas Gohr', 'email' => 'andi@splitbrain.org', 'date' => '2006-12-02', 'name' => 'CAPTCHA Plugin', 'desc' => 'Use a CAPTCHA challenge to protect the Wiki against automated spam', 'url' => 'http://wiki:splitbrain.org/plugin:captcha', ); } /** * register the eventhandlers */ function register(&$controller){ $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess', array()); $controller->register_hook('HTML_EDITFORM_INJECTION', 'BEFORE', $this, 'handle_editform_injection', array('editform' => true)); if($this->getConf('regprotect')){ $controller->register_hook('ACTION_REGISTER', 'BEFORE', $this, 'handle_act_register', array()); $controller->register_hook('HTML_REGISTERFORM_INJECTION', 'BEFORE', $this, 'handle_editform_injection', array('editform' => false)); } } /** * Will intercept the 'save' action and check for CAPTCHA first. */ function handle_act_preprocess(&$event, $param){ if('save' != $this->_act_clean($event->data)) return; // nothing to do for us // do nothing if logged in user and no CAPTCHA required if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']){ return; } // compare provided string with decrypted captcha $rand = PMA_blowfish_decrypt($_REQUEST['plugin__captcha_secret'],auth_cookiesalt()); $code = $this->_generateCAPTCHA($this->_fixedIdent(),$rand); if(!$_REQUEST['plugin__captcha_secret'] || !$_REQUEST['plugin__captcha'] || strtoupper($_REQUEST['plugin__captcha']) != $code){ // CAPTCHA test failed! Continue to edit instead of saving msg($this->getLang('testfailed'),-1); $event->data = 'preview'; } // if we arrive here it was a valid save } /** * Will intercept the register process and check for CAPTCHA first. */ function handle_act_register(&$event, $param){ // compare provided string with decrypted captcha $rand = PMA_blowfish_decrypt($_REQUEST['plugin__captcha_secret'],auth_cookiesalt()); $code = $this->_generateCAPTCHA($this->_fixedIdent(),$rand); if(!$_REQUEST['plugin__captcha_secret'] || !$_REQUEST['plugin__captcha'] || strtoupper($_REQUEST['plugin__captcha']) != $code){ // CAPTCHA test failed! Continue to edit instead of saving msg($this->getLang('testfailed'),-1); $event->preventDefault(); $event->stopPropagation(); return false; } // if we arrive here it was a valid save } /** * Create the additional fields for the edit form */ function handle_editform_injection(&$event, $param){ if($param['editform'] && !$event->data['writable']) return; // do nothing if logged in user and no CAPTCHA required if(!$this->getConf('forusers') && $_SERVER['REMOTE_USER']){ return; } global $ID; $rand = (float) (rand(0,10000))/10000; $code = $this->_generateCAPTCHA($this->_fixedIdent(),$rand); $secret = PMA_blowfish_encrypt($rand,auth_cookiesalt()); echo '