-
Notifications
You must be signed in to change notification settings - Fork 11
/
api.php
61 lines (59 loc) · 1.98 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
<?php
/**
* JSON RPC SERVER to process all calls for the stored Blockchain
* Version : 0.1.0
* Last Updated : 5/13/2015
*/
require_once(__DIR__ . '/inc/rb.php');
require_once(__DIR__ . '/inc/jsonrpc.php');
require_once(__DIR__ . '/api/include.php');
require_once(__DIR__ . '/config.php');
define("VERSION","0.1.0");
class jsonRPCServer {
public static function handle($object) {
// checks if a JSON-RCP request has been received
if (
$_SERVER['REQUEST_METHOD'] != 'POST' ||
empty($_SERVER['CONTENT_TYPE']) ||
$_SERVER['CONTENT_TYPE'] != 'application/json'
) {
// This is not a JSON-RPC request
return false;
}
// reads the input data
$request = json_decode(file_get_contents('php://input'),true);
// executes the task on local object
try {
if ($result = @call_user_func_array(array($object,$request['method']),$request['params'])) {
$response = array (
'id' => $request['id'],
'result' => $result,
'error' => NULL
);
} else {
$response = array (
'id' => $request['id'],
'result' => NULL,
'request' => $request,
'error' => 'unknown method or incorrect parameters '
);
}
} catch (Exception $e) {
$response = array (
'id' => $request['id'],
'result' => NULL,
'error' => $e->getMessage()
);
}
// output the response
if (!empty($request['id'])) { // notifications don't want response
header('content-type: application/json');
echo json_encode($response);
}
// finish
return true;
}
}
$getAPI = call_user_func(array($_GET['system'], "api"));
jsonRPCServer::handle($getAPI)
or print 'no request';