Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions resources/lib/UnityHTTPD.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public static function errorHandler(int $severity, string $message, string $file
return false;
}

/* if key is not found, dies */
public static function getPostData(string $key): string
{
if (!array_key_exists($key, $_POST)) {
Expand All @@ -236,19 +237,25 @@ public static function getPostData(string $key): string
return $_POST[$key];
}

/* returns null if not found and not $die_if_not_found */
public static function getQueryParameter(string $key, bool $die_if_not_found = true): ?string
public static function getOptionalPostData(string $key): ?string
{
return @$_POST[$key];
}

/* if key is not found, dies */
public static function getQueryParameter(string $key): string
{
if (!array_key_exists($key, $_GET)) {
if ($die_if_not_found) {
self::badRequest("\$_GET has no array key '$key'");
} else {
return null;
}
self::badRequest("\$_GET has no array key '$key'");
}
return $_GET[$key];
}

public static function getOptionalQueryParameter(string $key): ?string
{
return @$_GET[$key];
}

public static function getUploadedFileContents(
string $filename,
bool $do_delete_tmpfile_after_read = true,
Expand Down
2 changes: 1 addition & 1 deletion resources/templates/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// UnityHTTPD::validatePostCSRFToken();
if (
($_SESSION["is_admin"] ?? false) == true
&& ($_POST["form_type"] ?? null) == "clearView"
&& UnityHTTPD::getOptionalPostData("form_type") == "clearView"
) {
unset($_SESSION["viewUser"]);
UnityHTTPD::redirect(getURL("admin/user-mgmt.php"));
Expand Down
8 changes: 6 additions & 2 deletions webroot/admin/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

if ($_SERVER["REQUEST_METHOD"] == "POST") {
UnityHTTPD::validatePostCSRFToken();
if (!empty($_POST["pageSel"])) {
$SQL->editPage($_POST["pageSel"], $_POST["content"], $USER);
if (!empty($_POST["pageSel"]) && isset($_POST["content"])) {
$SQL->editPage(
UnityHTTPD::getPostData("pageSel"),
UnityHTTPD::getPostData("content"),
$USER
);
}
}

Expand Down
21 changes: 14 additions & 7 deletions webroot/admin/notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@

if ($_SERVER["REQUEST_METHOD"] == "POST") {
UnityHTTPD::validatePostCSRFToken();
switch ($_POST["form_type"]) {
switch (UnityHTTPD::getPostData("form_type")) {
case "newNotice":
$SQL->addNotice($_POST["title"], $_POST["date"], $_POST["content"], $USER);

$SQL->addNotice(
UnityHTTPD::getPostData("title"),
UnityHTTPD::getPostData("date"),
UnityHTTPD::getPostData("content"),
$USER
);
break;
case "editNotice":
$SQL->editNotice($_POST["id"], $_POST["title"], $_POST["date"], $_POST["content"]);

$SQL->editNotice(
UnityHTTPD::getPostData("id"),
UnityHTTPD::getPostData("title"),
UnityHTTPD::getPostData("date"),
UnityHTTPD::getPostData("content")
);
break;
case "delNotice":
$SQL->deleteNotice($_POST["id"]);

$SQL->deleteNotice(UnityHTTPD::getPostData("id"));
break;
}
}
Expand Down
19 changes: 12 additions & 7 deletions webroot/admin/pi-mgmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,36 @@
return new UnityUser(UnityHTTPD::getPostData("uid"), $LDAP, $SQL, $MAILER, $WEBHOOK);
};

$getPIGroupFromPost = function () {
global $LDAP, $SQL, $MAILER, $WEBHOOK;
return new UnityGroup(UnityHTTPD::getPostData("pi"), $LDAP, $SQL, $MAILER, $WEBHOOK);
};

if ($_SERVER["REQUEST_METHOD"] == "POST") {
UnityHTTPD::validatePostCSRFToken();
switch ($_POST["form_type"]) {
switch (UnityHTTPD::getPostData("form_type")) {
case "req":
$form_user = $getUserFromPost();
if ($_POST["action"] == "Approve") {
if (UnityHTTPD::getPostData("action") == "Approve") {
$group = $form_user->getPIGroup();
$group->approveGroup($OPERATOR);
} elseif ($_POST["action"] == "Deny") {
} elseif (UnityHTTPD::getPostData("action") == "Deny") {
$group = $form_user->getPIGroup();
$group->denyGroup($OPERATOR);
}
break;
case "reqChild":
$form_user = $getUserFromPost();
$parent_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $WEBHOOK);
if ($_POST["action"] == "Approve") {
$parent_group = $getPIGroupFromPost();
if (UnityHTTPD::getPostData("action") == "Approve") {
$parent_group->approveUser($form_user);
} elseif ($_POST["action"] == "Deny") {
} elseif (UnityHTTPD::getPostData("action") == "Deny") {
$parent_group->denyUser($form_user);
}
break;
case "remUserChild":
$form_user = $getUserFromPost();
$parent = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $WEBHOOK);
$parent = $getPIGroupFromPost();
$parent->removeUser($form_user);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions webroot/admin/user-mgmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

if ($_SERVER["REQUEST_METHOD"] == "POST") {
UnityHTTPD::validatePostCSRFToken();
switch ($_POST["form_type"]) {
switch (UnityHTTPD::getPostData("form_type")) {
case "viewAsUser":
$_SESSION["viewUser"] = $_POST["uid"];
$_SESSION["viewUser"] = UnityHTTPD::getPostData("uid");
UnityHTTPD::redirect(getURL("panel/account.php"));
break; /** @phpstan-ignore deadCode.unreachable */
}
Expand Down
2 changes: 1 addition & 1 deletion webroot/api/content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

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

$CHAR_WRAP = str2int(UnityHTTPD::getQueryParameter("line_wrap", false) ?? "80");
$CHAR_WRAP = str2int(UnityHTTPD::getOptionalQueryParameter("line_wrap") ?? "80");
$content_name = UnityHTTPD::getQueryParameter("content_name");
echo $SQL->getPage($content_name)["content"];
2 changes: 1 addition & 1 deletion webroot/js/ajax/ssh_generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$private = EC::createKey('Ed25519');
$public = $private->getPublicKey();
$public_str = $public->toString('OpenSSH');
if (UnityHTTPD::getQueryParameter("type", false) == "ppk") {
if (UnityHTTPD::getOptionalQueryParameter("type") == "ppk") {
$private_str = $private->toString('PuTTY');
} else {
$private_str = $private->toString('OpenSSH');
Expand Down
4 changes: 2 additions & 2 deletions webroot/panel/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
$USER->setSSHKeys($keys, $OPERATOR);
break;
case "loginshell":
$USER->setLoginShell($_POST["shellSelect"], $OPERATOR);
$USER->setLoginShell(UnityHTTPD::getPostData("shellSelect"), $OPERATOR);
break;
case "pi_request":
if ($USER->isPI()) {
Expand All @@ -72,7 +72,7 @@
if ($SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI)) {
UnityHTTPD::badRequest("already requested to be PI");
}
if ($_POST["tos"] != "agree") {
if (UnityHTTPD::getPostData("tos") != "agree") {
UnityHTTPD::badRequest("user did not agree to terms of service");
}
$USER->getPIGroup()->requestGroup($SEND_PIMESG_TO_ADMINS);
Expand Down
4 changes: 2 additions & 2 deletions webroot/panel/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
if ($_SERVER["REQUEST_METHOD"] == "POST") {
UnityHTTPD::validatePostCSRFToken();
if (isset($_POST["form_type"])) {
switch ($_POST["form_type"]) {
switch (UnityHTTPD::getPostData("form_type")) {
case "addPIform":
$pi_account = $getPIGroupFromPost();
if ($_POST["tos"] != "agree") {
if (UnityHTTPD::getPostData("tos") != "agree") {
UnityHTTPD::badRequest("user did not agree to terms of service");
}
if ($pi_account->exists()) {
Expand Down
6 changes: 3 additions & 3 deletions webroot/panel/pi.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

if ($_SERVER["REQUEST_METHOD"] == "POST") {
UnityHTTPD::validatePostCSRFToken();
switch ($_POST["form_type"]) {
switch (UnityHTTPD::getPostData("form_type")) {
case "userReq":
$form_user = $getUserFromPost();
if ($_POST["action"] == "Approve") {
if (UnityHTTPD::getPostData("action") == "Approve") {
$group->approveUser($form_user);
} elseif ($_POST["action"] == "Deny") {
} elseif (UnityHTTPD::getPostData("action") == "Deny") {
$group->denyUser($form_user);
}
break;
Expand Down