forked from hugowetterberg/jsonrpc_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonrpc_server.module
96 lines (81 loc) · 2.6 KB
/
jsonrpc_server.module
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Implements hook_server_info().
*/
function jsonrpc_server_server_info() {
return array(
'name' => 'JSON-RPC',
'path' => 'json-rpc',
'settings' => array(
'file' => array('inc', 'jsonrpc_server'),
'form' => '_jsonrpc_server_settings',
'submit' => '_jsonrpc_server_settings_submit',
),
);
}
/**
* Implements hook_server().
*/
function jsonrpc_server_server() {
// Remove Devel shutdown.
$GLOBALS['devel_shutdown'] = FALSE;
// Get the endpoint.
$endpoint_name = services_get_server_info('endpoint');
$endpoint = services_endpoint_load($endpoint_name);
$settings = isset($endpoint->server_settings[$endpoint->server]) ? $endpoint->server_settings[$endpoint->server] : NULL;
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'POST') {
// Strip out charset if is there.
list($content_type) = explode(';', $_SERVER['CONTENT_TYPE'], 2);
if (in_array($content_type, array('application/json', 'application/json-rpc', 'application/jsonrequest'))) {
// We'll use the inputstream module if it's installed because
// otherwise it's only possible to read the input stream once.
// And other parts of services or drupal might want to access it.
if (module_exists('inputstream')) {
$body = file_get_contents('drupal://input');
}
else {
$body = file_get_contents('php://input');
}
$input = drupal_json_decode($body);
}
}
// Allow GET only if was enabled.
elseif ($method == 'GET' && $settings['allow_get_requests']) {
/**
* Collapse multiple parameters with the same name in arrays.
*
* @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#GetProcedureCall
*/
$params = array();
foreach (explode('&', $_SERVER['QUERY_STRING']) as $pair) {
list($key, $value) = explode('=', $pair, 2);
$key = str_replace('[]', '', $key);
if (isset($params[$key])) {
if (!is_array($params[$key])) {
$params[$key] = array($params[$key]);
}
$params[$key][] = html_entity_decode($value);
}
else {
$params[$key] = html_entity_decode($value);
}
}
$input = array(
'version' => '1.1',
'method' => basename($_GET['q']),
'params' => $params,
);
}
// Some cleanup on "method" may be necessary.
$input['method'] = trim($input['method'], ' "');
// Start a new server instance.
$server = new JsonRpcServer($input, $method);
try {
// Invoke the server handler.
return $server->handle();
}
catch (ServicesException $e) {
return $server->buildResponse($e->getData());
}
}