From 70871478ca327182b355c7065048462e24278d92 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Mon, 16 Dec 2024 23:39:11 -0700 Subject: [PATCH] Make router.php automatically handle being in a web subdirectory --- src/php/router.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/php/router.php b/src/php/router.php index 6960333..0bb9fdc 100644 --- a/src/php/router.php +++ b/src/php/router.php @@ -1,5 +1,6 @@ $v) { @@ -22,10 +31,11 @@ function utf8ize($d) { switch ($view) { case 'dictionary': { - $html = file_get_contents(realpath(dirname(__FILE__) . '/./template-view.html')); + $html = file_get_contents(realpath($scriptDir . '/./template-view.html')); + $html = preg_replace('/ (href|src)="\//', ' $1="/' . $subdir, $html); $dict = isset($_GET['dict']) ? $_GET['dict'] : false; if ($dict !== false) { - require_once(realpath(dirname(__FILE__) . '/./api/PublicDictionary.php')); + require_once(realpath($scriptDir . '/./api/PublicDictionary.php')); $dictionary = new PublicDictionary($dict); $dictionary_data = $dictionary->details; if ($dictionary_data !== false) { @@ -48,11 +58,12 @@ switch ($view) { break; } case 'word': { - $html = file_get_contents(realpath(dirname(__FILE__) . '/./template-view.html')); + $html = file_get_contents(realpath($scriptDir . '/./template-view.html')); + $html = preg_replace('/ (href|src)="\//', ' $1="/' . $subdir, $html); $dict = isset($_GET['dict']) ? $_GET['dict'] : false; $word = isset($_GET['word']) ? $_GET['word'] : false; if ($dict !== false && $word !== false) { - require_once(realpath(dirname(__FILE__) . '/./api/PublicDictionary.php')); + require_once(realpath($scriptDir . '/./api/PublicDictionary.php')); $dictionary = new PublicDictionary($dict, true); $dictionary_data = $dictionary->details; if ($dictionary_data !== false) { @@ -89,8 +100,9 @@ switch ($view) { } default: { - $html = file_get_contents(realpath(dirname(__FILE__) . '/./template-index.html')); - $announcements = file_get_contents(realpath(dirname(__FILE__) . '/./announcements.json')); + $html = file_get_contents(realpath($scriptDir . '/./template-index.html')); + $html = preg_replace('/ (href|src)="\//', ' $1="/' . $subdir, $html); + $announcements = file_get_contents(realpath($scriptDir . '/./announcements.json')); $announcements = json_decode($announcements, true); $announcements_html = ''; foreach ($announcements as $announcement) { @@ -156,4 +168,4 @@ switch ($view) { return Response::html($html); break; } -} \ No newline at end of file +}