Put banners to a div
[elbandi:minifaucet.git] / core.php
1 <?php
2
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");
5 }
6
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';
14
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));
19
20 function urlFor($name, $params = array(), $appName = 'default')
21 {
22     return \Slim\Slim::getInstance($appName)->urlFor($name, $params);
23 }
24
25 function getAd($arr, $location = null)
26 {
27     if (!is_null($location)) {
28         $div_pre = sprintf("<div id=\"%s\">", $location);
29         $div_post = "</div>\n";
30     } else {
31         $div_pre = "";
32         $div_post = "\n";
33     }
34     return $div_pre.(!empty($arr) ? $arr[rand(0, count($arr)-1)] : "banner here").$div_post;
35 }
36
37 function getAdDivId($location)
38 {
39     return $location;
40 }
41
42 function relative_time($date)
43 {
44     $diff = time() - $date;
45     $poststr = $diff > 0 ? " ago" : "";
46     $adiff = abs($diff);
47     if ($adiff<60) {
48         return $adiff . " second" . plural($adiff) . $poststr;
49     }
50     if ($adiff<3600) { // 60*60
51             return round($adiff/60) . " minute" . plural($adiff) . $poststr;
52     }
53     if ($adiff<86400) { // 24*60*60
54             return round($adiff/3600) . " hour" . plural($adiff) . $poststr;
55     }
56     if ($adiff<604800) { // 7*24*60*60
57             return round($adiff/86400) . " day" . plural($adiff) . $poststr;
58     }
59     if ($adiff<2419200) { // 4*7*24*60*60
60             return $adiff . " week" . plural($adiff) . $poststr;
61     }
62     return "on " . date("F j, Y", strtotime($date));
63 }
64
65 function plural($a)
66 {
67         return ($a > 1 ? "s" : "");
68 }
69
70 function checkaddress($address)
71 {
72     global $allowEmail, $allowCoin, $coinType;
73     if ($allowCoin && determineValidity($address, $coinType)) {
74         return true;
75     }
76     if ($allowEmail && (filter_var($address, FILTER_VALIDATE_EMAIL) !== false)) {
77         return true;
78     }
79     return false;
80 }
81
82 function getIP()
83 {
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");
90     } else {
91         $ip = "UNKNOWN";
92     }
93     return $ip;
94 }
95
96 function getserverbalance($force = false)
97 {
98     global $apiKey, $guid, $rpchost;
99     if (!$force) {
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);
106             return $balance;
107         }
108     }
109     try {
110         if (!empty($apiKey)) {
111             $balance = getCoinbaseBalance();
112         } elseif (!empty($guid)) {
113             $balance = getBlockchainBalance();
114         } elseif (!empty($rpchost)) {
115             $balance = getBitcoindBalance();
116         } else {
117             $balance = -1;
118         }
119         $date = time();
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);
124         return $balance;
125     } catch (Exception $e) {
126         return 0;
127     }
128 }
129
130 function getCoinbaseBalance()
131 {
132     global $apiKey;
133     $coinbase = new Coinbase($apiKey);
134     return $coinbase->getBalance() * SUB_UNIT;
135 }
136
137 function getBlockchainBalance()
138 {
139     global $guid, $firstpassword;
140     $url = "https://blockchain.info/merchant/$guid/balance?password=$firstpassword";
141     return json_decode(file_get_contents($url))->balance;
142 }
143
144 function getBitcoindBalance()
145 {
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;
149 }
150
151 class NoCashException extends Exception
152 {
153 }
154
155 function sendMoney($address, $balance)
156 {
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);
164     } else {
165         throw new Exception("The site doesnt set wallet provider");
166     }
167 }
168
169 function sendCoinbaseMoney($address, $balance)
170 {
171     global $apiKey, $cashoutMessage, $fee;
172     $balance = $balance / SUB_UNIT;
173     try {
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);
180         } else {
181             throw new Exception($response, 0, $e);
182         }
183     }
184     return $response;
185 }
186
187 function sendBlockchainMoney($address, $balance)
188 {
189     global $guid, $firstpassword, $secondpassword, $cashoutMessage, $fee;
190
191     $url = "https://blockchain.info/merchant/$guid/payment?";
192     $url .= "password=$firstpassword&second_password=$secondpassword&to=$address&amount=$balance&";
193     if ($fee >= 50000) {
194         $url .= "fee=$fee";
195     }
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();
201         } else {
202             throw new Exception($response->error);
203         }
204     }
205     return $response;
206 }
207
208 function sendBitcoindMoney($address, $balance)
209 {
210     global $rpchost, $rpcssl, $rpcport, $rpcuser, $rpcpassword, $cashoutMessage, $fee;
211     $balance = $balance / SUB_UNIT;
212     try {
213         $bitcoin = new jsonRPCClient(sprintf('http%s://%s:%s@%s:%d/', $rpcssl ? "s" : "", $rpcuser, $rpcpassword, $rpchost, $rpcport));
214         if ($fee > 0) {
215             $bitcoin->settxfee(round($fee / SUB_UNIT, 8));
216         }
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);
222         } else {
223             throw new Exception($response, 0, $e);
224         }
225     }
226     return $response;
227 }
228
229 function sql_query($sql)
230 {
231     global $mysqli;
232     return $mysqli->query($sql);
233 }
234
235 function fetch_row($query)
236 {
237     return $query->fetch_row();
238 }
239
240 function fetch_assoc($query)
241 {
242     return $query->fetch_assoc();
243 }
244
245 function fetch_all($query, $resulttype = MYSQLI_NUM)
246 {
247     if (method_exists($query, 'fetch_all')) { # Compatibility layer with PHP < 5.3
248         $res = $query->fetch_all($resulttype);
249     } else {
250         for ($res = array(); $tmp = $query->fetch_array($resulttype);) {
251             $res[] = $tmp;
252         }
253     }
254     return $res;
255 }
256
257 function fetch_one($query)
258 {
259     $row = $query->fetch_row();
260     return $row[0];
261 }