/
ProxyPost.php
81 lines (73 loc) · 2.56 KB
/
ProxyPost.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace EWZ\Bundle\RecaptchaBundle\Extension\ReCaptcha\RequestMethod;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
/**
* Sends POST requests to the reCAPTCHA service though a proxy.
*/
class ProxyPost implements RequestMethod
{
/**
* HTTP Proxy informations.
*
* @var array
*/
private $httpProxy;
/**
* The reCAPTCHA verify server URL.
*
* @var string
*/
private $recaptchaVerifyUrl;
/**
* The timeout for the reCAPTCHA verification.
*
* @var int|null
*/
private $timeout;
/**
* Constructor
*
* @param array $httpProxy proxy data to connect to
* @param string $recaptchaVerifyServer
*/
public function __construct(array $httpProxy, $recaptchaVerifyServer, $timeout)
{
$this->httpProxy = $httpProxy;
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
$this->timeout = $timeout;
}
/**
* Submit the POST request with the specified parameters.
*
* @param RequestParameters $params Request parameters
* @return string Body of the reCAPTCHA response
*/
public function submit(RequestParameters $params)
{
/**
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n".sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth'])),
'method' => 'POST',
'content' => $params->toQueryString(),
// Force the peer to validate (not needed in 5.6.0+, but still works)
'verify_peer' => true,
// Force the peer validation to use www.google.com
$peer_key => 'www.google.com',
'proxy' => sprintf('tcp://%s:%s', $this->httpProxy['host'], $this->httpProxy['port']),
// While this is a non-standard request format, some proxy servers require it.
'request_fulluri' => true,
),
);
if (null !== $this->timeout) {
$options['http']['timeout'] = $this->timeout;
}
$context = stream_context_create($options);
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
}
}