Skip to content

Commit

Permalink
Add proxy support without curl fix excelwebzone#1
Browse files Browse the repository at this point in the history
Add proxy support without curl fix excelwebzone#1
  • Loading branch information
lordvacilaoerrocerto committed Feb 26, 2014
1 parent 4e89f57 commit a65bf9e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
23 changes: 22 additions & 1 deletion DependencyInjection/Configuration.php
Expand Up @@ -2,6 +2,7 @@

namespace EWZ\Bundle\RecaptchaBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand Down Expand Up @@ -29,7 +30,27 @@ public function getConfigTreeBuilder()
->scalarNode('locale_key')->defaultValue('kernel.default_locale')->end()
->end()
;


$this->addHttpClientConfiguration($rootNode);

return $treeBuilder;
}

private function addHttpClientConfiguration(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('http_proxy')
->addDefaultsIfNotSet()
->children()
->scalarNode('type')->defaultValue(null)->end()
->scalarNode('host')->defaultValue(null)->end()
->scalarNode('port')->defaultValue(null)->end()
->scalarNode('auth')->defaultValue(null)->end()
->end()
->end()
->end()
;
}

}
43 changes: 20 additions & 23 deletions Validator/Constraints/TrueValidator.php
Expand Up @@ -98,32 +98,29 @@ private function checkAnswer($privateKey, $remoteip, $challenge, $response, $ext
*/
private function httpPost($host, $path, $data, $port = 80)
{
$req = $this->getQSEncode($data);

$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: ".strlen($req)."\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;

$response = null;
if (!$fs = @fsockopen($host, $port, $errno, $errstr, 10)) {
throw new ValidatorException('Could not open socket');
$req = http_build_query($data);

$opts['http'] = array(
'method' => "POST",
'user-agent' => 'reCAPTCHA/PHP',
'timeout' => 10,
'header' => "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($req)."\r\n",
'content' => $req
);
$httpProxy = $this->container->getParameter('ewz_recaptcha.http_proxy');
if (isset($httpProxy['host'], $httpProxy['port'])) {
$opts['http']['proxy'] = 'tcp://' . $httpProxy['host'] . ':' . $httpProxy['port'];
$opts['http']['request_fulluri'] = true;
if (isset($httpProxy['auth'])) {
$opts['http']['header'] .= "Proxy-Authorization: Basic ".base64_encode($httpProxy['auth'])."\r\n";
}
}
$context = stream_context_create(($opts));

fwrite($fs, $http_request);

while (!feof($fs)) {
$response .= fgets($fs, 1160); // one TCP-IP packet
if (!$response = file_get_contents("http://$host$path", false, $context)) {
throw new ValidatorException('Could not open socket');
}

fclose($fs);

$response = explode("\r\n\r\n", $response, 2);

return $response;
return array(1=>$response);
}

/**
Expand Down

0 comments on commit a65bf9e

Please sign in to comment.