Update functions to PSR-2
This commit is contained in:
@@ -1,37 +1,44 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
// simple santization function to accept only alphanumeric characters
|
// simple santization function to accept only alphanumeric characters
|
||||||
function san($a,$b=""){
|
function san($a, $b = "")
|
||||||
|
{
|
||||||
$a = preg_replace("/[^a-zA-Z0-9".$b."]/", "", $a);
|
$a = preg_replace("/[^a-zA-Z0-9".$b."]/", "", $a);
|
||||||
|
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
|
|
||||||
function san_ip($a){
|
function san_ip($a)
|
||||||
|
{
|
||||||
$a = preg_replace("/[^a-fA-F0-9\[\]\.\:]/", "", $a);
|
$a = preg_replace("/[^a-fA-F0-9\[\]\.\:]/", "", $a);
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
|
|
||||||
function san_host($a){
|
function san_host($a)
|
||||||
|
{
|
||||||
$a = preg_replace("/[^a-zA-Z0-9\.\-\:\/]/", "", $a);
|
$a = preg_replace("/[^a-zA-Z0-9\.\-\:\/]/", "", $a);
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
|
|
||||||
// api error and exit
|
// api error and exit
|
||||||
function api_err($data){
|
function api_err($data)
|
||||||
|
{
|
||||||
global $_config;
|
global $_config;
|
||||||
echo json_encode(array("status"=>"error","data"=>$data, "coin"=>$_config['coin']));
|
echo json_encode(["status" => "error", "data" => $data, "coin" => $_config['coin']]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// api print ok and exit
|
// api print ok and exit
|
||||||
function api_echo($data){
|
function api_echo($data)
|
||||||
|
{
|
||||||
global $_config;
|
global $_config;
|
||||||
echo json_encode(array("status"=>"ok","data"=>$data, "coin"=>$_config['coin']));
|
echo json_encode(["status" => "ok", "data" => $data, "coin" => $_config['coin']]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// log function, shows only in cli atm
|
// log function, shows only in cli atm
|
||||||
function _log($data){
|
function _log($data)
|
||||||
|
{
|
||||||
$date = date("[Y-m-d H:i:s]");
|
$date = date("[Y-m-d H:i:s]");
|
||||||
$trace = debug_backtrace();
|
$trace = debug_backtrace();
|
||||||
$loc = count($trace) - 1;
|
$loc = count($trace) - 1;
|
||||||
@@ -39,10 +46,16 @@ function _log($data){
|
|||||||
|
|
||||||
$res = "$date ".$file.":".$trace[$loc]['line'];
|
$res = "$date ".$file.":".$trace[$loc]['line'];
|
||||||
|
|
||||||
if(!empty($trace[$loc]['class'])) $res.="---".$trace[$loc]['class'];
|
if (!empty($trace[$loc]['class'])) {
|
||||||
if(!empty($trace[$loc]['function'])&&$trace[$loc]['function']!='_log') $res.='->'.$trace[$loc]['function'].'()';
|
$res .= "---".$trace[$loc]['class'];
|
||||||
|
}
|
||||||
|
if (!empty($trace[$loc]['function']) && $trace[$loc]['function'] != '_log') {
|
||||||
|
$res .= '->'.$trace[$loc]['function'].'()';
|
||||||
|
}
|
||||||
$res .= " $data \n";
|
$res .= " $data \n";
|
||||||
if(php_sapi_name() === 'cli') echo $res;
|
if (php_sapi_name() === 'cli') {
|
||||||
|
echo $res;
|
||||||
|
}
|
||||||
global $_config;
|
global $_config;
|
||||||
if ($_config['enable_logging'] == true) {
|
if ($_config['enable_logging'] == true) {
|
||||||
@file_put_contents($_config['log_file'], $res, FILE_APPEND);
|
@file_put_contents($_config['log_file'], $res, FILE_APPEND);
|
||||||
@@ -50,7 +63,8 @@ function _log($data){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// converts PEM key to hex
|
// converts PEM key to hex
|
||||||
function pem2hex ($data) {
|
function pem2hex($data)
|
||||||
|
{
|
||||||
$data = str_replace("-----BEGIN PUBLIC KEY-----", "", $data);
|
$data = str_replace("-----BEGIN PUBLIC KEY-----", "", $data);
|
||||||
$data = str_replace("-----END PUBLIC KEY-----", "", $data);
|
$data = str_replace("-----END PUBLIC KEY-----", "", $data);
|
||||||
$data = str_replace("-----BEGIN EC PRIVATE KEY-----", "", $data);
|
$data = str_replace("-----BEGIN EC PRIVATE KEY-----", "", $data);
|
||||||
@@ -62,16 +76,17 @@ function pem2hex ($data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// converts hex key to PEM
|
// converts hex key to PEM
|
||||||
function hex2pem ($data, $is_private_key=false) {
|
function hex2pem($data, $is_private_key = false)
|
||||||
|
{
|
||||||
$data = hex2bin($data);
|
$data = hex2bin($data);
|
||||||
$data = base64_encode($data);
|
$data = base64_encode($data);
|
||||||
if($is_private_key) return "-----BEGIN EC PRIVATE KEY-----\n".$data."\n-----END EC PRIVATE KEY-----";
|
if ($is_private_key) {
|
||||||
|
return "-----BEGIN EC PRIVATE KEY-----\n".$data."\n-----END EC PRIVATE KEY-----";
|
||||||
|
}
|
||||||
return "-----BEGIN PUBLIC KEY-----\n".$data."\n-----END PUBLIC KEY-----";
|
return "-----BEGIN PUBLIC KEY-----\n".$data."\n-----END PUBLIC KEY-----";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Base58 encoding/decoding functions - all credits go to https://github.com/stephen-hill/base58php
|
// Base58 encoding/decoding functions - all credits go to https://github.com/stephen-hill/base58php
|
||||||
function base58_encode($string)
|
function base58_encode($string)
|
||||||
{
|
{
|
||||||
@@ -114,6 +129,7 @@ function hex2pem ($data, $is_private_key=false) {
|
|||||||
}
|
}
|
||||||
return (string)$output;
|
return (string)$output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function base58_decode($base58)
|
function base58_decode($base58)
|
||||||
{
|
{
|
||||||
$alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
$alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
||||||
@@ -159,7 +175,8 @@ function hex2pem ($data, $is_private_key=false) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// converts PEM key to the base58 version used by ARO
|
// converts PEM key to the base58 version used by ARO
|
||||||
function pem2coin ($data) {
|
function pem2coin($data)
|
||||||
|
{
|
||||||
$data = str_replace("-----BEGIN PUBLIC KEY-----", "", $data);
|
$data = str_replace("-----BEGIN PUBLIC KEY-----", "", $data);
|
||||||
$data = str_replace("-----END PUBLIC KEY-----", "", $data);
|
$data = str_replace("-----END PUBLIC KEY-----", "", $data);
|
||||||
$data = str_replace("-----BEGIN EC PRIVATE KEY-----", "", $data);
|
$data = str_replace("-----BEGIN EC PRIVATE KEY-----", "", $data);
|
||||||
@@ -169,25 +186,26 @@ function pem2coin ($data) {
|
|||||||
|
|
||||||
|
|
||||||
return base58_encode($data);
|
return base58_encode($data);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts the key in base58 to PEM
|
// converts the key in base58 to PEM
|
||||||
function coin2pem ($data, $is_private_key=false) {
|
function coin2pem($data, $is_private_key = false)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
$data = base58_decode($data);
|
$data = base58_decode($data);
|
||||||
$data = base64_encode($data);
|
$data = base64_encode($data);
|
||||||
|
|
||||||
$dat = str_split($data, 64);
|
$dat = str_split($data, 64);
|
||||||
$data = implode("\n", $dat);
|
$data = implode("\n", $dat);
|
||||||
|
|
||||||
if($is_private_key) return "-----BEGIN EC PRIVATE KEY-----\n".$data."\n-----END EC PRIVATE KEY-----\n";
|
if ($is_private_key) {
|
||||||
|
return "-----BEGIN EC PRIVATE KEY-----\n".$data."\n-----END EC PRIVATE KEY-----\n";
|
||||||
|
}
|
||||||
return "-----BEGIN PUBLIC KEY-----\n".$data."\n-----END PUBLIC KEY-----\n";
|
return "-----BEGIN PUBLIC KEY-----\n".$data."\n-----END PUBLIC KEY-----\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// sign data with private key
|
// sign data with private key
|
||||||
function ec_sign($data, $key){
|
function ec_sign($data, $key)
|
||||||
|
{
|
||||||
// transform the base58 key format to PEM
|
// transform the base58 key format to PEM
|
||||||
$private_key = coin2pem($key, true);
|
$private_key = coin2pem($key, true);
|
||||||
|
|
||||||
@@ -201,13 +219,11 @@ function ec_sign($data, $key){
|
|||||||
|
|
||||||
// the signature will be base58 encoded
|
// the signature will be base58 encoded
|
||||||
return base58_encode($signature);
|
return base58_encode($signature);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function ec_verify($data, $signature, $key){
|
function ec_verify($data, $signature, $key)
|
||||||
|
{
|
||||||
|
|
||||||
// transform the base58 key to PEM
|
// transform the base58 key to PEM
|
||||||
$public_key = coin2pem($key);
|
$public_key = coin2pem($key);
|
||||||
|
|
||||||
@@ -218,51 +234,61 @@ function ec_verify($data, $signature, $key){
|
|||||||
$res = openssl_verify($data, $signature, $pkey, OPENSSL_ALGO_SHA256);
|
$res = openssl_verify($data, $signature, $pkey, OPENSSL_ALGO_SHA256);
|
||||||
|
|
||||||
|
|
||||||
if($res===1) return true;
|
if ($res === 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST data to an URL (usualy peer). The data is an array, json encoded with is sent as $_POST['data']
|
// POST data to an URL (usualy peer). The data is an array, json encoded with is sent as $_POST['data']
|
||||||
function peer_post($url, $data=array(),$timeout=60,$debug=false){
|
function peer_post($url, $data = [], $timeout = 60, $debug = false)
|
||||||
|
{
|
||||||
global $_config;
|
global $_config;
|
||||||
if($debug) echo "\nPeer post: $url\n";
|
if ($debug) {
|
||||||
|
echo "\nPeer post: $url\n";
|
||||||
|
}
|
||||||
$postdata = http_build_query(
|
$postdata = http_build_query(
|
||||||
array(
|
[
|
||||||
'data' => json_encode($data),
|
'data' => json_encode($data),
|
||||||
"coin"=>$_config['coin']
|
"coin" => $_config['coin'],
|
||||||
)
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
$opts = array('http' =>
|
$opts = [
|
||||||
array(
|
'http' =>
|
||||||
|
[
|
||||||
'timeout' => $timeout,
|
'timeout' => $timeout,
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
'header' => 'Content-type: application/x-www-form-urlencoded',
|
'header' => 'Content-type: application/x-www-form-urlencoded',
|
||||||
'content' => $postdata
|
'content' => $postdata,
|
||||||
)
|
],
|
||||||
);
|
];
|
||||||
|
|
||||||
$context = stream_context_create($opts);
|
$context = stream_context_create($opts);
|
||||||
|
|
||||||
$result = file_get_contents($url, false, $context);
|
$result = file_get_contents($url, false, $context);
|
||||||
if($debug) echo "\nPeer response: $result\n";
|
if ($debug) {
|
||||||
|
echo "\nPeer response: $result\n";
|
||||||
|
}
|
||||||
$res = json_decode($result, true);
|
$res = json_decode($result, true);
|
||||||
|
|
||||||
// the function will return false if something goes wrong
|
// the function will return false if something goes wrong
|
||||||
if($res['status']!="ok"||$res['coin']!=$_config['coin']) return false;
|
if ($res['status'] != "ok" || $res['coin'] != $_config['coin']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return $res['data'];
|
return $res['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// convers hex to base58
|
// convers hex to base58
|
||||||
function hex2coin($hex){
|
function hex2coin($hex)
|
||||||
|
{
|
||||||
$data = hex2bin($hex);
|
$data = hex2bin($hex);
|
||||||
return base58_encode($data);
|
return base58_encode($data);
|
||||||
}
|
}
|
||||||
// converts base58 to hex
|
|
||||||
function coin2hex($data){
|
|
||||||
|
|
||||||
|
// converts base58 to hex
|
||||||
|
function coin2hex($data)
|
||||||
|
{
|
||||||
$bin = base58_decode($data);
|
$bin = base58_decode($data);
|
||||||
return bin2hex($bin);
|
return bin2hex($bin);
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user