Update util file to use PSR-2

This commit is contained in:
pxgamer
2018-05-29 15:49:35 +01:00
parent aae845548f
commit c6393d7e8d

150
util.php
View File

@@ -24,15 +24,14 @@ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE. OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
// make sure it's not accessible in the browser // make sure it's not accessible in the browser
if(php_sapi_name() !== 'cli') die("This should only be run as cli"); if (php_sapi_name() !== 'cli') {
die("This should only be run as cli");
}
require_once("include/init.inc.php"); require_once("include/init.inc.php");
$cmd = trim($argv[1]); $cmd = trim($argv[1]);
/** /**
* @api {php util.php} clean Clean * @api {php util.php} clean Clean
* @apiName clean * @apiName clean
@@ -44,15 +43,13 @@ $cmd=trim($argv[1]);
*/ */
if ($cmd == 'clean') { if ($cmd == 'clean') {
$tables=array("blocks","accounts","transactions","mempool"); $tables = ["blocks", "accounts", "transactions", "mempool"];
foreach($tables as $table) $db->run("DELETE FROM {$table}"); foreach ($tables as $table) {
$db->run("DELETE FROM {$table}");
echo "\n The database has been cleared\n";
} }
echo "\n The database has been cleared\n";
/** } /**
* @api {php util.php} pop Pop * @api {php util.php} pop Pop
* @apiName pop * @apiName pop
* @apiGroup UTIL * @apiGroup UTIL
@@ -66,11 +63,9 @@ echo "\n The database has been cleared\n";
elseif ($cmd == 'pop') { elseif ($cmd == 'pop') {
$no = intval($argv[2]); $no = intval($argv[2]);
$block=new Block; $block = new Block();
$block->pop($no); $block->pop($no);
} } /**
/**
* @api {php util.php} block-time Block-time * @api {php util.php} block-time Block-time
* @apiName block-time * @apiName block-time
* @apiGroup UTIL * @apiGroup UTIL
@@ -91,17 +86,16 @@ elseif($cmd=='block-time'){
$r = $db->run("SELECT * FROM blocks ORDER by height DESC LIMIT 100"); $r = $db->run("SELECT * FROM blocks ORDER by height DESC LIMIT 100");
$start = 0; $start = 0;
foreach ($r as $x) { foreach ($r as $x) {
if($start==0) $start=$x['date']; if ($start == 0) {
$start = $x['date'];
}
$time = $t - $x['date']; $time = $t - $x['date'];
$t = $x['date']; $t = $x['date'];
echo "$x[height] -> $time\n"; echo "$x[height] -> $time\n";
$end = $x['date']; $end = $x['date'];
} }
echo "Average block time: ".ceil(($start - $end) / 100)." seconds\n"; echo "Average block time: ".ceil(($start - $end) / 100)." seconds\n";
} /**
}
/**
* @api {php util.php} peer Peer * @api {php util.php} peer Peer
* @apiName peer * @apiName peer
* @apiGroup UTIL * @apiGroup UTIL
@@ -118,11 +112,13 @@ echo "Average block time: ".ceil(($start-$end)/100)." seconds\n";
elseif ($cmd == "peer") { elseif ($cmd == "peer") {
$res=peer_post($argv[2]."/peer.php?q=peer",array("hostname"=>$_config['hostname'])); $res = peer_post($argv[2]."/peer.php?q=peer", ["hostname" => $_config['hostname']]);
if($res!==false) echo "Peering OK\n"; if ($res !== false) {
else echo "Peering FAIL\n"; echo "Peering OK\n";
} else {
echo "Peering FAIL\n";
} }
/** } /**
* @api {php util.php} current Current * @api {php util.php} current Current
* @apiName current * @apiName current
* @apiGroup UTIL * @apiGroup UTIL
@@ -156,10 +152,9 @@ elseif($cmd=="peer"){
*/ */
elseif ($cmd == "current") { elseif ($cmd == "current") {
$block=new Block; $block = new Block();
var_dump($block->current()); var_dump($block->current());
} } /**
/**
* @api {php util.php} blocks Blocks * @api {php util.php} blocks Blocks
* @apiName blocks * @apiName blocks
* @apiGroup UTIL * @apiGroup UTIL
@@ -183,13 +178,14 @@ elseif ($cmd=="current") {
elseif ($cmd == "blocks") { elseif ($cmd == "blocks") {
$height = intval($argv[2]); $height = intval($argv[2]);
$limit = intval($argv[3]); $limit = intval($argv[3]);
if($limit<1) $limit=100; if ($limit < 1) {
$r=$db->run("SELECT * FROM blocks WHERE height>:height ORDER by height ASC LIMIT $limit",array(":height"=>$height)); $limit = 100;
}
$r = $db->run("SELECT * FROM blocks WHERE height>:height ORDER by height ASC LIMIT $limit", [":height" => $height]);
foreach ($r as $x) { foreach ($r as $x) {
echo "$x[height]\t$x[id]\n"; echo "$x[height]\t$x[id]\n";
} }
} } /**
/**
* @api {php util.php} recheck-blocks Recheck-Blocks * @api {php util.php} recheck-blocks Recheck-Blocks
* @apiName recheck-blocks * @apiName recheck-blocks
* @apiGroup UTIL * @apiGroup UTIL
@@ -200,7 +196,7 @@ elseif ($cmd=="current") {
* *
*/ */
elseif ($cmd == "recheck-blocks") { elseif ($cmd == "recheck-blocks") {
$blocks=array(); $blocks = [];
$block = new Block(); $block = new Block();
$r = $db->run("SELECT * FROM blocks ORDER by height ASC"); $r = $db->run("SELECT * FROM blocks ORDER by height ASC");
foreach ($r as $x) { foreach ($r as $x) {
@@ -210,15 +206,21 @@ elseif($cmd=="recheck-blocks"){
for ($i = 2; $i <= $max_height; $i++) { for ($i = 2; $i <= $max_height; $i++) {
$data = $blocks[$i]; $data = $blocks[$i];
$key=$db->single("SELECT public_key FROM accounts WHERE id=:id",array(":id"=>$data['generator'])); $key = $db->single("SELECT public_key FROM accounts WHERE id=:id", [":id" => $data['generator']]);
if(!$block->mine($key,$data['nonce'], $data['argon'], $data['difficulty'], $blocks[$i-1]['id'],$blocks[$i-1]['height'])) { if (!$block->mine(
$key,
$data['nonce'],
$data['argon'],
$data['difficulty'],
$blocks[$i - 1]['id'],
$blocks[$i - 1]['height']
)) {
_log("Invalid block detected. We should delete everything after $data[height] - $data[id]"); _log("Invalid block detected. We should delete everything after $data[height] - $data[id]");
break; break;
} }
} }
} } /**
/**
* @api {php util.php} peers Peers * @api {php util.php} peers Peers
* @apiName peers * @apiName peers
* @apiGroup UTIL * @apiGroup UTIL
@@ -235,12 +237,13 @@ elseif($cmd=="recheck-blocks"){
elseif ($cmd == "peers") { elseif ($cmd == "peers") {
$r = $db->run("SELECT * FROM peers ORDER by reserve ASC"); $r = $db->run("SELECT * FROM peers ORDER by reserve ASC");
$status = "active"; $status = "active";
if($x['reserve']==1) $status="reserve"; if ($x['reserve'] == 1) {
$status = "reserve";
}
foreach ($r as $x) { foreach ($r as $x) {
echo "$x[hostname]\t$status\n"; echo "$x[hostname]\t$status\n";
} }
} } /**
/**
* @api {php util.php} mempool Mempool * @api {php util.php} mempool Mempool
* @apiName mempool * @apiName mempool
* @apiGroup UTIL * @apiGroup UTIL
@@ -255,9 +258,7 @@ elseif($cmd=="recheck-blocks"){
elseif ($cmd == "mempool") { elseif ($cmd == "mempool") {
$res = $db->single("SELECT COUNT(1) from mempool"); $res = $db->single("SELECT COUNT(1) from mempool");
echo "Mempool size: $res\n"; echo "Mempool size: $res\n";
} /**
}
/**
* @api {php util.php} delete-peer Delete-peer * @api {php util.php} delete-peer Delete-peer
* @apiName delete-peer * @apiName delete-peer
* @apiGroup UTIL * @apiGroup UTIL
@@ -273,8 +274,10 @@ echo "Mempool size: $res\n";
*/ */
elseif ($cmd == "delete-peer") { elseif ($cmd == "delete-peer") {
$peer = trim($argv[2]); $peer = trim($argv[2]);
if(empty($peer)) die("Invalid peer"); if (empty($peer)) {
$db->run("DELETE FROM peers WHERE ip=:ip",array(":ip"=>$peer)); die("Invalid peer");
}
$db->run("DELETE FROM peers WHERE ip=:ip", [":ip" => $peer]);
echo "Peer removed\n"; echo "Peer removed\n";
} elseif ($cmd == "recheck-peers") { } elseif ($cmd == "recheck-peers") {
$r = $db->run("SELECT * FROM peers"); $r = $db->run("SELECT * FROM peers");
@@ -282,13 +285,12 @@ elseif($cmd=="delete-peer"){
$a = peer_post($x['hostname']."/peer.php?q=ping"); $a = peer_post($x['hostname']."/peer.php?q=ping");
if ($a != "pong") { if ($a != "pong") {
echo "$x[hostname] -> failed\n"; echo "$x[hostname] -> failed\n";
$db->run("DELETE FROM peers WHERE id=:id",array(":id"=>$x['id'])); $db->run("DELETE FROM peers WHERE id=:id", [":id" => $x['id']]);
} else echo "$x[hostname] ->ok \n"; } else {
echo "$x[hostname] ->ok \n";
} }
} }
} /**
/**
* @api {php util.php} peers-block Peers-Block * @api {php util.php} peers-block Peers-Block
* @apiName peers-block * @apiName peers-block
* @apiGroup UTIL * @apiGroup UTIL
@@ -310,15 +312,16 @@ elseif($cmd=="peers-block"){
} }
$r = $db->run("SELECT * FROM peers WHERE blacklisted<UNIX_TIMESTAMP()"); $r = $db->run("SELECT * FROM peers WHERE blacklisted<UNIX_TIMESTAMP()");
foreach ($r as $x) { foreach ($r as $x) {
$a=peer_post($x['hostname']."/peer.php?q=currentBlock",array(),5); $a = peer_post($x['hostname']."/peer.php?q=currentBlock", [], 5);
$enc = base58_encode($x['hostname']); $enc = base58_encode($x['hostname']);
if($argv[2]=="debug") echo "$enc\t"; if ($argv[2] == "debug") {
if($only_diff==false||$current!=$a['height']) echo "$x[hostname]\t$a[height]\n"; echo "$enc\t";
}
if ($only_diff == false || $current != $a['height']) {
echo "$x[hostname]\t$a[height]\n";
} }
} }
} /**
/**
* @api {php util.php} balance Balance * @api {php util.php} balance Balance
* @apiName balance * @apiName balance
* @apiGroup UTIL * @apiGroup UTIL
@@ -334,13 +337,14 @@ elseif($cmd=="peers-block"){
*/ */
elseif ($cmd == "balance") { elseif ($cmd == "balance") {
$id = san($argv[2]); $id = san($argv[2]);
$res=$db->single("SELECT balance FROM accounts WHERE id=:id OR public_key=:id2 LIMIT 1",array(":id"=>$id, ":id2"=>$id)); $res = $db->single(
"SELECT balance FROM accounts WHERE id=:id OR public_key=:id2 LIMIT 1",
[":id" => $id, ":id2" => $id]
);
echo "Balance: ".number_format($res)."\n"; echo "Balance: ".number_format($res)."\n";
} } /**
/**
* @api {php util.php} block Block * @api {php util.php} block Block
* @apiName block * @apiName block
* @apiGroup UTIL * @apiGroup UTIL
@@ -375,12 +379,10 @@ elseif($cmd=="balance"){
*/ */
elseif ($cmd == "block") { elseif ($cmd == "block") {
$id = san($argv[2]); $id = san($argv[2]);
$res=$db->row("SELECT * FROM blocks WHERE id=:id OR height=:id2 LIMIT 1",array(":id"=>$id, ":id2"=>$id)); $res = $db->row("SELECT * FROM blocks WHERE id=:id OR height=:id2 LIMIT 1", [":id" => $id, ":id2" => $id]);
var_dump($res); var_dump($res);
} /**
}
/**
* @api {php util.php} check-address Check-Address * @api {php util.php} check-address Check-Address
* @apiName check-address * @apiName check-address
* @apiGroup UTIL * @apiGroup UTIL
@@ -396,14 +398,17 @@ elseif($cmd=="block"){
*/ */
elseif ($cmd == "check-address") { elseif ($cmd == "check-address") {
$dst = trim($argv[2]); $dst = trim($argv[2]);
$acc=new Account; $acc = new Account();
if(!$acc->valid($dst)) die("Invalid address"); if (!$acc->valid($dst)) {
die("Invalid address");
}
$dst_b = base58_decode($dst); $dst_b = base58_decode($dst);
if(strlen($dst_b)!=64) die("Invalid address - ".strlen($dst_b)." bytes"); if (strlen($dst_b) != 64) {
die("Invalid address - ".strlen($dst_b)." bytes");
}
echo "The address is valid\n"; echo "The address is valid\n";
} } /**
/**
* @api {php util.php} get-address Get-Address * @api {php util.php} get-address Get-Address
* @apiName get-address * @apiName get-address
* @apiGroup UTIL * @apiGroup UTIL
@@ -419,14 +424,11 @@ elseif($cmd=="check-address"){
*/ */
elseif ($cmd == 'get-address') { elseif ($cmd == 'get-address') {
$public_key = trim($argv2); $public_key = trim($argv2);
if(strlen($public_key)<32) die("Invalid public key"); if (strlen($public_key) < 32) {
die("Invalid public key");
}
print($acc->get_address($public_key)); print($acc->get_address($public_key));
} else { } else {
echo "Invalid command\n"; echo "Invalid command\n";
} }
?>