|
| 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 | + * URL to which requests are POSTed. |
| 15 | + * @const string |
| 16 | + */ |
| 17 | + const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
| 18 | + |
| 19 | + private $httpProxy; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructor |
| 23 | + * |
| 24 | + * @param array $httpProxy proxy data to connect to |
| 25 | + */ |
| 26 | + public function __construct(array $httpProxy) |
| 27 | + { |
| 28 | + $this->httpProxy = $httpProxy; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Submit the POST request with the specified parameters. |
| 33 | + * |
| 34 | + * @param RequestParameters $params Request parameters |
| 35 | + * @return string Body of the reCAPTCHA response |
| 36 | + */ |
| 37 | + public function submit(RequestParameters $params) |
| 38 | + { |
| 39 | + /** |
| 40 | + * PHP 5.6.0 changed the way you specify the peer name for SSL context options. |
| 41 | + * Using "CN_name" will still work, but it will raise deprecated errors. |
| 42 | + */ |
| 43 | + $peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name'; |
| 44 | + $options = array( |
| 45 | + 'http' => array( |
| 46 | + 'header' => "Content-type: application/x-www-form-urlencoded\r\n".sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth'])), |
| 47 | + 'method' => 'POST', |
| 48 | + 'content' => $params->toQueryString(), |
| 49 | + // Force the peer to validate (not needed in 5.6.0+, but still works) |
| 50 | + 'verify_peer' => true, |
| 51 | + // Force the peer validation to use www.google.com |
| 52 | + $peer_key => 'www.google.com', |
| 53 | + |
| 54 | + 'proxy' => sprintf('tcp://%s:%s', $this->httpProxy['host'], $this->httpProxy['port']), |
| 55 | + // While this is a non-standard request format, some proxy servers require it. |
| 56 | + 'request_fulluri' => true, |
| 57 | + ), |
| 58 | + ); |
| 59 | + $context = stream_context_create($options); |
| 60 | + return file_get_contents(self::SITE_VERIFY_URL, false, $context); |
| 61 | + } |
| 62 | +} |
0 commit comments