42 lines
1 KiB
PHP
42 lines
1 KiB
PHP
<?php
|
|
$remoteUrl = 'https://stats.apps.coryd.dev/script.js';
|
|
$cacheKey = 'remote_stats_script';
|
|
$ttl = 3600;
|
|
$js = null;
|
|
$code = 200;
|
|
$redis = null;
|
|
|
|
try {
|
|
if (extension_loaded('redis')) {
|
|
$redis = new Redis();
|
|
$redis->connect('127.0.0.1', 6379);
|
|
|
|
if ($redis->exists($cacheKey)) $js = $redis->get($cacheKey);
|
|
}
|
|
} catch (Exception $e) {
|
|
error_log("Redis unavailable: " . $e->getMessage());
|
|
}
|
|
|
|
if (!is_string($js)) {
|
|
$ch = curl_init($remoteUrl);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
|
|
$js = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($redis && $code === 200 && $js) $redis->setex($cacheKey, $ttl, $js);
|
|
}
|
|
|
|
if (!is_string($js) || trim($js) === '') {
|
|
$js = '// Failed to fetch remote script';
|
|
$code = 502;
|
|
}
|
|
|
|
http_response_code($code);
|
|
header('Content-Type: application/javascript; charset=UTF-8');
|
|
header('Cache-Control: public, max-age=60');
|
|
echo $js;
|