Change handling of Captcha.

This commit is contained in:
Darko
2015-06-29 12:55:55 +02:00
parent 20e7d0486b
commit 8ab353226a
6 changed files with 55 additions and 86 deletions
+1 -31
View File
@@ -4,7 +4,6 @@ require_once SMARTY_DIR . 'Autoloader.php';
Smarty_Autoloader::register();
require_once NN_LIB . 'utility' . DS . 'SmartyUtils.php';
use newznab\controllers\Captcha;
use newznab\db\Settings;
class BasePage
@@ -17,7 +16,7 @@ class BasePage
/**
* Public access to Captcha object for error checking.
*
* @var \newznab\controllers\Captcha
* @var \Captcha
*/
public $captcha;
@@ -72,7 +71,6 @@ class BasePage
// Buffer settings/DB connection.
$this->settings = new Settings();
$this->smarty = new Smarty();
$this->captcha = new Captcha(['Settings' => $this->settings]);
$this->smarty->setTemplateDir(
[
@@ -157,7 +155,6 @@ class BasePage
$this->smarty->assign('ismod', 'false');
$this->smarty->assign('loggedin', 'false');
$this->floodCheck();
$this->handleCaptcha();
}
@@ -165,33 +162,6 @@ class BasePage
$this->smarty->assign('page', $this);
}
/**
* Allow display on pages that require captcha
* and handle captcha responses.
*
* @notes Optimized for speed over code brevity since it's
* executed on every singe page.
* Instantiating Captcha() doesn't initialize the underlying libraries.
* shouldDisplay() does it if applicable.
*/
private function handleCaptcha() {
if ($this->captcha->shouldDisplay($this->page)) {
$this->smarty->assign('showCaptcha', true);
$this->smarty->assign('sitekey', $this->captcha->getSiteKey());
if ($this->isPostBack()) {
if (!$this->captcha->processCaptcha($_POST, $_SERVER['REMOTE_ADDR'])) {
$this->smarty->assign('error', $this->captcha->getError());
}
//Delete this key after using so it doesn't interfere with normal $_POST
//processing. (i.e. contact-us)
unset($_POST[Captcha::RECAPTCHA_POSTKEY]);
}
} else {
$this->smarty->assign('showCaptcha', false);
}
}
/**
* Unquotes quoted strings recursively in an array.
*
+42 -51
View File
@@ -1,15 +1,12 @@
<?php
namespace newznab\controllers;
use newznab\db\Settings;
use ReCaptcha\ReCaptcha;
class Captcha {
/**
* @var \newznab\db\Settings
* Smarty $page
*
* @var \Page
*/
private $pdo;
private $page;
/**
* ReCaptcha Site Key from the
@@ -44,26 +41,13 @@ class Captcha {
private $error = false;
/**
* List of page routes to apply the captcha.
*
* @todo Find a better way to enumerate this, I hate literals.
* @var array
*/
private $captcha_pages = [
'login',
'register',
'contact-us',
'forgottenpassword'
];
/**
* $_POST key for the user-supplied ReCaptcha response.
*/
const RECAPTCHA_POSTKEY = 'g-recaptcha-response';
/**
* Error literal constants.
* Error key literals.
*/
const RECAPTCHA_ERROR_MISSING_SECRET = 'missing-input-secret';
const RECAPTCHA_ERROR_INVALID_SECRET = 'invalid-input-secret';
@@ -71,32 +55,49 @@ class Captcha {
const RECAPTCHA_ERROR_INVALID_RESPONSE = 'invalid-input-response';
/**
* Construct.
*
* @param array $options Class instances.
* Settings key literals
*/
public function __construct(array $options = []) {
$defaults = [
'Settings' => null
];
$options += $defaults;
const RECAPTCHA_SETTING_SITEKEY = 'recaptchapublickey';
const RECAPTCHA_SETTING_SECRETKEY = 'recaptchaprivatekey';
$this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings());
/**
* Construct and decide whether to show the captcha or not.
*
* @note Passing $page by reference to setup smarty vars easily.
* @param \Page $page
*/
public function __construct(&$page) {
if (!$page instanceof \Page) {
throw new \InvalidArgumentException('Invalid Page variable provided');
}
$this->page = $page;
if ($this->shouldDisplay()) {
$this->page->smarty->assign('showCaptcha', true);
$this->page->smarty->assign('sitekey', $this->sitekey);
if ($this->page->isPostBack()) {
if (!$this->processCaptcha($_POST, $_SERVER['REMOTE_ADDR'])) {
$this->page->smarty->assign('error', $this->getError());
}
//Delete this key after using so it doesn't interfere with normal $_POST
//processing. (i.e. contact-us)
unset($_POST[Captcha::RECAPTCHA_POSTKEY]);
}
} else {
$this->page->smarty->assign('showCaptcha', false);
}
}
/**
* If site admin setup keys properly,
* allow display of recaptcha.
*
* @param string|bool $page
* @return bool
*/
public function shouldDisplay($page = false) {
if ($page !== false) {
if (in_array($page, $this->captcha_pages) && $this->_bootstrapCaptcha()) {
return true;
}
} elseif ($this->_bootstrapCaptcha()) {
public function shouldDisplay() {
if ($this->_bootstrapCaptcha()) {
return true;
}
@@ -112,15 +113,6 @@ class Captcha {
return $this->error;
}
/**
* Return sitekey for captcha html display.
*
* @return bool|string
*/
public function getSiteKey() {
return $this->sitekey;
}
/**
* Process the submitted captcha and validate.
*
@@ -183,21 +175,20 @@ class Captcha {
* @return bool
*/
private function _bootstrapCaptcha() {
if ($this->recaptcha instanceof ReCaptcha) {
if ($this->recaptcha instanceof \ReCaptcha\ReCaptcha) {
return true;
}
$this->sitekey = $this->pdo->getSetting('recaptchapublickey');
$this->secretkey = $this->pdo->getSetting('recaptchaprivatekey');
$this->sitekey = $this->page->settings->getSetting(self::RECAPTCHA_SETTING_SITEKEY);
$this->secretkey = $this->page->settings->getSetting(self::RECAPTCHA_SETTING_SECRETKEY);
if ($this->sitekey != false && $this->sitekey != '') {
if ($this->secretkey != false && $this->secretkey != '') {
$this->recaptcha = new ReCaptcha($this->secretkey);
$this->recaptcha = new \ReCaptcha\ReCaptcha($this->secretkey);
return true;
}
}
return false;
}
}
+3 -1
View File
@@ -2,12 +2,14 @@
use newznab\utility\Utility;
$captcha = new Captcha($page);
if (isset($_POST["useremail"])) {
//
// send the contact info and report back to user.
//
if ($page->captcha->getError() === false) {
if ($captcha->getError() === false) {
$email = $_POST["useremail"];
$mailto = $page->settings->getSetting('email');
$mailsubj = "Contact Form Submitted";
+3 -1
View File
@@ -7,6 +7,8 @@ if ($page->users->isLoggedIn())
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
$captcha = new Captcha($page);
switch($action) {
case "reset":
if (!isset($_REQUEST['guid'])) {
@@ -40,7 +42,7 @@ switch($action) {
break;
case 'submit':
if ($page->captcha->getError() === false) {
if ($captcha->getError() === false) {
$page->smarty->assign('email', $_POST['email']);
if ($_POST['email'] == "") {
+3 -1
View File
@@ -2,11 +2,13 @@
$page->smarty->assign(['error' => '', 'username' => '', 'rememberme' => '']);
$captcha = new Captcha($page);
if ($page->isPostBack())
{
if (!isset($_POST["username"]) || !isset($_POST["password"])){
$page->smarty->assign('error', "Please enter your username and password.");
} elseif ($page->captcha->getError() === false) {
} elseif ($captcha->getError() === false) {
$username = htmlspecialchars($_POST["username"]);
$page->smarty->assign('username', $username);
$res = $page->users->getByUsername($username);
+3 -1
View File
@@ -32,9 +32,11 @@ else {
$page->smarty->assign('invite_code_query', '');
}
$captcha = new Captcha($page);
switch ($action) {
case 'submit':
if ($page->captcha->getError() === false) {
if ($captcha->getError() === false) {
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
$confirmpassword = htmlspecialchars($_POST['confirmpassword']);