Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit 0fe1176

Browse files
committed
Add an option api_host to custom the reCAPTCHA API server hostname
1 parent bdd66ca commit 0fe1176

10 files changed

Lines changed: 63 additions & 15 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ ewz_recaptcha:
7979
ajax: true
8080
```
8181
82+
`www.google.com` is blocked in Mainland China, you can override the default server like this:
83+
84+
``` yaml
85+
# app/config/config.yml
86+
87+
ewz_recaptcha:
88+
// ...
89+
api_host: recaptcha.net
90+
```
91+
8292
You can add HTTP Proxy configuration:
8393

8494
``` yaml

src/Bridge/Form/Extension/RecaptchaExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ protected function loadTypes()
3838
$this->app['ewz_recaptcha.public_key'],
3939
$this->app['ewz_recaptcha.enabled'],
4040
$this->app['ewz_recaptcha.ajax'],
41-
$this->app['ewz_recaptcha.locale_key']
41+
$this->app['ewz_recaptcha.locale_key'],
42+
$this->app['ewz_recaptcha.api_host']
4243
),
4344
);
4445
}

src/Bridge/RecaptchaServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function register(Application $app)
2424
$app['ewz_recaptcha.enabled'] = true;
2525
$app['ewz_recaptcha.verify_host'] = false;
2626
$app['ewz_recaptcha.ajax'] = false;
27+
$app['ewz_recaptcha.api_host'] = 'www.google.com';
2728
$app['ewz_recaptcha.http_proxy'] = array(
2829
'host' => null,
2930
'port' => null,

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getConfigTreeBuilder()
2727
->booleanNode('verify_host')->defaultFalse()->end()
2828
->booleanNode('ajax')->defaultFalse()->end()
2929
->scalarNode('locale_key')->defaultValue('%kernel.default_locale%')->end()
30+
->scalarNode('api_host')->defaultValue('www.google.com')->end()
3031
->booleanNode('locale_from_request')->defaultFalse()->end()
3132
->arrayNode('trusted_roles')->prototype('scalar')->treatNullLike(array())->end()
3233
->end()

src/Form/Type/EWZRecaptchaType.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
class EWZRecaptchaType extends AbstractType
1616
{
1717
/**
18-
* The reCAPTCHA server URL's.
18+
* The reCAPTCHA server URL.
19+
*
20+
* @var string
21+
*/
22+
protected $RECAPTCHA_API_SERVER;
23+
24+
/**
25+
* The reCAPTCHA JS server URL.
26+
*
27+
* @var string
1928
*/
20-
const RECAPTCHA_API_SERVER = 'https://www.google.com/recaptcha/api.js';
21-
const RECAPTCHA_API_JS_SERVER = '//www.google.com/recaptcha/api/js/recaptcha_ajax.js';
29+
protected $RECAPTCHA_API_JS_SERVER;
2230

2331
/**
2432
* The public key.
@@ -27,6 +35,13 @@ class EWZRecaptchaType extends AbstractType
2735
*/
2836
protected $publicKey;
2937

38+
/**
39+
* The API server host name.
40+
*
41+
* @var string
42+
*/
43+
protected $apiHost;
44+
3045
/**
3146
* Enable recaptcha?
3247
*
@@ -52,12 +67,15 @@ class EWZRecaptchaType extends AbstractType
5267
* @param bool $ajax Ajax status
5368
* @param LocaleResolver $localeResolver
5469
*/
55-
public function __construct($publicKey, $enabled, $ajax, LocaleResolver $localeResolver)
70+
public function __construct($publicKey, $enabled, $ajax, LocaleResolver $localeResolver, $apiHost='www.google.com')
5671
{
5772
$this->publicKey = $publicKey;
5873
$this->enabled = $enabled;
5974
$this->ajax = $ajax;
75+
$this->apiHost = $apiHost;
6076
$this->localeResolver = $localeResolver;
77+
$this->RECAPTCHA_API_JS_SERVER = '//'.$apiHost.'/recaptcha/api/js/recaptcha_ajax.js';
78+
$this->RECAPTCHA_API_SERVER = 'https://'.$apiHost.'/recaptcha/api.js';
6179
}
6280

6381
/**
@@ -68,6 +86,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
6886
$view->vars = array_replace($view->vars, array(
6987
'ewz_recaptcha_enabled' => $this->enabled,
7088
'ewz_recaptcha_ajax' => $this->ajax,
89+
'ewz_recaptcha_apihost' => $this->apiHost
7190
));
7291

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

81100
if (!$this->ajax) {
82101
$view->vars = array_replace($view->vars, array(
83-
'url_challenge' => sprintf('%s?hl=%s', self::RECAPTCHA_API_SERVER, $options['language']),
102+
'url_challenge' => sprintf('%s?hl=%s', $this->RECAPTCHA_API_SERVER, $options['language']),
84103
'public_key' => $this->publicKey,
85104
));
86105
} else {
87106
$view->vars = array_replace($view->vars, array(
88-
'url_api' => self::RECAPTCHA_API_JS_SERVER,
107+
'url_api' => $this->RECAPTCHA_API_JS_SERVER,
89108
'public_key' => $this->publicKey,
90109
));
91110
}
@@ -155,4 +174,14 @@ public function getPublicKey()
155174
{
156175
return $this->publicKey;
157176
}
177+
178+
/**
179+
* Gets the API host name.
180+
*
181+
* @return string The hostname for API
182+
*/
183+
public function getApiHost()
184+
{
185+
return $this->apiHost;
186+
}
158187
}

src/Resources/config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
- '%ewz_recaptcha.enabled%'
1616
- '%ewz_recaptcha.ajax%'
1717
- '@ewz_recaptcha.locale.resolver'
18+
- '%ewz_recaptcha.api_host%'
1819
tags:
1920
- { name: form.type }
2021

@@ -29,5 +30,6 @@ services:
2930
- '%ewz_recaptcha.verify_host%'
3031
- '@?security.authorization_checker'
3132
- '%ewz_recaptcha.trusted_roles%'
33+
- '%ewz_recaptcha.api_host%'
3234
tags:
3335
- { name: validator.constraint_validator, alias: 'ewz_recaptcha.true' }

src/Resources/views/Form/ewz_recaptcha_widget.html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<div style="width: 302px; height: 352px;">
4343
<div style="width: 302px; height: 352px; position: relative;">
4444
<div style="width: 302px; height: 352px; position: absolute;">
45-
<iframe src="https://www.google.com/recaptcha/api/fallback?k=<?php echo $public_key ?>"
45+
<iframe src="https://<?php echo $ewz_recaptcha_apihost ?>/recaptcha/api/fallback?k=<?php echo $public_key ?>"
4646
frameborder="0" scrolling="no"
4747
style="width: 302px; height:352px; border-style: none;"
4848
>

src/Resources/views/Form/ewz_recaptcha_widget.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<div style="width: 302px; height: 352px;">
4646
<div style="width: 302px; height: 352px; position: relative;">
4747
<div style="width: 302px; height: 352px; position: absolute;">
48-
<iframe src="https://www.google.com/recaptcha/api/fallback?k={{ form.vars.public_key }}"
48+
<iframe src="https://{{ form.vars.ewz_recaptcha_apihost }}/recaptcha/api/fallback?k={{ form.vars.public_key }}"
4949
frameborder="0" scrolling="no"
5050
style="width: 302px; height:352px; border-style: none;"
5151
>

src/Validator/Constraints/IsTrueValidator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class IsTrueValidator extends ConstraintValidator
2020
/**
2121
* Recaptcha Private Key.
2222
*
23-
* @var bool
23+
* @var string
2424
*/
2525
protected $privateKey;
2626

@@ -60,9 +60,11 @@ class IsTrueValidator extends ConstraintValidator
6060
protected $trusted_roles;
6161

6262
/**
63-
* The reCAPTCHA server URL's.
63+
* The reCAPTCHA verify server URL.
64+
*
65+
* @var string
6466
*/
65-
const RECAPTCHA_VERIFY_SERVER = 'https://www.google.com';
67+
protected $RECAPTCHA_VERIFY_SERVER;
6668

6769
/**
6870
* @param bool $enabled
@@ -80,7 +82,8 @@ public function __construct(
8082
array $httpProxy,
8183
$verifyHost,
8284
AuthorizationCheckerInterface $authorizationChecker = null,
83-
array $trusted_roles = array())
85+
array $trusted_roles = array(),
86+
$apiHost='www.google.com')
8487
{
8588
$this->enabled = $enabled;
8689
$this->privateKey = $privateKey;
@@ -89,6 +92,7 @@ public function __construct(
8992
$this->verifyHost = $verifyHost;
9093
$this->authorizationChecker = $authorizationChecker;
9194
$this->trusted_roles = $trusted_roles;
95+
$this->RECAPTCHA_VERIFY_SERVER = 'https://'.$apiHost;
9296
}
9397

9498
/**
@@ -145,7 +149,7 @@ private function checkAnswer($privateKey, $remoteip, $answer)
145149
return false;
146150
}
147151

148-
$response = $this->httpGet(self::RECAPTCHA_VERIFY_SERVER, '/recaptcha/api/siteverify', array(
152+
$response = $this->httpGet($this->RECAPTCHA_VERIFY_SERVER, '/recaptcha/api/siteverify', array(
149153
'secret' => $privateKey,
150154
'remoteip' => $remoteip,
151155
'response' => $answer,

tests/Form/Type/EWZRecaptchaTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function setUp()
1919
{
2020
$requestStack = $this->createMock(RequestStack::class);
2121
$localeResolver = new LocaleResolver('de', false, $requestStack);
22-
$this->type = new EWZRecaptchaType('key', true, true, $localeResolver);
22+
$this->type = new EWZRecaptchaType('key', true, true, $localeResolver, 'www.google.com');
2323
}
2424

2525
/**

0 commit comments

Comments
 (0)