Support including a short hash in json urls.

This commit is contained in:
flash 2024-01-28 03:50:24 +00:00
parent c8dc4aaac4
commit 1221acdf53

View file

@ -1,8 +1,14 @@
<?php <?php
define('FTM_ROOT', __DIR__ . '/..'); define('FTM_ROOT', __DIR__ . '/..');
define('FTM_HASH', 'sha1');
define('FTM_PATH_PUB', FTM_ROOT . '/public'); define('FTM_PATH_PUB', FTM_ROOT . '/public');
define('FTM_PATH_PRV', FTM_ROOT . '/private'); define('FTM_PATH_PRV', FTM_ROOT . '/private');
define('FTM_COMMON_CONFIG', FTM_PATH_PRV . '/common.ini');
define('FTM_SOUND_LIBRARY', FTM_PATH_PRV . '/sound-library.ini');
define('FTM_SOUND_PACKS', FTM_PATH_PRV . '/sound-packs.ini');
define('FTM_TEXT_TRIGGERS', FTM_PATH_PRV . '/text-triggers.ini');
define('FTM_LEGACY_SOUND_TYPE', [ define('FTM_LEGACY_SOUND_TYPE', [
'opus' => 'audio/ogg', 'opus' => 'audio/ogg',
'ogg' => 'audio/ogg', 'ogg' => 'audio/ogg',
@ -11,6 +17,14 @@ define('FTM_LEGACY_SOUND_TYPE', [
'wav' => 'audio/wav', 'wav' => 'audio/wav',
]); ]);
define('FTM_FILE_HASH_MAP', [
'common' => [FTM_COMMON_CONFIG],
'sounds' => [FTM_SOUND_LIBRARY, FTM_SOUND_PACKS],
'sounds2' => [FTM_SOUND_LIBRARY, FTM_SOUND_PACKS],
'texttriggers' => [FTM_TEXT_TRIGGERS],
'soundtriggers' => [FTM_TEXT_TRIGGERS],
]);
header('X-Powered-By: Futami'); header('X-Powered-By: Futami');
header('Cache-Control: max-age=86400, must-revalidate'); header('Cache-Control: max-age=86400, must-revalidate');
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
@ -34,6 +48,24 @@ function json_out($data): void {
exit; exit;
} }
function ftm_hash_str(string $data): string {
return hash(FTM_HASH, $data);
}
function ftm_hash_file(string $path): string {
return hash_file(FTM_HASH, $path);
}
function hash_mapped_file(string $name): string {
$files = FTM_FILE_HASH_MAP[$name] ?? [];
if(count($files) === 1)
return ftm_hash_file($files[0]);
$data = '';
foreach($files as $file)
$data .= file_get_contents($file);
return ftm_hash_str($data);
}
function match_etag($eTag): void { function match_etag($eTag): void {
if(filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH') === $eTag) { if(filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH') === $eTag) {
http_response_code(304); http_response_code(304);
@ -41,10 +73,10 @@ function match_etag($eTag): void {
} }
} }
function gen_etag(string $user, string $data): string { function gen_etag(string $user, string $data): string {
return sprintf('W/"%s-%s"', hash('sha1', $data), $user); return sprintf('W/"%s-%s"', ftm_hash_str($data), $user);
} }
function gen_etag_file(string $user, string $path): string { function gen_etag_file(string $user, string $path): string {
return gen_etag($user, hash_file('sha1', $path)); return gen_etag($user, ftm_hash_file($path));
} }
function etag(string $etag): void { function etag(string $etag): void {
match_etag($etag); match_etag($etag);
@ -60,22 +92,32 @@ function etag_file(string $user, string $path): void {
$reqPath = '/' . trim(parse_url((string)filter_input(INPUT_SERVER, 'REQUEST_URI'), PHP_URL_PATH), '/'); $reqPath = '/' . trim(parse_url((string)filter_input(INPUT_SERVER, 'REQUEST_URI'), PHP_URL_PATH), '/');
if($reqPath === '/common.json') { if($reqPath === '/common.json') {
$commonPath = FTM_PATH_PRV . '/common.ini'; etag_file('common', FTM_COMMON_CONFIG);
etag_file('common', $commonPath);
$common = parse_ini_file($commonPath, false, INI_SCANNER_TYPED); $config = parse_ini_file(FTM_COMMON_CONFIG, false, INI_SCANNER_TYPED);
$common = new stdClass;
$colours = $common['colours']; foreach($config as $key => $value) {
$common['colours'] = []; if($key === 'colours') {
foreach($colours as $n => $c) $dict = $value;
$common['colours'][] = compact('n', 'c'); $value = [];
foreach($dict as $name => $raw)
$value[] = ['n' => $name, 'c' => $raw];
} elseif(is_string($value) && str_starts_with($value, '::')) {
$parts = explode(':', substr($value, 2));
$hash = hash_mapped_file($parts[0]);
$value = sprintf($parts[1], $hash);
}
$common->{$key} = $value;
}
json_out($common); json_out($common);
} }
if($reqPath === '/sounds2.json') { if(preg_match('#^/sounds2(\.[a-f0-9]{8})?.json$#', $reqPath)) {
$sndLibData = file_get_contents(FTM_PATH_PRV . '/sound-library.ini'); $sndLibData = file_get_contents(FTM_SOUND_LIBRARY);
$sndPackData = file_get_contents(FTM_PATH_PRV . '/sound-packs.ini'); $sndPackData = file_get_contents(FTM_SOUND_PACKS);
etag_data('sounds2', $sndLibData . $sndPackData); etag_data('sounds2', $sndLibData . $sndPackData);
@ -101,9 +143,9 @@ if($reqPath === '/sounds2.json') {
json_out(compact('library', 'packs')); json_out(compact('library', 'packs'));
} }
if($reqPath === '/sounds.json') { if(preg_match('#^/sounds(\.[a-f0-9]{8})?.json$#', $reqPath)) {
$sndLibData = file_get_contents(FTM_PATH_PRV . '/sound-library.ini'); $sndLibData = file_get_contents(FTM_SOUND_LIBRARY);
$sndPackData = file_get_contents(FTM_PATH_PRV . '/sound-packs.ini'); $sndPackData = file_get_contents(FTM_SOUND_PACKS);
etag_data('sounds', $sndLibData . $sndPackData); etag_data('sounds', $sndLibData . $sndPackData);
@ -138,11 +180,10 @@ if($reqPath === '/sounds.json') {
json_out(compact('library', 'packs')); json_out(compact('library', 'packs'));
} }
if($reqPath === '/texttriggers.json') { if(preg_match('#^/texttriggers(\.[a-f0-9]{8})?.json$#', $reqPath)) {
$textTriggersPath = FTM_PATH_PRV . '/text-triggers.ini'; etag_file('texttriggers', FTM_TEXT_TRIGGERS);
etag_file('texttriggers', $textTriggersPath);
$triggers = parse_ini_file($textTriggersPath, true, INI_SCANNER_TYPED); $triggers = parse_ini_file(FTM_TEXT_TRIGGERS, true, INI_SCANNER_TYPED);
if($triggers === false) if($triggers === false)
$triggers = []; $triggers = [];
@ -152,12 +193,11 @@ if($reqPath === '/texttriggers.json') {
json_out($triggers); json_out($triggers);
} }
if($reqPath === '/soundtriggers.json') { if(preg_match('#^/soundtriggers(\.[a-f0-9]{8})?.json$#', $reqPath)) {
$textTriggersPath = FTM_PATH_PRV . '/text-triggers.ini'; etag_file('soundtriggers', FTM_TEXT_TRIGGERS);
etag_file('soundtriggers', $textTriggersPath);
$textTriggers = parse_ini_file($textTriggersPath, true, INI_SCANNER_TYPED); $textTriggers = parse_ini_file(FTM_TEXT_TRIGGERS, true, INI_SCANNER_TYPED);
$sndLib = parse_ini_file(FTM_PATH_PRV . '/sound-library.ini', true, INI_SCANNER_TYPED); $sndLib = parse_ini_file(FTM_SOUND_LIBRARY, true, INI_SCANNER_TYPED);
$soundTrigs = []; $soundTrigs = [];
foreach($textTriggers as $triggerInfo) { foreach($textTriggers as $triggerInfo) {