From bdd66ca0f9bbf3be590b3c7e2a685c9b3e79c896 Mon Sep 17 00:00:00 2001 From: Soha Jin Date: Sun, 25 Mar 2018 13:15:01 +0800 Subject: [PATCH 1/3] add Chinese translation --- src/Resources/translations/validators.zh_CN.xlf | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/Resources/translations/validators.zh_CN.xlf diff --git a/src/Resources/translations/validators.zh_CN.xlf b/src/Resources/translations/validators.zh_CN.xlf new file mode 100644 index 0000000..e30cf5e --- /dev/null +++ b/src/Resources/translations/validators.zh_CN.xlf @@ -0,0 +1,15 @@ + + + + + + This value is not a valid captcha. + 没有有效完成验证码。 + + + The captcha was not resolved on the right domain. + 验证码在不正确的域名上完成。 + + + + From 0fe11762e992cb4df3bb41a2ef52ae21dbf5be0a Mon Sep 17 00:00:00 2001 From: Soha Jin Date: Sun, 25 Mar 2018 13:15:13 +0800 Subject: [PATCH 2/3] Add an option `api_host` to custom the reCAPTCHA API server hostname --- README.md | 10 +++++ .../Form/Extension/RecaptchaExtension.php | 3 +- src/Bridge/RecaptchaServiceProvider.php | 1 + src/DependencyInjection/Configuration.php | 1 + src/Form/Type/EWZRecaptchaType.php | 41 ++++++++++++++++--- src/Resources/config/services.yml | 2 + .../views/Form/ewz_recaptcha_widget.html.php | 2 +- .../views/Form/ewz_recaptcha_widget.html.twig | 2 +- src/Validator/Constraints/IsTrueValidator.php | 14 ++++--- tests/Form/Type/EWZRecaptchaTypeTest.php | 2 +- 10 files changed, 63 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index fd7e1c3..22033b2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Bridge/Form/Extension/RecaptchaExtension.php b/src/Bridge/Form/Extension/RecaptchaExtension.php index 5b63fea..804542a 100644 --- a/src/Bridge/Form/Extension/RecaptchaExtension.php +++ b/src/Bridge/Form/Extension/RecaptchaExtension.php @@ -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'] ), ); } diff --git a/src/Bridge/RecaptchaServiceProvider.php b/src/Bridge/RecaptchaServiceProvider.php index a9cff4f..1e4fdca 100755 --- a/src/Bridge/RecaptchaServiceProvider.php +++ b/src/Bridge/RecaptchaServiceProvider.php @@ -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, diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index e104bb3..1c30685 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() diff --git a/src/Form/Type/EWZRecaptchaType.php b/src/Form/Type/EWZRecaptchaType.php index d7e76cf..c6cc9a4 100644 --- a/src/Form/Type/EWZRecaptchaType.php +++ b/src/Form/Type/EWZRecaptchaType.php @@ -15,10 +15,18 @@ class EWZRecaptchaType extends AbstractType { /** - * The reCAPTCHA server URL's. + * The reCAPTCHA server URL. + * + * @var string + */ + protected $RECAPTCHA_API_SERVER; + + /** + * 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 $RECAPTCHA_API_JS_SERVER; /** * The public key. @@ -27,6 +35,13 @@ class EWZRecaptchaType extends AbstractType */ protected $publicKey; + /** + * The API server host name. + * + * @var string + */ + protected $apiHost; + /** * Enable recaptcha? * @@ -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->RECAPTCHA_API_JS_SERVER = '//'.$apiHost.'/recaptcha/api/js/recaptcha_ajax.js'; + $this->RECAPTCHA_API_SERVER = 'https://'.$apiHost.'/recaptcha/api.js'; } /** @@ -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) { @@ -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->RECAPTCHA_API_SERVER, $options['language']), 'public_key' => $this->publicKey, )); } else { $view->vars = array_replace($view->vars, array( - 'url_api' => self::RECAPTCHA_API_JS_SERVER, + 'url_api' => $this->RECAPTCHA_API_JS_SERVER, 'public_key' => $this->publicKey, )); } @@ -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; + } } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 1df78d9..947c37c 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -15,6 +15,7 @@ services: - '%ewz_recaptcha.enabled%' - '%ewz_recaptcha.ajax%' - '@ewz_recaptcha.locale.resolver' + - '%ewz_recaptcha.api_host%' tags: - { name: form.type } @@ -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' } diff --git a/src/Resources/views/Form/ewz_recaptcha_widget.html.php b/src/Resources/views/Form/ewz_recaptcha_widget.html.php index d0c3baa..fe8844e 100644 --- a/src/Resources/views/Form/ewz_recaptcha_widget.html.php +++ b/src/Resources/views/Form/ewz_recaptcha_widget.html.php @@ -42,7 +42,7 @@
-