Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Proxy support #233

Closed
wants to merge 4 commits into from

2 participants

@zauberpony

I fixed the issues from the previous pull request.

Dependencies are no setter-injected, so the signature of the constructor does not need to be changed any more.

Unittests pass, I added one test.

Elmar Athmer added some commits
Elmar Athmer fix undefined variable notice
the errstr and errno need to be initialized, before they fsockopen can
write into them in case of a failed validation request
f89f28a
Elmar Athmer add proxy support for recaptcha validation f700c50
Elmar Athmer add documentation for recaptcha proxy defaults 10ea5f3
Elmar Athmer add info to proxy configuration nodes 6e766b4
@bamarni
Collaborator

@zauberpony : I've opened a PR (#234) which adds configuration to the validator, it should address your issue, once you'll confirm I'll merge it, thx

@bamarni bamarni closed this in c75972b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 9, 2013
  1. fix undefined variable notice

    Elmar Athmer authored
    the errstr and errno need to be initialized, before they fsockopen can
    write into them in case of a failed validation request
  2. add proxy support for recaptcha validation

    Elmar Athmer authored
  3. add documentation for recaptcha proxy defaults

    Elmar Athmer authored
  4. add info to proxy configuration nodes

    Elmar Athmer authored
This page is out of date. Refresh to see the latest.
View
11 DependencyInjection/Configuration.php
@@ -136,6 +136,17 @@ private function addRecaptcha(ArrayNodeDefinition $rootNode)
->scalarNode('server_url')->defaultValue('http://api.recaptcha.net')->end()
->scalarNode('public_key')->isRequired()->end()
->scalarNode('private_key')->isRequired()->end()
+ ->arrayNode('proxy')
+ ->canBeUnset()
+ ->treatNullLike(array('enabled' => false))
+ ->treatFalseLike(array('enabled' => false))
+ ->info('some hosts do only allow outgoing connections via proxy')
+ ->children()
+ ->booleanNode('enabled')->defaultFalse()->end()
+ ->scalarNode('host')->info('hostname of the proxy (without http://)')->end()
+ ->scalarNode('port')->info('port of the proxy server, typically 80 or 8080')->defaultValue('80')->end()
+ ->end()
+ ->end()
->arrayNode('ssl')
->canBeUnset()
->treatNullLike(array('use' => true))
View
1  DependencyInjection/GenemuFormExtension.php
@@ -127,6 +127,7 @@ private function registerRecaptchaConfiguration(array $configs, ContainerBuilder
$container->setParameter('genemu.form.recaptcha.public_key', $configs['public_key']);
$container->setParameter('genemu.form.recaptcha.code', $configs['code']);
$container->setParameter('genemu.form.recaptcha.options', $configs['configs']);
+ $container->setParameter('genemu.form.recaptcha.proxy', $configs['proxy']);
}
/**
View
20 Form/Core/Validator/ReCaptchaValidator.php
@@ -30,6 +30,7 @@ class ReCaptchaValidator implements EventSubscriberInterface
private $request;
private $privateKey;
private $code;
+ private $proxy;
/**
* @param Request $request
@@ -40,6 +41,8 @@ public function __construct(Request $request, $privateKey, $code = null)
{
$this->code = $code;
$this->request = $request;
+ // proxy is disabled by default
+ $this->proxy = array('enabled' => false);
if (empty($code)) {
if (empty($privateKey)) {
@@ -100,13 +103,16 @@ public function validate(DataEvent $event)
*/
private function check(array $datas, array $options)
{
+ $errno = 0;
+ $errstr = '';
$response = '';
$datas = http_build_query($datas, null, '&');
$httpRequest = sprintf($this->httpRequest, $options['path'], $options['host'], strlen($datas), $datas);
+
if (false === ($fs = @fsockopen(
- $options['host'],
- $options['port'],
+ $this->proxy['enabled'] ? $this->proxy['host'] : $options['host'],
+ $this->proxy['enabled'] ? $this->proxy['port'] : $options['port'],
$errno, $errstr,
$options['timeout']
))) {
@@ -129,4 +135,14 @@ public static function getSubscribedEvents()
{
return array(FormEvents::POST_BIND => 'validate');
}
+
+ public function getProxy()
+ {
+ return $this->proxy;
+ }
+
+ public function setProxy($proxy)
+ {
+ $this->proxy = $proxy;
+ }
}
View
4 Resources/config/form.xml
@@ -7,6 +7,7 @@
<parameters>
<parameter key="genemu.form.recaptcha.public_key" />
<parameter key="genemu.form.recaptcha.private_key" />
+ <parameter key="genemu.form.recaptcha.proxy" />
<parameter key="genemu.form.recaptcha.server_url" />
<parameter key="genemu.form.recaptcha.code" />
<parameter key="genemu.form.recaptcha.options" type="collection" />
@@ -20,6 +21,9 @@
<argument type="service" id="request" strict="false" />
<argument>%genemu.form.recaptcha.private_key%</argument>
<argument>%genemu.form.recaptcha.code%</argument>
+ <call method="setProxy">
+ <argument on-invalid="ignore">%genemu.form.recaptcha.proxy%</argument>
+ </call>
</service>
<service id="genemu.form.core.type.recaptcha" class="Genemu\Bundle\FormBundle\Form\Core\Type\ReCaptchaType">
View
5 Resources/doc/recaptcha/default.md
@@ -9,4 +9,9 @@ genemu_form:
ssl:
use: true
server_url: 'https://api-secure.recaptcha.net'
+ # use a proxy server for outgoing code verification requests
+ proxy:
+ enabled: false
+ host: '' # hostname of the proxyserver
+ port: 80
```
View
11 Tests/Form/Core/Type/ReCaptchaTypeTest.php
@@ -77,6 +77,17 @@ public function testCode($code, $isValid)
$this->assertEquals($isValid, $form->isValid());
}
+ /**
+ * assert, that the proxy is disabled by default
+ */
+ public function testProxy() {
+ $request = new Request(array(), array('recaptcha_response_field' => 'foobar'));
+ $validator = new ReCaptchaValidator($request, 'privateKey', 'foobar');
+
+ $proxy = $validator->getProxy();
+ $this->assertFalse($proxy['enabled']);
+ }
+
public function provideCodes()
{
return array(
Something went wrong with that request. Please try again.