This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcaptcha.php
404 lines (368 loc) · 12 KB
/
funcaptcha.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/*
* FunCaptcha
* PHP Integration Library
*
* @version 1.1.0
*
* Copyright (c) 2013 SwipeAds -- http://www.funcaptcha.co
* AUTHOR:
* Kevin Gosschalk
*
* 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.
*
*/
define("FUNCAPTCHA_SERVER", "funcaptcha.co");
// Define class if it does not already exist
if ( ! class_exists('FUNCAPTCHA')):
class FUNCAPTCHA {
// Set defaults for values that can be specified via the config file or passed in via __construct.
protected $funcaptcha_public_key = '';
protected $funcaptcha_private_key = '';
protected $funcaptcha_host = 'funcaptcha.co';
protected $funcaptcha_challenge_url = '';
protected $funcaptcha_debug = FALSE;
protected $funcaptcha_api_type = "wordpress";
protected $funcaptcha_plugin_version = "1.3.1";
protected $funcaptcha_security_level = 0;
protected $funcaptcha_lightbox_mode = FALSE;
protected $funcaptcha_lightbox_button_id = "";
protected $funcaptcha_lightbox_submit_javascript = "";
protected $session_token;
protected $funcaptcha_theme = 0;
protected $funcaptcha_proxy;
protected $funcaptcha_json_path = "json.php";
protected $version = '1.1.0';
/**
* Constructor
*
*/
public function __construct()
{
$this->funcaptcha_host = FUNCAPTCHA_SERVER;
if ($this->funcaptcha_api_type == "vBulletin") {
$this->funcaptcha_json_path = DIR . "/includes/json.php";
}
if ($this->funcaptcha_host == "")
{
$this->msgLog("ERROR", "Warning: Host is not set.");
}
else
{
$this->msgLog("DEBUG", "Set Host: '$this->funcaptcha_host'");
}
}
/**
* Returns FunCaptcha HTML to display in form
*
* @param string $public_key - FunCaptcha public key
* @param array $args - Additional information to pass to FunCaptcha servers
* @return string
*/
public function getFunCaptcha($public_key, $args=null)
{
$this->funcaptcha_public_key = $public_key;
if ($this->funcaptcha_public_key == "" || $this->funcaptcha_public_key == null)
{
$this->msgLog("ERROR", "Warning: Public key is not set.");
}
else
{
$this->msgLog("DEBUG", "Public key: '$this->funcaptcha_public_key'");
}
//send your public key, your site name, the users ip and browser type.
$data = array(
'public_key' => $this->funcaptcha_public_key,
'site' => $_SERVER["SERVER_NAME"],
'userip' => $_SERVER["REMOTE_ADDR"],
'userbrowser' => $_SERVER['HTTP_USER_AGENT'],
'api_type' => $this->funcaptcha_api_type,
'plugin_version' => $this->funcaptcha_plugin_version,
'security_level' => $this->funcaptcha_security_level,
'lightbox' => $this->funcaptcha_lightbox_mode,
'lightbox_button_id' => $this->funcaptcha_lightbox_button_id,
'lightbox_submit_js' => $this->funcaptcha_lightbox_submit_javascript,
'theme' => $this->funcaptcha_theme,
'args' => $args
);
//get session token.
$session = $this->doPostReturnObject('/fc/gt/', $data);
$this->session_token = $session->token;
$this->funcaptcha_challenge_url = $session->challenge_url;
if (!$this->funcaptcha_challenge_url)
{
$this->msgLog("ERROR", "Warning: Couldn't retrieve challenge url.");
}
else
{
$this->msgLog("DEBUG", "Challenge url: '$this->funcaptcha_challenge_url'");
}
if (!$this->session_token)
{
$this->msgLog("ERROR", "Warning: Couldn't retrieve session.");
}
else
{
$this->msgLog("DEBUG", "Session token: '$this->session_token'");
}
if ($this->session_token && $this->funcaptcha_challenge_url && $this->funcaptcha_host)
{
//return html to generate captcha.
$url = "https://";
$url.= $this->funcaptcha_host;
$url.= $this->funcaptcha_challenge_url;
$url.= "?cache=" . time();
$html = "<div id='FunCaptcha'></div><input type='hidden' id='FunCaptcha-Token' name='fc-token' value='" . $this->session_token . "'><script src='". $url ."' type='text/javascript' language='JavaScript'></script>".$session->noscript;
return array(
"html" => $html,
"server_html" => isset($session->server_html) ? $server_html : "",
"script_url" => $url,
"session_token" => $this->session_token,
"api_server" => "https://" . $this->funcaptcha_host,
"no_script" => $session->noscript
);
}
else
{
//if failed to connect, display helpful message.
$style = "padding: 10px; border: 1px solid #b1abb2; background: #f1f1f1; color: #000000;";
$message = "The CAPTCHA cannot be displayed. This may be a configuration or server problem. You may not be able to continue. Please visit our <a href='http://funcaptcha.co/status' target='_blank'>status page</a> for more information or to contact us.";
$html = "<p style=\"$style\">$message</p>\n";
return array(
"html" => $html,
"api_server" => "https://" . $this->funcaptcha_host,
);
}
}
/**
* Set security level of FunCaptcha
*
* Possible options are:
* 0 - Automatic-- security rises for suspicious users
* 20 - Enhanced security-- always use Enhanced security
*
* See our website for more details on these options
*
* @param int $security - Security level
* @return boolean
*/
public function setSecurityLevel($security) {
$this->funcaptcha_security_level = $security;
$this->msgLog("DEBUG", "Security Level: '$this->funcaptcha_security_level'");
}
/**
* Set theme of FunCaptcha
*
* See here for options: https://www.funcaptcha.co/themes/
*
* @param int $theme - theme option, 0 is default.
* @return boolean
*/
public function setTheme($theme) {
$this->funcaptcha_theme = $theme;
$this->msgLog("DEBUG", "Theme: '$this->funcaptcha_theme'");
}
/**
* Set proxy for FunCaptcha
*
* @param int $proxy - Proxy server (including port, eg: 111.11.11.111:8080)
* @return boolean
*/
public function setProxy($proxy) {
$this->funcaptcha_proxy = $proxy;
$this->msgLog("DEBUG", "Proxy: '$this->funcaptcha_proxy'");
}
/**
* Set lightbox mode of FunCaptcha
*
*
* See our website for more details on these options
*
* @param boolean $enable - Enable lightbox mode.
* @param boolean $submit_button_id - ID of button to be used to display lightbox.
* @param boolean $submit_javascript_function_name - Name of javascript function to call on lightbox FunCaptcha completion.
* @return boolean
*/
public function setLightboxMode($enable, $submit_button_id=null, $submit_javascript_function_name=null) {
$this->funcaptcha_lightbox_mode = $enable;
$this->funcaptcha_lightbox_button_id = $submit_button_id;
$this->funcaptcha_lightbox_submit_javascript = $submit_javascript_function_name;
$this->msgLog("DEBUG", "Lightbox mode: '$this->funcaptcha_lightbox_mode'");
$this->msgLog("DEBUG", "Lightbox Button ID: '$this->funcaptcha_lightbox_button_id'");
$this->msgLog("DEBUG", "Lightbox JS Name: '$this->funcaptcha_lightbox_submit_javascript'");
}
/**
* Verify if user has solved the FunCaptcha
*
* @param string $private_key - FunCaptcha private key
* @param array $args - Additional information to pass to FunCaptcha servers
* @return boolean
*/
public function checkResult($private_key, $args=null) {
$this->funcaptcha_private_key = $private_key;
$this->msgLog("DEBUG", ("Session token to check: " . $_POST['fc-token']));
if ($this->funcaptcha_private_key == "")
{
$this->msgLog("ERROR", "Warning: Private key is not set.");
}
else
{
$this->msgLog("DEBUG", "Private key: '$this->funcaptcha_private_key'");
}
if ($_POST['fc-token']) {
$data = array(
'private_key' => $this->funcaptcha_private_key,
'session_token' => $_POST['fc-token'],
'fc_rc_challenge' => $_POST['fc_rc_challenge'],
'args' => $args
);
$result = $this->doPostReturnObject('/fc/v/', $data);
}
else
{
$this->msgLog("ERROR", "Unable check the result. Please check that you passed in the correct public, private keys.");
}
return $result->solved;
}
/**
* Internal function - does HTTPs post and returns result.
*
* @param string $url_path - server path
* @param array $data - data to send
* @return object
*/
protected function doPostReturnObject($url_path, $data) {
$result = "";
$fields_string = "";
$data_string = "";
foreach($data as $key=>$value) {
if (is_array($value)) {
if ( ! empty($value)) {
foreach ($value as $k => $v) {
$data_string .= $key . '['. $k .']=' . $v . '&';
}
} else {
$data_string .= $key . '=&';
}
} else {
$data_string .= $key.'='.$value.'&';
}
}
rtrim($data_string,'&');
$curl_url = "https://";
$curl_url.= $this->funcaptcha_host;
$curl_url.= $url_path;
// Log it.
$this->msgLog("DEBUG", "cURl: url='$curl_url', data='$data_string'");
if (function_exists('curl_init') and function_exists('curl_exec')) {
if ($ch = curl_init($curl_url))
{
// Set the cURL options.
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
if (isset($this->funcaptcha_proxy)) {
curl_setopt($ch,CURLOPT_PROXY, $this->funcaptcha_proxy);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute the cURL request.
$result = curl_exec($ch);
curl_close($ch);
}
else
{
// Log it.
$this->msgLog("DEBUG", "Unable to enable cURL: url='$curl_url'");
}
} else {
// Log it.
// Build a header
$http_request = "POST $url_path HTTP/1.1\r\n";
$http_request .= "Host: $this->funcaptcha_host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($data_string) . "\r\n";
$http_request .= "User-Agent: FunCaptcha/PHP " . $this->funcaptcha_plugin_version . "\r\n";
$http_request .= "Connection: Close\r\n";
$http_request .= "\r\n";
$http_request .= $data_string ."\r\n";
$result = '';
$errno = $errstr = "";
$fs = fsockopen("ssl://" . $this->funcaptcha_host, 443, $errno, $errstr, 10);
if( false == $fs ) {
$this->msgLog("ERROR", "Could not open socket");
} else {
fwrite($fs, $http_request);
while (!feof($fs)) {
$result .= fgets($fs, 4096);
}
$result = explode("\r\n\r\n", $result, 2);
$result = $result[1];
}
}
$result = $this->JSONDecode($result);
return $result;
}
/**
* Internal function - does does JSON decoding of data from server.
*
* @param string $string - json to decode
* @return object
*/
protected function JSONDecode($string) {
$result = array();
if (function_exists("json_decode")) {
try {
$result = json_decode( $string);
} catch (Exception $e) {
$this->msgLog("ERROR", "Exception when calling json_decode: " . $e->getMessage());
$result = null;
}
} else if (file_Exists($this->funcaptcha_json_path)) {
require_once($this->funcaptcha_json_path);
$json = new Services_JSON();
$result = $json->decode($string);
} else {
$this->msgLog("ERROR", "No JSON decode function available.");
}
return $result;
}
/**
* Log a message
*
* @param string $type - type of error
* @param string $message - message to log
* @return null
*/
protected function msgLog($type, $message)
{
// Is it an error message?
if (FALSE !== stripos($type, "error"))
{
error_log($message);
}
// Build the full message.
$debug_message = "<p style='padding: 10px; border: 1px solid #2389d1; background: #43c0ff; color: #134276;'><strong>$type:</strong> $message</p>\n";
// Output to screen if in debug mode
if ($this->funcaptcha_debug)
{
echo "$debug_message";
}
}
/**
* Debug mode, enables showing output of errors.
*
* @param boolean $mode debug state
*/
public function debugMode($mode)
{
$this->funcaptcha_debug = $mode;
}
}
endif;