-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.php
62 lines (54 loc) · 1.86 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
require_once('vendor/autoload.php');
use Denpa\Bitcoin\Client as DogecClient;
include 'config.php';
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
$dogec_rpc = new DogecClient('http://' . $username . ':' . $password . '@' . $host . ':' . $port);
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MYSQL database" . $mysqli->connect_errno;
die();
}
$exists = $mysqli->query("SHOW TABLES LIKE 'invoice_status'")->num_rows == 1;
if (!$exists) {
echo "Table does not exist. Make sure to import sql file into database";
return;
}
if ($_GET) {
if (array_key_exists("address", $_GET) && !array_key_exists("api_key", $_GET)) {
$address = preg_replace('/[^A-Za-z0-9\-]/', '', $_GET['address']);
$valid_address = $dogec_rpc->validateAddress($address)['isvalid'];
if (!$valid_address) {
echo json_encode([
"status"=>400,
"message"=>"Invald Address"
]);
return;
}
$exist_address = $mysqli->query("SELECT `key` FROM api_keys WHERE dogec_addr = '$address'")->num_rows;
if ($exist_address == 1) {
echo json_encode([
"status"=>400,
"message"=>"Address already exists"
]);
return;
}
else {
$api_key = md5(uniqid($prefix=$address));
$query = "INSERT INTO api_keys(`dogec_addr`, `key`) VALUES('$address', '$api_key')";
$mysqli->query($query);
echo json_encode([
"status"=>200,
"api_key"=>$api_key
]);
return;
}
}
else {
echo json_encode([
"status"=>400,
"message"=>"Invalid Request"
]);
}
}