Skip to content
2 changes: 1 addition & 1 deletion resources/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
require_once __DIR__ . "/lib/UnityGithub.php";
require_once __DIR__ . "/lib/exceptions/SSOException.php";

// run init script
require_once __DIR__ . "/config.php";
require __DIR__ . "/init.php";
5 changes: 5 additions & 0 deletions resources/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use UnityWebPortal\lib\UnityConfig;

define("CONFIG", UnityConfig::getConfig(__DIR__ . "/../defaults", __DIR__ . "/../deployment"));
68 changes: 9 additions & 59 deletions resources/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* init.php - Initialization script that is run on every page of Unity
*/

use UnityWebPortal\lib\UnityConfig;
use UnityWebPortal\lib\UnityLDAP;
use UnityWebPortal\lib\UnityMailer;
use UnityWebPortal\lib\UnitySQL;
Expand All @@ -18,67 +17,16 @@

session_start();

$CONFIG = UnityConfig::getConfig(__DIR__ . "/../defaults", __DIR__ . "/../deployment");

$REDIS = new UnityRedis(
$CONFIG["redis"]["host"] ?? "",
$CONFIG["redis"]["port"] ?? ""
);

$REDIS = new UnityRedis();
if (isset($GLOBALS["ldapconn"])) {
$LDAP = $GLOBALS["ldapconn"];
} else {
$LDAP = new UnityLDAP(
$CONFIG["ldap"]["uri"],
$CONFIG["ldap"]["user"],
$CONFIG["ldap"]["pass"],
__DIR__ . "/../deployment/custom_user_mappings",
$CONFIG["ldap"]["basedn"],
$CONFIG["ldap"]["user_ou"],
$CONFIG["ldap"]["group_ou"],
$CONFIG["ldap"]["pigroup_ou"],
$CONFIG["ldap"]["orggroup_ou"],
$CONFIG["ldap"]["admin_group"],
$CONFIG["ldap"]["user_group"],
$CONFIG["ldap"]["def_user_shell"]
);
$LDAP = new UnityLDAP();
$GLOBALS["ldapconn"] = $LDAP;
}

$SQL = new UnitySQL(
$CONFIG["sql"]["host"],
$CONFIG["sql"]["dbname"],
$CONFIG["sql"]["user"],
$CONFIG["sql"]["pass"]
);

$MAILER = new UnityMailer(
__DIR__ . "/mail",
__DIR__ . "/../deployment/mail_overrides",
$CONFIG["smtp"]["host"],
$CONFIG["smtp"]["port"],
$CONFIG["smtp"]["security"],
$CONFIG["smtp"]["user"],
$CONFIG["smtp"]["pass"],
$CONFIG["smtp"]["ssl_verify"],
$CONFIG["site"]["url"] . $CONFIG["site"]["prefix"],
$CONFIG["mail"]["sender"],
$CONFIG["mail"]["sender_name"],
$CONFIG["mail"]["support"],
$CONFIG["mail"]["support_name"],
$CONFIG["mail"]["admin"],
$CONFIG["mail"]["admin_name"],
$CONFIG["mail"]["pi_approve"],
$CONFIG["mail"]["pi_approve_name"]
);

$WEBHOOK = new UnityWebhook(
__DIR__ . "/mail",
__DIR__ . "/../deployment/mail_overrides",
$CONFIG["webhook"]["url"],
$CONFIG["site"]["url"] . $CONFIG["site"]["prefix"]
);

$SQL = new UnitySQL();
$MAILER = new UnityMailer();
$WEBHOOK = new UnityWebhook();
$GITHUB = new UnityGithub();

if (isset($_SERVER["REMOTE_USER"])) { // Check if SSO is enabled on this page
Expand All @@ -89,7 +37,9 @@
$eppn = $_SERVER["REMOTE_USER"];
UnitySite::errorLog("SSO Failure", "{$e} ($errorid)");
UnitySite::die(
"Invalid eppn: '$eppn'. Please contact {$CONFIG["mail"]["support"]} (id: $errorid)",
"Invalid eppn: '$eppn'. Please contact support at "
. CONFIG["mail"]["support"]
. " (id: $errorid)",
true
);
}
Expand All @@ -106,7 +56,7 @@

$_SESSION["user_exists"] = $USER->exists();
$_SESSION["is_pi"] = $USER->isPI();
$SEND_PIMESG_TO_ADMINS = $CONFIG["mail"]["send_pimesg_to_admins"];
$SEND_PIMESG_TO_ADMINS = CONFIG["mail"]["send_pimesg_to_admins"];

$SQL->addLog(
$OPERATOR->uid,
Expand Down
64 changes: 16 additions & 48 deletions resources/lib/UnityLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ class UnityLDAP extends ldapConn
"top"
);

// string vars for OUs
private $STR_BASEOU;
private $STR_USEROU;
private $STR_GROUPOU;
private $STR_PIGROUPOU;
private $STR_ORGGROUPOU;
private $STR_ADMINGROUP;
private $custom_mappings_path = __DIR__ . "/../../deployment/custom_user_mappings";

// Instance vars for various ldapEntry objects
private $baseOU;
Expand All @@ -40,45 +34,19 @@ class UnityLDAP extends ldapConn
private $org_groupOU;
private $adminGroup;
private $userGroup;

private $custom_mappings_path;

private $def_user_shell;

public function __construct(
$host,
$dn,
$pass,
$custom_user_mappings,
$base_ou,
$user_ou,
$group_ou,
$pigroup_ou,
$orggroup_ou,
$admin_group,
$user_group_dn,
$def_user_shell
) {
parent::__construct($host, $dn, $pass);

$this->STR_BASEOU = $base_ou;
$this->STR_USEROU = $user_ou;
$this->STR_GROUPOU = $group_ou;
$this->STR_PIGROUPOU = $pigroup_ou;
$this->STR_ORGGROUPOU = $orggroup_ou;
$this->STR_ADMINGROUP = $admin_group;

$this->baseOU = $this->getEntry($base_ou);
$this->userOU = $this->getEntry($user_ou);
$this->groupOU = $this->getEntry($group_ou);
$this->pi_groupOU = $this->getEntry($pigroup_ou);
$this->org_groupOU = $this->getEntry($orggroup_ou);
$this->adminGroup = $this->getEntry($admin_group);
$this->userGroup = $this->getEntry($user_group_dn);

$this->custom_mappings_path = $custom_user_mappings;

$this->def_user_shell = $def_user_shell;
public function __construct()
{
parent::__construct(CONFIG["ldap"]["uri"], CONFIG["ldap"]["user"], CONFIG["ldap"]["pass"]);
$this->baseOU = $this->getEntry(CONFIG["ldap"]["basedn"]);
$this->userOU = $this->getEntry(CONFIG["ldap"]["user_ou"]);
$this->groupOU = $this->getEntry(CONFIG["ldap"]["group_ou"]);
$this->pi_groupOU = $this->getEntry(CONFIG["ldap"]["pigroup_ou"]);
$this->org_groupOU = $this->getEntry(CONFIG["ldap"]["orggroup_ou"]);
$this->adminGroup = $this->getEntry(CONFIG["ldap"]["admin_group"]);
$this->userGroup = $this->getEntry(CONFIG["ldap"]["user_group"]);
$this->def_user_shell = CONFIG["ldap"]["def_user_shell"];
}

public function getUserOU()
Expand Down Expand Up @@ -429,24 +397,24 @@ public function getAllOrgGroupsAttributes($attributes)
public function getUserEntry($uid)
{
$uid = ldap_escape($uid, "", LDAP_ESCAPE_DN);
return $this->getEntry(unityLDAP::RDN . "=$uid," . $this->STR_USEROU);
return $this->getEntry(unityLDAP::RDN . "=$uid," . CONFIG["ldap"]["user_ou"]);
}

public function getGroupEntry($gid)
{
$gid = ldap_escape($gid, "", LDAP_ESCAPE_DN);
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_GROUPOU);
return $this->getEntry(unityLDAP::RDN . "=$gid," . CONFIG["ldap"]["group_ou"]);
}

public function getPIGroupEntry($gid)
{
$gid = ldap_escape($gid, "", LDAP_ESCAPE_DN);
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_PIGROUPOU);
return $this->getEntry(unityLDAP::RDN . "=$gid," . CONFIG["ldap"]["pigroup_ou"]);
}

public function getOrgGroupEntry($gid)
{
$gid = ldap_escape($gid, "", LDAP_ESCAPE_DN);
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
return $this->getEntry(unityLDAP::RDN . "=$gid," . CONFIG["ldap"]["orggroup_ou"]);
}
}
66 changes: 23 additions & 43 deletions resources/lib/UnityMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
class UnityMailer extends PHPMailer
{
private $template_dir; // location of all email templates
private $override_template_dir;
private $template_dir = __DIR__ . "/../mail"; // location of all email templates
private $override_template_dir = __DIR__ . "/../../deployment/mail_overrides";

private $MSG_LINKREF;
private $MSG_SENDER_EMAIL;
Expand All @@ -23,38 +23,31 @@ class UnityMailer extends PHPMailer
private $MSG_PI_APPROVAL_EMAIL;
private $MSG_PI_APPROVAL_NAME;

public function __construct(
$template_dir,
$override_template_dir,
$hostname,
$port,
$security,
$user,
$pass,
$ssl_verify,
$msg_linkref,
$msg_sender_email,
$msg_sender_name,
$msg_support_email,
$msg_support_name,
$msg_admin_email,
$msg_admin_name,
$msg_pi_approval_email,
$msg_pi_approval_name
) {
public function __construct()
{
parent::__construct();
$this->isSMTP();

if (empty($hostname)) {
$this->MSG_LINKREF = CONFIG["site"]["url"] . CONFIG["site"]["prefix"];
$this->MSG_SENDER_EMAIL = CONFIG["mail"]["sender"];
$this->MSG_SENDER_NAME = CONFIG["mail"]["sender_name"];
$this->MSG_SUPPORT_EMAIL = CONFIG["mail"]["support"];
$this->MSG_SUPPORT_NAME = CONFIG["mail"]["support_name"];
$this->MSG_ADMIN_EMAIL = CONFIG["mail"]["admin"];
$this->MSG_ADMIN_NAME = CONFIG["mail"]["admin_name"];
$this->MSG_PI_APPROVAL_EMAIL = CONFIG["mail"]["pi_approve"];
$this->MSG_PI_APPROVAL_NAME = CONFIG["mail"]["pi_approve_name"];
if (empty(CONFIG["smtp"]["host"])) {
throw new Exception("SMTP server hostname not set");
}
$this->Host = $hostname;
$this->Host = CONFIG["smtp"]["host"];

if (empty($port)) {
if (empty(CONFIG["smtp"]["port"])) {
throw new Exception("SMTP server port not set");
}
$this->Port = $port;
$this->Port = CONFIG["smtp"]["port"];

$security = CONFIG["smtp"]["security"];
$security_conf_valid = empty($security) || $security == "tls" || $security == "ssl";
if (!$security_conf_valid) {
throw new Exception(
Expand All @@ -63,18 +56,18 @@ public function __construct(
}
$this->SMTPSecure = $security;

if (!empty($user)) {
if (!empty(CONFIG["smtp"]["user"])) {
$this->SMTPAuth = true;
$this->Username = $user;
$this->Username = CONFIG["smtp"]["user"];
} else {
$this->SMTPAuth = false;
}

if (!empty($pass)) {
$this->Password = $pass;
if (!empty(CONFIG["smtp"]["pass"])) {
$this->Password = CONFIG["smtp"]["pass"];
}

if ($ssl_verify == "false") {
if (CONFIG["smtp"]["ssl_verify"] == "false") {
$this->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
Expand All @@ -83,19 +76,6 @@ public function __construct(
)
);
}

$this->template_dir = $template_dir;
$this->override_template_dir = $override_template_dir;

$this->MSG_LINKREF = $msg_linkref;
$this->MSG_SENDER_EMAIL = $msg_sender_email;
$this->MSG_SENDER_NAME = $msg_sender_name;
$this->MSG_SUPPORT_EMAIL = $msg_support_email;
$this->MSG_SUPPORT_NAME = $msg_support_name;
$this->MSG_ADMIN_EMAIL = $msg_admin_email;
$this->MSG_ADMIN_NAME = $msg_admin_name;
$this->MSG_PI_APPROVAL_EMAIL = $msg_pi_approval_email;
$this->MSG_PI_APPROVAL_NAME = $msg_pi_approval_name;
}

public function sendMail($recipients, $template = null, $data = null)
Expand Down
4 changes: 3 additions & 1 deletion resources/lib/UnityRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class UnityRedis

private $enabled;

public function __construct($host, $port)
public function __construct()
{
$host = @CONFIG["redis"]["host"] ?? "";
$port = @CONFIG["redis"]["port"] ?? "";
if (empty($host)) {
$this->enabled = false;
} else {
Expand Down
8 changes: 6 additions & 2 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ class UnitySQL

private $conn;

public function __construct($db_host, $db, $db_user, $db_pass)
public function __construct()
{
$this->conn = new PDO("mysql:host=" . $db_host . ";dbname=" . $db, $db_user, $db_pass);
$this->conn = new PDO(
"mysql:host=" . CONFIG["sql"]["host"] . ";dbname=" . CONFIG["sql"]["dbname"],
CONFIG["sql"]["user"],
CONFIG["sql"]["pass"]
);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

Expand Down
20 changes: 6 additions & 14 deletions resources/lib/UnityWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@

class UnityWebhook
{
private $template_dir;
private $override_template_dir;
private $url;
private $template_dir = __DIR__ . "/../mail";
private $override_template_dir = __DIR__ . "/../../deployment/mail_overrides";
private $url = CONFIG["webhook"]["url"];
private $MSG_LINKREF;
private $Subject; // set by template

public function __construct(
$template_dir,
$override_template_dir,
$url,
$msg_linkref
) {
$this->template_dir = $template_dir;
$this->override_template_dir = $override_template_dir;
$this->url = $url;
$this->MSG_LINKREF = $msg_linkref;
public function __construct()
{
$this->MSG_LINKREF = CONFIG["site"]["url"] . CONFIG["site"]["prefix"];
}

public function htmlToMarkdown($html)
{
// Define regex patterns for each markdown format
Expand Down
Loading