clean up action.php, remove support for deprecated forms

This commit is contained in:
Andreas Gohr
2023-12-06 11:58:05 +01:00
parent 1cd9cde7fa
commit 5e79b2eea7

View File

@ -1,8 +1,8 @@
<?php <?php
use dokuwiki\Extension\ActionPlugin; use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\EventHandler;
use dokuwiki\Extension\Event; use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
use dokuwiki\Form\Form; use dokuwiki\Form\Form;
use dokuwiki\plugin\captcha\FileCookie; use dokuwiki\plugin\captcha\FileCookie;
use dokuwiki\plugin\captcha\IpCounter; use dokuwiki\plugin\captcha\IpCounter;
@ -21,35 +21,29 @@ class action_plugin_captcha extends ActionPlugin
public function register(EventHandler $controller) public function register(EventHandler $controller)
{ {
// check CAPTCHA success // check CAPTCHA success
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_captcha_input', []); $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleCaptchaInput', []);
// inject in edit form // inject in edit form
$controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); //old $controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'handleFormOutput', []);
$controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); //new
// inject in user registration // inject in user registration
$controller->register_hook('HTML_REGISTERFORM_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); //old $controller->register_hook('FORM_REGISTER_OUTPUT', 'BEFORE', $this, 'handleFormOutput', []);
$controller->register_hook('FORM_REGISTER_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); //new
// inject in password reset // inject in password reset
$controller->register_hook('HTML_RESENDPWDFORM_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); //old $controller->register_hook('FORM_RESENDPWD_OUTPUT', 'BEFORE', $this, 'handleFormOutput', []);
$controller->register_hook('FORM_RESENDPWD_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); //new
// inject in login form // inject in login form
$controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); // old $controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleFormOutput', []);
$controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handle_form_output', []); // new
// check on login // check on login
$controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handle_login', []); $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handleLogin', []);
// clean up captcha cookies // clean up captcha cookies
$controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'handle_indexer', []); $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'handleIndexer', []);
$this->getConf('loginprotect');
// log authentication failures // log authentication failures
if ((int)$this->getConf('loginprotect') > 1) { if ((int)$this->getConf('loginprotect') > 1) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_auth', []); $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleAuth', []);
} }
} }
@ -63,7 +57,7 @@ class action_plugin_captcha extends ActionPlugin
* @param string $act cleaned action mode * @param string $act cleaned action mode
* @return bool * @return bool
*/ */
protected function needs_checking($act) protected function needsChecking($act)
{ {
global $INPUT; global $INPUT;
@ -90,7 +84,7 @@ class action_plugin_captcha extends ActionPlugin
* @param string $act cleaned action mode * @param string $act cleaned action mode
* @return string the new mode to use * @return string the new mode to use
*/ */
protected function abort_action($act) protected function abortAction($act)
{ {
global $INPUT; global $INPUT;
@ -131,7 +125,7 @@ class action_plugin_captcha extends ActionPlugin
* @param Event $event * @param Event $event
* @param $param * @param $param
*/ */
public function handle_login(Event $event, $param) public function handleLogin(Event $event, $param)
{ {
global $INPUT; global $INPUT;
if (!$this->protectLogin()) return; // no protection wanted if (!$this->protectLogin()) return; // no protection wanted
@ -154,12 +148,12 @@ class action_plugin_captcha extends ActionPlugin
/** /**
* Intercept all actions and check for CAPTCHA first. * Intercept all actions and check for CAPTCHA first.
*/ */
public function handle_captcha_input(Event $event, $param) public function handleCaptchaInput(Event $event, $param)
{ {
global $INPUT; global $INPUT;
$act = act_clean($event->data); $act = act_clean($event->data);
if (!$this->needs_checking($act)) return; if (!$this->needsChecking($act)) return;
// do nothing if logged in user and no CAPTCHA required // do nothing if logged in user and no CAPTCHA required
if (!$this->getConf('forusers') && $INPUT->server->str('REMOTE_USER')) { if (!$this->getConf('forusers') && $INPUT->server->str('REMOTE_USER')) {
@ -170,22 +164,18 @@ class action_plugin_captcha extends ActionPlugin
/** @var helper_plugin_captcha $helper */ /** @var helper_plugin_captcha $helper */
$helper = plugin_load('helper', 'captcha'); $helper = plugin_load('helper', 'captcha');
if (!$helper->check()) { if (!$helper->check()) {
$event->data = $this->abort_action($act); $event->data = $this->abortAction($act);
} }
} }
/** /**
* Inject the CAPTCHA in a DokuForm or \dokuwiki\Form\Form * Inject the CAPTCHA in a \dokuwiki\Form\Form
*/ */
public function handle_form_output(Event $event, $param) public function handleFormOutput(Event $event, $param)
{ {
global $INPUT; global $INPUT;
if ( if ($event->name === 'FORM_LOGIN_OUTPUT' && !$this->protectLogin()) {
($event->name === 'FORM_LOGIN_OUTPUT' || $event->name === 'HTML_LOGINFORM_OUTPUT')
&&
!$this->protectLogin()
) {
// no login protection wanted // no login protection wanted
return; return;
} }
@ -194,11 +184,7 @@ class action_plugin_captcha extends ActionPlugin
$form = $event->data; $form = $event->data;
// get position of submit button // get position of submit button
if (is_a($form, Form::class)) { $pos = $form->findPositionByAttribute('type', 'submit');
$pos = $form->findPositionByAttribute('type', 'submit');
} else {
$pos = $form->findElementByAttribute('type', 'submit');
}
if (!$pos) return; // no button -> source view mode if (!$pos) return; // no button -> source view mode
// do nothing if logged in user and no CAPTCHA required // do nothing if logged in user and no CAPTCHA required
@ -212,17 +198,13 @@ class action_plugin_captcha extends ActionPlugin
$out = $helper->getHTML(); $out = $helper->getHTML();
// insert before the submit button // insert before the submit button
if (is_a($form, Form::class)) { $form->addHTML($out, $pos);
$form->addHTML($out, $pos);
} else {
$form->insertElement($pos, $out);
}
} }
/** /**
* Clean cookies once per day * Clean cookies once per day
*/ */
public function handle_indexer(Event $event, $param) public function handleIndexer(Event $event, $param)
{ {
$lastrun = getCacheName('captcha', '.captcha'); $lastrun = getCacheName('captcha', '.captcha');
$last = @filemtime($lastrun); $last = @filemtime($lastrun);
@ -238,7 +220,7 @@ class action_plugin_captcha extends ActionPlugin
/** /**
* Count failed login attempts * Count failed login attempts
*/ */
public function handle_auth(Event $event, $param) public function handleAuth(Event $event, $param)
{ {
global $INPUT; global $INPUT;
$act = act_clean($event->data); $act = act_clean($event->data);