Skip to content

Commit

Permalink
Merge pull request #44 from albertcht/develop
Browse files Browse the repository at this point in the history
Refactor options config from develop branch
  • Loading branch information
albertcht committed Sep 30, 2017
2 parents e4bbbdb + 738f1b7 commit 5caa2a0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 65 deletions.
51 changes: 35 additions & 16 deletions README.md
Expand Up @@ -16,6 +16,9 @@ In reCAPTCHA v2, users need to click the button: "I'm not a robot" to prove they
composer require albertcht/invisible-recaptcha
```

## Notice
After version 1.8, configs part has been refactored. Please pass your options config to `InvisibleRecaptcha` except `siteKey` and `secretKey`.

## Laravel 5

### Setup
Expand All @@ -26,19 +29,26 @@ Add ServiceProvider to the providers array in `app/config/app.php`.
AlbertCht\InvisibleReCaptcha\InvisibleReCaptchaServiceProvider::class,
```

> It also supports package discovery for Laravel 5.5.
### Configuration
Before you set your config, remember to choose `invisible reCAPTCHA` while applying for keys.
![invisible_recaptcha_setting](http://i.imgur.com/zIAlKbY.jpg)

Add `INVISIBLE_RECAPTCHA_SITEKEY`, `INVISIBLE_RECAPTCHA_SECRETKEY`, `INVISIBLE_RECAPTCHA_BADGEHIDE`(optional), `INVISIBLE_RECAPTCHA_DATABADGE`(optional), `INVISIBLE_RECAPTCHA_DEBUG`(optional) to **.env** file.
Add `INVISIBLE_RECAPTCHA_SITEKEY`, `INVISIBLE_RECAPTCHA_SECRETKEY` to **.env** file.

```
// required
INVISIBLE_RECAPTCHA_SITEKEY={siteKey}
INVISIBLE_RECAPTCHA_SECRETKEY={secretKey}
// optional
INVISIBLE_RECAPTCHA_BADGEHIDE=false
INVISIBLE_RECAPTCHA_DATABADGE='bottomright'
INVISIBLE_RECAPTCHA_TIMEOUT=5
INVISIBLE_RECAPTCHA_DEBUG=false
```

> There are three different captcha styles you can set: `bottomright`, `bottomleft`, `inline`
> If you set `INVISIBLE_RECAPTCHA_BADGEHIDE` to true, you can hide the badge logo.
Expand Down Expand Up @@ -87,26 +97,28 @@ $validate = Validator::make(Input::all(), [

set in application/config/config.php :
```php
$config['composer_autoload'] = TRUE; //around line 134
$config['composer_autoload'] = TRUE;
```

add lines in application/config/config.php :
```php
$config['recaptcha.sitekey'] = 'keyhere';
$config['recaptcha.secret'] = 'secrethere';
$config['recaptcha.badgehide'] = FALSE;
$config['recaptcha.databadge'] = 'bottomritht';
$config['recaptcha.debug'] = FALSE;
$config['recaptcha.sitekey'] = 'sitekey';
$config['recaptcha.secret'] = 'secretkey';
// optional
$config['recaptcha.options'] [
'hideBadge' => false,
'dataBadge' => 'bottomright',
'timeout' => 5,
'debug' => false
];
```

In controller, use:
```php
$data['captcha'] = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha(
$this->config->item('recaptcha.sitekey'),
$this->config->item('recaptcha.secret'),
$this->config->item('recaptcha.badgehide'),
$this->config->item('recaptcha.databadge'),
$this->config->item('recaptcha.debug'),
$this->config->item('recaptcha.options'),
);
```

Expand All @@ -129,12 +141,19 @@ Checkout example below:

require_once "vendor/autoload.php";

$siteKey = '';
$secretKey = '';
$hideBadge = false;
$dataBadge = 'bottomright';
$debug = false;
$captcha = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secretKey, $hideBadge, $dataBadge, $debug);
$siteKey = 'sitekey';
$secretKey = 'secretkey';
// optional
$options [
'hideBadge' => false,
'dataBadge' => 'bottomright',
'timeout' => 5,
'debug' => false
];
$captcha = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secretKey, $options);

// you can override single option config like this
$captcha->setOption('debug', true);

if (!empty($_POST)) {
var_dump($captcha->verifyResponse($_POST['g-recaptcha-response']));
Expand Down
86 changes: 49 additions & 37 deletions src/InvisibleReCaptcha.php
Expand Up @@ -31,25 +31,11 @@ class InvisibleReCaptcha
protected $secretKey;

/**
* The config to determine if hide the badge.
* The other config options.
*
* @var boolean
* @var array
*/
protected $hideBadge;

/**
* Reposition the reCAPTCHA badge.
*
* @var string
*/
protected $dataBadge;

/**
* The config to determine if show debug info.
*
* @var boolean
*/
protected $debug;
protected $options;

/**
* @var \GuzzleHttp\Client
Expand All @@ -61,16 +47,18 @@ class InvisibleReCaptcha
*
* @param string $secretKey
* @param string $siteKey
* @param boolean $hideBadge
* @param array $options
*/
public function __construct($siteKey, $secretKey, $hideBadge = false, $dataBadge = 'bottomright', $debug = false)
public function __construct($siteKey, $secretKey, $options = [])
{
$this->siteKey = $siteKey;
$this->secretKey = $secretKey;
$this->hideBadge = $hideBadge;
$this->dataBadge = $dataBadge;
$this->debug = $debug;
$this->client = new Client(['timeout' => 5]);
$this->setOptions($options);
$this->setClient(
new Client([
'timeout' => $this->getOption('timeout', 5)
])
);
}

/**
Expand Down Expand Up @@ -104,11 +92,11 @@ public function render($lang = null)
{
$html = '<script src="' . $this->getPolyfillJs() . '"></script>' . PHP_EOL;
$html .= '<div id="_g-recaptcha"></div>' . PHP_EOL;
if ($this->hideBadge) {
if ($this->getOption('hideBadge', false)) {
$html .= '<style>.grecaptcha-badge{display:none;!important}</style>' . PHP_EOL;
}
$html .= '<div class="g-recaptcha" data-sitekey="' . $this->siteKey .'" ';
$html .= 'data-size="invisible" data-callback="_submitForm" data-badge="' . $this->dataBadge . '"></div>';
$html .= 'data-size="invisible" data-callback="_submitForm" data-badge="' . $this->getOption('dataBadge', 'bottomright') . '"></div>';
$html .= '<script src="' . $this->getCaptchaJs($lang) . '" async defer></script>' . PHP_EOL;
$html .= '<script>var _submitForm,_captchaForm,_captchaSubmit,_execute=true;</script>';
$html .= '<script>window.onload=function(){';
Expand All @@ -119,7 +107,7 @@ public function render($lang = null)
$html .= "_captchaForm.addEventListener('submit',";
$html .= "function(e){e.preventDefault();if(typeof _beforeSubmit==='function'){";
$html .= "_execute=_beforeSubmit();}if(_execute){grecaptcha.execute();}});";
if ($this->debug) {
if ($this->getOption('debug', false)) {
$html .= $this->renderDebug();
}
$html .= "}</script>" . PHP_EOL;
Expand Down Expand Up @@ -228,33 +216,57 @@ public function getSecretKey()
}

/**
* Getter function of hideBadge
* Set options
*
* @return strnig
* @param array $options
*/
public function getHideBadge()
public function setOptions($options)
{
return $this->hideBadge;
$this->options = $options;
}

/**
* Getter function of dataBadge
* Set option
*
* @return strnig
* @param string $key
* @param string $value
*/
public function getDataBadge()
public function setOption($key, $value)
{
return $this->dataBadge;
$this->options[$key] = $value;
}

/**
* Getter function of debug
* Getter function of options
*
* @return strnig
*/
public function getDebug()
public function getOptions()
{
return $this->options;
}

/**
* Get default option value for options. (for support under PHP 7.0)
*
* @param string $key
* @param string $value
*
* @return string
*/
public function getOption($key, $value = null)
{
return array_key_exists($key, $this->options) ? $this->options[$key] : $value;
}

/**
* Set guzzle client
*
* @param \GuzzleHttp\Client $client
*/
public function setClient(Client $client)
{
return $this->debug;
$this->client = $client;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/InvisibleReCaptchaServiceProvider.php
Expand Up @@ -31,9 +31,7 @@ public function register()
return new InvisibleReCaptcha(
$app['config']['captcha.siteKey'],
$app['config']['captcha.secretKey'],
$app['config']['captcha.hideBadge'],
$app['config']['captcha.dataBadge'],
$app['config']['captcha.debug']
$app['config']['captcha.options']
);
});

Expand Down
17 changes: 14 additions & 3 deletions src/config/captcha.php
@@ -1,9 +1,20 @@
<?php

return [
// your recapthca site key.
'siteKey' => env('INVISIBLE_RECAPTCHA_SITEKEY'),
// your recapthca secret key.
'secretKey' => env('INVISIBLE_RECAPTCHA_SECRETKEY'),
'hideBadge' => env('INVISIBLE_RECAPTCHA_BADGEHIDE', false),
'dataBadge' => env('INVISIBLE_RECAPTCHA_DATABADGE', 'bottomright'),
'debug' => env('INVISIBLE_RECAPTCHA_DEBUG', false)
// other options to customize your configs
'options' => [
// set true if you want to hide your recaptcha badge
'hideBadge' => env('INVISIBLE_RECAPTCHA_BADGEHIDE', false),
// optional, reposition the reCAPTCHA badge. 'inline' allows you to control the CSS.
// available values: bottomright, bottomleft, inline
'dataBadge' => env('INVISIBLE_RECAPTCHA_DATABADGE', 'bottomright'),
// timeout value for guzzle client
'timeout' => env('INVISIBLE_RECAPTCHA_TIMEOUT', 5),
// set true to show binding status on your javascript console
'debug' => env('INVISIBLE_RECAPTCHA_DEBUG', false)
]
];
26 changes: 20 additions & 6 deletions tests/CaptchaTest.php
Expand Up @@ -13,8 +13,12 @@ class CaptchaTest extends TestCase
{
const SITE_KEY = 'site_key';
const SECRET_KEY = 'secret_key';
const BADGE_HIDE = false;
const DEBUG = false;
const OPTIONS = [
'hideBadge' => false,
'dataBadge' => 'bottomright',
'timeout' => 5,
'debug' => false
];

protected $captcha;

Expand All @@ -23,20 +27,30 @@ protected function setUp()
$this->captcha = new InvisibleReCaptcha(
static::SITE_KEY,
static::SECRET_KEY,
static::BADGE_HIDE,
static::DEBUG
static::OPTIONS
);
}

public function testConstructor()
{
$this->assertEquals(static::SITE_KEY, $this->captcha->getSiteKey());
$this->assertEquals(static::SECRET_KEY, $this->captcha->getSecretKey());
$this->assertEquals(static::BADGE_HIDE, $this->captcha->getHideBadge());
$this->assertEquals(static::DEBUG, $this->captcha->getDebug());
$this->assertTrue($this->captcha->getClient() instanceof \GuzzleHttp\Client);
}

public function testGetOptions()
{
$this->assertEquals(static::OPTIONS, $this->captcha->getOptions());
}

public function testSetOption()
{
$this->captcha->setOption('debug', true);
$this->captcha->setOption('timeout', 10);
$this->assertEquals(10, $this->captcha->getOption('timeout'));
$this->assertTrue($this->captcha->getOption('debug'));
}

public function testGetCaptchaJs()
{
$js = 'https://www.google.com/recaptcha/api.js';
Expand Down

0 comments on commit 5caa2a0

Please sign in to comment.