diff --git a/action.php b/action.php index 92aab02..36b3667 100644 --- a/action.php +++ b/action.php @@ -63,6 +63,15 @@ function register(&$controller) { array('editform' => false, 'oldhook' => false) ); } + if($this->getConf('loginprotect')) { + $controller->register_hook( + 'HTML_LOGINFORM_OUTPUT', + 'BEFORE', + $this, + 'handle_login_form', + array() + ); + } } /** @@ -70,6 +79,12 @@ function register(&$controller) { */ function handle_act_preprocess(&$event, $param) { $act = $this->_act_clean($event->data); + + if($this->getConf('loginprotect')) { + $this->handle_login(); + return; + } + if(!('save' == $act || ($this->getConf('regprotect') && 'register' == $act && $_POST['save'])) @@ -97,6 +112,7 @@ function handle_act_preprocess(&$event, $param) { /** * Create the additional fields for the edit form + * @author Myron Turner */ function handle_editform_output(&$event, $param) { // check if source view -> no captcha needed @@ -125,7 +141,38 @@ function handle_editform_output(&$event, $param) { $event->data->insertElement($pos++, $out); } } - + /** + * Insert captcha into login form if loginprotect is true + * @url parameter: chk=captcha_check, identifies login mode + * @author Myron Turner + */ + function handle_login_form(&$event, $param) { + $pos = $event->data->findElementByAttribute('type', 'submit'); + $helper = plugin_load('helper', 'captcha'); + $out = $helper->getHTML(); + $event->data->_hidden['chk'] = 'captcha_check'; + $event->data->insertElement($pos+1, $out); + } + + /** + * Redirect with additional parameters if captcha fails and + * output 'testfailed' message on re-load + * + * @url_param: do=logout => to force logout + * + * @author Myron Turner + */ + function handle_login() { + if(isset($_REQUEST['chk'])) { + $helper = $this->loadHelper('captcha', true); + if(!$helper->check()) { + msg($helper->getLang('testfailed'), -1); + $url = wl('',array('do'=>'logout'), true, '&') ; + send_redirect($url); + exit(); + } + } + } /** * Pre-Sanitize the action command * diff --git a/conf/default.php b/conf/default.php index 7861b94..8f5fbf6 100644 --- a/conf/default.php +++ b/conf/default.php @@ -13,3 +13,4 @@ $conf['height'] = 22; $conf['question'] = 'What\'s the answer to life, the universe and everything?'; $conf['answer'] = '42'; +$conf['loginprotect'] = 0; diff --git a/conf/metadata.php b/conf/metadata.php index c0c66d4..e17ebb7 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -13,3 +13,4 @@ $meta['height'] = array('numeric', '_pattern' => '/[0-9]+/'); $meta['question'] = array('string'); $meta['answer'] = array('string'); +$meta['loginprotect'] = array('onoff'); diff --git a/lang/en/settings.php b/lang/en/settings.php index eee2f82..94889c0 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -21,3 +21,4 @@ $lang['height'] = "Height of the CAPTCHA image (pixel)"; $lang['question'] = "Question for fixed question mode"; $lang['answer'] = "Answer for fixed question mode"; +$lang['loginprotect'] = "Protect login form"; diff --git a/plugin.info.txt b/plugin.info.txt index ae49ee6..cfbc5a6 100644 --- a/plugin.info.txt +++ b/plugin.info.txt @@ -1,7 +1,7 @@ base captcha author Andreas Gohr email andi@splitbrain.org -date 2014-01-05 +date 2014-12-28 name CAPTCHA Plugin desc Use a CAPTCHA challenge to protect DokuWiki against automated spam url http://www.dokuwiki.org/plugin:captcha