Révision e326068a
Ajouté par Assos Assos il y a plus de 5 ans
drupal7/sites/all/modules/recaptcha/recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php | ||
---|---|---|
3 | 3 |
* This is a PHP library that handles calling reCAPTCHA. |
4 | 4 |
* |
5 | 5 |
* @copyright Copyright (c) 2015, Google Inc. |
6 |
* @link http://www.google.com/recaptcha |
|
6 |
* @link https://www.google.com/recaptcha
|
|
7 | 7 |
* |
8 | 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | 9 |
* of this software and associated documentation files (the "Software"), to deal |
... | ... | |
26 | 26 |
|
27 | 27 |
namespace ReCaptcha\RequestMethod; |
28 | 28 |
|
29 |
use ReCaptcha\ReCaptcha; |
|
29 | 30 |
use ReCaptcha\RequestMethod; |
30 | 31 |
use ReCaptcha\RequestParameters; |
31 | 32 |
|
32 | 33 |
/** |
33 | 34 |
* Sends a POST request to the reCAPTCHA service, but makes use of fsockopen() |
34 | 35 |
* instead of get_file_contents(). This is to account for people who may be on |
35 |
* servers where allow_furl_open is disabled.
|
|
36 |
* servers where allow_url_open is disabled. |
|
36 | 37 |
*/ |
37 | 38 |
class SocketPost implements RequestMethod |
38 | 39 |
{ |
39 |
/** |
|
40 |
* reCAPTCHA service host. |
|
41 |
* @const string |
|
42 |
*/ |
|
43 |
const RECAPTCHA_HOST = 'www.google.com'; |
|
44 |
|
|
45 |
/** |
|
46 |
* @const string reCAPTCHA service path |
|
47 |
*/ |
|
48 |
const SITE_VERIFY_PATH = '/recaptcha/api/siteverify'; |
|
49 |
|
|
50 |
/** |
|
51 |
* @const string Bad request error |
|
52 |
*/ |
|
53 |
const BAD_REQUEST = '{"success": false, "error-codes": ["invalid-request"]}'; |
|
54 |
|
|
55 |
/** |
|
56 |
* @const string Bad response error |
|
57 |
*/ |
|
58 |
const BAD_RESPONSE = '{"success": false, "error-codes": ["invalid-response"]}'; |
|
59 |
|
|
60 | 40 |
/** |
61 | 41 |
* Socket to the reCAPTCHA service |
62 | 42 |
* @var Socket |
... | ... | |
64 | 44 |
private $socket; |
65 | 45 |
|
66 | 46 |
/** |
67 |
* Constructor
|
|
47 |
* Only needed if you want to override the defaults
|
|
68 | 48 |
* |
69 | 49 |
* @param \ReCaptcha\RequestMethod\Socket $socket optional socket, injectable for testing |
50 |
* @param string $siteVerifyUrl URL for reCAPTCHA sitevrerify API |
|
70 | 51 |
*/ |
71 |
public function __construct(Socket $socket = null) |
|
52 |
public function __construct(Socket $socket = null, $siteVerifyUrl = null)
|
|
72 | 53 |
{ |
73 |
if (!is_null($socket)) { |
|
74 |
$this->socket = $socket; |
|
75 |
} else { |
|
76 |
$this->socket = new Socket(); |
|
77 |
} |
|
54 |
$this->socket = (is_null($socket)) ? new Socket() : $socket; |
|
55 |
$this->siteVerifyUrl = (is_null($siteVerifyUrl)) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl; |
|
78 | 56 |
} |
79 | 57 |
|
80 | 58 |
/** |
... | ... | |
87 | 65 |
{ |
88 | 66 |
$errno = 0; |
89 | 67 |
$errstr = ''; |
68 |
$urlParsed = parse_url($this->siteVerifyUrl); |
|
90 | 69 |
|
91 |
if (false === $this->socket->fsockopen('ssl://' . self::RECAPTCHA_HOST, 443, $errno, $errstr, 30)) {
|
|
92 |
return self::BAD_REQUEST;
|
|
70 |
if (false === $this->socket->fsockopen('ssl://' . $urlParsed['host'], 443, $errno, $errstr, 30)) {
|
|
71 |
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
|
|
93 | 72 |
} |
94 | 73 |
|
95 | 74 |
$content = $params->toQueryString(); |
96 | 75 |
|
97 |
$request = "POST " . self::SITE_VERIFY_PATH . " HTTP/1.1\r\n";
|
|
98 |
$request .= "Host: " . self::RECAPTCHA_HOST . "\r\n";
|
|
76 |
$request = "POST " . $urlParsed['path'] . " HTTP/1.1\r\n";
|
|
77 |
$request .= "Host: " . $urlParsed['host'] . "\r\n";
|
|
99 | 78 |
$request .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
100 | 79 |
$request .= "Content-length: " . strlen($content) . "\r\n"; |
101 | 80 |
$request .= "Connection: close\r\n\r\n"; |
... | ... | |
111 | 90 |
$this->socket->fclose(); |
112 | 91 |
|
113 | 92 |
if (0 !== strpos($response, 'HTTP/1.1 200 OK')) { |
114 |
return self::BAD_RESPONSE;
|
|
93 |
return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}';
|
|
115 | 94 |
} |
116 | 95 |
|
117 | 96 |
$parts = preg_split("#\n\s*\n#Uis", $response); |
Formats disponibles : Unified diff
Weekly update of contrib modules