From 8ab353226af7c37ee026e6fe6fa464ce91cc252a Mon Sep 17 00:00:00 2001 From: Darko Date: Mon, 29 Jun 2015 12:55:55 +0200 Subject: [PATCH] Change handling of Captcha. --- newznab/controllers/BasePage.php | 32 +---------- newznab/controllers/Captcha.php | 93 +++++++++++++++----------------- www/pages/contact-us.php | 4 +- www/pages/forgottenpassword.php | 4 +- www/pages/login.php | 4 +- www/pages/register.php | 4 +- 6 files changed, 55 insertions(+), 86 deletions(-) diff --git a/newznab/controllers/BasePage.php b/newznab/controllers/BasePage.php index 70a4f1f27..4c059bd44 100644 --- a/newznab/controllers/BasePage.php +++ b/newznab/controllers/BasePage.php @@ -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. * diff --git a/newznab/controllers/Captcha.php b/newznab/controllers/Captcha.php index e4e2555f3..dfafed57d 100644 --- a/newznab/controllers/Captcha.php +++ b/newznab/controllers/Captcha.php @@ -1,15 +1,12 @@ 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; } - } \ No newline at end of file diff --git a/www/pages/contact-us.php b/www/pages/contact-us.php index 1b79f7fa0..261c5ffc8 100644 --- a/www/pages/contact-us.php +++ b/www/pages/contact-us.php @@ -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"; diff --git a/www/pages/forgottenpassword.php b/www/pages/forgottenpassword.php index cb091d4ce..c22b0f8db 100644 --- a/www/pages/forgottenpassword.php +++ b/www/pages/forgottenpassword.php @@ -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'] == "") { diff --git a/www/pages/login.php b/www/pages/login.php index 16a4d4957..67a37af25 100644 --- a/www/pages/login.php +++ b/www/pages/login.php @@ -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); diff --git a/www/pages/register.php b/www/pages/register.php index 5ce789041..4b127a402 100644 --- a/www/pages/register.php +++ b/www/pages/register.php @@ -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']);