Skip to content

Commit a9f6512

Browse files
committed
move UnitySite to UnityHTTPD
1 parent 65a3f57 commit a9f6512

30 files changed

+98
-104
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ repos:
6767
files: \.php$
6868
exclude: |
6969
(?x)^(
70-
resources/lib/UnitySite\.php$|
70+
resources/lib/UnityHTTPD\.php$|
7171
workers/.*|
7272
)$

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* The maximum line length for any PHP file is 100 characters, instead of PSR-12's 120 characters.
88
* Comments should be used sparingly.
99
* Empty lines should be used sparingly.
10-
* No code should call `die()` or `exit()`, instead `UnitySite::die()`. This will avoid the premature death of our automated testing processes.
10+
* No code should call `die()` or `exit()`, instead `UnityHTTPD::die()`. This will avoid the premature death of our automated testing processes.
1111
* Instead of `assert`, use `\ensure`. This will enforce conditions even in production.
1212

1313
This repository will automatically check PRs for linting compliance.

resources/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
require_once __DIR__ . "/lib/UnitySQL.php";
2020
require_once __DIR__ . "/lib/UnityMailer.php";
2121
require_once __DIR__ . "/lib/UnitySSO.php";
22-
require_once __DIR__ . "/lib/UnitySite.php";
22+
require_once __DIR__ . "/lib/UnityHTTPD.php";
2323
require_once __DIR__ . "/lib/UnityConfig.php";
2424
require_once __DIR__ . "/lib/UnityWebhook.php";
2525
require_once __DIR__ . "/lib/UnityRedis.php";

resources/init.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use UnityWebPortal\lib\UnityRedis;
1313
use UnityWebPortal\lib\UnityWebhook;
1414
use UnityWebPortal\lib\UnityGithub;
15-
use UnityWebPortal\lib\UnitySite;
15+
use UnityWebPortal\lib\UnityHTTPD;
1616

17-
register_shutdown_function(array("UnityWebPortal\lib\UnitySite", "shutdown"));
17+
register_shutdown_function(array("UnityWebPortal\lib\UnityHTTPD", "shutdown"));
1818
// shutdown function logs errors, don't want duplicate output
1919
ini_set("log_errors", false);
2020

resources/lib/UnityGroup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ public function newUserRequest(
349349
$send_mail = true
350350
) {
351351
if ($this->userExists($new_user)) {
352-
UnitySite::errorLog("warning", "user '$new_user' already in group");
352+
UnityHTTPD::errorLog("warning", "user '$new_user' already in group");
353353
return;
354354
}
355355
if ($this->requestExists($new_user)) {
356-
UnitySite::errorLog("warning", "user '$new_user' already requested group membership");
356+
UnityHTTPD::errorLog("warning", "user '$new_user' already requested group membership");
357357
return;
358358
}
359359
if ($this->SQL->accDeletionRequestExists($new_user->uid)) {
Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace UnityWebPortal\lib;
44

5-
use phpseclib3\Crypt\PublicKeyLoader;
65
use UnityWebPortal\lib\exceptions\NoDieException;
76
use UnityWebPortal\lib\exceptions\ArrayKeyException;
87

9-
class UnitySite
8+
class UnityHTTPD
109
{
1110
public static function die($x = null, $show_user = false)
1211
{
@@ -172,28 +171,4 @@ public static function alert(string $message)
172171
// json_encode escapes quotes
173172
echo "<script type='text/javascript'>alert(" . json_encode($message) . ");</script>";
174173
}
175-
176-
public static function testValidSSHKey($key_str)
177-
{
178-
// key loader still throws, these just mute warnings for phpunit
179-
// https://github.com/phpseclib/phpseclib/issues/2079
180-
if ($key_str == "") {
181-
return false;
182-
}
183-
// https://github.com/phpseclib/phpseclib/issues/2076
184-
// https://github.com/phpseclib/phpseclib/issues/2077
185-
// there are actually valid JSON keys (JWK), but I don't think anybody uses it
186-
if (!is_null(@json_decode($key_str))) {
187-
return false;
188-
}
189-
try {
190-
PublicKeyLoader::load($key_str);
191-
return true;
192-
// phpseclib should throw only NoKeyLoadedException but that is not the case
193-
// https://github.com/phpseclib/phpseclib/pull/2078
194-
// } catch (\phpseclib3\Exception\NoKeyLoadedException $e) {
195-
} catch (\Throwable $e) {
196-
return false;
197-
}
198-
}
199174
}

resources/lib/UnityLDAP.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function getNextUIDGIDNumber($uid)
9999
return $customMappedID;
100100
}
101101
if (!is_null($customMappedID) && in_array($customMappedID, $IDNumsInUse)) {
102-
UnitySite::errorLog(
102+
UnityHTTPD::errorLog(
103103
"warning",
104104
"user '$uid' has a custom mapped IDNumber $customMappedID but it's already in use!",
105105
);
@@ -157,7 +157,7 @@ private function getCustomIDMappings()
157157
array_push($output, $row);
158158
}
159159
} else {
160-
UnitySite::errorLog(
160+
UnityHTTPD::errorLog(
161161
"warning",
162162
"custom ID mapping file '$filename' ignored, extension != .csv",
163163
);
@@ -330,7 +330,7 @@ public function getAllPIGroupOwnerAttributes($attributes)
330330
array_map(fn($x) => $x["uid"][0], $owner_attributes)
331331
);
332332
if (count($owners_not_found) > 0) {
333-
UnitySite::errorLog(
333+
UnityHTTPD::errorLog(
334334
"warning",
335335
"PI group owners not found: " . json_encode($owners_not_found) . "\n"
336336
);
@@ -353,7 +353,7 @@ public function getAllUID2PIGIDs()
353353
if (array_key_exists($uid, $uid2pigids)) {
354354
array_push($uid2pigids[$uid], $gid);
355355
} else {
356-
UnitySite::errorLog(
356+
UnityHTTPD::errorLog(
357357
"warning",
358358
"user '$uid' is a member of a PI group but is not a Unity user!"
359359
);

resources/lib/UnityRedis.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function setCache($object, $key, $data)
3636
$keyStr = $object;
3737
}
3838
if (is_null($data)) {
39-
UnitySite::errorLog("warning", "setting '$keyStr' to null");
39+
UnityHTTPD::errorLog("warning", "setting '$keyStr' to null");
4040
}
4141
$this->client->set($keyStr, serialize($data));
4242
}

resources/lib/utils.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use UnityWebPortal\lib\exceptions\ArrayKeyException;
44
use UnityWebPortal\lib\exceptions\EnsureException;
5+
use phpseclib3\Crypt\PublicKeyLoader;
56

67
function arrayGet($array, ...$keys)
78
{
@@ -28,3 +29,27 @@ function ensure(bool $condition, ?string $message = null)
2829
throw new EnsureException($message ?? "ensure condition is false");
2930
}
3031
}
32+
33+
function testValidSSHKey($key_str)
34+
{
35+
// key loader still throws, these just mute warnings for phpunit
36+
// https://github.com/phpseclib/phpseclib/issues/2079
37+
if ($key_str == "") {
38+
return false;
39+
}
40+
// https://github.com/phpseclib/phpseclib/issues/2076
41+
// https://github.com/phpseclib/phpseclib/issues/2077
42+
// there are actually valid JSON keys (JWK), but I don't think anybody uses it
43+
if (!is_null(@json_decode($key_str))) {
44+
return false;
45+
}
46+
try {
47+
PublicKeyLoader::load($key_str);
48+
return true;
49+
// phpseclib should throw only NoKeyLoadedException but that is not the case
50+
// https://github.com/phpseclib/phpseclib/pull/2078
51+
// } catch (\phpseclib3\Exception\NoKeyLoadedException $e) {
52+
} catch (\Throwable $e) {
53+
return false;
54+
}
55+
}

resources/templates/header.php

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

3-
use UnityWebPortal\lib\UnitySite;
3+
use UnityWebPortal\lib\UnityHTTPD;
44

55
if ($_SERVER["REQUEST_METHOD"] == "POST") {
66
if (
77
($_SESSION["is_admin"] ?? false) == true
88
&& ($_POST["form_type"] ?? null) == "clearView"
99
) {
1010
unset($_SESSION["viewUser"]);
11-
UnitySite::redirect(CONFIG["site"]["prefix"] . "/admin/user-mgmt.php");
11+
UnityHTTPD::redirect(CONFIG["site"]["prefix"] . "/admin/user-mgmt.php");
1212
}
1313
// Webroot files need to handle their own POSTs before loading the header
14-
// so that they can do UnitySite::badRequest before anything else has been printed.
14+
// so that they can do UnityHTTPD::badRequest before anything else has been printed.
1515
// They also must not redirect like standard PRG practice because this
1616
// header also needs to handle POST data. So this header does the PRG redirect
1717
// for all pages.
1818
unset($_POST); // unset ensures that header must not come before POST handling
19-
UnitySite::redirect(CONFIG["site"]["prefix"] . $_SERVER['REQUEST_URI']);
19+
UnityHTTPD::redirect(CONFIG["site"]["prefix"] . $_SERVER['REQUEST_URI']);
2020
}
2121

2222
if (isset($SSO)) {
2323
if (
2424
!$_SESSION["user_exists"]
2525
&& !str_ends_with($_SERVER['PHP_SELF'], "/panel/new_account.php")
2626
) {
27-
UnitySite::redirect(CONFIG["site"]["prefix"] . "/panel/new_account.php");
27+
UnityHTTPD::redirect(CONFIG["site"]["prefix"] . "/panel/new_account.php");
2828
}
2929
}
3030

0 commit comments

Comments
 (0)