diff --git a/defaults/config.ini.default b/defaults/config.ini.default index bf7e5e70..245d8403 100644 --- a/defaults/config.ini.default +++ b/defaults/config.ini.default @@ -93,6 +93,9 @@ sender_name = "Unity Sender" pi_approve = "piapproval@unitywebportal.test" ; Only PI approval messages will go to this email pi_approve_name = "Unity PI Approval" +[webhook] ; webhook to send messages to admins +url = "https://hooks.slack.com/services/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" + [page] ; which sql objects to use for the content on these pages home = "home" support = "support" diff --git a/resources/autoload.php b/resources/autoload.php index 60dca262..af41edfc 100644 --- a/resources/autoload.php +++ b/resources/autoload.php @@ -17,6 +17,7 @@ require_once __DIR__ . "/lib/UnitySSO.php"; require_once __DIR__ . "/lib/UnitySite.php"; require_once __DIR__ . "/lib/UnityConfig.php"; +require_once __DIR__ . "/lib/UnityWebhook.php"; require_once __DIR__ . "/lib/UnityRedis.php"; // run init script diff --git a/resources/init.php b/resources/init.php index 5452fc1f..a1c559f1 100644 --- a/resources/init.php +++ b/resources/init.php @@ -11,7 +11,8 @@ UnitySQL, UnitySSO, UnityUser, - UnityRedis + UnityRedis, + UnityWebhook }; // @@ -77,6 +78,14 @@ $CONFIG["mail"]["pi_approve_name"] ); +// Creates Webhook service +$WEBHOOK = new UnityWebhook( + __DIR__ . "/mail", + __DIR__ . "/../deployment/mail_overrides", + $CONFIG["webhook"]["url"], + $CONFIG["site"]["url"] . $CONFIG["site"]["prefix"] +); + // // SSO Init // @@ -86,11 +95,11 @@ // SSO is available $_SESSION["SSO"] = $SSO; - $USER = new UnityUser($SSO["user"], $LDAP, $SQL, $MAILER, $REDIS); + $USER = new UnityUser($SSO["user"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); $_SESSION["is_admin"] = $USER->isAdmin(); if (isset($_SESSION["viewUser"]) && $_SESSION["is_admin"]) { - $USER = new UnityUser($_SESSION["viewUser"], $LDAP, $SQL, $MAILER, $REDIS); + $USER = new UnityUser($_SESSION["viewUser"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); } $_SESSION["user_exists"] = $USER->exists(); diff --git a/resources/lib/UnityGroup.php b/resources/lib/UnityGroup.php index 35e361ae..ac0dd9bb 100644 --- a/resources/lib/UnityGroup.php +++ b/resources/lib/UnityGroup.php @@ -17,6 +17,7 @@ class UnityGroup private $LDAP; private $SQL; private $MAILER; + private $WEBHOOK; private $REDIS; /** @@ -26,7 +27,7 @@ class UnityGroup * @param LDAP $LDAP LDAP Connection * @param SQL $SQL SQL Connection */ - public function __construct($pi_uid, $LDAP, $SQL, $MAILER, $REDIS) + public function __construct($pi_uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK) { $this->pi_uid = $pi_uid; @@ -34,6 +35,7 @@ public function __construct($pi_uid, $LDAP, $SQL, $MAILER, $REDIS) $this->SQL = $SQL; $this->MAILER = $MAILER; $this->REDIS = $REDIS; + $this->WEBHOOK = $WEBHOOK; } public function equals($other_group) @@ -85,6 +87,16 @@ public function requestGroup($send_mail = true) "group_request" ); + $this->WEBHOOK->sendWebhook( + "group_request_admin", + array( + "user" => $this->getOwner()->getUID(), + "org" => $this->getOwner()->getOrg(), + "name" => $this->getOwner()->getFullname(), + "email" => $this->getOwner()->getMail() + ) + ); + $this->MAILER->sendMail( "admin", "group_request_admin", @@ -356,7 +368,8 @@ public function getRequests() $this->LDAP, $this->SQL, $this->MAILER, - $this->REDIS + $this->REDIS, + $this->WEBHOOK ); array_push($out, [$user, $request["timestamp"]]); } @@ -389,7 +402,8 @@ public function getGroupMembers($ignorecache = false) $this->LDAP, $this->SQL, $this->MAILER, - $this->REDIS + $this->REDIS, + $this->WEBHOOK ); array_push($out, $user_obj); array_push($cache_arr, $user_obj->getUID()); @@ -508,7 +522,8 @@ public function getOwner() $this->LDAP, $this->SQL, $this->MAILER, - $this->REDIS + $this->REDIS, + $this->WEBHOOK ); } diff --git a/resources/lib/UnityLDAP.php b/resources/lib/UnityLDAP.php index b5a5d905..91612579 100644 --- a/resources/lib/UnityLDAP.php +++ b/resources/lib/UnityLDAP.php @@ -234,7 +234,7 @@ public function getUnassignedID($uid) // // Functions that return user/group objects // - public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = false) + public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false) { $out = array(); @@ -242,7 +242,7 @@ public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = $users = $UnityRedis->getCache("sorted_users", ""); if (!is_null($users)) { foreach ($users as $user) { - array_push($out, new UnityUser($user, $this, $UnitySQL, $UnityMailer, $UnityRedis)); + array_push($out, new UnityUser($user, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook)); } return $out; @@ -252,13 +252,14 @@ public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = $users = $this->userOU->getChildren(true); foreach ($users as $user) { - array_push($out, new UnityUser($user->getAttribute("cn")[0], $this, $UnitySQL, $UnityMailer, $UnityRedis)); + $params = array($user->getAttribute("cn")[0], $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook); + array_push($out, new UnityUser(...$params)); } return $out; } - public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = false) + public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false) { $out = array(); @@ -266,7 +267,8 @@ public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecach $groups = $UnityRedis->getCache("sorted_groups", ""); if (!is_null($groups)) { foreach ($groups as $group) { - array_push($out, new UnityGroup($group, $this, $UnitySQL, $UnityMailer, $UnityRedis)); + $params = array($group, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook); + array_push($out, new UnityGroup(...$params)); } return $out; @@ -281,14 +283,15 @@ public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecach $this, $UnitySQL, $UnityMailer, - $UnityRedis + $UnityRedis, + $UnityWebhook )); } return $out; } - public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = false) + public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false) { $out = array(); @@ -296,7 +299,7 @@ public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecac $orgs = $UnityRedis->getCache("sorted_orgs", ""); if (!is_null($orgs)) { foreach ($orgs as $org) { - array_push($out, new UnityOrg($org, $this, $UnitySQL, $UnityMailer, $UnityRedis)); + array_push($out, new UnityOrg($org, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook)); } return $out; @@ -311,7 +314,8 @@ public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecac $this, $UnitySQL, $UnityMailer, - $UnityRedis + $UnityRedis, + $UnityWebhook )); } diff --git a/resources/lib/UnityOrg.php b/resources/lib/UnityOrg.php index 455508f2..73a69396 100644 --- a/resources/lib/UnityOrg.php +++ b/resources/lib/UnityOrg.php @@ -12,14 +12,16 @@ class UnityOrg private $SQL; private $LDAP; private $REDIS; + private $WEBHOOK; - public function __construct($orgid, $LDAP, $SQL, $MAILER, $REDIS) + public function __construct($orgid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK) { $this->orgid = $orgid; $this->LDAP = $LDAP; $this->SQL = $SQL; $this->MAILER = $MAILER; + $this->WEBHOOK = $WEBHOOK; $this->REDIS = $REDIS; } @@ -82,7 +84,7 @@ public function getOrgMembers($ignorecache = false) $out = array(); $cache_arr = array(); foreach ($members as $member) { - $user_obj = new UnityUser($member, $this->LDAP, $this->SQL, $this->MAILER, $this->REDIS); + $user_obj = new UnityUser($member, $this->LDAP, $this->SQL, $this->MAILER, $this->REDIS, $this->WEBHOOK); array_push($out, $user_obj); array_push($cache_arr, $user_obj->getUID()); } diff --git a/resources/lib/UnityUser.php b/resources/lib/UnityUser.php index 44f64690..44a0e2aa 100644 --- a/resources/lib/UnityUser.php +++ b/resources/lib/UnityUser.php @@ -16,8 +16,9 @@ class UnityUser private $SQL; private $MAILER; private $REDIS; + private $WEBHOOK; - public function __construct($uid, $LDAP, $SQL, $MAILER, $REDIS) + public function __construct($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK) { $this->uid = $uid; @@ -25,6 +26,7 @@ public function __construct($uid, $LDAP, $SQL, $MAILER, $REDIS) $this->SQL = $SQL; $this->MAILER = $MAILER; $this->REDIS = $REDIS; + $this->WEBHOOK = $WEBHOOK; } public function equals($other_user) @@ -514,7 +516,8 @@ public function getPIGroup() $this->LDAP, $this->SQL, $this->MAILER, - $this->REDIS + $this->REDIS, + $this->WEBHOOK ); } @@ -525,7 +528,8 @@ public function getOrgGroup() $this->LDAP, $this->SQL, $this->MAILER, - $this->REDIS + $this->REDIS, + $this->WEBHOOK ); } @@ -547,7 +551,8 @@ public function getGroups($ignorecache = false) $this->LDAP, $this->SQL, $this->MAILER, - $this->REDIS + $this->REDIS, + $this->WEBHOOK ); array_push($out, $group_obj); } diff --git a/resources/lib/UnityWebhook.php b/resources/lib/UnityWebhook.php new file mode 100644 index 00000000..00c80619 --- /dev/null +++ b/resources/lib/UnityWebhook.php @@ -0,0 +1,69 @@ +template_dir = $template_dir; + $this->override_template_dir = $override_template_dir; + $this->url = $url; + $this->MSG_LINKREF = $msg_linkref; + } + + public function htmlToMarkdown($html) + { + // Define regex patterns for each markdown format + $bold = '/<(b|strong)\b[^>]*>(.*?)<\/(b|strong)>/s'; + $italic = '/]*>(.*?)<\/i>/s'; + $strikethrough = '/]*>(.*?)<\/del>/s'; + $link = '/]*href=["\']?([^"\'\s]*)[^>]*>(.*?)<\/a>/s'; + + // Replace each HTML tag with its corresponding markdown format + $md = preg_replace($bold, '*$2*', $html); + $md = preg_replace($italic, '_$1_', $md); + $md = preg_replace($strikethrough, '~$1~', $md); + $md = preg_replace($link, '$2: $1', $md); + + // Replace any remaining HTML tags with an empty string + $md = strip_tags($md); + + return $md; + } + + public function sendWebhook($template = null, $data = null) + { + $template_filename = $template . ".php"; + if (file_exists($this->override_template_dir . "/" . $template_filename)) { + $template_path = $this->override_template_dir . "/" . $template_filename; + } else { + $template_path = $this->template_dir . "/" . $template_filename; + } + + ob_start(); + include $template_path; + $mes_html = ob_get_clean(); + + $message = $this->htmlToMarkdown($mes_html); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('text' => $message))); + $result = curl_exec($ch); + curl_close($ch); + return $result; + } +} diff --git a/webroot/admin/ajax/get_group_members.php b/webroot/admin/ajax/get_group_members.php index df624211..ebf6e25f 100644 --- a/webroot/admin/ajax/get_group_members.php +++ b/webroot/admin/ajax/get_group_members.php @@ -12,7 +12,7 @@ die("PI UID not set"); } -$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS); +$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); $members = $group->getGroupMembers(); $requests = $group->getRequests(); diff --git a/webroot/admin/pi-mgmt.php b/webroot/admin/pi-mgmt.php index 897049e7..af17ba17 100644 --- a/webroot/admin/pi-mgmt.php +++ b/webroot/admin/pi-mgmt.php @@ -11,7 +11,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST["uid"])) { - $form_user = new UnityUser($_POST["uid"], $LDAP, $SQL, $MAILER, $REDIS); + $form_user = new UnityUser($_POST["uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); } switch ($_POST["form_name"]) { @@ -28,12 +28,12 @@ break; case "remGroup": - $remGroup = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS); + $remGroup = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); $remGroup->removeGroup(); break; case "reqChild": - $parent_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS); + $parent_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); if ($_POST["action"] == "Approve") { // initialize user if not initialized if (!$form_user->exists()) { @@ -49,7 +49,7 @@ break; case "remUserChild": // remove user button clicked - $parent = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS); + $parent = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); $parent->removeUser($form_user); break; @@ -78,7 +78,7 @@ $requests = $SQL->getRequests(); foreach ($requests as $request) { - $request_user = new UnityUser($request["uid"], $LDAP, $SQL, $MAILER, $REDIS); + $request_user = new UnityUser($request["uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); echo ""; echo "" . $request_user->getFirstname() . " " . $request_user->getLastname() . ""; @@ -113,7 +113,7 @@ getAllPIGroups($SQL, $MAILER, $REDIS); + $accounts = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, $WEBHOOK); usort($accounts, function ($a, $b) { return strcmp($a->getPIUID(), $b->getPIUID()); diff --git a/webroot/admin/user-mgmt.php b/webroot/admin/user-mgmt.php index 72d10675..599cc73e 100644 --- a/webroot/admin/user-mgmt.php +++ b/webroot/admin/user-mgmt.php @@ -36,7 +36,7 @@ getAllUsers($SQL, $MAILER, $REDIS); + $users = $LDAP->getAllUsers($SQL, $MAILER, $REDIS, $WEBHOOK); usort($users, function ($a, $b) { return strcmp($a->getUID(), $b->getUID()); diff --git a/webroot/panel/ajax/get_group_members.php b/webroot/panel/ajax/get_group_members.php index 229264b9..92015cbf 100644 --- a/webroot/panel/ajax/get_group_members.php +++ b/webroot/panel/ajax/get_group_members.php @@ -8,7 +8,7 @@ die("PI UID not set"); } -$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS); +$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); $members = $group->getGroupMembers(); // verify that the user querying is actually in the group diff --git a/webroot/panel/groups.php b/webroot/panel/groups.php index ede3ba24..56fd01c3 100644 --- a/webroot/panel/groups.php +++ b/webroot/panel/groups.php @@ -10,7 +10,7 @@ if (isset($_POST["form_name"])) { if (isset($_POST["pi"])) { - $pi_account = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS); + $pi_account = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); if (!$pi_account->exists()) { // "\'" instead of "'", otherwise it will close a single quote used to place the message array_push($modalErrors, "This PI doesn\'t exist"); @@ -65,7 +65,7 @@ echo "
Pending Requests
"; echo ""; foreach ($req_filtered as $request) { - $requested_account = new UnityGroup($request["request_for"], $LDAP, $SQL, $MAILER, $REDIS); + $requested_account = new UnityGroup($request["request_for"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); $requested_owner = $requested_account->getOwner(); echo ""; echo ""; diff --git a/webroot/panel/modal/pi_search.php b/webroot/panel/modal/pi_search.php index 4604cd7e..09cdcba1 100644 --- a/webroot/panel/modal/pi_search.php +++ b/webroot/panel/modal/pi_search.php @@ -7,7 +7,7 @@ die("No Results"); } -$assocs = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS); +$assocs = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, $WEBHOOK); $MAX_COUNT = 10; // Max results of PI search diff --git a/webroot/panel/new_account.php b/webroot/panel/new_account.php index 67186663..dc3aa6b3 100644 --- a/webroot/panel/new_account.php +++ b/webroot/panel/new_account.php @@ -22,7 +22,7 @@ } if ($_POST["new_user_sel"] == "not_pi") { - $form_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS); + $form_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); if (!$form_group->exists()) { array_push($errors, "The selected PI does not exist"); } diff --git a/webroot/panel/pi.php b/webroot/panel/pi.php index e2c422db..85258581 100644 --- a/webroot/panel/pi.php +++ b/webroot/panel/pi.php @@ -13,7 +13,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST["uid"])) { - $form_user = new UnityUser($_POST["uid"], $LDAP, $SQL, $MAILER, $REDIS); + $form_user = new UnityUser($_POST["uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); } switch ($_POST["form_name"]) { diff --git a/workers/update-ldap-cache.php b/workers/update-ldap-cache.php index caefc0b8..026b94f5 100644 --- a/workers/update-ldap-cache.php +++ b/workers/update-ldap-cache.php @@ -3,7 +3,7 @@ require_once "../resources/autoload.php"; // Get Users -$users = $LDAP->getAllUsers($SQL, $MAILER, $REDIS, true); +$users = $LDAP->getAllUsers($SQL, $MAILER, $REDIS, $WEBHOOK, true); $sorted_uids = array(); @@ -32,7 +32,7 @@ $REDIS->setCache("sorted_users", "", $sorted_uids); // Get groups -$groups = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, true); +$groups = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, $WEBHOOK, true); $sorted_groups = array(); @@ -52,7 +52,7 @@ $REDIS->setCache("sorted_groups", "", $sorted_groups); // Get Orgs -$orgs = $LDAP->getAllOrgGroups($SQL, $MAILER, $REDIS, true); +$orgs = $LDAP->getAllOrgGroups($SQL, $MAILER, $REDIS, $WEBHOOK, true); $sorted_orgs = array();
" . $requested_owner->getFirstname() . " " . $requested_owner->getLastname() . "