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 'coindata.php';
9 require_once 'recaptchalib.php';
10 require_once 'validator.php';
11 require_once 'Coinbase/Coinbase.php';
12 require_once 'jsonRPCClient.php';
13 require_once 'Slim/Slim.php';
15 define("COIN_NAME", getCoinName($coinType));
16 define("UP_COIN_NAME", ucfirst(COIN_NAME));
17 define("SUB_UNIT", getSubunitDivider($coinType));
18 define("SUB_UNIT_NAME", getSubunitName($coinType));
20 function urlFor($name, $params = array(), $appName = 'default')
22 return \Slim\Slim::getInstance($appName)->urlFor($name, $params);
25 function getAd($arr, $location = null)
27 if (!is_null($location)) {
28 $div_pre = sprintf("<div id=\"%s\">", $location);
29 $div_post = "</div>\n";
34 return $div_pre.(!empty($arr) ? $arr[rand(0, count($arr)-1)] : "banner here").$div_post;
37 function getAdDivId($location)
42 function relative_time($date)
44 $diff = time() - $date;
45 $poststr = $diff > 0 ? " ago" : "";
48 return $adiff . " second" . plural($adiff) . $poststr;
50 if ($adiff<3600) { // 60*60
51 return round($adiff/60) . " minute" . plural($adiff) . $poststr;
53 if ($adiff<86400) { // 24*60*60
54 return round($adiff/3600) . " hour" . plural($adiff) . $poststr;
56 if ($adiff<604800) { // 7*24*60*60
57 return round($adiff/86400) . " day" . plural($adiff) . $poststr;
59 if ($adiff<2419200) { // 4*7*24*60*60
60 return $adiff . " week" . plural($adiff) . $poststr;
62 return "on " . date("F j, Y", strtotime($date));
67 return ($a > 1 ? "s" : "");
70 function checkaddress($address)
72 global $allowEmail, $allowCoin, $coinType;
73 if ($allowCoin && determineValidity($address, $coinType)) {
76 if ($allowEmail && (filter_var($address, FILTER_VALIDATE_EMAIL) !== false)) {
84 if (getenv("HTTP_CLIENT_IP")) {
85 $ip = getenv("HTTP_CLIENT_IP");
86 } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
87 $ip = getenv("HTTP_X_FORWARDED_FOR");
88 } elseif (getenv("REMOTE_ADDR")) {
89 $ip = getenv("REMOTE_ADDR");
96 function getserverbalance($force = false)
98 global $apiKey, $guid, $rpchost;
100 // we store the server balance in sql with a spec address called 'SERVERBALANCE'
101 $balance_sql = "SELECT balance FROM balances WHERE email='SERVERBALANCE' ";
102 $balance_sql .= "AND totalbalance > ".(time() - 1800).";";
103 $balance_query = sql_query($balance_sql);
104 if ($balance_query->num_rows) {
105 $balance = fetch_one($balance_query);
110 if (!empty($apiKey)) {
111 $balance = getCoinbaseBalance();
112 } elseif (!empty($guid)) {
113 $balance = getBlockchainBalance();
114 } elseif (!empty($rpchost)) {
115 $balance = getBitcoindBalance();
120 $insert_sql = "INSERT INTO balances(balance, totalbalance, email, referredby) ";
121 $insert_sql .= "VALUES($balance, '$date', 'SERVERBALANCE', 0) ON DUPLICATE KEY ";
122 $insert_sql .= "UPDATE balance = $balance, totalbalance = '$date';";
123 sql_query($insert_sql);
125 } catch (Exception $e) {
130 function getCoinbaseBalance()
133 $coinbase = new Coinbase($apiKey);
134 return $coinbase->getBalance() * SUB_UNIT;
137 function getBlockchainBalance()
139 global $guid, $firstpassword;
140 $url = "https://blockchain.info/merchant/$guid/balance?password=$firstpassword";
141 return json_decode(file_get_contents($url))->balance;
144 function getBitcoindBalance()
146 global $rpchost, $rpcssl, $rpcport, $rpcuser, $rpcpassword;
147 $bitcoin = new jsonRPCClient(sprintf('http%s://%s:%s@%s:%d/', $rpcssl ? "s" : "", $rpcuser, $rpcpassword, $rpchost, $rpcport));
148 return $bitcoin->getbalance() * SUB_UNIT;
151 class NoCashException extends Exception
155 function sendMoney($address, $balance)
157 global $apiKey, $guid, $rpchost;
158 if (!empty($apiKey)) {
159 return sendCoinbaseMoney($address, $balance);
160 } elseif (!empty($guid)) {
161 return sendBlockchainMoney($address, $balance);
162 } elseif (!empty($rpchost)) {
163 return sendBitcoindMoney($address, $balance);
165 throw new Exception("The site doesnt set wallet provider");
169 function sendCoinbaseMoney($address, $balance)
171 global $apiKey, $cashoutMessage, $fee;
172 $balance = $balance / SUB_UNIT;
174 $coinbase = new Coinbase($apiKey);
175 $response = $coinbase->sendMoney($address, sprintf("%.8f", $balance), $cashoutMessage, $fee > 0 ? ($fee / SUB_UNIT) : null);
176 } catch (Exception $e) {
177 $response = $e->getMessage();
178 if (strpos($response, "You don't have that much") !== false) {
179 throw new NoCashException($response, 0, $e);
181 throw new Exception($response, 0, $e);
187 function sendBlockchainMoney($address, $balance)
189 global $guid, $firstpassword, $secondpassword, $cashoutMessage, $fee;
191 $url = "https://blockchain.info/merchant/$guid/payment?";
192 $url .= "password=$firstpassword&second_password=$secondpassword&to=$address&amount=$balance&";
196 $url .= "note=" . urlencode($cashoutMessage);
197 $response = json_decode(file_get_contents($url));
198 if (isset($response->error)) {
199 if ($response->error == 'No free outputs to spend') {
200 throw new NoCashException();
202 throw new Exception($response->error);
208 function sendBitcoindMoney($address, $balance)
210 global $rpchost, $rpcssl, $rpcport, $rpcuser, $rpcpassword, $cashoutMessage, $fee;
211 $balance = $balance / SUB_UNIT;
213 $bitcoin = new jsonRPCClient(sprintf('http%s://%s:%s@%s:%d/', $rpcssl ? "s" : "", $rpcuser, $rpcpassword, $rpchost, $rpcport));
215 $bitcoin->settxfee(round($fee / SUB_UNIT, 8));
217 $response = $bitcoin->sendtoaddress($address, $balance, $cashoutMessage);
218 } catch (Exception $e) {
219 $response = $e->getMessage();
220 if (strpos($response, "Insufficient funds") !== false) {
221 throw new NoCashException($response, 0, $e);
223 throw new Exception($response, 0, $e);
229 function sql_query($sql)
232 return $mysqli->query($sql);
235 function fetch_row($query)
237 return $query->fetch_row();
240 function fetch_assoc($query)
242 return $query->fetch_assoc();
245 function fetch_all($query, $resulttype = MYSQLI_NUM)
247 if (method_exists($query, 'fetch_all')) { # Compatibility layer with PHP < 5.3
248 $res = $query->fetch_all($resulttype);
250 for ($res = array(); $tmp = $query->fetch_array($resulttype);) {
257 function fetch_one($query)
259 $row = $query->fetch_row();