| 1 |
<?php |
| 2 |
/** |
| 3 |
* This is a PHP library that handles calling reCAPTCHA. |
| 4 |
* |
| 5 |
* BSD 3-Clause License |
| 6 |
* @copyright (c) 2019, Google Inc. |
| 7 |
* @link https://www.google.com/recaptcha |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or without |
| 11 |
* modification, are permitted provided that the following conditions are met: |
| 12 |
* 1. Redistributions of source code must retain the above copyright notice, this |
| 13 |
* list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
* this list of conditions and the following disclaimer in the documentation |
| 17 |
* and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. Neither the name of the copyright holder nor the names of its |
| 20 |
* contributors may be used to endorse or promote products derived from |
| 21 |
* this software without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 24 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 29 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 31 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
*/ |
| 34 |
|
| 35 |
namespace ReCaptcha; |
| 36 |
|
| 37 |
/** |
| 38 |
* The response returned from the service. |
| 39 |
*/ |
| 40 |
class Response |
| 41 |
{ |
| 42 |
/** |
| 43 |
* Success or failure. |
| 44 |
* @var boolean |
| 45 |
*/ |
| 46 |
private $success = false; |
| 47 |
|
| 48 |
/** |
| 49 |
* Error code strings. |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
private $errorCodes = array(); |
| 53 |
|
| 54 |
/** |
| 55 |
* The hostname of the site where the reCAPTCHA was solved. |
| 56 |
* @var string |
| 57 |
*/ |
| 58 |
private $hostname; |
| 59 |
|
| 60 |
/** |
| 61 |
* Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ) |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
private $challengeTs; |
| 65 |
|
| 66 |
/** |
| 67 |
* APK package name |
| 68 |
* @var string |
| 69 |
*/ |
| 70 |
private $apkPackageName; |
| 71 |
|
| 72 |
/** |
| 73 |
* Score assigned to the request |
| 74 |
* @var float |
| 75 |
*/ |
| 76 |
private $score; |
| 77 |
|
| 78 |
/** |
| 79 |
* Action as specified by the page |
| 80 |
* @var string |
| 81 |
*/ |
| 82 |
private $action; |
| 83 |
|
| 84 |
/** |
| 85 |
* Build the response from the expected JSON returned by the service. |
| 86 |
* |
| 87 |
* @param string $json |
| 88 |
* @return \ReCaptcha\Response |
| 89 |
*/ |
| 90 |
public static function fromJson($json) |
| 91 |
{ |
| 92 |
$responseData = json_decode($json, true); |
| 93 |
|
| 94 |
if (!$responseData) { |
| 95 |
return new Response(false, array(ReCaptcha::E_INVALID_JSON)); |
| 96 |
} |
| 97 |
|
| 98 |
$hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null; |
| 99 |
$challengeTs = isset($responseData['challenge_ts']) ? $responseData['challenge_ts'] : null; |
| 100 |
$apkPackageName = isset($responseData['apk_package_name']) ? $responseData['apk_package_name'] : null; |
| 101 |
$score = isset($responseData['score']) ? floatval($responseData['score']) : null; |
| 102 |
$action = isset($responseData['action']) ? $responseData['action'] : null; |
| 103 |
|
| 104 |
if (isset($responseData['success']) && $responseData['success'] == true) { |
| 105 |
return new Response(true, array(), $hostname, $challengeTs, $apkPackageName, $score, $action); |
| 106 |
} |
| 107 |
|
| 108 |
if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) { |
| 109 |
return new Response(false, $responseData['error-codes'], $hostname, $challengeTs, $apkPackageName, $score, $action); |
| 110 |
} |
| 111 |
|
| 112 |
return new Response(false, array(ReCaptcha::E_UNKNOWN_ERROR), $hostname, $challengeTs, $apkPackageName, $score, $action); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Constructor. |
| 117 |
* |
| 118 |
* @param boolean $success |
| 119 |
* @param string $hostname |
| 120 |
* @param string $challengeTs |
| 121 |
* @param string $apkPackageName |
| 122 |
* @param float $score |
| 123 |
* @param string $action |
| 124 |
* @param array $errorCodes |
| 125 |
*/ |
| 126 |
public function __construct($success, array $errorCodes = array(), $hostname = null, $challengeTs = null, $apkPackageName = null, $score = null, $action = null) |
| 127 |
{ |
| 128 |
$this->success = $success; |
| 129 |
$this->hostname = $hostname; |
| 130 |
$this->challengeTs = $challengeTs; |
| 131 |
$this->apkPackageName = $apkPackageName; |
| 132 |
$this->score = $score; |
| 133 |
$this->action = $action; |
| 134 |
$this->errorCodes = $errorCodes; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Is success? |
| 139 |
* |
| 140 |
* @return boolean |
| 141 |
*/ |
| 142 |
public function isSuccess() |
| 143 |
{ |
| 144 |
return $this->success; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get error codes. |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public function getErrorCodes() |
| 153 |
{ |
| 154 |
return $this->errorCodes; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get hostname. |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
public function getHostname() |
| 163 |
{ |
| 164 |
return $this->hostname; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get challenge timestamp |
| 169 |
* |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function getChallengeTs() |
| 173 |
{ |
| 174 |
return $this->challengeTs; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Get APK package name |
| 179 |
* |
| 180 |
* @return string |
| 181 |
*/ |
| 182 |
public function getApkPackageName() |
| 183 |
{ |
| 184 |
return $this->apkPackageName; |
| 185 |
} |
| 186 |
/** |
| 187 |
* Get score |
| 188 |
* |
| 189 |
* @return float |
| 190 |
*/ |
| 191 |
public function getScore() |
| 192 |
{ |
| 193 |
return $this->score; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Get action |
| 198 |
* |
| 199 |
* @return string |
| 200 |
*/ |
| 201 |
public function getAction() |
| 202 |
{ |
| 203 |
return $this->action; |
| 204 |
} |
| 205 |
|
| 206 |
public function toArray() |
| 207 |
{ |
| 208 |
return array( |
| 209 |
'success' => $this->isSuccess(), |
| 210 |
'hostname' => $this->getHostname(), |
| 211 |
'challenge_ts' => $this->getChallengeTs(), |
| 212 |
'apk_package_name' => $this->getApkPackageName(), |
| 213 |
'score' => $this->getScore(), |
| 214 |
'action' => $this->getAction(), |
| 215 |
'error-codes' => $this->getErrorCodes(), |
| 216 |
); |
| 217 |
} |
| 218 |
} |
| 219 |
|