Skip to content

Commit 53e0bc1

Browse files
committed
shorten key before adding to message
1 parent 69b32a8 commit 53e0bc1

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

resources/lib/utils.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,14 @@ function str2int(string $x, ...$args): int
9595
throw new ValueError("not digits: $x");
9696
}
9797
}
98+
99+
/* example with 3 leading chars and 3 trailing chars: "foobarbaz" -> "foo...baz" */
100+
function shortenString(string $x, int $leading_chars, int $trailing_chars, string $ellipsis = "...")
101+
{
102+
if ($leading_chars + strlen($ellipsis) + $trailing_chars > strlen($x)) {
103+
return $x;
104+
}
105+
return substr($x, 0, $leading_chars) .
106+
$ellipsis .
107+
substr($x, -1 * $trailing_chars, $trailing_chars);
108+
}

webroot/panel/account.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,18 @@
4242
}
4343
$keys = array_map("trim", $keys);
4444
foreach ($keys as $key) {
45+
$keyShort = shortenString($key, 10, 10);
4546
try {
4647
$keyWasAdded = $USER->addSSHKey($key, $OPERATOR);
4748
} catch (NoKeyLoadedException) {
48-
UnityHTTPD::messageError("SSH Key Not Added: Invalid", $key);
49+
UnityHTTPD::messageError("SSH Key Not Added: Invalid", $keyShort);
4950
UnityHTTPD::redirect();
5051
}
5152
if ($keyWasAdded) {
52-
UnityHTTPD::messageSuccess("SSH Key Added", $key);
53+
UnityHTTPD::messageSuccess("SSH Key Added", $keyShort);
5354
UnityHTTPD::redirect();
5455
} else {
55-
UnityHTTPD::messageInfo("SSH Key Not Added: Already Exists", $key);
56+
UnityHTTPD::messageInfo("SSH Key Not Added: Already Exists", $keyShort);
5657
UnityHTTPD::redirect();
5758
}
5859
}

0 commit comments

Comments
 (0)