|
6 | 6 |
|
7 | 7 | require_once $LOC_HEADER;
|
8 | 8 |
|
9 |
| -$invalid_ssh_dialogue = "<script type='text/javascript'> |
10 |
| -alert('Invalid SSH key. Please verify your public key file is valid.'); |
11 |
| -</script>"; |
12 |
| - |
13 | 9 | if ($_SERVER['REQUEST_METHOD'] == "POST") {
|
14 |
| - switch ($_POST["form_type"]) { |
| 10 | + switch (UnitySite::arrayGetOrBadRequest($_POST, "form_type")) { |
15 | 11 | case "addKey":
|
16 |
| - $added_keys = array(); |
17 |
| - |
18 |
| - switch ($_POST["add_type"]) { |
| 12 | + $keys = array(); |
| 13 | + switch (UnitySite::arrayGetOrBadRequest($_POST, "add_type")) { |
19 | 14 | case "paste":
|
20 |
| - $key = $_POST["key"]; |
21 |
| - if (UnitySite::testValidSSHKey($key)) { |
22 |
| - array_push($added_keys, $key); |
23 |
| - } else { |
24 |
| - echo $invalid_ssh_dialogue; |
25 |
| - } |
| 15 | + array_push($keys, UnitySite::arrayGetOrBadRequest($_POST, "key")); |
26 | 16 | break;
|
27 | 17 | case "import":
|
28 |
| - $keyfile = $_FILES["keyfile"]["tmp_name"]; |
29 |
| - $key = file_get_contents($keyfile); |
30 |
| - if (UnitySite::testValidSSHKey($key)) { |
31 |
| - array_push($added_keys, $key); |
32 |
| - } else { |
33 |
| - echo $invalid_ssh_dialogue; |
34 |
| - } |
| 18 | + $keyPath = UnitySite::arrayGetOrBadRequest($_FILES, "keyfile", "tmp_name"); |
| 19 | + $key = file_get_contents($keyPath); |
| 20 | + array_push($keys, $key); |
35 | 21 | break;
|
36 | 22 | case "generate":
|
37 |
| - array_push($added_keys, $_POST["gen_key"]); |
| 23 | + array_push($keys, UnitySite::arrayGetOrBadRequest($_POST, "gen_key")); |
38 | 24 | break;
|
39 | 25 | case "github":
|
40 |
| - $gh_user = $_POST["gh_user"]; |
41 |
| - $keys = $GITHUB->getSshPublicKeys($gh_user); |
42 |
| - foreach ($keys as $key) { |
43 |
| - if (UnitySite::testValidSSHKey($key)) { |
44 |
| - array_push($added_keys, $key); |
45 |
| - } |
46 |
| - } |
| 26 | + $githubUsername = UnitySite::arrayGetOrBadRequest($_POST, "gh_user"); |
| 27 | + $githubKeys = $GITHUB->getSshPublicKeys($githubUsername); |
| 28 | + $keys = array_merge($keys, $githubKeys); |
47 | 29 | break;
|
48 | 30 | }
|
49 |
| - |
50 |
| - if (!empty($added_keys)) { |
51 |
| - $added_keys = UnitySite::removeTrailingWhitespace($added_keys); |
52 |
| - $totalKeys = array_merge($USER->getSSHKeys(), $added_keys); |
53 |
| - $USER->setSSHKeys($totalKeys, $OPERATOR); |
| 31 | + if (!empty($keys)) { |
| 32 | + $keys = array_map("trim", $keys); |
| 33 | + $validKeys = array_filter($keys, ["UnityWebPortal\lib\UnitySite", "testValidSSHKey"]); |
| 34 | + $USER->setSSHKeys(array_merge($USER->getSSHKeys(), $validKeys)); |
| 35 | + if (count($keys) != count($validKeys)) { |
| 36 | + UnitySite::alert("invalid SSH key"); |
| 37 | + } |
54 | 38 | }
|
55 | 39 | break;
|
56 | 40 | case "delKey":
|
|
0 commit comments