Skip to content

Commit 6dbdc13

Browse files
author
Daniel Aléxis
authored
API JSON support (hestiacp#1465)
* API JSON support * Fix small errors
1 parent 6ccd09f commit 6dbdc13

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/hst_autocompile.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
# For building from local source folder use "~localsrc" keyword as hesia branch name,
66
# and the script will not try to download the arhive from github, since '~' char is
77
# not accepted in branch name.
8-
# -> ./hst_autocompile.sh --hestia '~localsrc' 'n'
8+
# Compile but dont install -> ./hst_autocompile.sh --hestia '~localsrc' 'n'
9+
# Compile but install -> ./hst_autocompile.sh --hestia '~localsrc' 'y'
910

1011
# Clear previous screen output
1112
clear

web/api/index.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22
define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/bin/');
33

4-
if (isset($_POST['user']) || isset($_POST['hash'])) {
5-
4+
function api($hst_hash, $hst_user, $hst_password, $hst_returncode, $hst_cmd, $hst_arg1, $hst_arg2, $hst_arg3, $hst_arg4, $hst_arg5, $hst_arg6, $hst_arg7, $hst_arg8, $hst_arg9){
5+
//This exists, so native JSON can be used without the repeating the code twice, so future code changes are easier and dont need to be replicated twice
66
// Authentication
7-
if (empty($_POST['hash'])) {
8-
if ($_POST['user'] != 'admin') {
7+
if (empty($hst_hash)) {
8+
if ($hst_user != 'admin') {
99
echo 'Error: authentication failed';
1010
exit;
1111
}
1212

13-
$password = $_POST['password'];
13+
$password = $hst_password;
1414
if (!isset($password)){
1515
echo 'Error: missing authentication';
1616
exit;
@@ -52,7 +52,7 @@
5252
exit;
5353
}
5454
} else {
55-
$key = '/usr/local/hestia/data/keys/' . basename($_POST['hash']);
55+
$key = '/usr/local/hestia/data/keys/' . basename($hst_hash);
5656
if (file_exists($key) && is_file($key)) {
5757
exec(HESTIA_CMD ."v-check-api-key ".escapeshellarg($key)." ".$v_ip, $output, $return_var);
5858
unset($output);
@@ -69,17 +69,16 @@
6969
}
7070

7171
// Prepare arguments
72-
if (isset($_POST['cmd'])) $cmd = escapeshellarg($_POST['cmd']);
73-
if (isset($_POST['arg1'])) $arg1 = escapeshellarg($_POST['arg1']);
74-
if (isset($_POST['arg2'])) $arg2 = escapeshellarg($_POST['arg2']);
75-
if (isset($_POST['arg3'])) $arg3 = escapeshellarg($_POST['arg3']);
76-
if (isset($_POST['arg4'])) $arg4 = escapeshellarg($_POST['arg4']);
77-
if (isset($_POST['arg5'])) $arg5 = escapeshellarg($_POST['arg5']);
78-
if (isset($_POST['arg6'])) $arg6 = escapeshellarg($_POST['arg6']);
79-
if (isset($_POST['arg7'])) $arg7 = escapeshellarg($_POST['arg7']);
80-
if (isset($_POST['arg8'])) $arg8 = escapeshellarg($_POST['arg8']);
81-
if (isset($_POST['arg9'])) $arg9 = escapeshellarg($_POST['arg9']);
82-
72+
if (isset($hst_cmd)) $cmd = escapeshellarg($hst_cmd);
73+
if (isset($hst_arg1)) $arg1 = escapeshellarg($hst_arg1);
74+
if (isset($hst_arg2)) $arg2 = escapeshellarg($hst_arg2);
75+
if (isset($hst_arg3)) $arg3 = escapeshellarg($hst_arg3);
76+
if (isset($hst_arg4)) $arg4 = escapeshellarg($hst_arg4);
77+
if (isset($hst_arg5)) $arg5 = escapeshellarg($hst_arg5);
78+
if (isset($hst_arg6)) $arg6 = escapeshellarg($hst_arg6);
79+
if (isset($hst_arg7)) $arg7 = escapeshellarg($hst_arg7);
80+
if (isset($hst_arg8)) $arg8 = escapeshellarg($hst_arg8);
81+
if (isset($hst_arg9)) $arg9 = escapeshellarg($hst_arg9);
8382
// Build query
8483
$cmdquery = HESTIA_CMD.$cmd." ";
8584
if(!empty($arg1)){
@@ -104,16 +103,16 @@
104103
// Check command
105104
if ($cmd == "'v-make-tmp-file'") {
106105
// Used in DNS Cluster
107-
$fp = fopen($_POST['arg2'], 'w');
108-
fwrite($fp, $_POST['arg1']."\n");
106+
$fp = fopen($hst_arg2, 'w');
107+
fwrite($fp, $hst_arg1."\n");
109108
fclose($fp);
110109
$return_var = 0;
111110
} else {
112111
// Run normal cmd query
113112
exec ($cmdquery, $output, $return_var);
114113
}
115114

116-
if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
115+
if ((!empty($hst_returncode)) && ($hst_returncode == 'yes')) {
117116
echo $return_var;
118117
} else {
119118
if (($return_var == 0) && (empty($output))) {
@@ -122,7 +121,19 @@
122121
echo implode("\n",$output)."\n";
123122
}
124123
}
124+
}
125+
126+
if (isset($_POST['user']) || isset($_POST['hash'])) {
127+
128+
api($_POST['hash'], $_POST['user'], $_POST['password'], $_POST['returncode'], $_POST['cmd'], $_POST['arg1'], $_POST['arg2'], $_POST['arg3'], $_POST['arg4'], $_POST['arg5'], $_POST['arg6'], $_POST['arg7'], $_POST['arg8'], $_POST['arg9']);
129+
130+
} else if (json_decode(file_get_contents("php://input"), true) != NULL){ //JSON POST support
131+
$json_data = json_decode(file_get_contents("php://input"), true);
132+
api($json_data['hash'], $json_data['user'], $json_data['password'], $json_data['returncode'], $json_data['cmd'], $json_data['arg1'], $json_data['arg2'], $json_data['arg3'], $json_data['arg4'], $json_data['arg5'], $json_data['arg6'], $json_data['arg7'], $json_data['arg8'], $json_data['arg9']);
133+
125134
} else {
126135
echo "Error: data received is null or invalid, check https://docs.hestiacp.com/admin_docs/rest_api.html";
127136
exit;
128137
}
138+
139+
?>

0 commit comments

Comments
 (0)