forked from earth-faucet/faucet
/
core.php
244 lines (221 loc) · 7.06 KB
/
core.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
<?php
if (version_compare(PHP_VERSION, '5.3.0') < 0) {
exit("Sorry, this version of MiniFaucet will only run on PHP version 5.3 or greater!\n");
}
require_once 'config.php';
require_once 'coindata.php';
require_once 'recaptchalib.php';
require_once 'validator.php';
require_once 'Coinbase/Coinbase.php';
require_once 'jsonRPCClient.php';
require_once 'Slim/Slim.php';
define("COIN_NAME", getCoinName($coinType));
define("UP_COIN_NAME", ucfirst(COIN_NAME));
define("SUB_UNIT", getSubunitDivider($coinType));
define("SUB_UNIT_NAME", getSubunitName($coinType));
function urlFor($name, $params = array(), $appName = 'default')
{
return \Slim\Slim::getInstance($appName)->urlFor($name, $params);
}
function getAd($arr)
{
return !empty($arr) ? $arr[rand(0, count($arr)-1)] : "banner here\n";
}
function relative_time($date)
{
$diff = time() - $date;
$poststr = $diff > 0 ? " ago" : "";
$adiff = abs($diff);
if ($adiff<60) {
return $adiff . " second" . plural($adiff) . $poststr;
}
if ($adiff<3600) { // 60*60
return round($adiff/60) . " minute" . plural($adiff) . $poststr;
}
if ($adiff<86400) { // 24*60*60
return round($adiff/3600) . " hour" . plural($adiff) . $poststr;
}
if ($adiff<604800) { // 7*24*60*60
return round($adiff/86400) . " day" . plural($adiff) . $poststr;
}
if ($adiff<2419200) { // 4*7*24*60*60
return $adiff . " week" . plural($adiff) . $poststr;
}
return "on " . date("F j, Y", strtotime($date));
}
function plural($a)
{
return ($a > 1 ? "s" : "");
}
function checkaddress($address)
{
global $allowEmail, $allowCoin, $coinType;
if ($allowCoin && determineValidity($address, $coinType)) {
return true;
}
if ($allowEmail && (filter_var($address, FILTER_VALIDATE_EMAIL) !== false)) {
return true;
}
return false;
}
function getIP()
{
if (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} elseif (getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} elseif (getenv("REMOTE_ADDR")) {
$ip = getenv("REMOTE_ADDR");
} else {
$ip = "UNKNOWN";
}
return $ip;
}
function getserverbalance($force = false)
{
global $apiKey, $guid, $rpchost;
if (!$force) {
// we store the server balance in sql with a spec address called 'SERVERBALANCE'
$balance_sql = "SELECT balance FROM balances WHERE email='SERVERBALANCE' ";
$balance_sql .= "AND totalbalance > ".(time() - 1800).";";
$balance_query = sql_query($balance_sql);
if ($balance_query->num_rows) {
$balance = fetch_one($balance_query);
return $balance;
}
}
try {
if (!empty($apiKey)) {
$balance = getCoinbaseBalance();
} elseif (!empty($guid)) {
$balance = getBlockchainBalance();
} elseif (!empty($rpchost)) {
$balance = getBitcoindBalance();
} else {
$balance = -1;
}
$date = time();
$insert_sql = "INSERT INTO balances(balance, totalbalance, email, referredby) ";
$insert_sql .= "VALUES($balance, '$date', 'SERVERBALANCE', 0) ON DUPLICATE KEY ";
$insert_sql .= "UPDATE balance = $balance, totalbalance = '$date';";
sql_query($insert_sql);
return $balance;
} catch (Exception $e) {
return 0;
}
}
function getCoinbaseBalance()
{
global $apiKey;
$coinbase = new Coinbase($apiKey);
return $coinbase->getBalance() * SUB_UNIT;
}
function getBlockchainBalance()
{
global $guid, $firstpassword;
$url = "https://blockchain.info/merchant/$guid/balance?password=$firstpassword";
return json_decode(file_get_contents($url))->balance;
}
function getBitcoindBalance()
{
global $rpchost, $rpcssl, $rpcport, $rpcuser, $rpcpassword;
$bitcoin = new jsonRPCClient(sprintf('http%s://%s:%s@%s:%d/', $rpcssl ? "s" : "", $rpcuser, $rpcpassword, $rpchost, $rpcport));
return $bitcoin->getbalance() * SUB_UNIT;
}
class NoCashException extends Exception
{
}
function sendMoney($address, $balance)
{
global $apiKey, $guid, $rpchost;
if (!empty($apiKey)) {
return sendCoinbaseMoney($address, $balance);
} elseif (!empty($guid)) {
return sendBlockchainMoney($address, $balance);
} elseif (!empty($rpchost)) {
return sendBitcoindMoney($address, $balance);
} else {
throw new Exception("The site doesnt set wallet provider");
}
}
function sendCoinbaseMoney($address, $balance)
{
global $apiKey, $cashoutMessage, $fee;
$balance = $balance / SUB_UNIT;
try {
$coinbase = new Coinbase($apiKey);
$response = $coinbase->sendMoney($address, sprintf("%.8f", $balance), $cashoutMessage, $fee > 0 ? ($fee / SUB_UNIT) : null);
} catch (Exception $e) {
$response = $e->getMessage();
if (strpos($response, "You don't have that much") !== false) {
throw new NoCashException($response, 0, $e);
} else {
throw new Exception($response, 0, $e);
}
}
return $response;
}
function sendBlockchainMoney($address, $balance)
{
global $guid, $firstpassword, $secondpassword, $cashoutMessage, $fee;
$url = "https://blockchain.info/merchant/$guid/payment?";
$url .= "password=$firstpassword&second_password=$secondpassword&to=$address&amount=$balance&";
if ($fee >= 50000) {
$url .= "fee=$fee";
}
$url .= "note=" . urlencode($cashoutMessage);
$response = json_decode(file_get_contents($url));
if (isset($response->error)) {
if ($response->error == 'No free outputs to spend') {
throw new NoCashException();
} else {
throw new Exception($response->error);
}
}
return $response;
}
function sendBitcoindMoney($address, $balance)
{
global $rpchost, $rpcssl, $rpcport, $rpcuser, $rpcpassword, $cashoutMessage, $fee;
$balance = $balance / SUB_UNIT;
try {
$bitcoin = new jsonRPCClient(sprintf('http%s://%s:%s@%s:%d/', $rpcssl ? "s" : "", $rpcuser, $rpcpassword, $rpchost, $rpcport));
if ($fee > 0) {
$bitcoin->settxfee(round($fee / SUB_UNIT, 8));
}
$response = $bitcoin->sendtoaddress($address, $balance, $cashoutMessage);
} catch (Exception $e) {
$response = $e->getMessage();
if (strpos($response, "Insufficient funds") !== false) {
throw new NoCashException($response, 0, $e);
} else {
throw new Exception($response, 0, $e);
}
}
return $response;
}
function sql_query($sql)
{
global $mysqli;
return $mysqli->query($sql);
}
function fetch_row($query)
{
return $query->fetch_row();
}
function fetch_all($query, $resulttype = MYSQLI_NUM)
{
if (method_exists($query, 'fetch_all')) { # Compatibility layer with PHP < 5.3
$res = $query->fetch_all($resulttype);
} else {
for ($res = array(); $tmp = $query->fetch_array($resulttype);) {
$res[] = $tmp;
}
}
return $res;
}
function fetch_one($query)
{
$row = $query->fetch_row();
return $row[0];
}