Skip to content

Commit 0dd4e8a

Browse files
committed
json ajax
1 parent b89829c commit 0dd4e8a

File tree

5 files changed

+16
-30
lines changed

5 files changed

+16
-30
lines changed

webroot/admin/ajax/get_page_contents.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010

1111
$pageid = UnityHTTPD::getQueryParameter("pageid");
1212
$page = $SQL->getPage($pageid);
13-
echo $page["content"];
13+
header('Content-Type: application/json; charset=utf-8');
14+
echo jsonEncode(["content" => $page["content"]]);

webroot/admin/content.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
$("#pageForm > select[name=pageSel]").change(function(e) {
5858
$.ajax({
5959
url: `${url}?pageid=` + $(this).val(),
60+
dataType: "json",
6061
success: function(result) {
61-
mainEditor.setData(result);
62+
mainEditor.setData(result.content);
6263
}});
6364
});
6465

webroot/js/ajax/ssh_generate.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@
55
use phpseclib3\Crypt\EC;
66
use UnityWebPortal\lib\UnityHTTPD;
77

8-
echo "<pre>";
9-
108
$private = EC::createKey('Ed25519');
119
$public = $private->getPublicKey();
12-
13-
echo "<section class='pubKey'>";
14-
echo $public->toString('OpenSSH');
15-
echo "</section>";
16-
echo "<section class='privKey'>";
10+
$public_str = $public->toString('OpenSSH');
1711
if (UnityHTTPD::getQueryParameter("type", false) == "ppk") {
18-
echo $private->toString('PuTTY');
12+
$private_str = $private->toString('PuTTY');
1913
} else {
20-
echo $private->toString('OpenSSH');
14+
$private_str = $private->toString('OpenSSH');
2115
}
22-
echo "</section>";
23-
24-
echo "</pre>";
16+
header('Content-Type: application/json; charset=utf-8');
17+
echo jsonEncode(["public" => $public_str, "private" => $private_str]);

webroot/js/ajax/ssh_validate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
require_once __DIR__ . "/../../../vendor/autoload.php";
44
require_once __DIR__ . "/../../../resources/lib/utils.php";
5-
6-
echo testValidSSHKey($_POST["key"]) ? "true" : "false";
5+
header('Content-Type: application/json; charset=utf-8');
6+
echo jsonEncode(["is_valid" => testValidSSHKey($_POST["key"])]);

webroot/panel/modal/new_key.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,12 @@
6969
});
7070

7171
function generateKey(type) {
72-
var pubSection = "<section class='pubKey'>";
73-
var privSection = "<section class='privKey'>";
74-
var endingSection = "</section>";
75-
7672
$.ajax({
7773
url: "<?php echo getURL("js/ajax/ssh_generate.php"); ?>?type=" + type,
74+
dataType: "json",
7875
success: function(result) {
79-
var pubKey = result.substr(result.indexOf(pubSection) + pubSection.length,
80-
result.indexOf(endingSection) - result.indexOf(pubSection) - pubSection.length);
81-
var privKey = result.substr(result.indexOf(privSection) + privSection.length,
82-
result.indexOf(endingSection, result.indexOf(endingSection) + 1) -
83-
result.indexOf(privSection) - privSection.length);
84-
$("input[type=hidden][name=gen_key]").val(pubKey);
85-
downloadFile(privKey, "privkey." + type); // Force download of private key
86-
76+
$("input[type=hidden][name=gen_key]").val(result.public);
77+
downloadFile(result.private, "privkey." + type); // Force download of private key
8778
$("#newKeyform").submit();
8879
}
8980
});
@@ -104,13 +95,13 @@ function generateKey(type) {
10495
var key = $(this).val();
10596
$.ajax({
10697
url: "<?php echo getURL("js/ajax/ssh_validate.php"); ?>",
98+
dataType: "json",
10799
type: "POST",
108100
data: {
109101
key: key
110102
},
111103
success: function(result) {
112-
const res = result.replace(key, "");
113-
if (res == "true") {
104+
if (result.is_valid) {
114105
$("input[id=add-key]").prop("disabled", false);
115106
$("textarea[name=key]").css("box-shadow", "none");
116107
} else {

0 commit comments

Comments
 (0)