Skip to content

Commit 9f30fb6

Browse files
committed
replace strlen hack with json
1 parent d1c4f66 commit 9f30fb6

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

webroot/js/ajax/ssh_generate.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44

55
use phpseclib3\Crypt\RSA;
66

7-
echo "<pre>";
8-
97
$private = RSA::createKey(2048);
108
$public = $private->getPublicKey();
119

12-
echo "<section class='pubKey'>";
13-
echo $public->toString('OpenSSH');
14-
echo "</section>";
15-
echo "<section class='privKey'>";
16-
if (isset($_GET["type"]) && $_GET["type"] == "ppk") {
17-
echo $private->toString('PuTTY');
18-
} else {
19-
echo $private;
10+
switch ($_GET["type"]) {
11+
case "key":
12+
break;
13+
case "ppk":
14+
$private = $private->toString("PuTTY");
15+
break;
16+
default:
17+
$SITE->bad_request("invalid type '" . $_GET["type"] . "'");
2018
}
21-
echo "</section>";
2219

23-
echo "</pre>";
20+
echo json_encode([
21+
"pubkey" => $public->toString('OpenSSH'),
22+
"privkey" => $private
23+
]);
24+
25+

webroot/panel/modal/new_key.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,13 @@
6464
});
6565

6666
function generateKey(type) {
67-
var pubSection = "<section class='pubKey'>";
68-
var privSection = "<section class='privKey'>";
69-
var endingSection = "</section>";
70-
7167
$.ajax({
7268
url: "<?php echo $CONFIG["site"]["prefix"]; ?>/js/ajax/ssh_generate.php?type=" + type,
7369
success: function(result) {
74-
var pubKey = result.substr(result.indexOf(pubSection) + pubSection.length,
75-
result.indexOf(endingSection) - result.indexOf(pubSection) - pubSection.length);
76-
var privKey = result.substr(result.indexOf(privSection) + privSection.length,
77-
result.indexOf(endingSection, result.indexOf(endingSection) + 1) -
78-
result.indexOf(privSection) - privSection.length);
79-
$("input[type=hidden][name=gen_key]").val(pubKey);
80-
downloadFile(privKey, "privkey." + type); // Force download of private key
81-
70+
success: function(outputJsonStr) {
71+
const output = JSON.parse(outputJsonStr);
72+
$("input[type=hidden][name=gen_key]").val(output.pubkey);
73+
downloadFile(output.privkey, `privkey.${type}`); // Force download of private key
8274
$("#newKeyform").submit();
8375
}
8476
});
@@ -104,8 +96,7 @@ function generateKey(type) {
10496
key: key
10597
},
10698
success: function(result) {
107-
const res = result.replace(key, "");
108-
if (res == "true") {
99+
if (result == "true") {
109100
$("input[id=add-key]").prop("disabled", false);
110101
$("textarea[name=key]").css("box-shadow", "none");
111102
} else {

0 commit comments

Comments
 (0)