forked from craigwatson/bitcoind-status
/
functions.php
477 lines (413 loc) · 14.4 KB
/
functions.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* Dogecoin Status Page
*
* @category File
* @package DogecoinStatus
* @author Craig Watson <craig@cwatson.org>
* @Forked By Floppy69 <a.maaded@gmail.com>
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @link https://github.com/floppy69/dogecoind-status
*/
$curl_requests = 0;
$default_app_title = 'Dogecoin Node Status';
/**
* Connects to Dogecoin daemon and retrieves information, then writes to cache
*
* @param string $from_cache Whether to get the data from cache or not
*
* @return array
*/
function getData($from_cache = false)
{
global $config;
global $cache_message;
global $geo_cache;
global $curl_requests;
// If we're getting data from cache, do it
if (($from_cache === true) && (is_file($config['cache_file']))) {
$cache = json_decode(file_get_contents($config['cache_file']), true);
// Only proceed if the cache is an array - invalid otherwise
if (is_array($cache)) {
if ($cache['config_hash'] == md5(json_encode($config))) {
if (time() < $cache['cache_expiry']) {
return $cache;
}
} else {
$cache_message = 'Configuration has changed, cache has been refreshed.';
}
}
}
// Include EasyBitcoin library and set up connection
include_once './php/easybitcoin.php';
$dogecoin = new Dogecoin($config['rpc_user'], $config['rpc_pass'], $config['rpc_host'], $config['rpc_port']);
// Setup SSL if configured
if ($config['rpc_ssl'] === true) {
$dogecoin->setSSL($config['rpc_ssl_ca']);
}
// Get blockchain and network info
$data = $dogecoin->getblockchaininfo();
$net_info = $dogecoin->getnetworkinfo();
$data['connections'] = $net_info['connections'];
$data['subversion'] = $net_info['subversion'];
// Handle errors if they happened
if (!$data) {
$return_data['error'] = $dogecoin->error;
$return_data['status'] = $dogecoin->status;
$return_data['display_connection_chart'] = false;
$return_data['display_peer_chart'] = false;
writeToCache($return_data);
return $return_data;
}
// Get geolocation cache data
if ($config['cache_geo_data'] === true) {
$geo_cache = getGeoCache($config['geo_cache_file']);
$data['geo'] = $geo_cache;
}
// Get free disk space
if ($config['display_free_disk_space'] === true) {
$data['free_disk_space'] = getFreeDiskSpace($config['disk_space_mount_point']);
}
if ($config['display_ip'] === true) {
// Use dogecoind IP
if ($config['use_dogecoind_ip'] === true) {
$data['node_ip'] = $net_info['localaddresses'][0]['address'];
} else {
$data['node_ip'] = $_SERVER['SERVER_ADDR'];
}
}
// Create geo handle
if (($config['display_ip_location'] === true) || ($config['geolocate_peer_ip'] === true)) {
$geo_curl = curl_init();
} else {
$geo_curl = false;
}
// Add peer info
if ($config['display_peer_info'] === true) {
$data['peers'] = parsePeers($dogecoin->getpeerinfo(), $geo_curl);
}
// Node geolocation
if (($config['display_ip_location'] === true) && ($config['display_ip'] === true)) {
$data['ip_location'] = getGeolocation($data['node_ip'], $geo_curl);
}
// Dogecoin Daemon uptime
if (($config['display_dogecoind_uptime'] === true) && (strcmp(PHP_OS, "Linux") == 0)) {
$data['dogecoind_uptime'] = getProcessUptime($config['dogecoind_process_name']);
}
// Create handle
if ($config['display_max_height'] || $config['display_sochain_info']) {
$dogecoin_curly = curl_init();
}
// Get max height from chain.so
if ($config['display_max_height'] === true) {
if ($config['display_testnet'] === true) {
$exec_result = json_decode(curlRequest("https://sochain.com/api/v2/get_info/DOGETEST", $dogecoin_curly), true);
} else {
$exec_result = json_decode(curlRequest("https://sochain.com/api/v2/get_info/DOGE", $dogecoin_curly), true);
}
$data['max_height'] = $exec_result['data']['blocks'];
$data['node_height_percent'] = round(($data['blocks']/$data['max_height'])*100, 1);
}
// Get node info from chain.so
if ($config['display_sochain_info'] === true) {
$pricebtc = json_decode(curlRequest("https://chain.so/api/v2/get_price/DOGE/BTC", $dogecoin_curly), true);
$priceusd = json_decode(curlRequest("https://chain.so/api/v2/get_price/DOGE/USD", $dogecoin_curly), true);
$data['sochain_info']['sochain_USD'] = $priceusd['data']['prices'][0]["price"];
$data['sochain_info']['sochain_BTC'] = $pricebtc['data']['prices'][0]["price"];
}
// Close handles
if ($config['display_max_height'] || $config['display_sochain_info']) {
curl_close($dogecoin_curly);
}
if (($config['display_ip_location'] === true) || ($config['geolocate_peer_ip'] === true)) {
curl_close($geo_curl);
}
// Work out if we should display charts or not
$data['display_connection_chart'] = displayChart($config['display_chart'], $config['stats_file'], $config['stats_min_data_points']);
$data['display_peer_chart'] = displayChart($config['display_peer_chart'], $config['peercount_file'], $config['peercount_min_data_points']);
// Write geolocation cache
if ($config['cache_geo_data'] === true) {
file_put_contents($config['geo_cache_file'], json_encode($geo_cache), LOCK_EX);
}
writeToCache($data);
return $data;
}
/**
* Serializes an array and write to file
*
* @param array $data Data to write
*
* @return void
*/
function writeToCache($data)
{
global $config;
if ($config['use_cache'] === true) {
$data['cache_time'] = time();
$data['cache_expiry'] = $data['cache_time']+$config['max_cache_time'];
$data['config_hash'] = md5(json_encode($config));
$raw_data = json_encode($data);
file_put_contents($config['cache_file'], $raw_data, LOCK_EX);
}
}
/**
* Parses an array of peers and applies our filtering
*
* @param array $peers The array of peers to parse
* @param handle $curl_handle A reusable handle to pass to getGeolocation()
*
* @return array
*/
function parsePeers($peers, $curl_handle)
{
global $config;
$to_return = array();
foreach ($peers as $peer) {
// Extract IP address for later
$peer_addr_array = preg_split('/\:(?=[^:]*$)/', $peer['addr']);
$peer_ip = $peer_addr_array[0];
// Continue if peer is 'dark'
if ($config['hide_dark_peers'] === true) {
if ((strcmp($peer_ip, '127.0.0.1') == 0) || (strpos($peer_ip, '.onion') !== false)) {
continue;
}
}
// Continue if peer is to be ignored
if (is_array($config['peers_to_ignore']) & in_array($peer_ip, $config['peers_to_ignore'])) {
continue;
}
// Continue if peer reports no pingtime
if ($config['ignore_unknown_ping'] === true) {
if (!array_key_exists('pingtime', $peer)) {
continue;
}
}
// Do geolocation
if ($config['geolocate_peer_ip'] === true) {
$peer['geo_data'] = getGeolocation($peer_ip, $curl_handle);
}
// Override peer addr with IP
if ($config['display_peer_port'] === false) {
array_pop($peer_addr_array);
$peer['addr'] = str_replace(array('[',']'), '', implode(':', $peer_addr_array));
}
$to_return[] = $peer;
}
return $to_return;
}
/**
* Reads IP geolocation data from file
*
* @param string $file_path The file to read cache from
*
* @return Array
*/
function getGeoCache($file_path)
{
global $config;
$to_return = array();
if ($config['cache_geo_data'] === true) {
if (is_file($file_path)) {
$raw = json_decode(file_get_contents($file_path), true);
if (is_array($raw)) {
$cache = $raw;
} else {
$cache = array();
}
} else {
$cache = array();
}
} else {
$cache = array();
}
foreach ($cache as $ip => $data) {
if (!array_key_exists('expiry', $data)) {
continue;
} elseif (time() > $data['expiry']) {
continue;
} else {
$to_return[$ip] = $data;
}
}
return $cache;
}
/**
* Gets location of an IP via Geolocation
*
* @param string $ip_address The IP to Geolocate
* @param curl_resource $curl_handle The CURL handle to pass to the curlRequest call
*
* @return array An array of shortened country code and full country name
*/
function getGeolocation($ip_address, $curl_handle)
{
global $config;
global $geo_cache;
// Default flag + array
$run_curl = false;
$to_return = array(
'country_code' => 'blank',
'country_name' => 'Unavailable',
);
// Check for IP in cache + return if found
if ($config['cache_geo_data'] === true) {
if (array_key_exists($ip_address, $geo_cache)) {
return $geo_cache[$ip_address];
}
}
// If not found in cache, ping API
$exec_result = curlRequest("http://www.geoplugin.net/php.gp?ip=$ip_address", $curl_handle, true);
if ($exec_result !== false) {
$array = unserialize($exec_result);
// If match found, store it
$to_return['country_code'] = $array['geoplugin_countryCode'];
$to_return['country_name'] = $array['geoplugin_countryName'];
// If we're configured to cache, add expiry + cache it
if ($config['cache_geo_data'] === true) {
$geo_cache[$ip_address]['expiry'] = time() + $config['geo_cache_time'];
$geo_cache[$ip_address]['country_code'] = $array['geoplugin_countryCode'];
$geo_cache[$ip_address]['country_name'] = $array['geoplugin_countryName'];
}
}
return $to_return;
}
/**
* Wrapper function for CURL calls
*
* @param string $url The URL to CURL
* @param curl_resource $curl_handle An initialised CURL Handle to use
* @param boolean $fail_on_error Whether to fail if return code is >= 400
*
* @return string
*/
function curlRequest($url, $curl_handle, $fail_on_error = false)
{
global $curl_requests;
if ($curl_handle === false) {
return false;
}
if ($fail_on_error === true) {
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
}
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Dogecoin Node Status Page');
curl_setopt($curl_handle, CURLOPT_URL, $url);
$curl_requests++;
return curl_exec($curl_handle);
}
/**
* Generates a QR Code image for donations
*
* @return String
*/
function generateDonationImage()
{
global $config;
$alt_text = 'Donate ' . $config['donation_amount'] . ' DOGE to ' . $config['donation_address'];
return "\n" . '<img src="https://chart.googleapis.com/chart?chld=H|2&chs=225x225&cht=qr&chl=' . $config['donation_address'] . '" alt="' . $alt_text . '" />' . "\n";
}
/**
* Small function to split out chart-display logic
*
* @param boolean $config_var Master variable from $config
* @param string $data_file The filename holding the stats to display
* @param int $min_data_points The minimum number of data points to display
*
* @return boolean Whether to display the chart or not
*/
function displayChart($config_var, $data_file, $min_data_points)
{
if (($config_var === true) & (is_file($data_file))) {
if (count(json_decode(file_get_contents($data_file), true)) > $min_data_points) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Gets free disk space
*
* @param String $mount_point The mount point to check
*
* @return String
*/
function getFreeDiskSpace($mount_point)
{
return convertToSI(disk_free_space($mount_point)) . '<br />';
}
/**
* Formats a number into SI storage units
*
* @param Int $bytes The number of bytes to convert
*
* @return String Expression of $bytes in SI units
*/
function convertToSI($bytes)
{
if ($bytes === 0) {
return '0B';
} else {
$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
$base = 1024;
$return_class = min((int)log($bytes, $base), count($si_prefix) - 1);
return sprintf('%1.2f', $bytes / pow($base, $return_class)) . ' ' . $si_prefix[$return_class];
}
}
/**
* Generate "time ago" text from timestamp.
*
* @param Int $ptime UNIX timestamp
*
* @return String Time ago text.
*/
function elapsedTime($ptime)
{
$etime = time() - $ptime;
if ($etime < 1) {
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str);
}
}
}
/**
* Gets uptime of a process - reference: http://unix.stackexchange.com/q/27276/39264
*
* @param string $process The name of the pricess to find
*
* @return string A textual representation of the lifetime of the process
*/
function getProcessUptime($process)
{
$process_pid = exec("pidof $process");
$system_uptime = exec('cut -d "." -f1 /proc/uptime');
$pid_uptime = round((intval(exec("cut -d \" \" -f16 /proc/$process_pid//stat"))/100), 0);
$seconds = $system_uptime-$pid_uptime;
$days = floor($seconds / 86400);
$hours = str_pad(floor(($seconds - ($days*86400)) / 3600), 2, "0", STR_PAD_LEFT);
$mins = str_pad(floor(($seconds - ($days*86400) - ($hours*3600)) / 60), 2, "0", STR_PAD_LEFT);
$secs = str_pad(floor($seconds % 60), 2, "0", STR_PAD_LEFT);
return "$days days, $hours:$mins:$secs";
}