Skip to content

Commit 8cef3d5

Browse files
authored
getURL(), getHyperlink() (#385)
1 parent 7adb9d9 commit 8cef3d5

25 files changed

+110
-80
lines changed

defaults/config.ini.default

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repo = "https://github.com/UnityHPC/unity-web-portal" ; Upstream URL for the we
88
[site]
99
prefix = "" ; prefix of website, no ending / should be included
1010
name = "Unity Cluster" ; Name of the website
11-
url = "https://127.0.0.1:8000/" ; URL of the website
11+
url = "http://127.0.0.1:8000/" ; URL of the website
1212
description = "The Unity Web Portal is a lightweight HPC cluster front-end" ; Description of the website
1313
logo = "logo.png" ; path to logo file, in the webroot/assets/branding folder
1414
terms_of_service_url = "https://github.com" ; this can be external or a portal page created with "content management"

resources/lib/UnityHTTPD.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public static function die(mixed $x = null, bool $show_user = false): never
4242
*/
4343
public static function redirect(?string $dest = null): never
4444
{
45-
$dest ??= pathJoin(CONFIG["site"]["prefix"], $_SERVER["REQUEST_URI"]);
46-
$dest = htmlspecialchars($dest);
45+
$dest ??= getURL($_SERVER["REQUEST_URI"]);
4746
header("Location: $dest");
4847
http_response_code(302);
4948
if (CONFIG["site"]["enable_redirect_message"]) {

resources/lib/UnityMailer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class UnityMailer extends PHPMailer
1313
private string $template_dir = __DIR__ . "/../mail"; // location of all email templates
1414
private string $override_template_dir = __DIR__ . "/../../deployment/mail_overrides";
1515

16-
private string $MSG_LINKREF;
1716
private string $MSG_SENDER_EMAIL;
1817
private string $MSG_SENDER_NAME;
1918
private string $MSG_SUPPORT_EMAIL;
@@ -28,7 +27,6 @@ public function __construct()
2827
parent::__construct();
2928
$this->isSMTP();
3029

31-
$this->MSG_LINKREF = CONFIG["site"]["url"] . CONFIG["site"]["prefix"];
3230
$this->MSG_SENDER_EMAIL = CONFIG["mail"]["sender"];
3331
$this->MSG_SENDER_NAME = CONFIG["mail"]["sender_name"];
3432
$this->MSG_SUPPORT_EMAIL = CONFIG["mail"]["support"];

resources/lib/UnityWebhook.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ class UnityWebhook
77
private string $template_dir = __DIR__ . "/../mail";
88
private string $override_template_dir = __DIR__ . "/../../deployment/mail_overrides";
99
private string $url = CONFIG["webhook"]["url"];
10-
private string $MSG_LINKREF;
1110
private string $Subject; // set by template
1211

13-
public function __construct()
14-
{
15-
$this->MSG_LINKREF = CONFIG["site"]["url"] . CONFIG["site"]["prefix"];
16-
}
1712
public function htmlToMarkdown(string $html): string
1813
{
1914
// Define regex patterns for each markdown format

resources/lib/utils.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,27 @@ function mbDetectEncoding(string $string, ?array $encodings = null, mixed $_ = n
7272
}
7373

7474
/* https://stackoverflow.com/a/15575293/18696276 */
75-
function pathJoin()
75+
function pathNormalize(string $path)
7676
{
77-
$paths = [];
78-
foreach (func_get_args() as $arg) {
79-
if ($arg !== "") {
80-
$paths[] = $arg;
81-
}
77+
return preg_replace("#/+#", "/", $path);
78+
}
79+
80+
function getURL(...$relative_url_components)
81+
{
82+
if (!preg_match("#^\w+://#", CONFIG["site"]["url"])) {
83+
throw new RuntimeException('CONFIG[site][url] does not have a scheme! (ex: "https://")');
8284
}
83-
return preg_replace("#/+#", "/", join("/", $paths));
85+
$matches = [];
86+
preg_match("#(^\w+://)(.*)#", CONFIG["site"]["url"], $matches);
87+
[$_, $site_url_scheme, $site_url_noscheme] = $matches;
88+
$path = join("/", [$site_url_noscheme, CONFIG["site"]["prefix"], ...$relative_url_components]);
89+
$path_normalized = pathNormalize($path);
90+
return $site_url_scheme . $path_normalized;
91+
}
92+
93+
function getHyperlink($text, ...$url_components)
94+
{
95+
$text = htmlspecialchars($text);
96+
$url = getURL(...$url_components);
97+
return "<a href='$url'>$text</a>";
8498
}

resources/mail/footer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
>
44
<span>
55
You are receiving this email because you have an account
6-
on the <a target='_blank' href='<?php echo $this->MSG_LINKREF; ?>'>Unity Cluster</a>.
6+
on the <?php echo getHyperlink("Unity Cluster", "/"); ?>.
77
If you would like to stop receiving these emails,
88
you may request to close your account by replying to this email.
99
</span>

resources/mail/group_created.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<p>
99
Your request for a PI account on the Unity cluster has been approved.
1010
You can access the management page for your group
11-
<a href="<?php echo $this->MSG_LINKREF; ?>/panel/pi.php">on this page</a>.
11+
<?php echo getHyperlink("on this page", "panel/pi.php"); ?>.
1212
</p>
1313

1414
<p>Do not hesitate to reply if you have any questions!</p>

resources/mail/group_request_admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
<p>
2121
You can approve this account
22-
<a href="<?php echo $this->MSG_LINKREF; ?>/admin/pi-mgmt.php">here</a>
22+
<?php echo getHyperlink("here", "admin/pi-mgmt.php"); ?>
2323
.
2424
</p>

resources/mail/group_user_added.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<p>Hello,</p>
77

88
<p>You have been approved to join the PI group <?php echo $data["group"]; ?>.
9-
Navigate to the <a href="<?php echo $this->MSG_LINKREF; ?>/panel/groups.php">my groups</a>
9+
Navigate to the <?php echo getHyperlink("my groups", "panel/groups.php"); ?>
1010
page to see your PI groups.</p>
1111

1212
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>

resources/mail/group_user_request_owner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
</p>
2323

2424
<p>You can approve or deny this user on the
25-
<a href="<?php echo $this->MSG_LINKREF; ?>/panel/pi.php">my users</a> page</p>
25+
<?php echo getHyperlink("my users", "panel/pi.php"); ?> page</p>

0 commit comments

Comments
 (0)