|
1 | 1 | <?php |
2 | 2 | define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/bin/'); |
3 | 3 |
|
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 |
6 | 6 | // Authentication |
7 | | - if (empty($_POST['hash'])) { |
8 | | - if ($_POST['user'] != 'admin') { |
| 7 | + if (empty($hst_hash)) { |
| 8 | + if ($hst_user != 'admin') { |
9 | 9 | echo 'Error: authentication failed'; |
10 | 10 | exit; |
11 | 11 | } |
12 | 12 |
|
13 | | - $password = $_POST['password']; |
| 13 | + $password = $hst_password; |
14 | 14 | if (!isset($password)){ |
15 | 15 | echo 'Error: missing authentication'; |
16 | 16 | exit; |
|
52 | 52 | exit; |
53 | 53 | } |
54 | 54 | } else { |
55 | | - $key = '/usr/local/hestia/data/keys/' . basename($_POST['hash']); |
| 55 | + $key = '/usr/local/hestia/data/keys/' . basename($hst_hash); |
56 | 56 | if (file_exists($key) && is_file($key)) { |
57 | 57 | exec(HESTIA_CMD ."v-check-api-key ".escapeshellarg($key)." ".$v_ip, $output, $return_var); |
58 | 58 | unset($output); |
|
69 | 69 | } |
70 | 70 |
|
71 | 71 | // 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); |
83 | 82 | // Build query |
84 | 83 | $cmdquery = HESTIA_CMD.$cmd." "; |
85 | 84 | if(!empty($arg1)){ |
|
104 | 103 | // Check command |
105 | 104 | if ($cmd == "'v-make-tmp-file'") { |
106 | 105 | // 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"); |
109 | 108 | fclose($fp); |
110 | 109 | $return_var = 0; |
111 | 110 | } else { |
112 | 111 | // Run normal cmd query |
113 | 112 | exec ($cmdquery, $output, $return_var); |
114 | 113 | } |
115 | 114 |
|
116 | | - if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) { |
| 115 | + if ((!empty($hst_returncode)) && ($hst_returncode == 'yes')) { |
117 | 116 | echo $return_var; |
118 | 117 | } else { |
119 | 118 | if (($return_var == 0) && (empty($output))) { |
|
122 | 121 | echo implode("\n",$output)."\n"; |
123 | 122 | } |
124 | 123 | } |
| 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 | + |
125 | 134 | } else { |
126 | 135 | echo "Error: data received is null or invalid, check https://docs.hestiacp.com/admin_docs/rest_api.html"; |
127 | 136 | exit; |
128 | 137 | } |
| 138 | + |
| 139 | +?> |
0 commit comments