Skip to content

Commit

Permalink
Merge pull request #197 from moesoha/soha-1
Browse files Browse the repository at this point in the history
 Add an option `api_host` to custom the reCAPTCHA API server hostname
  • Loading branch information
excelwebzone committed Mar 25, 2018
2 parents 3219e7a + 96f6391 commit 6166ea6
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 15 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ ewz_recaptcha:
ajax: true
```

`www.google.com` is blocked in Mainland China, you can override the default server like this:

``` yaml
# app/config/config.yml

ewz_recaptcha:
// ...
api_host: recaptcha.net
```

You can add HTTP Proxy configuration:

``` yaml
Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/Form/Extension/RecaptchaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ protected function loadTypes()
$this->app['ewz_recaptcha.public_key'],
$this->app['ewz_recaptcha.enabled'],
$this->app['ewz_recaptcha.ajax'],
$this->app['ewz_recaptcha.locale_key']
$this->app['ewz_recaptcha.locale_key'],
$this->app['ewz_recaptcha.api_host']
),
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/RecaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function register(Application $app)
$app['ewz_recaptcha.enabled'] = true;
$app['ewz_recaptcha.verify_host'] = false;
$app['ewz_recaptcha.ajax'] = false;
$app['ewz_recaptcha.api_host'] = 'www.google.com';
$app['ewz_recaptcha.http_proxy'] = array(
'host' => null,
'port' => null,
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getConfigTreeBuilder()
->booleanNode('verify_host')->defaultFalse()->end()
->booleanNode('ajax')->defaultFalse()->end()
->scalarNode('locale_key')->defaultValue('%kernel.default_locale%')->end()
->scalarNode('api_host')->defaultValue('www.google.com')->end()
->booleanNode('locale_from_request')->defaultFalse()->end()
->arrayNode('trusted_roles')->prototype('scalar')->treatNullLike(array())->end()
->end()
Expand Down
41 changes: 35 additions & 6 deletions src/Form/Type/EWZRecaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
class EWZRecaptchaType extends AbstractType
{
/**
* The reCAPTCHA server URL's.
* The reCAPTCHA server URL.
*
* @var string
*/
protected $recaptchaApiServer;

/**
* The reCAPTCHA JS server URL.
*
* @var string
*/
const RECAPTCHA_API_SERVER = 'https://www.google.com/recaptcha/api.js';
const RECAPTCHA_API_JS_SERVER = '//www.google.com/recaptcha/api/js/recaptcha_ajax.js';
protected $recaptchaApiJsServer;

/**
* The public key.
Expand All @@ -27,6 +35,13 @@ class EWZRecaptchaType extends AbstractType
*/
protected $publicKey;

/**
* The API server host name.
*
* @var string
*/
protected $apiHost;

/**
* Enable recaptcha?
*
Expand All @@ -52,12 +67,15 @@ class EWZRecaptchaType extends AbstractType
* @param bool $ajax Ajax status
* @param LocaleResolver $localeResolver
*/
public function __construct($publicKey, $enabled, $ajax, LocaleResolver $localeResolver)
public function __construct($publicKey, $enabled, $ajax, LocaleResolver $localeResolver, $apiHost = 'www.google.com')
{
$this->publicKey = $publicKey;
$this->enabled = $enabled;
$this->ajax = $ajax;
$this->apiHost = $apiHost;
$this->localeResolver = $localeResolver;
$this->recaptchaApiJsServer = sprintf('//%s/recaptcha/api/js/recaptcha_ajax.js', $apiHost);
$this->recaptchaApiServer = sprintf('https://%s/recaptcha/api.js', $apiHost);
}

/**
Expand All @@ -68,6 +86,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars = array_replace($view->vars, array(
'ewz_recaptcha_enabled' => $this->enabled,
'ewz_recaptcha_ajax' => $this->ajax,
'ewz_recaptcha_apihost' => $this->apiHost
));

if (!$this->enabled) {
Expand All @@ -80,12 +99,12 @@ public function buildView(FormView $view, FormInterface $form, array $options)

if (!$this->ajax) {
$view->vars = array_replace($view->vars, array(
'url_challenge' => sprintf('%s?hl=%s', self::RECAPTCHA_API_SERVER, $options['language']),
'url_challenge' => sprintf('%s?hl=%s', $this->recaptchaApiServer, $options['language']),
'public_key' => $this->publicKey,
));
} else {
$view->vars = array_replace($view->vars, array(
'url_api' => self::RECAPTCHA_API_JS_SERVER,
'url_api' => $this->recaptchaApiJsServer,
'public_key' => $this->publicKey,
));
}
Expand Down Expand Up @@ -155,4 +174,14 @@ public function getPublicKey()
{
return $this->publicKey;
}

/**
* Gets the API host name.
*
* @return string The hostname for API
*/
public function getApiHost()
{
return $this->apiHost;
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
- '%ewz_recaptcha.enabled%'
- '%ewz_recaptcha.ajax%'
- '@ewz_recaptcha.locale.resolver'
- '%ewz_recaptcha.api_host%'
tags:
- { name: form.type }

Expand All @@ -29,5 +30,6 @@ services:
- '%ewz_recaptcha.verify_host%'
- '@?security.authorization_checker'
- '%ewz_recaptcha.trusted_roles%'
- '%ewz_recaptcha.api_host%'
tags:
- { name: validator.constraint_validator, alias: 'ewz_recaptcha.true' }
15 changes: 15 additions & 0 deletions src/Resources/translations/validators.zh_CN.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>This value is not a valid captcha.</source>
<target>没有有效完成验证码。</target>
</trans-unit>
<trans-unit id="2">
<source>The captcha was not resolved on the right domain.</source>
<target>验证码在不正确的域名上完成。</target>
</trans-unit>
</body>
</file>
</xliff>
2 changes: 1 addition & 1 deletion src/Resources/views/Form/ewz_recaptcha_widget.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<div style="width: 302px; height: 352px;">
<div style="width: 302px; height: 352px; position: relative;">
<div style="width: 302px; height: 352px; position: absolute;">
<iframe src="https://www.google.com/recaptcha/api/fallback?k=<?php echo $public_key ?>"
<iframe src="https://<?php echo $ewz_recaptcha_apihost ?>/recaptcha/api/fallback?k=<?php echo $public_key ?>"
frameborder="0" scrolling="no"
style="width: 302px; height:352px; border-style: none;"
>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/Form/ewz_recaptcha_widget.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<div style="width: 302px; height: 352px;">
<div style="width: 302px; height: 352px; position: relative;">
<div style="width: 302px; height: 352px; position: absolute;">
<iframe src="https://www.google.com/recaptcha/api/fallback?k={{ form.vars.public_key }}"
<iframe src="https://{{ form.vars.ewz_recaptcha_apihost }}/recaptcha/api/fallback?k={{ form.vars.public_key }}"
frameborder="0" scrolling="no"
style="width: 302px; height:352px; border-style: none;"
>
Expand Down
14 changes: 9 additions & 5 deletions src/Validator/Constraints/IsTrueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IsTrueValidator extends ConstraintValidator
/**
* Recaptcha Private Key.
*
* @var bool
* @var string
*/
protected $privateKey;

Expand Down Expand Up @@ -60,9 +60,11 @@ class IsTrueValidator extends ConstraintValidator
protected $trusted_roles;

/**
* The reCAPTCHA server URL's.
* The reCAPTCHA verify server URL.
*
* @var string
*/
const RECAPTCHA_VERIFY_SERVER = 'https://www.google.com';
protected $recaptchaVerifyServer;

/**
* @param bool $enabled
Expand All @@ -80,7 +82,8 @@ public function __construct(
array $httpProxy,
$verifyHost,
AuthorizationCheckerInterface $authorizationChecker = null,
array $trusted_roles = array())
array $trusted_roles = array(),
$apiHost = 'www.google.com')
{
$this->enabled = $enabled;
$this->privateKey = $privateKey;
Expand All @@ -89,6 +92,7 @@ public function __construct(
$this->verifyHost = $verifyHost;
$this->authorizationChecker = $authorizationChecker;
$this->trusted_roles = $trusted_roles;
$this->recaptchaVerifyServer = 'https://'.$apiHost;
}

/**
Expand Down Expand Up @@ -145,7 +149,7 @@ private function checkAnswer($privateKey, $remoteip, $answer)
return false;
}

$response = $this->httpGet(self::RECAPTCHA_VERIFY_SERVER, '/recaptcha/api/siteverify', array(
$response = $this->httpGet($this->recaptchaVerifyServer, '/recaptcha/api/siteverify', array(
'secret' => $privateKey,
'remoteip' => $remoteip,
'response' => $answer,
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Type/EWZRecaptchaTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp()
{
$requestStack = $this->createMock(RequestStack::class);
$localeResolver = new LocaleResolver('de', false, $requestStack);
$this->type = new EWZRecaptchaType('key', true, true, $localeResolver);
$this->type = new EWZRecaptchaType('key', true, true, $localeResolver, 'www.google.com');
}

/**
Expand Down

0 comments on commit 6166ea6

Please sign in to comment.