3 if (version_compare(PHP_VERSION, '5.3.0') < 0) {
4 exit("Sorry, this version of MiniFaucet will only run on PHP version 5.3 or greater!\n");
7 require_once 'config.php';
8 require_once 'recaptchalib.php';
9 require_once 'validator.php';
10 require_once 'Coinbase/Coinbase.php';
11 require_once 'jsonRPCClient.php';
12 require_once 'Slim/Slim.php';
14 define("SUB_UNIT", 100 * 1000 * 1000);
16 function urlFor($name, $params = array(), $appName = 'default')
18 return \Slim\Slim::getInstance($appName)->urlFor($name, $params);
23 return !empty($arr) ? $arr[rand(0, count($arr)-1)] : "banner here\n";
26 function relative_time($date)
28 $diff = time() - $date;
29 $poststr = $diff > 0 ? " ago" : "";
32 return $adiff . " second" . plural($adiff) . $poststr;
34 if ($adiff<3600) { // 60*60
35 return round($adiff/60) . " minute" . plural($adiff) . $poststr;
37 if ($adiff<86400) { // 24*60*60
38 return round($adiff/3600) . " hour" . plural($adiff) . $poststr;
40 if ($adiff<604800) { // 7*24*60*60
41 return round($adiff/86400) . " day" . plural($adiff) . $poststr;
43 if ($adiff<2419200) { // 4*7*24*60*60
44 return $adiff . " week" . plural($adiff) . $poststr;
46 return "on " . date("F j, Y", strtotime($date));
51 return ($a > 1 ? "s" : "");
54 function checkaddress($address)
56 global $allowEmail, $allowBTC;
57 if ($allowBTC && determineValidity($address, "BTC")) {
60 if ($allowEmail && (filter_var($address, FILTER_VALIDATE_EMAIL) !== false)) {
68 if (getenv("HTTP_CLIENT_IP")) {
69 $ip = getenv("HTTP_CLIENT_IP");
70 } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
71 $ip = getenv("HTTP_X_FORWARDED_FOR");
72 } elseif (getenv("REMOTE_ADDR")) {
73 $ip = getenv("REMOTE_ADDR");
80 function getserverbalance()
82 global $apiKey, $guid, $rpchost;
83 // we store the server balance in sql with a spec address called 'SERVERBALANCE'
84 $balance_sql = "SELECT balance FROM balances WHERE email='SERVERBALANCE_' ";
85 $balance_sql .= "AND totalbalance > ".(time() - 1800).";";
86 $balance_query = sql_query($balance_sql);
87 if ($balance_query->num_rows) {
88 $balance = fetch_one($balance_query);
92 if (!empty($apiKey)) {
93 $balance = getCoinbaseBalance();
94 } elseif (!empty($guid)) {
95 $balance = getBlockchainBalance();
96 } elseif (!empty($rpchost)) {
97 $balance = getBitcoindBalance();
102 $insert_sql = "INSERT INTO balances(balance, totalbalance, email, referredby) ";
103 $insert_sql .= "VALUES($balance, '$date', 'SERVERBALANCE', 0) ON DUPLICATE KEY ";
104 $insert_sql .= "UPDATE balance = $balance, totalbalance = '$date';";
105 sql_query($insert_sql);
107 } catch (Exception $e) {
112 function getCoinbaseBalance()
115 $coinbase = new Coinbase($apiKey);
116 return $coinbase->getBalance() * SUB_UNIT;
119 function getBlockchainBalance()
121 global $guid, $firstpassword;
122 $url = "https://blockchain.info/merchant/$guid/balance?password=$firstpassword";
123 return json_decode(file_get_contents($url))->balance;
126 function getBitcoindBalance()
128 global $rpchost, $rpcssl, $rpcport, $rpcuser, $rpcpassword;
129 $bitcoin = new jsonRPCClient(sprintf('http%s://%s:%s@%s:%d/', $rpcssl ? "s" : "", $rpcuser, $rpcpassword, $rpchost, $rpcport));
130 return $bitcoin->getbalance() * SUB_UNIT;
133 class NoCashException extends Exception
137 function sendMoney($address, $balance)
139 global $apiKey, $guid, $rpchost;
140 if (!empty($apiKey)) {
141 return sendCoinbaseMoney($address, $balance);
142 } elseif (!empty($guid)) {
143 return sendBlockchainMoney($address, $balance);
144 } elseif (!empty($rpchost)) {
145 return sendBitcoindMoney($address, $balance);
147 throw new Exception("The site doesnt set wallet provider");
151 function sendCoinbaseMoney($address, $balance)
153 global $apiKey, $cashoutMessage, $fee;
154 $balance = $balance / SUB_UNIT;
156 $coinbase = new Coinbase($apiKey);
157 $response = $coinbase->sendMoney($address, sprintf("%.8f", $balance), $cashoutMessage, $fee > 0 ? ($fee / SUB_UNIT) : null);
158 } catch (Exception $e) {
159 $response = $e->getMessage();
160 if (strpos($response, "You don't have that much") !== false) {
161 throw new NoCashException($response, 0, $e);
163 throw new Exception($response, 0, $e);
169 function sendBlockchainMoney($address, $balance)
171 global $guid, $firstpassword, $secondpassword, $cashoutMessage, $fee;
173 $url = "https://blockchain.info/merchant/$guid/payment?";
174 $url .= "password=$firstpassword&second_password=$secondpassword&to=$address&amount=$balance&";
178 $url .= "note=" . urlencode($cashoutMessage);
179 $response = json_decode(file_get_contents($url));
180 if (isset($response->error)) {
181 if ($response->error == 'No free outputs to spend') {
182 throw new NoCashException();
184 throw new Exception($response->error);
190 function sendBitcoindMoney($address, $balance)
192 global $rpchost, $rpcssl, $rpcport, $rpcuser, $rpcpassword, $cashoutMessage, $fee;
193 $balance = $balance / SUB_UNIT;
195 $bitcoin = new jsonRPCClient(sprintf('http%s://%s:%s@%s:%d/', $rpcssl ? "s" : "", $rpcuser, $rpcpassword, $rpchost, $rpcport));
197 $bitcoin->settxfee(round($fee / SUB_UNIT, 8));
199 $response = $bitcoin->sendtoaddress($address, $balance, $cashoutMessage);
200 } catch (Exception $e) {
201 $response = $e->getMessage();
202 if (strpos($response, "Insufficient funds") !== false) {
203 throw new NoCashException($response, 0, $e);
205 throw new Exception($response, 0, $e);
211 function sql_query($sql)
214 return $mysqli->query($sql);
217 function fetch_row($query)
219 return $query->fetch_row();
222 function fetch_all($query, $resulttype = MYSQLI_NUM)
224 if (method_exists($query, 'fetch_all')) { # Compatibility layer with PHP < 5.3
225 $res = $query->fetch_all($resulttype);
227 for ($res = array(); $tmp = $query->fetch_array($resulttype);) {
234 function fetch_one($query)
236 $row = $query->fetch_row();