Skip to content

Commit e20c65a

Browse files
committed
setup arrayGet
1 parent 9cdf8e1 commit e20c65a

File tree

7 files changed

+66
-29
lines changed

7 files changed

+66
-29
lines changed

resources/autoload.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
require_once __DIR__ . "/lib/UnityWebhook.php";
2525
require_once __DIR__ . "/lib/UnityRedis.php";
2626
require_once __DIR__ . "/lib/UnityGithub.php";
27+
require_once __DIR__ . "/lib/exceptions/NoDieException.php";
2728
require_once __DIR__ . "/lib/exceptions/SSOException.php";
29+
require_once __DIR__ . "/lib/exceptions/ArrayKeyException.php";
2830

2931
require_once __DIR__ . "/config.php";
3032
require __DIR__ . "/init.php";

resources/lib/UnitySite.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use phpseclib3\Crypt\PublicKeyLoader;
66
use UnityWebPortal\lib\exceptions\NoDieException;
7+
use UnityWebPortal\lib\exceptions\ArrayKeyException;
78

89
class UnitySite
910
{
@@ -105,20 +106,47 @@ public static function shutdown()
105106
);
106107
}
107108

108-
public static function arrayGetOrBadRequest(array $array, ...$keys)
109+
public static function arrayGet($array, ...$keys)
109110
{
110111
$cursor = $array;
111112
$keysTraversed = [];
112113
foreach ($keys as $key) {
113114
array_push($keysTraversed, $key);
114115
if (!isset($cursor[$key])) {
115-
self::badRequest("array key not found: " . json_encode($keysTraversed));
116+
throw new ArrayKeyException(
117+
"key not found: \$array" .
118+
// [1, 2, "foo"] => [1][2]["foo"]
119+
implode("", array_map(fn($x) => json_encode([$x]), $keysTraversed))
120+
);
116121
}
117122
$cursor = $cursor[$key];
118123
}
119124
return $cursor;
120125
}
121126

127+
public static function getPostData(...$keys)
128+
{
129+
try {
130+
return self::arrayGet($_POST, ...$keys);
131+
} catch (ArrayKeyException $e) {
132+
self::badRequest(strval($e));
133+
}
134+
}
135+
136+
public static function getUploadedFileContents($filename, $do_delete_tmpfile_after_read = true)
137+
{
138+
try {
139+
$tmpfile_path = self::arrayGet($_FILES, $filename, "tmp_name");
140+
} catch (ArrayKeyException $e) {
141+
self::badRequest(strval($e));
142+
}
143+
$contents = file_get_contents($tmpfile_path);
144+
if ($do_delete_tmpfile_after_read) {
145+
unlink($tmpfile_path);
146+
}
147+
return $contents;
148+
}
149+
122150
// in firefox, the user can disable alert/confirm/prompt after the 2nd or 3rd popup
123151
// after I disable alerts, if I quit and reopen my browser, the alerts come back
124152
public static function alert(string $message)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib\exceptions;
4+
5+
class ArrayKeyException extends \Exception
6+
{
7+
}

test/functional/SSHKeyAddTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ private function addSshKeysImport(array $keys): void {
3131
__DIR__ . "/../../webroot/panel/account.php",
3232
["form_type" => "addKey", "add_type" => "import"]
3333
);
34+
$this->assertFalse(file_exists($tmp_path));
3435
} finally {
35-
unlink($tmp_path);
3636
unset($_FILES["keyfile"]);
3737
}
3838
}

test/phpunit-bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require_once __DIR__ . "/../resources/lib/UnityGithub.php";
1818
require_once __DIR__ . "/../resources/lib/exceptions/NoDieException.php";
1919
require_once __DIR__ . "/../resources/lib/exceptions/SSOException.php";
20+
require_once __DIR__ . "/../resources/lib/exceptions/ArrayKeyException.php";
2021

2122
$_SERVER["HTTP_HOST"] = "phpunit"; // used for config override
2223
require_once __DIR__ . "/../resources/config.php";

test/unit/UnitySiteTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use PHPUnit\Framework\Attributes\DataProvider;
7-
use UnityWebPortal\lib\exceptions\NoDieException;
7+
use UnityWebPortal\lib\exceptions\ArrayKeyException;
88
// use PHPUnit\Framework\Attributes\BackupGlobals;
99
// use PHPUnit\Framework\Attributes\RunTestsInSeparateProcess;
1010

@@ -81,7 +81,7 @@ public function testTestValidSSHKey(bool $expected, string $key)
8181
$this->assertEquals($expected, UnitySite::testValidSSHKey($key));
8282
}
8383

84-
public function testArrayGetOrBadRequestReturnsValueWhenKeyExists()
84+
public function testArrayGetReturnsValueWhenKeyExists()
8585
{
8686
$array = [
8787
"a" => [
@@ -90,50 +90,50 @@ public function testArrayGetOrBadRequestReturnsValueWhenKeyExists()
9090
]
9191
]
9292
];
93-
$result = UnitySite::arrayGetOrBadRequest($array, "a", "b", "c");
93+
$result = UnitySite::arrayGet($array, "a", "b", "c");
9494
$this->assertSame(123, $result);
9595
}
9696

97-
public function testArrayGetOrBadRequestReturnsArrayWhenTraversingPartially()
97+
public function testArrayGetReturnsArrayWhenTraversingPartially()
9898
{
9999
$array = [
100100
"foo" => [
101101
"bar" => "baz"
102102
]
103103
];
104-
$result = UnitySite::arrayGetOrBadRequest($array, "foo");
104+
$result = UnitySite::arrayGet($array, "foo");
105105
$this->assertSame(["bar" => "baz"], $result);
106106
}
107107

108-
public function testArrayGetOrBadRequestThrowsOnMissingKeyFirstLevel()
108+
public function testArrayGetThrowsOnMissingKeyFirstLevel()
109109
{
110110
$array = ["x" => 1];
111-
$this->expectException(NoDieException::class);
112-
$this->expectExceptionMessage('["y"]');
113-
UnitySite::arrayGetOrBadRequest($array, "y");
111+
$this->expectException(ArrayKeyException::class);
112+
$this->expectExceptionMessage('$array["y"]');
113+
UnitySite::arrayGet($array, "y");
114114
}
115115

116-
public function testArrayGetOrBadRequestThrowsOnMissingKeyNested()
116+
public function testArrayGetThrowsOnMissingKeyNested()
117117
{
118118
$array = ["a" => []];
119-
$this->expectException(NoDieException::class);
119+
$this->expectException(ArrayKeyException::class);
120120
// Should include both levels
121-
$this->expectExceptionMessage('["a","b"]');
122-
UnitySite::arrayGetOrBadRequest($array, "a", "b");
121+
$this->expectExceptionMessage('$array["a"]["b"]');
122+
UnitySite::arrayGet($array, "a", "b");
123123
}
124124

125-
public function testArrayGetOrBadRequestThrowsWhenValueIsNullButKeyNotSet()
125+
public function testArrayGetThrowsWhenValueIsNullButKeyNotSet()
126126
{
127127
$array = ["a" => null];
128-
$this->expectException(NoDieException::class);
129-
$this->expectExceptionMessage('["a"]');
130-
UnitySite::arrayGetOrBadRequest($array, "a");
128+
$this->expectException(ArrayKeyException::class);
129+
$this->expectExceptionMessage('$array["a"]');
130+
UnitySite::arrayGet($array, "a");
131131
}
132132

133-
public function testArrayGetOrBadRequestReturnsValueWhenValueIsFalsyButSet()
133+
public function testArrayGetReturnsValueWhenValueIsFalsyButSet()
134134
{
135135
$array = ["a" => 0];
136-
$result = UnitySite::arrayGetOrBadRequest($array, "a");
136+
$result = UnitySite::arrayGet($array, "a");
137137
$this->assertSame(0, $result);
138138
}
139139

webroot/panel/account.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@
77
$hasGroups = count($USER->getPIGroupGIDs()) > 0;
88

99
if ($_SERVER['REQUEST_METHOD'] == "POST") {
10-
switch (UnitySite::arrayGetOrBadRequest($_POST, "form_type")) {
10+
switch (UnitySite::getPostData("form_type")) {
1111
case "addKey":
1212
$keys = array();
13-
switch (UnitySite::arrayGetOrBadRequest($_POST, "add_type")) {
13+
switch (UnitySite::getPostData("add_type")) {
1414
case "paste":
15-
array_push($keys, UnitySite::arrayGetOrBadRequest($_POST, "key"));
15+
array_push($keys, UnitySite::getPostData("key"));
1616
break;
1717
case "import":
18-
$keyPath = UnitySite::arrayGetOrBadRequest($_FILES, "keyfile", "tmp_name");
19-
$key = file_get_contents($keyPath);
18+
$key = UnitySite::getUploadedFileContents("keyfile");
2019
array_push($keys, $key);
2120
break;
2221
case "generate":
23-
array_push($keys, UnitySite::arrayGetOrBadRequest($_POST, "gen_key"));
22+
array_push($keys, UnitySite::getPostData("gen_key"));
2423
break;
2524
case "github":
26-
$githubUsername = UnitySite::arrayGetOrBadRequest($_POST, "gh_user");
25+
$githubUsername = UnitySite::getPostData("gh_user");
2726
$githubKeys = $GITHUB->getSshPublicKeys($githubUsername);
2827
$keys = array_merge($keys, $githubKeys);
2928
break;

0 commit comments

Comments
 (0)