Update functions to PSR-2

This commit is contained in:
pxgamer
2018-05-29 15:38:24 +01:00
parent d8f1f4ea57
commit 636f4013dc

View File

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