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.
*/
// 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");
$cmd = trim($argv[1]);
/**
* @api {php util.php} clean Clean
* @apiName clean
@@ -44,15 +43,13 @@ $cmd=trim($argv[1]);
*/
if ($cmd == 'clean') {
$tables=array("blocks","accounts","transactions","mempool");
foreach($tables as $table) $db->run("DELETE FROM {$table}");
echo "\n The database has been cleared\n";
$tables = ["blocks", "accounts", "transactions", "mempool"];
foreach ($tables as $table) {
$db->run("DELETE FROM {$table}");
}
/**
echo "\n The database has been cleared\n";
} /**
* @api {php util.php} pop Pop
* @apiName pop
* @apiGroup UTIL
@@ -66,11 +63,9 @@ echo "\n The database has been cleared\n";
elseif ($cmd == 'pop') {
$no = intval($argv[2]);
$block=new Block;
$block = new Block();
$block->pop($no);
}
/**
} /**
* @api {php util.php} block-time Block-time
* @apiName block-time
* @apiGroup UTIL
@@ -91,17 +86,16 @@ elseif($cmd=='block-time'){
$r = $db->run("SELECT * FROM blocks ORDER by height DESC LIMIT 100");
$start = 0;
foreach ($r as $x) {
if($start==0) $start=$x['date'];
if ($start == 0) {
$start = $x['date'];
}
$time = $t - $x['date'];
$t = $x['date'];
echo "$x[height] -> $time\n";
$end = $x['date'];
}
echo "Average block time: ".ceil(($start - $end) / 100)." seconds\n";
}
/**
} /**
* @api {php util.php} peer Peer
* @apiName peer
* @apiGroup UTIL
@@ -118,11 +112,13 @@ echo "Average block time: ".ceil(($start-$end)/100)." seconds\n";
elseif ($cmd == "peer") {
$res=peer_post($argv[2]."/peer.php?q=peer",array("hostname"=>$_config['hostname']));
if($res!==false) echo "Peering OK\n";
else echo "Peering FAIL\n";
$res = peer_post($argv[2]."/peer.php?q=peer", ["hostname" => $_config['hostname']]);
if ($res !== false) {
echo "Peering OK\n";
} else {
echo "Peering FAIL\n";
}
/**
} /**
* @api {php util.php} current Current
* @apiName current
* @apiGroup UTIL
@@ -156,10 +152,9 @@ elseif($cmd=="peer"){
*/
elseif ($cmd == "current") {
$block=new Block;
$block = new Block();
var_dump($block->current());
}
/**
} /**
* @api {php util.php} blocks Blocks
* @apiName blocks
* @apiGroup UTIL
@@ -183,13 +178,14 @@ elseif ($cmd=="current") {
elseif ($cmd == "blocks") {
$height = intval($argv[2]);
$limit = intval($argv[3]);
if($limit<1) $limit=100;
$r=$db->run("SELECT * FROM blocks WHERE height>:height ORDER by height ASC LIMIT $limit",array(":height"=>$height));
if ($limit < 1) {
$limit = 100;
}
$r = $db->run("SELECT * FROM blocks WHERE height>:height ORDER by height ASC LIMIT $limit", [":height" => $height]);
foreach ($r as $x) {
echo "$x[height]\t$x[id]\n";
}
}
/**
} /**
* @api {php util.php} recheck-blocks Recheck-Blocks
* @apiName recheck-blocks
* @apiGroup UTIL
@@ -200,7 +196,7 @@ elseif ($cmd=="current") {
*
*/
elseif ($cmd == "recheck-blocks") {
$blocks=array();
$blocks = [];
$block = new Block();
$r = $db->run("SELECT * FROM blocks ORDER by height ASC");
foreach ($r as $x) {
@@ -210,15 +206,21 @@ elseif($cmd=="recheck-blocks"){
for ($i = 2; $i <= $max_height; $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]");
break;
}
}
}
/**
} /**
* @api {php util.php} peers Peers
* @apiName peers
* @apiGroup UTIL
@@ -235,12 +237,13 @@ elseif($cmd=="recheck-blocks"){
elseif ($cmd == "peers") {
$r = $db->run("SELECT * FROM peers ORDER by reserve ASC");
$status = "active";
if($x['reserve']==1) $status="reserve";
if ($x['reserve'] == 1) {
$status = "reserve";
}
foreach ($r as $x) {
echo "$x[hostname]\t$status\n";
}
}
/**
} /**
* @api {php util.php} mempool Mempool
* @apiName mempool
* @apiGroup UTIL
@@ -255,9 +258,7 @@ elseif($cmd=="recheck-blocks"){
elseif ($cmd == "mempool") {
$res = $db->single("SELECT COUNT(1) from mempool");
echo "Mempool size: $res\n";
}
/**
} /**
* @api {php util.php} delete-peer Delete-peer
* @apiName delete-peer
* @apiGroup UTIL
@@ -273,8 +274,10 @@ echo "Mempool size: $res\n";
*/
elseif ($cmd == "delete-peer") {
$peer = trim($argv[2]);
if(empty($peer)) die("Invalid peer");
$db->run("DELETE FROM peers WHERE ip=:ip",array(":ip"=>$peer));
if (empty($peer)) {
die("Invalid peer");
}
$db->run("DELETE FROM peers WHERE ip=:ip", [":ip" => $peer]);
echo "Peer removed\n";
} elseif ($cmd == "recheck-peers") {
$r = $db->run("SELECT * FROM peers");
@@ -282,13 +285,12 @@ elseif($cmd=="delete-peer"){
$a = peer_post($x['hostname']."/peer.php?q=ping");
if ($a != "pong") {
echo "$x[hostname] -> failed\n";
$db->run("DELETE FROM peers WHERE id=:id",array(":id"=>$x['id']));
} else echo "$x[hostname] ->ok \n";
$db->run("DELETE FROM peers WHERE id=:id", [":id" => $x['id']]);
} else {
echo "$x[hostname] ->ok \n";
}
}
/**
} /**
* @api {php util.php} peers-block Peers-Block
* @apiName peers-block
* @apiGroup UTIL
@@ -310,15 +312,16 @@ elseif($cmd=="peers-block"){
}
$r = $db->run("SELECT * FROM peers WHERE blacklisted<UNIX_TIMESTAMP()");
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']);
if($argv[2]=="debug") echo "$enc\t";
if($only_diff==false||$current!=$a['height']) echo "$x[hostname]\t$a[height]\n";
if ($argv[2] == "debug") {
echo "$enc\t";
}
if ($only_diff == false || $current != $a['height']) {
echo "$x[hostname]\t$a[height]\n";
}
}
/**
} /**
* @api {php util.php} balance Balance
* @apiName balance
* @apiGroup UTIL
@@ -334,13 +337,14 @@ elseif($cmd=="peers-block"){
*/
elseif ($cmd == "balance") {
$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";
}
/**
} /**
* @api {php util.php} block Block
* @apiName block
* @apiGroup UTIL
@@ -375,12 +379,10 @@ elseif($cmd=="balance"){
*/
elseif ($cmd == "block") {
$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);
}
/**
} /**
* @api {php util.php} check-address Check-Address
* @apiName check-address
* @apiGroup UTIL
@@ -396,14 +398,17 @@ elseif($cmd=="block"){
*/
elseif ($cmd == "check-address") {
$dst = trim($argv[2]);
$acc=new Account;
if(!$acc->valid($dst)) die("Invalid address");
$acc = new Account();
if (!$acc->valid($dst)) {
die("Invalid address");
}
$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";
}
/**
} /**
* @api {php util.php} get-address Get-Address
* @apiName get-address
* @apiGroup UTIL
@@ -419,14 +424,11 @@ elseif($cmd=="check-address"){
*/
elseif ($cmd == 'get-address') {
$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));
} else {
echo "Invalid command\n";
}
?>