root / drupal7 / sites / all / modules / recaptcha / recaptcha-php / src / ReCaptcha / RequestMethod / SocketPost.php @ e326068a
1 | be58a50c | Assos Assos | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * This is a PHP library that handles calling reCAPTCHA.
|
||
4 | *
|
||
5 | * @copyright Copyright (c) 2015, Google Inc.
|
||
6 | e326068a | Assos Assos | * @link https://www.google.com/recaptcha
|
7 | be58a50c | Assos Assos | *
|
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
9 | * of this software and associated documentation files (the "Software"), to deal
|
||
10 | * in the Software without restriction, including without limitation the rights
|
||
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
12 | * copies of the Software, and to permit persons to whom the Software is
|
||
13 | * furnished to do so, subject to the following conditions:
|
||
14 | *
|
||
15 | * The above copyright notice and this permission notice shall be included in
|
||
16 | * all copies or substantial portions of the Software.
|
||
17 | *
|
||
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
24 | * THE SOFTWARE.
|
||
25 | */
|
||
26 | |||
27 | namespace ReCaptcha\RequestMethod; |
||
28 | |||
29 | e326068a | Assos Assos | use ReCaptcha\ReCaptcha; |
30 | be58a50c | Assos Assos | use ReCaptcha\RequestMethod; |
31 | use ReCaptcha\RequestParameters; |
||
32 | |||
33 | /**
|
||
34 | * Sends a POST request to the reCAPTCHA service, but makes use of fsockopen()
|
||
35 | * instead of get_file_contents(). This is to account for people who may be on
|
||
36 | e326068a | Assos Assos | * servers where allow_url_open is disabled.
|
37 | be58a50c | Assos Assos | */
|
38 | class SocketPost implements RequestMethod |
||
39 | { |
||
40 | /**
|
||
41 | * Socket to the reCAPTCHA service
|
||
42 | * @var Socket
|
||
43 | */
|
||
44 | private $socket; |
||
45 | |||
46 | /**
|
||
47 | e326068a | Assos Assos | * Only needed if you want to override the defaults
|
48 | be58a50c | Assos Assos | *
|
49 | * @param \ReCaptcha\RequestMethod\Socket $socket optional socket, injectable for testing
|
||
50 | e326068a | Assos Assos | * @param string $siteVerifyUrl URL for reCAPTCHA sitevrerify API
|
51 | be58a50c | Assos Assos | */
|
52 | e326068a | Assos Assos | public function __construct(Socket $socket = null, $siteVerifyUrl = null) |
53 | be58a50c | Assos Assos | { |
54 | e326068a | Assos Assos | $this->socket = (is_null($socket)) ? new Socket() : $socket; |
55 | $this->siteVerifyUrl = (is_null($siteVerifyUrl)) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl; |
||
56 | be58a50c | Assos Assos | } |
57 | |||
58 | /**
|
||
59 | * Submit the POST request with the specified parameters.
|
||
60 | *
|
||
61 | * @param RequestParameters $params Request parameters
|
||
62 | * @return string Body of the reCAPTCHA response
|
||
63 | */
|
||
64 | public function submit(RequestParameters $params) |
||
65 | { |
||
66 | $errno = 0; |
||
67 | $errstr = ''; |
||
68 | e326068a | Assos Assos | $urlParsed = parse_url($this->siteVerifyUrl); |
69 | be58a50c | Assos Assos | |
70 | e326068a | Assos Assos | if (false === $this->socket->fsockopen('ssl://' . $urlParsed['host'], 443, $errno, $errstr, 30)) { |
71 | return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; |
||
72 | be58a50c | Assos Assos | } |
73 | |||
74 | $content = $params->toQueryString(); |
||
75 | |||
76 | e326068a | Assos Assos | $request = "POST " . $urlParsed['path'] . " HTTP/1.1\r\n"; |
77 | $request .= "Host: " . $urlParsed['host'] . "\r\n"; |
||
78 | be58a50c | Assos Assos | $request .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
79 | $request .= "Content-length: " . strlen($content) . "\r\n"; |
||
80 | $request .= "Connection: close\r\n\r\n"; |
||
81 | $request .= $content . "\r\n\r\n"; |
||
82 | |||
83 | $this->socket->fwrite($request); |
||
84 | $response = ''; |
||
85 | |||
86 | while (!$this->socket->feof()) { |
||
87 | $response .= $this->socket->fgets(4096); |
||
88 | } |
||
89 | |||
90 | $this->socket->fclose(); |
||
91 | |||
92 | if (0 !== strpos($response, 'HTTP/1.1 200 OK')) { |
||
93 | e326068a | Assos Assos | return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; |
94 | be58a50c | Assos Assos | } |
95 | |||
96 | $parts = preg_split("#\n\s*\n#Uis", $response); |
||
97 | |||
98 | return $parts[1]; |
||
99 | } |
||
100 | } |