Update api file to PSR-2

This commit is contained in:
pxgamer
2018-05-29 15:43:26 +01:00
parent f95d94f205
commit 2fded5d4eb

234
api.php
View File

@@ -24,7 +24,6 @@ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @api {get} /api.php 01. Basic Information
* @apiName Info
@@ -66,9 +65,6 @@ OR OTHER DEALINGS IN THE SOFTWARE.
* }
*/
require_once("include/init.inc.php");
error_reporting(0);
$ip = san_ip($_SERVER['REMOTE_ADDR']);
@@ -78,10 +74,10 @@ if($_config['public_api']==false&&!in_array($ip,$_config['allowed_hosts'])){
api_err("private-api");
}
$acc = new Account;
$block = new Block;
$acc = new Account();
$block = new Block();
$trx = new Transaction;
$trx = new Transaction();
$q = $_GET['q'];
if (!empty($_POST['data'])) {
$data = json_decode($_POST['data'], true);
@@ -89,7 +85,6 @@ if(!empty($_POST['data'])){
$data = $_GET;
}
/**
* @api {get} /api.php?q=getAddress 02. getAddress
* @apiName getAddress
@@ -103,10 +98,11 @@ if(!empty($_POST['data'])){
if ($q == "getAddress") {
$public_key = $data['public_key'];
if(strlen($public_key)<32) api_err("Invalid public key");
api_echo($acc->get_address($public_key));
if (strlen($public_key) < 32) {
api_err("Invalid public key");
}
elseif($q=="base58"){
api_echo($acc->get_address($public_key));
} elseif ($q == "base58") {
/**
* @api {get} /api.php?q=base58 03. base58
* @apiName base58
@@ -119,8 +115,7 @@ elseif($q=="base58"){
*/
api_echo(base58_encode($data['data']));
}
elseif($q=="getBalance"){
} elseif ($q == "getBalance") {
/**
* @api {get} /api.php?q=getBalance 04. getBalance
* @apiName getBalance
@@ -135,13 +130,18 @@ elseif($q=="getBalance"){
$public_key = $data['public_key'];
$account = $data['account'];
if(!empty($public_key)&&strlen($public_key)<32) api_err("Invalid public key");
if(!empty($public_key)) $account=$acc->get_address($public_key);
if(empty($account)) api_err("Invalid account id");
if (!empty($public_key) && strlen($public_key) < 32) {
api_err("Invalid public key");
}
if (!empty($public_key)) {
$account = $acc->get_address($public_key);
}
if (empty($account)) {
api_err("Invalid account id");
}
$account = san($account);
api_echo($acc->balance($account));
}
elseif($q=="getPendingBalance"){
} elseif ($q == "getPendingBalance") {
/**
* @api {get} /api.php?q=getPendingBalance 05. getPendingBalance
* @apiName getPendingBalance
@@ -155,13 +155,18 @@ elseif($q=="getPendingBalance"){
*/
$account = $data['account'];
if(!empty($public_key)&&strlen($public_key)<32) api_err("Invalid public key");
if(!empty($public_key)) $account=$acc->get_address($public_key);
if(empty($account)) api_err("Invalid account id");
if (!empty($public_key) && strlen($public_key) < 32) {
api_err("Invalid public key");
}
if (!empty($public_key)) {
$account = $acc->get_address($public_key);
}
if (empty($account)) {
api_err("Invalid account id");
}
$account = san($account);
api_echo($acc->pending_balance($account));
}
elseif($q=="getTransactions"){
} elseif ($q == "getTransactions") {
/**
* @api {get} /api.php?q=getTransactions 06. getTransactions
* @apiName getTransactions
@@ -189,15 +194,20 @@ elseif($q=="getTransactions"){
*/
$account = san($data['account']);
if(!empty($public_key)&&strlen($public_key)<32) api_err("Invalid public key");
if(!empty($public_key)) $account=$acc->get_address($public_key);
if(empty($account)) api_err("Invalid account id");
if (!empty($public_key) && strlen($public_key) < 32) {
api_err("Invalid public key");
}
if (!empty($public_key)) {
$account = $acc->get_address($public_key);
}
if (empty($account)) {
api_err("Invalid account id");
}
$limit = intval($data['limit']);
$transactions = $acc->get_mempool_transactions($account);
$transactions = array_merge($transactions, $acc->get_transactions($account, $limit));
api_echo($transactions);
} elseif ($q == "getTransaction") {
/**
* @api {get} /api.php?q=getTransaction 07. getTransaction
@@ -227,7 +237,9 @@ elseif($q=="getTransactions"){
$res = $trx->get_transaction($id);
if ($res === false) {
$res = $trx->get_mempool_transaction($id);
if($res===false) api_err("invalid transaction");
if ($res === false) {
api_err("invalid transaction");
}
}
api_Echo($res);
} elseif ($q == "getPublicKey") {
@@ -243,12 +255,15 @@ elseif($q=="getTransactions"){
*/
$account = san($data['account']);
if(empty($account)) api_err("Invalid account id");
if (empty($account)) {
api_err("Invalid account id");
}
$public_key = $acc->public_key($account);
if($public_key===false) api_err("No public key found for this account");
else api_echo($public_key);
if ($public_key === false) {
api_err("No public key found for this account");
} else {
api_echo($public_key);
}
} elseif ($q == "generateAccount") {
/**
* @api {get} /api.php?q=generateAccount 09. generateAccount
@@ -279,13 +294,10 @@ elseif($q=="getTransactions"){
* @apiSuccess {string} signature Signature signed by the generator
* @apiSuccess {numeric} difficulty The base target / difficulty
* @apiSuccess {string} argon Mining argon hash
*/
$current = $block->current();
api_echo($current);
} elseif ($q == "getBlock") {
/**
* @api {get} /api.php?q=getBlock 11. getBlock
@@ -306,8 +318,11 @@ elseif($q=="getTransactions"){
*/
$height = san($data['height']);
$ret = $block->get($height);
if($ret==false) api_err("Invalid block");
else api_echo($ret);
if ($ret == false) {
api_err("Invalid block");
} else {
api_echo($ret);
}
} elseif ($q == "getBlockTransactions") {
/**
* @api {get} /api.php?q=getBlockTransactions 12. getBlockTransactions
@@ -336,9 +351,11 @@ elseif($q=="getTransactions"){
$height = san($data['height']);
$block = san($data['block']);
$ret = $trx->get_transactions($height, $block);
if($ret===false) api_err("Invalid block");
else api_echo($ret);
if ($ret === false) {
api_err("Invalid block");
} else {
api_echo($ret);
}
} elseif ($q == "version") {
/**
* @api {get} /api.php?q=version 13. version
@@ -350,7 +367,6 @@ elseif($q=="getTransactions"){
* @apiSuccess {string} data Version
*/
api_echo(VERSION);
} elseif ($q == "send") {
/**
* @api {get} /api.php?q=send 14. send
@@ -371,7 +387,9 @@ elseif($q=="getTransactions"){
*/
$current = $block->current();
if($current['height']>10790&&$current['height']<10810) api_err("Hard fork in progress. Please retry the transaction later!"); //10800
if ($current['height'] > 10790 && $current['height'] < 10810) {
api_err("Hard fork in progress. Please retry the transaction later!"); //10800
}
$acc = new Account;
$block = new Block;
@@ -380,60 +398,93 @@ elseif($q=="getTransactions"){
$dst = san($data['dst']);
if(!$acc->valid($dst)) api_err("Invalid destination address");
if (!$acc->valid($dst)) {
api_err("Invalid destination address");
}
$dst_b = base58_decode($dst);
if(strlen($dst_b)!=64) api_err("Invalid destination address");
if (strlen($dst_b) != 64) {
api_err("Invalid destination address");
}
$public_key = san($data['public_key']);
if(!$acc->valid_key($public_key)) api_err("Invalid public key");
if (!$acc->valid_key($public_key)) {
api_err("Invalid public key");
}
$private_key = san($data['private_key']);
if(!$acc->valid_key($private_key)) api_err("Invalid private key");
if (!$acc->valid_key($private_key)) {
api_err("Invalid private key");
}
$signature = san($data['signature']);
if(!$acc->valid_key($signature)) api_err("Invalid signature");
if (!$acc->valid_key($signature)) {
api_err("Invalid signature");
}
$date = $data['date'] + 0;
if($date==0) $date=time();
if($date<time()-(3600*24*48)) api_err("The date is too old");
if($date>time()+86400) api_err("Invalid Date");
if ($date == 0) {
$date = time();
}
if ($date < time() - (3600 * 24 * 48)) {
api_err("The date is too old");
}
if ($date > time() + 86400) {
api_err("Invalid Date");
}
$version = intval($data['version']);
$message = $data['message'];
if(strlen($message)>128) api_err("The message must be less than 128 chars");
if (strlen($message) > 128) {
api_err("The message must be less than 128 chars");
}
$val = $data['val'] + 0;
$fee = $val * 0.0025;
if($fee<0.00000001) $fee=0.00000001;
if ($fee < 0.00000001) {
$fee = 0.00000001;
}
if($fee>10&&$current['height']>10800) $fee=10; //10800
if($val<0.00000001) api_err("Invalid value");
if ($fee > 10 && $current['height'] > 10800) {
$fee = 10; //10800
}
if ($val < 0.00000001) {
api_err("Invalid value");
}
if($version<1) $version=1;
if ($version < 1) {
$version = 1;
}
$val = number_format($val, 8, '.', '');
$fee = number_format($fee, 8, '.', '');
if(empty($public_key)&&empty($private_key)) api_err("Either the private key or the public key must be sent");
if (empty($public_key) && empty($private_key)) {
api_err("Either the private key or the public key must be sent");
}
if(empty($private_key)&&empty($signature)) api_err("Either the private_key or the signature must be sent");
if(empty($public_key))
{
if (empty($private_key) && empty($signature)) {
api_err("Either the private_key or the signature must be sent");
}
if (empty($public_key)) {
$pk = coin2pem($private_key, true);
$pkey = openssl_pkey_get_private($pk);
$pub = openssl_pkey_get_details($pkey);
$public_key = pem2coin($pub['key']);
}
$transaction=array("val"=>$val, "fee"=>$fee, "dst"=>$dst, "public_key"=>$public_key,"date"=>$date, "version"=>$version,"message"=>$message, "signature"=>$signature);
$transaction = [
"val" => $val,
"fee" => $fee,
"dst" => $dst,
"public_key" => $public_key,
"date" => $date,
"version" => $version,
"message" => $message,
"signature" => $signature,
];
if (!empty($private_key)) {
$signature = $trx->sign($transaction, $private_key);
$transaction['signature'] = $signature;
}
@@ -441,29 +492,34 @@ elseif($q=="getTransactions"){
$transaction['id'] = $hash;
if(!$trx->check($transaction)) api_err("Transaction signature failed");
if (!$trx->check($transaction)) {
api_err("Transaction signature failed");
}
$res = $db->single("SELECT COUNT(1) FROM mempool WHERE id=:id", [":id" => $hash]);
if ($res != 0) {
api_err("The transaction is already in mempool");
}
$res=$db->single("SELECT COUNT(1) FROM mempool WHERE id=:id",array(":id"=>$hash));
if($res!=0) api_err("The transaction is already in mempool");
$res=$db->single("SELECT COUNT(1) FROM transactions WHERE id=:id",array(":id"=>$hash));
if($res!=0) api_err("The transaction is already in a block");
$res = $db->single("SELECT COUNT(1) FROM transactions WHERE id=:id", [":id" => $hash]);
if ($res != 0) {
api_err("The transaction is already in a block");
}
$src = $acc->get_address($public_key);
$transaction['src'] = $src;
$balance=$db->single("SELECT balance FROM accounts WHERE id=:id",array(":id"=>$src));
if($balance<$val+$fee) api_err("Not enough funds");
$balance = $db->single("SELECT balance FROM accounts WHERE id=:id", [":id" => $src]);
if ($balance < $val + $fee) {
api_err("Not enough funds");
}
$memspent=$db->single("SELECT SUM(val+fee) FROM mempool WHERE src=:src",array(":src"=>$src));
if($balance-$memspent<$val+$fee) api_err("Not enough funds (mempool)");
$memspent = $db->single("SELECT SUM(val+fee) FROM mempool WHERE src=:src", [":src" => $src]);
if ($balance - $memspent < $val + $fee) {
api_err("Not enough funds (mempool)");
}
$trx->add_mempool($transaction, "local");
@@ -481,7 +537,6 @@ elseif($q=="getTransactions"){
$res = $db->single("SELECT COUNT(1) FROM mempool");
api_echo($res);
} elseif ($q == 'randomNumber') {
/**
* @api {get} /api.php?q=randomNumber 16. randomNumber
@@ -498,11 +553,16 @@ elseif($q=="getTransactions"){
$height = san($_GET['height']);
$max = intval($_GET['max']);
if(empty($_GET['min'])) $min=1;
else $min=intval($_GET['min']);
if (empty($_GET['min'])) {
$min = 1;
} else {
$min = intval($_GET['min']);
}
$blk=$db->single("SELECT id FROM blocks WHERE height=:h",array(":h"=>$height));
if($blk===false) api_err("Unknown block. Future?");
$blk = $db->single("SELECT id FROM blocks WHERE height=:h", [":h" => $height]);
if ($blk === false) {
api_err("Unknown block. Future?");
}
$base = hash("sha256", $blk.$_GET['seed']);
$seed1 = hexdec(substr($base, 0, 12));
@@ -510,10 +570,6 @@ elseif($q=="getTransactions"){
mt_srand($seed1, MT_RAND_MT19937);
$res = mt_rand($min, $max);
api_echo($res);
} else {
api_err("Invalid request");
}
?>