-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfaucethub.php
More file actions
172 lines (144 loc) · 5.59 KB
/
faucethub.php
File metadata and controls
172 lines (144 loc) · 5.59 KB
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
<?php
$faucethub_lib_version = "v1.0.1";
/*
FaucetHub PHP Library
Changelog
v1.0.1
Added currency parameter to checkaddress() function
v1.0
Bumped version to first full release
Added checkaddress() function
b0.02
Implemented a version of the suggestions mentioned here https://bitcointalk.org/index.php?topic=1692263.msg16985624#msg16985624
- Timeout support
- Error handling for timeouts and invalid responses
- Tweaks to the referral payout flag
- Removed silent retry on curl failure
b0.01
First version named
Credits to FaucetBOX.com for creating the original version this library is based on
*/
// Dummy class to allow drop-in compatibility with faucet-box library
class FaucetHub
{
protected $api_key;
protected $currency;
protected $timeout;
public $last_status = null;
protected $api_base = "https://faucethub.io/api/v1/";
public function __construct($api_key, $currency = "BTC", $disable_curl = false, $verify_peer = true, $timeout = null) {
$this->api_key = $api_key;
$this->currency = $currency;
$this->disable_curl = $disable_curl;
$this->verify_peer = $verify_peer;
$this->curl_warning = false;
$this->setTimeout($timeout);
}
public function setTimeout($timeout) {
if($timeout === null) {
$socket_timeout = ini_get('default_socket_timeout');
$script_timeout = ini_get('max_execution_time');
$timeout = min($script_timeout / 2, $socket_timeout);
}
$this->timeout = $timeout;
}
public function __execPHP($method, $params = array()) {
$params = array_merge($params, array("api_key" => $this->api_key, "currency" => $this->currency));
$opts = array(
"http" => array(
"method" => "POST",
"header" => "Content-type: application/x-www-form-urlencoded\r\n",
"content" => http_build_query($params),
"timeout" => $this->timeout,
),
"ssl" => array(
"verify_peer" => $this->verify_peer
)
);
$ctx = stream_context_create($opts);
$fp = fopen($this->api_base . $method, 'rb', null, $ctx);
if (!$fp) {
return json_encode(array(
'status' => 503,
'message' => 'Connection to FaucetHub failed, please try again later',
), TRUE);
}
$response = stream_get_contents($fp);
if($response && !$this->disable_curl) {
$this->curl_warning = true;
}
fclose($fp);
return $response;
}
public function __exec($method, $params = array()) {
$this->last_status = null;
if($this->disable_curl) {
$response = $this->__execPHP($method, $params);
} else {
$response = $this->__execCURL($method, $params);
}
$response = json_decode($response, true);
if($response) {
$this->last_status = $response['status'];
} else {
$this->last_status = null;
$response = array(
'status' => 502,
'message' => 'Invalid response',
);
}
return $response;
}
public function __execCURL($method, $params = array()) {
$params = array_merge($params, array("api_key" => $this->api_key, "currency" => $this->currency));
$ch = curl_init($this->api_base . $method);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_peer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, (int)$this->timeout);
$response = curl_exec($ch);
if(!$response) {
//$response = $this->__execPHP($method, $params); // disabled the exec fallback when using curl
return json_encode(array(
'status' => 504,
'message' => 'Connection error',
), TRUE);
}
curl_close($ch);
return $response;
}
public function send($to, $amount, $referral = false, $ip_address = "", $metadata = array()) {
$referral = ($referral === true) ? 'true' : 'false';
$r = $this->__exec("send", array("to" => $to, "amount" => $amount, "referral" => $referral, "ip_address" => $ip_address, "metadata" => json_encode($metadata, true)));
if (array_key_exists("status", $r)) {
return $r; // simply return the raw response, we'll need this in the ProcessPayout function.
}
return array(
'success' => false,
'message' => 'Unknown error.',
'html' => '<div class="alert alert-danger">Unknown error.</div>',
'response' => json_encode($r)
);
}
public function sendReferralEarnings($to, $amount, $ip_address = "") {
return $this->send($to, $amount, true, $ip_address);
}
public function getPayouts($count) {
$r = $this->__exec("payouts", array("count" => $count) );
return $r;
}
public function getCurrencies() {
$r = $this->__exec("currencies");
return $r;
}
public function getBalance() {
$r = $this->__exec("balance");
return $r;
}
public function checkAddress($address, $currency = "BTC") {
$r = $this->__exec("checkaddress", array('address' => $address, 'currency' => $currency));
return $r;
}
}
?>