Projet

Général

Profil

Paste
Télécharger (5,52 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / recaptcha / recaptcha-php / src / ReCaptcha / Response.php @ e326068a

1
<?php
2
/**
3
 * This is a PHP library that handles calling reCAPTCHA.
4
 *
5
 * @copyright Copyright (c) 2015, Google Inc.
6
 * @link      https://www.google.com/recaptcha
7
 *
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;
28

    
29
/**
30
 * The response returned from the service.
31
 */
32
class Response
33
{
34
    /**
35
     * Success or failure.
36
     * @var boolean
37
     */
38
    private $success = false;
39

    
40
    /**
41
     * Error code strings.
42
     * @var array
43
     */
44
    private $errorCodes = array();
45

    
46
    /**
47
     * The hostname of the site where the reCAPTCHA was solved.
48
     * @var string
49
     */
50
    private $hostname;
51

    
52
    /**
53
     * Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
54
     * @var string
55
     */
56
    private $challengeTs;
57

    
58
    /**
59
     * APK package name
60
     * @var string
61
     */
62
    private $apkPackageName;
63

    
64
    /**
65
     * Score assigned to the request
66
     * @var float
67
     */
68
    private $score;
69

    
70
    /**
71
     * Action as specified by the page
72
     * @var string
73
     */
74
    private $action;
75

    
76
    /**
77
     * Build the response from the expected JSON returned by the service.
78
     *
79
     * @param string $json
80
     * @return \ReCaptcha\Response
81
     */
82
    public static function fromJson($json)
83
    {
84
        $responseData = json_decode($json, true);
85

    
86
        if (!$responseData) {
87
            return new Response(false, array(ReCaptcha::E_INVALID_JSON));
88
        }
89

    
90
        $hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null;
91
        $challengeTs = isset($responseData['challenge_ts']) ? $responseData['challenge_ts'] : null;
92
        $apkPackageName = isset($responseData['apk_package_name']) ? $responseData['apk_package_name'] : null;
93
        $score = isset($responseData['score']) ? floatval($responseData['score']) : null;
94
        $action = isset($responseData['action']) ? $responseData['action'] : null;
95

    
96
        if (isset($responseData['success']) && $responseData['success'] == true) {
97
            return new Response(true, array(), $hostname, $challengeTs, $apkPackageName, $score, $action);
98
        }
99

    
100
        if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
101
            return new Response(false, $responseData['error-codes'], $hostname, $challengeTs, $apkPackageName, $score, $action);
102
        }
103

    
104
        return new Response(false, array(ReCaptcha::E_UNKNOWN_ERROR), $hostname, $challengeTs, $apkPackageName, $score, $action);
105
    }
106

    
107
    /**
108
     * Constructor.
109
     *
110
     * @param boolean $success
111
     * @param string $hostname
112
     * @param string $challengeTs
113
     * @param string $apkPackageName
114
     * @param float $score
115
     * @param strong $action
116
     * @param array $errorCodes
117
     */
118
    public function __construct($success, array $errorCodes = array(), $hostname = null, $challengeTs = null, $apkPackageName = null, $score = null, $action = null)
119
    {
120
        $this->success = $success;
121
        $this->hostname = $hostname;
122
        $this->challengeTs = $challengeTs;
123
        $this->apkPackageName = $apkPackageName;
124
        $this->score = $score;
125
        $this->action = $action;
126
        $this->errorCodes = $errorCodes;
127
    }
128

    
129
    /**
130
     * Is success?
131
     *
132
     * @return boolean
133
     */
134
    public function isSuccess()
135
    {
136
        return $this->success;
137
    }
138

    
139
    /**
140
     * Get error codes.
141
     *
142
     * @return array
143
     */
144
    public function getErrorCodes()
145
    {
146
        return $this->errorCodes;
147
    }
148

    
149
    /**
150
     * Get hostname.
151
     *
152
     * @return string
153
     */
154
    public function getHostname()
155
    {
156
        return $this->hostname;
157
    }
158

    
159
    /**
160
     * Get challenge timestamp
161
     *
162
     * @return string
163
     */
164
    public function getChallengeTs()
165
    {
166
        return $this->challengeTs;
167
    }
168

    
169
    /**
170
     * Get APK package name
171
     *
172
     * @return string
173
     */
174
    public function getApkPackageName()
175
    {
176
        return $this->apkPackageName;
177
    }
178
    /**
179
     * Get score
180
     *
181
     * @return float
182
     */
183
    public function getScore()
184
    {
185
        return $this->score;
186
    }
187

    
188
    /**
189
     * Get action
190
     *
191
     * @return string
192
     */
193
    public function getAction()
194
    {
195
        return $this->action;
196
    }
197

    
198
    public function toArray()
199
    {
200
        return array(
201
            'success' => $this->isSuccess(),
202
            'hostname' => $this->getHostname(),
203
            'challenge_ts' => $this->getChallengeTs(),
204
            'apk_package_name' => $this->getApkPackageName(),
205
            'score' => $this->getScore(),
206
            'action' => $this->getAction(),
207
            'error-codes' => $this->getErrorCodes(),
208
        );
209
    }
210
}