1
0
Fork 0
mirror of https://github.com/Alamantus/Lexiconga.git synced 2025-08-01 19:47:34 +02:00

Make router.php automatically handle being in a web subdirectory

This commit is contained in:
Robbie Antenesse 2024-12-16 23:39:11 -07:00
parent 89fa069d42
commit 70871478ca

View file

@ -1,5 +1,6 @@
<?php <?php
require_once(realpath(dirname(__FILE__) . '/./api/Response.php')); $scriptDir = dirname(__FILE__);
require_once(realpath($scriptDir . '/./api/Response.php'));
$show_upgrade_screen = false; $show_upgrade_screen = false;
if ($show_upgrade_screen) { if ($show_upgrade_screen) {
@ -9,6 +10,14 @@ if ($show_upgrade_screen) {
$view = isset($_GET['view']) ? $_GET['view'] : false; $view = isset($_GET['view']) ? $_GET['view'] : false;
$scriptName = str_replace($scriptDir, '', __FILE__);
$subdir = $_SERVER['SCRIPT_NAME'];
$subdir = substr($subdir, 0, strrpos($subdir, $scriptName));
$subdir = trim($subdir, '/ ');
if (!empty($subdir)) {
$subdir .= '/';
}
function utf8ize($d) { function utf8ize($d) {
if (is_array($d)) { if (is_array($d)) {
foreach ($d as $k => $v) { foreach ($d as $k => $v) {
@ -22,10 +31,11 @@ function utf8ize($d) {
switch ($view) { switch ($view) {
case 'dictionary': { 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; $dict = isset($_GET['dict']) ? $_GET['dict'] : false;
if ($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 = new PublicDictionary($dict);
$dictionary_data = $dictionary->details; $dictionary_data = $dictionary->details;
if ($dictionary_data !== false) { if ($dictionary_data !== false) {
@ -48,11 +58,12 @@ switch ($view) {
break; break;
} }
case 'word': { 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; $dict = isset($_GET['dict']) ? $_GET['dict'] : false;
$word = isset($_GET['word']) ? $_GET['word'] : false; $word = isset($_GET['word']) ? $_GET['word'] : false;
if ($dict !== false && $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 = new PublicDictionary($dict, true);
$dictionary_data = $dictionary->details; $dictionary_data = $dictionary->details;
if ($dictionary_data !== false) { if ($dictionary_data !== false) {
@ -89,8 +100,9 @@ switch ($view) {
} }
default: { default: {
$html = file_get_contents(realpath(dirname(__FILE__) . '/./template-index.html')); $html = file_get_contents(realpath($scriptDir . '/./template-index.html'));
$announcements = file_get_contents(realpath(dirname(__FILE__) . '/./announcements.json')); $html = preg_replace('/ (href|src)="\//', ' $1="/' . $subdir, $html);
$announcements = file_get_contents(realpath($scriptDir . '/./announcements.json'));
$announcements = json_decode($announcements, true); $announcements = json_decode($announcements, true);
$announcements_html = ''; $announcements_html = '';
foreach ($announcements as $announcement) { foreach ($announcements as $announcement) {
@ -156,4 +168,4 @@ switch ($view) {
return Response::html($html); return Response::html($html);
break; break;
} }
} }