forked from MediaFrontPage/mediafrontpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonservice.php
executable file
·101 lines (91 loc) · 3.08 KB
/
jsonservice.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
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
97
98
99
100
101
<?php
require_once "config.php";
require_once "functions.php";
function error_array($code, $message) {
return array ( "error" => array ( "code" => $code, "message" => $message));
}
$jsonRequest = urldecode(file_get_contents("php://input"));
$arrRequest = json_decode($jsonRequest, true);
if(!empty($DEBUG) && $DEBUG && !empty($arrRequest['jsonrpc']) && ($arrRequest['jsonrpc'] == "2.0") && !empty($xbmcjsonserviceoverride)) {
// Use XBMC test harness
if(!empty($arrRequest['method']) && file_exists($arrRequest['method'])) {
$response = file_get_contents($arrRequest['method']);
} else {
//json rpc call procedure
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $xbmcjsonserviceoverride);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonRequest);
$response = curl_exec($ch);
curl_close($ch);
}
echo $response;
} else {
if(!empty($arrRequest)) {
switch ($arrRequest["method"]) {
case "SaveLayout":
$arrResult = save_layout($arrRequest);
break;
case "GetWidgets":
$arrResult = get_widgets($arrRequest);
break;
case "XBMCPassthough":
$arrResult = xbmc_passthough($arrRequest);
break;
default:
$arrResult = error_array(-32601, "Method not found.");
}
} else {
$arrResult = error_array(-32700, "Parse error.");
}
echo json_encode($arrResult);
}
function save_layout($arrRequest) {
if(!empty($arrRequest["params"]) && is_array($arrRequest["params"])) {
$layoutfile = "layout.php";
if(is_writable($layoutfile)) {
$layout_code_string .= "<?php\n".'$arrLayout = '.return_array_code($arrRequest["params"]).";\n?>\n";
if ($handle = fopen($layoutfile, 'w')) {
if(fwrite($handle, $layout_code_string)) {
$arrResult = array ( "result" => array ( "success" => true, "message" => "Layout file ($layoutfile) saved." ) );
fclose($handle);
} else {
$arrResult = error_array(-32502, "Problem writing to file ($layoutfile).");
}
} else {
$arrResult = error_array(-32501, "Problem opening file ($layoutfile).");
}
} else {
$arrResult = error_array(-32500, "File not writeable.");
}
} else {
$arrResult = error_array(-32602, "Invalid parameters.");
}
return $arrResult;
}
function get_widgets($arrRequest) {
if(!empty($arrRequest["params"]) && is_array($arrRequest["params"])) {
$wIndex = array();
foreach (glob("widgets/*.php") as $filename) {
include_once $filename;
}
$arrResult = array ( "result" => array ( "widgets" => $wIndex ) );
} else {
$arrResult = error_array(-32602, "Invalid parameters.");
}
return $arrResult;
}
function xbmc_passthough($arrRequest) {
if(!empty($arrRequest["params"]) && is_array($arrRequest["params"])) {
$request = json_encode($arrRequest["params"]);
$arrResult = jsoncall($request);
if(empty($arrResult) || !is_array($arrResult)) {
$arrResult = error_array(-32503, "Invalid response.");
}
} else {
$arrResult = error_array(-32602, "Invalid parameters.");
}
return $arrResult;
}
?>