Skip to content

Commit 42091f8

Browse files
committed
add functions errorLog, badRequest
1 parent 8ff1cc7 commit 42091f8

File tree

12 files changed

+80
-16
lines changed

12 files changed

+80
-16
lines changed

resources/lib/UnitySite.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace UnityWebPortal\lib;
44

55
use phpseclib3\Crypt\PublicKeyLoader;
6-
use UnityWebPortal\lib\exceptions\TestingDieException;
6+
use UnityWebPortal\lib\exceptions\PhpUnitNoDieException;
77

88
class UnitySite
99
{
1010
public static function die($x)
1111
{
12-
if ($GLOBALS["PHPUNIT_NO_DIE_PLEASE"] ?? false) {
12+
if (@$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] == true) {
1313
throw new PhpUnitNoDieException(strval($x));
1414
} else {
1515
\die($x);
@@ -20,10 +20,39 @@ public static function redirect($destination)
2020
{
2121
if ($_SERVER["PHP_SELF"] != $destination) {
2222
header("Location: $destination");
23-
die("Redirect failed, click <a href='$destination'>here</a> to continue.");
23+
self::die("Redirect failed, click <a href='$destination'>here</a> to continue.");
2424
}
2525
}
2626

27+
public static function headerResponseCode(int $code)
28+
{
29+
$responseCodeMessage = @http_response_code($code) ?? "";
30+
$msg = $_SERVER["SERVER_PROTOCOL"] . " " . strval($code) . " " . $responseCodeMessage;
31+
header($msg, true, $ncode);
32+
}
33+
34+
public static function errorLog(string $title, string $message)
35+
{
36+
error_log(
37+
"$title: " . json_encode(
38+
[
39+
"message" => $message,
40+
"REMOTE_USER" => @$_SERVER["REMOTE_USER"], // "@": allow null default value
41+
"REMOTE_ADDR" => @$_SERVER["REMOTE_ADDR"], // "@": allow null default value
42+
// getTrace() is a list but the JSON is very verbose
43+
"trace" => explode(PHP_EOL, (new \Exception())->getTraceAsString())
44+
]
45+
)
46+
);
47+
}
48+
49+
public static function badRequest($message)
50+
{
51+
self::headerResponseCode(400);
52+
self::errorLog("bad request", $message);
53+
self::die($message);
54+
}
55+
2756
public static function removeTrailingWhitespace($arr)
2857
{
2958
$out = array();

test/phpunit-bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
require_once __DIR__ . "/../resources/lib/UnityWebhook.php";
1515
require_once __DIR__ . "/../resources/lib/UnityRedis.php";
1616
require_once __DIR__ . "/../resources/lib/UnityGithub.php";
17+
require_once __DIR__ . "/../resources/lib/exceptions/PhpUnitNoDieException.php";
18+
19+
$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] = true;
1720

1821
global $HTTP_HEADER_TEST_INPUTS;
1922
$HTTP_HEADER_TEST_INPUTS = [

test/unit/UnitySiteTest.php

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

55
use PHPUnit\Framework\TestCase;
66
use PHPUnit\Framework\Attributes\DataProvider;
7+
// use PHPUnit\Framework\Attributes\BackupGlobals;
8+
// use PHPUnit\Framework\Attributes\RunTestsInSeparateProcess;
79

810
class UnitySiteTest extends TestCase
911
{
@@ -75,8 +77,28 @@ public static function SSHKeyProvider()
7577
#[DataProvider("SSHKeyProvider")]
7678
public function testTestValidSSHKey(bool $expected, string $key)
7779
{
78-
$SITE = new UnitySite();
79-
$this->assertEquals($expected, $SITE->testValidSSHKey($key));
80+
$this->assertEquals($expected, UnitySite::testValidSSHKey($key));
8081
}
8182

83+
// I suspect that this test could have unexpected interactions with other tests.
84+
// even with RunTestsInSeparateProcess and BackupGlobalState, http_response_code()
85+
// still persists to the next test. header("HTTP/1.1 false") puts it back to its
86+
// initial value, but this is a hack and does not inspire confidence.
87+
// #[BackupGlobals(true)]
88+
// #[RunTestsInSeparateProcess]
89+
// public function testHeaderResponseCode()
90+
// {
91+
// $this->assertEquals(false, http_response_code());
92+
// $this->assertArrayNotHasKey("SERVER_PROTOCOL", $_SERVER);
93+
// try {
94+
// $_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1";
95+
// UnitySite::headerResponseCode(400);
96+
// $this->assertEquals(400, http_response_code());
97+
// UnitySite::headerResponseCode(401);
98+
// $this->assertEquals(401, http_response_code());
99+
// } finally {
100+
// unset($_SERVER["SERVER_PROTOCOL"]);
101+
// header("HTTP/1.1 false");
102+
// }
103+
// }
82104
}

webroot/admin/ajax/get_group_members.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
require_once __DIR__ . "/../../../resources/autoload.php";
44

55
use UnityWebPortal\lib\UnityGroup;
6+
use UnityWebPortal\lib\UnitySite;
67

78
if (!$USER->isAdmin()) {
8-
die();
9+
UnitySite::die();
910
}
1011

1112
if (!isset($_GET["pi_uid"])) {
12-
die("PI UID not set");
13+
UnitySite::badRequest("PI UID not set");
1314
}
1415

1516
$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);

webroot/admin/ajax/get_page_contents.php

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

33
require_once __DIR__ . "/../../../resources/autoload.php";
44

5+
use UnityWebPortal\lib\UnitySite;
6+
57
if (!$USER->isAdmin()) {
6-
die();
8+
UnitySite::die();
79
}
810

911
if (!isset($_GET["pageid"])) {
10-
die("Pageid not found");
12+
UnitySite::badRequest("Pageid not found");
1113
}
1214

1315
$page = $SQL->getPage($_GET["pageid"]);

webroot/admin/content.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
require_once __DIR__ . "/../../resources/autoload.php";
44

5+
use UnityWebPortal\lib\UnitySite;
6+
57
if (!$USER->isAdmin()) {
6-
die();
8+
UnitySite::die();
79
}
810

911
if ($_SERVER["REQUEST_METHOD"] == "POST") {

webroot/admin/notices.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
require_once __DIR__ . "/../../resources/autoload.php";
44

5+
use UnityWebPortal\lib\UnitySite;
6+
57
if (!$USER->isAdmin()) {
6-
die();
8+
UnitySite::die();
79
}
810

911
if ($_SERVER["REQUEST_METHOD"] == "POST") {

webroot/admin/pi-mgmt.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use UnityWebPortal\lib\UnityUser;
66
use UnityWebPortal\lib\UnityGroup;
7+
use UnityWebPortal\lib\UnitySite;
78

89
if (!$USER->isAdmin()) {
9-
die();
10+
UnitySite::die();
1011
}
1112

1213
if ($_SERVER["REQUEST_METHOD"] == "POST") {

webroot/admin/user-mgmt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use UnityWebPortal\lib\UnitySite;
66

77
if (!$USER->isAdmin()) {
8-
die();
8+
UnitySite::die();
99
}
1010

1111
if ($_SERVER["REQUEST_METHOD"] == "POST") {

webroot/api/content/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use UnityWebPortal\lib\UnitySite;
4+
35
header('Content-type: text/plain');
46

57
require_once __DIR__ . "/../../../resources/autoload.php";
@@ -11,7 +13,7 @@
1113
}
1214

1315
if (!isset($_GET["content_name"])) {
14-
die();
16+
UnitySite::badRequest("content_name not set");
1517
}
1618

1719
echo $SQL->getPage($_GET["content_name"])["content"];

0 commit comments

Comments
 (0)