Skip to content

Commit

Permalink
add MissingRecaptchaApiKey Exception & transform private to protected…
Browse files Browse the repository at this point in the history
… & editorconfig
  • Loading branch information
cake17 committed Jan 25, 2015
1 parent c2f7be2 commit ab7fa83
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.bat]
end_of_line = crlf
[*.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion src/Console/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @author cake17
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://www.cake-websites.com
* @link http://cake17.github.io/
*
*/
namespace Recaptcha\Console;
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/Component/RecaptchaComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class RecaptchaComponent extends Component
* Initialize config data and properties.
*
* @param array $config The config data.
*
* @return void
*/
public function initialize(array $config)
Expand All @@ -41,6 +42,7 @@ public function initialize(array $config)
* startup callback
*
* @param \Cake\Event\Event $event : Event
*
* @return void
*/
public function startup(Event $event)
Expand Down
21 changes: 21 additions & 0 deletions src/Lib/Exception/MissingRecaptchaApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* MissingRecaptchaApiKey Exception
*
* @author cake17
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://cake17.github.io/
*
*/
namespace Recaptcha\Lib\Exception;

use Cake\Core\Exception\Exception;

/**
* Exception raised when an api key is not provided in recaptcha config file.
*
*/
class MissingRecaptchaApiKey extends Exception
{
protected $_messageTemplate = 'The api key for recapatcha could not be found. You must register one <a href="%s">%s</a>';
}
38 changes: 20 additions & 18 deletions src/Lib/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,43 @@
namespace Recaptcha\Lib;

use Recaptcha\Lib\ReCaptchaResponse;
use Recaptcha\View\Exception\MissingRecaptchaApiKey;

class ReCaptcha
{
/**
* @var
* @var string
*/
private static $_signupUrl = "https://www.google.com/recaptcha/admin";
protected static $signupUrl = "https://www.google.com/recaptcha/admin";

/**
* @var
* @var string
*/
private static $_siteVerifyUrl = "https://www.google.com/recaptcha/api/siteverify?";
protected static $siteVerifyUrl = "https://www.google.com/recaptcha/api/siteverify?";

/**
* @var
* @var string
*/
private $_secret;
protected $secret;

/**
* @var
* @var string
*/
private static $_version = "php_1.0";
protected static $version = "php_1.0";

/**
* Constructor.
*
* @param string $secret shared secret between site and ReCAPTCHA server.
*
* @return void
*/
public function __construct($secret)
{
if ($secret == null || $secret == "") {
die("To use reCAPTCHA you must get an API key from <a href='" .
self::$_signupUrl . "'>" . self::$_signupUrl . "</a>");
throw new MissingRecaptchaApiKey(self::$signupUrl);
}
$this->_secret = $secret;
$this->secret = $secret;
}

/**
Expand All @@ -76,7 +78,7 @@ public function __construct($secret)
*
* @return string - encoded request.
*/
private function __encodeQS($data)
protected function encodeQS($data)
{
$req = "";
foreach ($data as $key => $value) {
Expand All @@ -96,9 +98,9 @@ private function __encodeQS($data)
*
* @return array response
*/
private function __submitHttpGet($path, $data)
protected function submitHttpGet($path, $data)
{
$req = $this->__encodeQS($data);
$req = $this->encodeQS($data);
$response = file_get_contents($path . $req);
return $response;
}
Expand All @@ -122,12 +124,12 @@ public function verifyResponse($remoteIp, $response)
return $recaptchaResponse;
}

$getResponse = $this->__submitHttpGet(
self::$_siteVerifyUrl,
$getResponse = $this->submitHttpGet(
self::$siteVerifyUrl,
array(
'secret' => $this->_secret,
'secret' => $this->secret,
'remoteip' => $remoteIp,
'v' => self::$_version,
'v' => self::$version,
'response' => $response
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/Lib/ReCaptchaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
class ReCaptchaResponse
{
/**
* @var $success
* @var bool $success
*/
public $success;

/**
* @var $errorCodes
* @var string $errorCodes
*/
public $errorCodes;
}
41 changes: 27 additions & 14 deletions src/View/Helper/RecaptchaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ class RecaptchaHelper extends Helper
*
* @var string
*/
protected $_defaultLang = 'en';
protected $defaultLang = 'en';

/**
* Default theme
* If no theme is found anywhere
*
* @var string
*/
protected $_defaultTheme = 'light';
protected $defaultTheme = 'light';

/**
* Default type
* If no type is found anywhere
*
* @var string
*/
protected $_defaultType = 'image';
protected $defaultType = 'image';

/**
* Default configuration.
Expand Down Expand Up @@ -108,14 +108,19 @@ class RecaptchaHelper extends Helper
/**
* Render the recaptcha div and js script
*
* @param string $sitekey : Key
* @param string $lang : Lang
* @param string $theme : Theme
* @param string $type : Type
*
* @return string HTML
*/
public function display($siteKey = null, $lang = null, $theme = null, $type = null)
{
$lang = $this->_language($lang);
$siteKey = $this->_siteKey($siteKey);
$theme = $this->_theme($theme);
$type = $this->_type($type);
$lang = $this->language($lang);
$siteKey = $this->siteKey($siteKey);
$theme = $this->theme($theme);
$type = $this->type($type);

return '<div class="g-recaptcha" data-sitekey="' . $siteKey . '" data-theme="' . $theme . '" data-type="' . $type . '"></div>
<script type="text/javascript"
Expand All @@ -130,9 +135,11 @@ public function display($siteKey = null, $lang = null, $theme = null, $type = nu
* - If empty : use I18n locale
* - If not correct : use defaultLang var
*
* @param string $lang : Lang
*
* @return string language in code 2 (fr, en, ...)
*/
protected function _language($lang)
protected function language($lang)
{
if (empty($lang)) {
$lang = $this->config('lang');
Expand All @@ -143,7 +150,7 @@ protected function _language($lang)

// in case the language is not in accepted languages, 'en' language is chosen
if (!in_array($lang, $this->config('langAccepted'))) {
$lang = $this->_defaultLang;
$lang = $this->defaultLang;
}
return $lang;
}
Expand All @@ -153,9 +160,11 @@ protected function _language($lang)
* - First the one given in the display() method
* - If empty : the default one from config file
*
* @param string $siteKey : Key
*
* @return string siteKey
*/
protected function _siteKey($siteKey)
protected function siteKey($siteKey)
{
if (empty($siteKey)) {
$siteKey = $this->config('siteKey');
Expand All @@ -169,16 +178,18 @@ protected function _siteKey($siteKey)
* - If empty : the default one from config file
* - If not correct : use defaultTheme var
*
* @param string $theme : Theme
*
* @return string theme
*/
protected function _theme($theme)
protected function theme($theme)
{
if (empty($theme)) {
$theme = $this->config('theme');
}
// in case the theme is not in accepted themes, the default theme is chosen
if (!in_array($theme, $this->config('themeAccepted'))) {
$theme = $this->_defaultTheme;
$theme = $this->defaultTheme;
}
return $theme;
}
Expand All @@ -189,16 +200,18 @@ protected function _theme($theme)
* - If empty : the default one from config file
* - If not correct : use defaultType var
*
* @param string $type : Type
*
* @return string type
*/
protected function _type($type)
protected function type($type)
{
if (empty($type)) {
$type = $this->config('type');
}
// in case the theme is not in accepted themes, the default theme is chosen
if (!in_array($type, $this->config('typeAccepted'))) {
$type = $this->_defaultType;
$type = $this->defaultType;
}
return $type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* RecaptchaComponentTest
*
* @author cake17
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://cake17.github.io/
*
*/
namespace Recaptcha\Test\TestCase\Controller\Component;

use Cake\Controller\ComponentRegistry;
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/View/Helper/RecaptchaHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* RecaptchaHelperTest
*
* @author cake17
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://cake17.github.io/
*
*/
namespace Recaptcha\Test\TestCase\View\Helper;

use Cake\TestSuite\TestCase;
Expand Down

0 comments on commit ab7fa83

Please sign in to comment.