-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvemedialib.php
248 lines (212 loc) · 7.32 KB
/
solvemedialib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/*
* Copyright (c) 2009 by Jeff Weisberg
* Author: Jeff Weisberg
* Created: 2009-Jun-22 16:44 (EDT)
* Function: SolveMedia API php code
*
* $Id: solvemedialib.php,v 1.13 2011/09/13 16:05:57 jaw Exp $
*
* This is a PHP library that handles calling SolveMedia.
* - Documentation and latest version
* http://www.solvemedia.com/
* - Get a SolveMedia API Keys
* http://api.solvemedia.com/public/signup
*/
/* This code is based on code from,
* and copied, modified and distributed with permission in accordance with its terms:
*
* Copyright (c) 2007 reCAPTCHA
* AUTHORS:
* Mike Crawford
* Ben Maurer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* The solvemedia server URL's
*/
define('ADCOPY_API_SERVER', 'http://api.solvemedia.com');
define('ADCOPY_API_SECURE_SERVER', 'https://api-secure.solvemedia.com');
define('ADCOPY_VERIFY_SERVER', 'verify.solvemedia.com');
define('ADCOPY_SIGNUP', 'http://api.solvemedia.com/public/signup');
/**
* Encodes the given data into a query string format
* @param $data - array of string elements to be encoded
* @return string - encoded request
*/
function _adcopy_qsencode($data)
{
$req = '';
foreach($data as $key => $value)
{
$req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
}
// Cut the last '&'
$req = substr($req, 0, strlen($req)-1);
return $req;
}
/**
* Submits an HTTP POST to a solvemedia server
* @param string $host
* @param string $path
* @param array $data
* @param int port
* @return array response
*/
function _adcopy_http_post($host, $path, $data, $port = 80)
{
$req = _adcopy_qsencode ($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: solvemedia/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
$response = '';
if(false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10)))
{
die ('Could not open socket');
}
fwrite($fs, $http_request);
while(!feof($fs))
{
$response .= fgets($fs, 1024); // One TCP-IP packet [sic]
}
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response;
}
/**
* Gets the challenge HTML (javascript and non-javascript version).
* This is called from the browser, and the resulting solvemedia HTML widget
* is embedded within the HTML form it was called from.
* @param string $pubkey A public key for solvemedia
* @param string $error The error given by solvemedia (optional, default is null)
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
* @return string - The HTML to be embedded in the user's form.
*/
function solvemedia_get_html($pubkey, $error = null, $use_ssl = false)
{
if ($pubkey == null || $pubkey == '')
{
die ("To use solvemedia you must get an API key from <a href='" . ADCOPY_SIGNUP . "'>" . ADCOPY_SIGNUP . "</a>");
}
if ($use_ssl)
{
$server = ADCOPY_API_SECURE_SERVER;
}
else
{
$server = ADCOPY_API_SERVER;
}
$errorpart = '';
if($error)
{
$errorpart = ';error=1';
}
return '<script type="text/javascript" src="'. $server . '/papi/challenge.script?k=' . $pubkey . $errorpart . '"></script>
<noscript>
<iframe src="'. $server . '/papi/challenge.noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="adcopy_challenge" rows="3" cols="40"></textarea>
<input type="hidden" name="adcopy_response" value="manual_challenge"/>
</noscript>';
}
/**
* A SolveMediaResponse is returned from solvemedia_check_answer()
*/
class SolveMediaResponse
{
var $is_valid;
var $error;
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
* @param string $privkey
* @param string $remoteip
* @param string $challenge
* @param string $response
* @param string $hashkey
* @return SolveMediaResponse
*/
function solvemedia_check_answer($privkey, $remoteip, $challenge, $response, $hashkey = '')
{
if($privkey == null || $privkey == '')
{
die ("To use solvemedia you must get an API key from <a href='" . ADCOPY_SIGNUP . "'>" . ADCOPY_SIGNUP . "</a>");
}
if ($remoteip == null || $remoteip == '')
{
die ("For security reasons, you must pass the remote ip to solvemedia");
}
//discard spam submissions
if(($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) || htmlspecialchars(trim($response)) === 'manual_challenge')
{
$adcopy_response = new SolveMediaResponse();
$adcopy_response->is_valid = false;
$adcopy_response->error = 'incorrect-solution';
return $adcopy_response;
}
$response = _adcopy_http_post (ADCOPY_VERIFY_SERVER, "/papi/verify",
array (
'privatekey' => $privkey,
'remoteip' => $remoteip,
'challenge' => $challenge,
'response' => $response
)
);
$answers = explode ("\n", $response[1]);
$adcopy_response = new SolveMediaResponse();
if(strlen($hashkey))
{
# validate message authenticator
$hash = sha1($answers[0] . $challenge . $hashkey);
if($hash != $answers[2])
{
$adcopy_response->is_valid = false;
$adcopy_response->error = 'hash-fail';
return $adcopy_response;
}
}
if(trim ($answers[0]) == 'true')
{
$adcopy_response->is_valid = true;
}
else
{
$adcopy_response->is_valid = false;
$adcopy_response->error = $answers[1];
}
return $adcopy_response;
}
/**
* gets a URL where the user can sign up for solvemedia. If your application
* has a configuration page where you enter a key, you should provide a link
* using this function.
* @param string $domain The domain where the page is hosted
* @param string $appname The name of your application
*/
function solvemedia_get_signup_url ($domain = null, $appname = null)
{
return ADCOPY_SIGNUP . "?" . _adcopy_qsencode (array ('domain' => $domain, 'app' => $appname));
}
/* Mailhide related code */
/* [ deleted ] */
?>