diff --git a/emhttp/auth-request.php b/emhttp/auth-request.php index e32448694..4a82843a1 100644 --- a/emhttp/auth-request.php +++ b/emhttp/auth-request.php @@ -14,30 +14,8 @@ if (isset($_COOKIE[session_name()])) { session_write_close(); } -// Function to recursively find JS files in a directory -function findJsFiles($directory) { - if (!is_dir($directory)) { - return []; - } - - $jsFiles = []; - $iterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS) - ); - - foreach ($iterator as $file) { - if ($file->isFile() && $file->getExtension() === 'js') { - $path = $file->getPathname(); - $baseDir = '/usr/local/emhttp'; - if (strpos($path, $baseDir) === 0) { - $path = substr($path, strlen($baseDir)); - } - $jsFiles[] = $path; - } - } - - return $jsFiles; -} +// Include JS caching functions +require_once '/usr/local/emhttp/webGui/include/JSCache.php'; // Base whitelist of files $arrWhitelist = [ @@ -69,15 +47,9 @@ $arrWhitelist = [ '/webGui/images/UN-logotype-gradient.svg' ]; -// Add JS files from the unraid-components directory +// Add JS files from the unraid-components directory using cache $webComponentsDirectory = '/usr/local/emhttp/plugins/dynamix.my.servers/unraid-components/'; -$jsFiles = findJsFiles($webComponentsDirectory); - -// Add logo asset if needed -// Note: In the original JS, there was a getters.paths().webgui.logo.assetPath that we'd add -// Since we don't have access to that here, you may need to add the specific logo path if known - -// Merge the JS files with the whitelist +$jsFiles = getCachedJSFiles($webComponentsDirectory); $arrWhitelist = array_merge($arrWhitelist, $jsFiles); if (in_array(preg_replace(['/\?v=\d+$/','/\?\d+$/'],'',$_SERVER['REQUEST_URI']),$arrWhitelist)) { diff --git a/emhttp/plugins/dynamix/include/JSCache.php b/emhttp/plugins/dynamix/include/JSCache.php new file mode 100644 index 000000000..ad7bd99a8 --- /dev/null +++ b/emhttp/plugins/dynamix/include/JSCache.php @@ -0,0 +1,53 @@ +isFile() && $file->getExtension() === 'js') { + $path = $file->getPathname(); + $baseDir = '/usr/local/emhttp'; + if (strpos($path, $baseDir) === 0) { + $path = substr($path, strlen($baseDir)); + } + $jsFiles[] = $path; + } + } + + return $jsFiles; +} + +// Function to get JS files with caching +function getCachedJSFiles($directory) { + $cacheFile = '/tmp/js_files_cache.php'; + $cacheLifetime = 300; // Cache lifetime in seconds (5 minutes) + + // Check if cache exists and is still valid + $useCache = false; + if (file_exists($cacheFile)) { + $cacheTime = filemtime($cacheFile); + if (time() - $cacheTime < $cacheLifetime) { + $useCache = true; + } + } + + if ($useCache) { + // Use cached JS files list + return include $cacheFile; + } else { + // Generate new JS files list + $jsFiles = findJsFiles($directory); + + // Store in cache file + file_put_contents($cacheFile, '