From db3cb85d0992dd49ca2fdf33ea35c0cad60e312f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 22 Sep 2011 23:26:59 -0400 Subject: Implement ReCaptcha entirely as a plugin, remove all of the ugliness from my old implementation. --- src/controllers/Users.class.php | 22 ++++++++-------------- src/lib/Plugin.class.php | 3 +++ src/plugins/ReCaptcha.class.php | 33 +++++++++++++++++++++++++++++++++ src/views/pages/users/new.html.php | 10 +++------- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/controllers/Users.class.php b/src/controllers/Users.class.php index ed6f739..a0eebdb 100644 --- a/src/controllers/Users.class.php +++ b/src/controllers/Users.class.php @@ -60,6 +60,8 @@ class Users extends Controller { // since there will never be a remainder to `users/new', we can // use that parameter to pass in some data. if (!isset($vars['errors'])) $vars['errors'] = array(); + global $mm; $pm = $mm->pluginManager(); + $vars['antispam_html'] = $pm->callHook('antispam_html'); $this->showView('users/new', $vars); } @@ -124,16 +126,14 @@ class Users extends Controller { * explained. */ private function create_user() { + global $mm; + $db = $mm->database(); + $pm = $mm->pluginManager(); + $vars = array(); @$vars['username' ] = $_POST['auth_name']; @$vars['password1'] = $_POST['auth_password' ]; @$vars['password2'] = $_POST['auth_password_verify']; - @$recaptcha_response = $_POST['recaptcha_response_field']; - @$recaptcha_challenge = $_POST['recaptcha_challenge_field']; - - global $mm; $db = $mm->database(); - $publickey = $db->getPluginConf('ReCaptcha', 'public_key'); - $privatekey = $db->getPluginConf('ReCaptcha', 'private_key'); $vars['errors'] = array(); if ($db->getUID($vars['username'])!==false) @@ -147,14 +147,8 @@ class Users extends Controller { if ($matches && $vars['password2'] == '') { $vars['errors'][] = 'no pw'; } - require_once('recaptchalib.php'); - $resp = recaptcha_check_answer($privatekey, - $_SERVER['REMOTE_ADDR'], - $recaptcha_challenge, - $recaptcha_response); - if (!$resp->is_valid) { - $vars['errors'][] = 'recaptcha'; - $vars['recaptcha_error'] = $resp->error; + foreach ($pm->callHook('antispam_verify') as $plugin=>$valid) { + if (!$valid) $vars['errors'][] = 'plugin_'.$plugin; } if (count($vars['errors']) > 0) { diff --git a/src/lib/Plugin.class.php b/src/lib/Plugin.class.php index 8c7fad8..9d2fc2e 100644 --- a/src/lib/Plugin.class.php +++ b/src/lib/Plugin.class.php @@ -19,4 +19,7 @@ abstract class Plugin { } public abstract function init(); + + public function antispam_html() { return ''; } + public function antispam_verify() { return true; } } diff --git a/src/plugins/ReCaptcha.class.php b/src/plugins/ReCaptcha.class.php index c25147f..165493b 100644 --- a/src/plugins/ReCaptcha.class.php +++ b/src/plugins/ReCaptcha.class.php @@ -1,4 +1,6 @@ '', @@ -11,4 +13,35 @@ class ReCaptcha extends Plugin { 'private_key'=>'text'); } public function init() {} + + private $resp = null; + private function getResp() { + if ($this->resp===null) { + require_once('recaptchalib.php'); + @$response = $_POST['recaptcha_response_field']; + @$challenge = $_POST['recaptcha_challenge_field']; + $this->resp = recaptcha_check_answer($this->config['private_key'], + $_SERVER['REMOTE_ADDR'], + $challenge, + $response); + } + return $this->resp; + } + + private function getError() { + if ($_POST["recaptcha_response_field"] && !$this->antispam_verify()) { + return $this->getResp()->error; + } else { + return false; + } + } + + public function antispam_verify() { + return $this->getResp()->is_valid; + } + + public function antispam_html() { + require_once('recaptchalib.php'); + return recaptcha_get_html($this->config['public_key'], $this->getError()); + } } diff --git a/src/views/pages/users/new.html.php b/src/views/pages/users/new.html.php index 326f0bc..147e3c0 100644 --- a/src/views/pages/users/new.html.php +++ b/src/views/pages/users/new.html.php @@ -30,13 +30,9 @@ if (in_array('no pw', $VARS['errors'])) { $t->inputNewPassword('auth_password','Password', $password); $t->closeFieldset(); -global $mm; $db = $mm->database(); -$public_key = $db->getPluginConf('ReCaptcha', 'public_key'); -$recaptcha_error = null; -if (isset($VARS['recaptcha_error'])) - $recaptcha_error = $VARS['recaptcha_error']; -require_once('recaptchalib.php'); -echo recaptcha_get_html($public_key, $recaptcha_error); +foreach ($VARS['antispam_html'] as $html) { + echo $html; +} $t->tag('input', array('type'=>'submit', 'value'=>'Submit')); -- cgit v1.2.3