From cd71188d3a653d8562d412b81f790a41ef1b6fe4 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Mon, 8 Feb 2016 17:47:09 +0100 Subject: [PATCH] SimpleCaptcha plugin to stop basic bots --- actions/register.php | 6 +- lib/default.php | 1 + plugins/SimpleCaptcha/SimpleCaptchaPlugin.php | 66 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 plugins/SimpleCaptcha/SimpleCaptchaPlugin.php diff --git a/actions/register.php b/actions/register.php index 15b6b23cab..0d020663b9 100644 --- a/actions/register.php +++ b/actions/register.php @@ -131,7 +131,11 @@ class RegisterAction extends Action // TRANS: Client error displayed when trying to register while already logged in. $this->clientError(_('Already logged in.')); } else if ($_SERVER['REQUEST_METHOD'] == 'POST') { - $this->tryRegister(); + try { + $this->tryRegister(); + } catch (ClientException $e) { + $this->showForm($e->getMessage()); + } } else { $this->showForm(); } diff --git a/lib/default.php b/lib/default.php index f0430ee2fd..c17c2e0bf4 100644 --- a/lib/default.php +++ b/lib/default.php @@ -324,6 +324,7 @@ $default = 'OStatus' => array(), 'Poll' => array(), 'SearchSub' => array(), + 'SimpleCaptcha' => array(), 'TagSub' => array(), 'WebFinger' => array(), ), diff --git a/plugins/SimpleCaptcha/SimpleCaptchaPlugin.php b/plugins/SimpleCaptcha/SimpleCaptchaPlugin.php new file mode 100644 index 0000000000..9a3f5c7489 --- /dev/null +++ b/plugins/SimpleCaptcha/SimpleCaptchaPlugin.php @@ -0,0 +1,66 @@ +. + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +/** + * @package Plugin + * @maintainer Mikael Nordfeldth + */ +class SimpleCaptchaPlugin extends Plugin +{ + public function onEndRegistrationFormData(Action $action) + { + $action->elementStart('li'); + // TRANS: Field label. + $action->input('simplecaptcha', _m('Captcha'), null, + // TRANS: The instruction box for our simple captcha plugin + sprintf(_m('Copy this to the textbox: "%s"'), $this->getCaptchaText()), + // TRANS: Placeholder in the text box + /* name=id */ null, /* required */ true, ['placeholder'=>_m('Prove that you are sentient.')]); + $action->elementEnd('li'); + return true; + } + + protected function getCaptchaText() + { + return common_config('site', 'name'); + } + + public function onStartRegistrationTry(Action $action) + { + if ($action->arg('simplecaptcha') !== $this->getCaptchaText()) { + throw new ClientException(_m('Captcha does not match!')); + } + return true; + } + + public function onPluginVersion(array &$versions) + { + $versions[] = array('name' => 'Simple Captcha', + 'version' => GNUSOCIAL_VERSION, + 'author' => 'Mikael Nordfeldth', + 'homepage' => 'https://gnu.io/social', + 'rawdescription' => + // TRANS: Plugin description. + _m('A simple captcha to get rid of spambots.')); + + return true; + } +}