Skip to content

replace die() with Exception or conditional #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
3 changes: 3 additions & 0 deletions resources/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use UnityWebPortal\lib\UnityRedis;
use UnityWebPortal\lib\UnityWebhook;
use UnityWebPortal\lib\UnityGithub;
use UnityWebPortal\lib\UnitySite;

//
// Initialize Session
Expand Down Expand Up @@ -125,6 +126,8 @@
}
}

$SITE = new UnitySite();

//
// Define vars
//
Expand Down
6 changes: 3 additions & 3 deletions resources/lib/UnitySite.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

class UnitySite
{
public static function redirect($destination)
public function redirect($destination)
{
if ($_SERVER["PHP_SELF"] != $destination) {
header("Location: $destination");
die("Redirect failed, click <a href='$destination'>here</a> to continue.");
}
}

public static function removeTrailingWhitespace($arr)
public function removeTrailingWhitespace($arr)
{
$out = array();
foreach ($arr as $str) {
Expand All @@ -25,7 +25,7 @@ public static function removeTrailingWhitespace($arr)
return $out;
}

public static function testValidSSHKey($key_str)
public function testValidSSHKey($key_str)
{
// key loader still throws, these just mute warnings for phpunit
// https://github.com/phpseclib/phpseclib/issues/2079
Expand Down
9 changes: 3 additions & 6 deletions resources/templates/header.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php

use UnityWebPortal\lib\UnitySite;

if (isset($SSO)) {
if (!$_SESSION["user_exists"]) {
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel/new_account.php");
$SITE->redirect($CONFIG["site"]["prefix"] . "/panel/new_account.php");
}
}

Expand Down Expand Up @@ -119,7 +116,7 @@
if (isset($_SESSION["is_admin"]) && $_SESSION["is_admin"]) {
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["form_name"]) && $_POST["form_name"] == "clearView") {
unset($_SESSION["viewUser"]);
UnitySite::redirect($CONFIG["site"]["prefix"] . "/admin/user-mgmt.php");
$SITE->redirect($CONFIG["site"]["prefix"] . "/admin/user-mgmt.php");
}

if (isset($_SESSION["viewUser"])) {
Expand All @@ -135,4 +132,4 @@
echo "</div>";
}
}
?>
?>
4 changes: 2 additions & 2 deletions webroot/admin/ajax/get_group_members.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use UnityWebPortal\lib\UnityGroup;

if (!$USER->isAdmin()) {
die();
throw new Exception("access denied");
}

if (!isset($_GET["pi_uid"])) {
die("PI UID not set");
throw new Exception("PI UID not set");
}

$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
Expand Down
4 changes: 2 additions & 2 deletions webroot/admin/ajax/get_page_contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require_once __DIR__ . "/../../../resources/autoload.php";

if (!$USER->isAdmin()) {
die();
throw new Exception("access denied");
}

if (!isset($_GET["pageid"])) {
die("Pageid not found");
throw new Exception("Pageid not defined");
}

$page = $SQL->getPage($_GET["pageid"]);
Expand Down
2 changes: 1 addition & 1 deletion webroot/admin/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once __DIR__ . "/../../resources/autoload.php";

if (!$USER->isAdmin()) {
die();
throw new Exception("access denied");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
Expand Down
2 changes: 1 addition & 1 deletion webroot/admin/notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once __DIR__ . "/../../resources/autoload.php";

if (!$USER->isAdmin()) {
die();
throw new Exception("access denied");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
Expand Down
2 changes: 1 addition & 1 deletion webroot/admin/pi-mgmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use UnityWebPortal\lib\UnityGroup;

if (!$USER->isAdmin()) {
die();
throw new Exception("access denied");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
Expand Down
6 changes: 2 additions & 4 deletions webroot/admin/user-mgmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

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

use UnityWebPortal\lib\UnitySite;

if (!$USER->isAdmin()) {
die();
throw new Exception("access denied");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
switch ($_POST["form_name"]) {
case "viewAsUser":
$_SESSION["viewUser"] = $_POST["uid"];
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel");
$SITE->redirect($CONFIG["site"]["prefix"] . "/panel/account.php");
break;
}
}
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 @@ -11,7 +11,7 @@
}

if (!isset($_GET["content_name"])) {
die();
throw new Exception("content_name not set");
}

echo $SQL->getPage($_GET["content_name"])["content"];
3 changes: 1 addition & 2 deletions webroot/js/ajax/ssh_validate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once __DIR__ . "/../../../resources/lib/UnitySite.php";
require_once __DIR__ . "/../../../vendor/autoload.php";

echo UnityWebPortal\lib\UnitySite::testValidSSHKey($_POST["key"]) ? "true" : "false";
echo (new UnityWebPortal\lib\UnitySite())->testValidSSHKey($_POST["key"]) ? "true" : "false";
10 changes: 4 additions & 6 deletions webroot/panel/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

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

use UnityWebPortal\lib\UnitySite;

require_once $LOC_HEADER;

$invalid_ssh_dialogue = "<script type='text/javascript'>
Expand All @@ -18,7 +16,7 @@
switch ($_POST["add_type"]) {
case "paste":
$key = $_POST["key"];
if (UnitySite::testValidSSHKey($key)) {
if ($SITE->testValidSSHKey($key)) {
array_push($added_keys, $key);
} else {
echo $invalid_ssh_dialogue;
Expand All @@ -27,7 +25,7 @@
case "import":
$keyfile = $_FILES["keyfile"]["tmp_name"];
$key = file_get_contents($keyfile);
if (UnitySite::testValidSSHKey($key)) {
if ($SITE->testValidSSHKey($key)) {
array_push($added_keys, $key);
} else {
echo $invalid_ssh_dialogue;
Expand All @@ -40,15 +38,15 @@
$gh_user = $_POST["gh_user"];
$keys = $GITHUB->getSshPublicKeys($gh_user);
foreach ($keys as $key) {
if (UnitySite::testValidSSHKey($key)) {
if ($SITE->testValidSSHKey($key)) {
array_push($added_keys, $key);
}
}
break;
}

if (!empty($added_keys)) {
$added_keys = UnitySite::removeTrailingWhitespace($added_keys);
$added_keys = $SITE->removeTrailingWhitespace($added_keys);
$totalKeys = array_merge($USER->getSSHKeys(), $added_keys);
$USER->setSSHKeys($totalKeys, $OPERATOR);
}
Expand Down
40 changes: 19 additions & 21 deletions webroot/panel/ajax/get_group_members.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use UnityWebPortal\lib\UnityGroup;

if (!isset($_GET["pi_uid"])) {
die("PI UID not set");
throw new Exception("PI UID not set");
}

$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
Expand All @@ -20,25 +20,23 @@
}
}

if (!$found) {
die();
}

$count = count($members);
foreach ($members as $key => $member) {
if ($member->getUID() == $group->getOwner()->getUID()) {
continue;
if ($found) {
$count = count($members);
foreach ($members as $key => $member) {
if ($member->getUID() == $group->getOwner()->getUID()) {
continue;
}

if ($key >= $count - 1) {
echo "<tr class='expanded $key last'>";
} else {
echo "<tr class='expanded $key'>";
}

echo "<td>" . $member->getFullname() . "</td>";
echo "<td>" . $member->getUID() . "</td>";
echo "<td><a href='mailto:" . $member->getMail() . "'>" . $member->getMail() . "</a></td>";
echo "<td><input type='hidden' name='uid' value='" . $member->getUID() . "'></td>";
echo "</tr>";
}

if ($key >= $count - 1) {
echo "<tr class='expanded $key last'>";
} else {
echo "<tr class='expanded $key'>";
}

echo "<td>" . $member->getFullname() . "</td>";
echo "<td>" . $member->getUID() . "</td>";
echo "<td><a href='mailto:" . $member->getMail() . "'>" . $member->getMail() . "</a></td>";
echo "<td><input type='hidden' name='uid' value='" . $member->getUID() . "'></td>";
echo "</tr>";
}
7 changes: 0 additions & 7 deletions webroot/panel/index.php

This file was deleted.

42 changes: 21 additions & 21 deletions webroot/panel/modal/pi_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@

$search_query = $_GET["search"]; // Search is passed as a get var
if (empty($search_query)) {
die("<span>No Results</span>");
}

$assocs = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, $WEBHOOK);
echo "<span>No Results</span>";
} else {
$assocs = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, $WEBHOOK);

$MAX_COUNT = 10; // Max results of PI search
$MAX_COUNT = 10; // Max results of PI search

$out = array();
foreach ($assocs as $assoc_obj) {
$assoc = $assoc_obj->getPIUID();
// loop through each association
if (strpos($assoc, $search_query) !== false) {
array_push($out, $assoc);
if (count($out) >= $MAX_COUNT) {
break;
}
}
$fn = strtolower($assoc_obj->getOwner()->getFullName());
if (strpos($fn, strtolower($search_query)) !== false) {
if (!in_array($assoc, $out)) {
$out = array();
foreach ($assocs as $assoc_obj) {
$assoc = $assoc_obj->getPIUID();
// loop through each association
if (strpos($assoc, $search_query) !== false) {
array_push($out, $assoc);
if (count($out) >= $MAX_COUNT) {
break;
}
}
$fn = strtolower($assoc_obj->getOwner()->getFullName());
if (strpos($fn, strtolower($search_query)) !== false) {
if (!in_array($assoc, $out)) {
array_push($out, $assoc);
if (count($out) >= $MAX_COUNT) {
break;
}
}
}
}
}

foreach ($out as $pi_acct) {
echo "<span>$pi_acct</span>";
foreach ($out as $pi_acct) {
echo "<span>$pi_acct</span>";
}
}
3 changes: 1 addition & 2 deletions webroot/panel/new_account.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

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

use UnityWebPortal\lib\UnitySite;
use UnityWebPortal\lib\UnityGroup;

require_once $LOC_HEADER;

if ($USER->exists()) {
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel/index.php"); // Redirect if account already exists
$SITE->redirect($CONFIG["site"]["prefix"] . "/panel/index.php"); // Redirect if account already exists
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
Expand Down
3 changes: 1 addition & 2 deletions webroot/panel/pi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
require_once __DIR__ . "/../../resources/autoload.php";

use UnityWebPortal\lib\UnityUser;
use UnityWebPortal\lib\UnitySite;

$group = $USER->getPIGroup();

if (!$USER->isPI()) {
die();
throw new Exception("access denied");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
Expand Down
Loading