-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsweetcaptcha.php
101 lines (77 loc) · 2.52 KB
/
sweetcaptcha.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
<?php
/*
* Define you SweetCaptcha credentials.
* Don't have any? Sign up at http://sweetcaptcha.com and get them by email
*/
define('SWEETCAPTCHA_APP_ID', 500000); // your application id (change me)
define('SWEETCAPTCHA_KEY', 'changeme'); // your application key (change me)
define('SWEETCAPTCHA_SECRET', 'changeme'); // your application secret (change me)
define('SWEETCAPTCHA_PUBLIC_URL', 'sweetcaptcha.php'); // public http url to this file
/////==== Do not change below here ===/////
/**
* Handles remote negotiation with Sweetcaptcha.com.
*
* @version 1.1.0
* @updated November 14, 2013
*/
$sweetcaptcha = new Sweetcaptcha(
SWEETCAPTCHA_APP_ID,
SWEETCAPTCHA_KEY,
SWEETCAPTCHA_SECRET,
SWEETCAPTCHA_PUBLIC_URL
);
if (isset($_POST['ajax']) and $method = $_POST['ajax']) {
echo $sweetcaptcha->$method(isset($_POST['params']) ? $_POST['params'] : array());
}
class Sweetcaptcha {
private $appid;
private $key;
private $secret;
private $path;
const API_URL = 'sweetcaptcha.com';
const API_PORT = 80;
function __construct($appid, $key, $secret, $path) {
$this->appid = $appid;
$this->key = $key;
$this->secret = $secret;
$this->path = $path;
}
private function api($method, $params) {
$basic = array(
'method' => $method,
'appid' => $this->appid,
'key' => $this->key,
'path' => $this->path,
'user_ip' => $_SERVER['REMOTE_ADDR'],
'platform' => 'php'
);
return $this->call(array_merge(isset($params[0]) ? $params[0] : $params, $basic));
}
private function call($params) {
$param_data = "";
foreach ($params as $param_name => $param_value) {
$param_data .= urlencode($param_name) .'='. urlencode($param_value) .'&';
}
if (!($fs = fsockopen(self::API_URL, self::API_PORT, $errno, $errstr, 10))) {
die ("Couldn't connect to server");
}
$req = "POST /api.php HTTP/1.0\r\n";
$req .= "Host: ".self::API_URL."\r\n";
$req .= "Content-Type: application/x-www-form-urlencoded\r\n";
$req .= "Referer: " . $_SERVER['HTTP_HOST']. "\r\n";
$req .= "Content-Length: " . strlen($param_data) . "\r\n\r\n";
$req .= $param_data;
$response = '';
fwrite($fs, $req);
while (!feof($fs)) {
$response .= fgets($fs, 1160);
}
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response[1];
}
public function __call($method, $params) {
return $this->api($method, $params);
}
}
?>