Skip to content

Commit 1706d1e

Browse files
authored
use JSON for ajax (#412)
1 parent b89829c commit 1706d1e

File tree

10 files changed

+35
-36
lines changed

10 files changed

+35
-36
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
This will enforce conditions even in production.
1717
- No code should call `json_encode()`, instead `\jsonEncode()`.
1818
This will throw errors and escape slashes by default.
19+
- No code should call `json_decode()`, instead `\jsonDecode()`.
20+
This will throw an exception rather than returning `null`.
1921
- No code should call `mb_convert_encoding()`, instead `\mbConvertEncoding()`.
2022
This will throw an exception rather than returning `false`.
2123
- No code should call `mb_detect_encoding()`, instead `\mbDetectEncoding()`.

resources/lib/UnityGithub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function getSshPublicKeys(string $username): array
1818
if ($curl_output === false) {
1919
throw new CurlException(curl_error($curl));
2020
}
21-
$keys = json_decode($curl_output, false);
21+
$keys = jsonDecode($curl_output, false);
2222
curl_close($curl);
2323

2424
// normally returns array of objects each with a ->key attribute

resources/lib/utils.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ function jsonEncode(mixed $value, int $flags = 0, int $depth = 512): string
3030
return json_encode($value, $flags, $depth);
3131
}
3232

33+
function jsonDecode(...$args): mixed
34+
{
35+
$output = json_decode(...$args);
36+
if ($output === null) {
37+
throw new Exception("json_decode returned null");
38+
}
39+
return $output;
40+
}
41+
3342
function mbConvertEncoding(
3443
string $string,
3544
string $to_encoding,

test/assert-utils-used.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fi
88
declare -A utils=(
99
["assert"]="ensure"
1010
["json_encode"]="jsonEncode"
11+
["json_decode"]="jsonDecode"
1112
["mb_detect_encoding"]="mbDetectEncoding"
1213
["mb_convert_encoding"]="mbConvertEncoding"
1314
["intval"]="str2int"

test/unit/AjaxSshValidateTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ public function testSshValidate(bool $is_valid, string $pubkey)
2424
$_POST["key"] = $pubkey;
2525
ob_start();
2626
include __DIR__ . "/../../webroot/js/ajax/ssh_validate.php";
27-
$output = ob_get_clean();
28-
if ($is_valid) {
29-
$this->assertEquals("true", $output);
30-
} else {
31-
$this->assertEquals("false", $output);
32-
}
27+
$output_str = ob_get_clean();
28+
$output = jsonDecode($output_str, true);
29+
$this->assertEquals($is_valid, $output["is_valid"]);
3330
}
3431
}

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
require_once __DIR__ . "/../../../vendor/autoload.php";
44
require_once __DIR__ . "/../../../resources/lib/utils.php";
5+
require_once __DIR__ . "/../../../resources/lib/UnityHTTPD.php";
56

6-
echo testValidSSHKey($_POST["key"]) ? "true" : "false";
7+
use UnityWebPortal\lib\UnityHTTPD;
8+
9+
header('Content-Type: application/json; charset=utf-8');
10+
echo jsonEncode(["is_valid" => testValidSSHKey(UnityHTTPD::getPostData("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)