Skip to content

Instantly share code, notes, and snippets.

@TheExpertNoob
Last active October 22, 2024 00:28
Show Gist options
  • Save TheExpertNoob/2ad34d60c19dc2b2b4ecc788fe1fd7ef to your computer and use it in GitHub Desktop.
Save TheExpertNoob/2ad34d60c19dc2b2b4ecc788fe1fd7ef to your computer and use it in GitHub Desktop.
Retro Rom scanner for Tinfoil.
<?php
// Configuration values
$expectedHauth = '0123456789ABCDEF0123456789ABCDEF';
$altHauth = 'FEDCBA9876543210FEDCBA9876543210';
$defaultHtml = "../index.html";
// Get HAUTH header
$hauth = $_SERVER['HTTP_HAUTH'] ?? null;
// Check if HAUTH matches expectedHauth or altHauth
if ($hauth === $expectedHauth || $hauth === $altHauth) {
// Define the base URL for the files directory
$baseUrl = "https://domainexample.com/Retro/";
// Initialize an array to hold the file details
$files = [];
// Define allowed extensions
$allowedExtensions = ['gba', 'gbc', 'gb', 'nds', 'nes', 'nez', 'fds', 'fam', 'sfc', 'srm', 'swc', 'smc', 'fig', '32x', 'sms', 'smd', 'v64', 'z64', 'n64', 'pbp', 'psp', 'cso', 'prx', 'cdi', 'chd', 'gdi'];
// Recursive function to scan directories
function scanDirectory($dir, $baseUrl, $allowedExtensions, &$files) {
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
// Skip '.' and '..'
if ($entry == '.' || $entry == '..') continue;
$path = $dir . '/' . $entry;
// If it's a directory, recursively scan it
if (is_dir($path)) {
scanDirectory($path, $baseUrl . $entry . '/', $allowedExtensions, $files);
}
// If it's a file, check the extension and add to the array
$ext = pathinfo($entry, PATHINFO_EXTENSION);
if (is_file($path) && in_array($ext, $allowedExtensions)) {
$files[] = [
'url' => $baseUrl . $entry,
'size' => filesize($path) // Get file size in bytes
];
}
}
closedir($handle);
}
}
// Start scanning from the current directory
scanDirectory(__DIR__, $baseUrl, $allowedExtensions, $files);
// Prepare the JSON response
$response = [
"files" => $files,
"success" => "Welcome to Retro Roms!"
];
// Set the Content-Type header to application/json
header('Content-Type: application/json');
// Output the JSON response
echo str_replace('\\/', '/', json_encode($response, JSON_PRETTY_PRINT));
exit();
}
// If HAUTH doesn't match
header("Location: $defaultHtml");
exit();
?>
@TheExpertNoob
Copy link
Author

Just an index file to scan the directory it is in for various retro rom files and spit out a Tinfoil index json.
Change the HAUTH lines for yours, as well as the baseURL.
Drop all roms and this index.php file into a directory named Retro into your host's root directory.

@TheExpertNoob
Copy link
Author

Updated for recursive directory scanning.

@TheExpertNoob
Copy link
Author

Added all "supposed" Tinfoil supported extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment