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