|
<?php |
|
// index.php |
|
|
|
// 配置区域 |
|
define('ADMIN_PASSWORD', 'admin123'); // 管理员密码 |
|
define('CRON_TOKEN', 'cron123'); // 定时任务token |
|
define('CACHE_DIR', 'cache/'); // 缓存目录 |
|
define('URLS_FILE', '_urls.php'); // 链接存储文件 |
|
define('USE_REWRITE', true); // 是否使用伪静态URL,true为启用,false为禁用 |
|
|
|
// 初始化 |
|
session_start(); |
|
if (!file_exists(CACHE_DIR)) mkdir(CACHE_DIR, 0755, true); |
|
if (!file_exists(URLS_FILE)) file_put_contents(URLS_FILE, "<?php\n\$urls = array();\n?>"); |
|
|
|
// 包含链接文件 |
|
require_once URLS_FILE; |
|
|
|
// 处理URL请求 |
|
if (isset($_GET['url'])) { |
|
handleUrlRequest($_GET['url']); |
|
exit; |
|
} |
|
|
|
// 处理定时任务 |
|
if (isset($_GET['cron'])) { |
|
handleCronRequest($_GET['cron']); |
|
exit; |
|
} |
|
|
|
// 处理一键缓存 |
|
if (isset($_GET['refresh_all'])) { |
|
handleRefreshAll(); |
|
exit; |
|
} |
|
|
|
// 处理管理界面 |
|
handleAdminInterface(); |
|
|
|
// 函数定义 |
|
|
|
/** |
|
* 处理URL请求 |
|
*/ |
|
function handleUrlRequest($url) { |
|
global $urls; |
|
|
|
$urlHash = md5($url); |
|
$cacheFile = CACHE_DIR . $urlHash . '.tmp'; |
|
|
|
// 如果缓存存在,直接返回 |
|
if (file_exists($cacheFile)) { |
|
header('Content-Type: ' . mime_content_type($cacheFile)); |
|
readfile($cacheFile); |
|
return; |
|
} |
|
|
|
// 检查URL是否在列表中 |
|
if (!isset($urls[$urlHash])) { |
|
http_response_code(404); |
|
echo "URL not found in database"; |
|
return; |
|
} |
|
|
|
// 下载并缓存文件 |
|
$content = @file_get_contents($url); |
|
if ($content === false) { |
|
http_response_code(500); |
|
echo "Failed to fetch URL content"; |
|
return; |
|
} |
|
|
|
file_put_contents($cacheFile, $content); |
|
header('Content-Type: ' . mime_content_type($cacheFile)); |
|
echo $content; |
|
} |
|
|
|
/** |
|
* 处理定时任务 |
|
*/ |
|
function handleCronRequest($token) { |
|
global $urls; |
|
|
|
if ($token !== CRON_TOKEN) { |
|
http_response_code(403); |
|
echo "Invalid token"; |
|
return; |
|
} |
|
|
|
$updated = 0; |
|
foreach ($urls as $urlHash => $url) { |
|
$cacheFile = CACHE_DIR . $urlHash . '.tmp'; |
|
$remoteContent = @file_get_contents($url); |
|
|
|
if ($remoteContent === false) continue; |
|
|
|
// 如果缓存不存在或内容不同,则更新 |
|
if (!file_exists($cacheFile) || md5_file($cacheFile) !== md5($remoteContent)) { |
|
file_put_contents($cacheFile, $remoteContent); |
|
$updated++; |
|
} |
|
} |
|
|
|
echo "Cron job completed. Updated $updated files."; |
|
} |
|
|
|
/** |
|
* 处理一键缓存 |
|
*/ |
|
function handleRefreshAll() { |
|
global $urls; |
|
|
|
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) { |
|
http_response_code(403); |
|
echo "Access denied"; |
|
return; |
|
} |
|
|
|
$updated = 0; |
|
foreach ($urls as $urlHash => $url) { |
|
$cacheFile = CACHE_DIR . $urlHash . '.tmp'; |
|
$remoteContent = @file_get_contents($url); |
|
|
|
if ($remoteContent === false) continue; |
|
|
|
file_put_contents($cacheFile, $remoteContent); |
|
$updated++; |
|
} |
|
|
|
header('Location: ' . $_SERVER['PHP_SELF'] . '?refresh_success=' . $updated); |
|
exit; |
|
} |
|
|
|
/** |
|
* 处理管理界面 |
|
*/ |
|
function handleAdminInterface() { |
|
global $urls; |
|
|
|
// 检查登录状态 |
|
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) { |
|
// 处理登录 |
|
if (isset($_POST['password']) && $_POST['password'] === ADMIN_PASSWORD) { |
|
$_SESSION['logged_in'] = true; |
|
} else { |
|
showLoginForm(); |
|
return; |
|
} |
|
} |
|
|
|
// 处理登出 |
|
if (isset($_GET['logout'])) { |
|
session_destroy(); |
|
header('Location: ' . $_SERVER['PHP_SELF']); |
|
exit; |
|
} |
|
|
|
// 处理添加URL |
|
if (isset($_POST['new_url'])) { |
|
$newUrl = trim($_POST['new_url']); |
|
if (!empty($newUrl) && filter_var($newUrl, FILTER_VALIDATE_URL)) { |
|
$urlHash = md5($newUrl); |
|
$urls[$urlHash] = $newUrl; |
|
saveUrls(); |
|
} |
|
} |
|
|
|
// 处理删除URL |
|
if (isset($_GET['delete'])) { |
|
$urlHash = $_GET['delete']; |
|
if (isset($urls[$urlHash])) { |
|
unset($urls[$urlHash]); |
|
saveUrls(); |
|
|
|
// 删除对应的缓存文件 |
|
$cacheFile = CACHE_DIR . $urlHash . '.tmp'; |
|
if (file_exists($cacheFile)) { |
|
unlink($cacheFile); |
|
} |
|
} |
|
header('Location: ' . $_SERVER['PHP_SELF']); |
|
exit; |
|
} |
|
|
|
// 显示管理界面 |
|
showAdminInterface(); |
|
} |
|
|
|
/** |
|
* 保存URL列表 |
|
*/ |
|
function saveUrls() { |
|
global $urls; |
|
$content = "<?php\n\$urls = array(\n"; |
|
foreach ($urls as $hash => $url) { |
|
$content .= " '$hash' => '$url',\n"; |
|
} |
|
$content .= ");\n?>"; |
|
file_put_contents(URLS_FILE, $content); |
|
} |
|
|
|
/** |
|
* 显示登录表单 |
|
*/ |
|
function showLoginForm() { |
|
echo <<<HTML |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Login - Cache URL Manager</title> |
|
<style> |
|
body { font-family: Arial, sans-serif; max-width: 500px; margin: 0 auto; padding: 20px; } |
|
form { background: #f9f9f9; padding: 20px; border-radius: 5px; } |
|
input[type="password"] { width: 100%; padding: 8px; margin: 8px 0; box-sizing: border-box; } |
|
input[type="submit"] { background: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; } |
|
input[type="submit"]:hover { background: #45a049; } |
|
</style> |
|
</head> |
|
<body> |
|
<h2>Login</h2> |
|
<form method="post"> |
|
<label for="password">Password:</label> |
|
<input type="password" id="password" name="password" required> |
|
<input type="submit" value="Login"> |
|
</form> |
|
</body> |
|
</html> |
|
HTML; |
|
} |
|
|
|
/** |
|
* 显示管理界面 |
|
*/ |
|
function showAdminInterface() { |
|
global $urls; |
|
|
|
// 获取当前URL信息 |
|
$scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http"; |
|
$host = $_SERVER['HTTP_HOST']; |
|
$port = $_SERVER['SERVER_PORT']; |
|
$path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/'); |
|
|
|
// 构建完整的URL |
|
$baseUrl = "$scheme://$host"; |
|
if (($scheme === 'http' && $port != 80) || ($scheme === 'https' && $port != 443)) { |
|
$baseUrl .= ":$port"; |
|
} |
|
$baseUrl .= "$path"; |
|
|
|
// 根据是否启用伪静态设置不同的URL格式 |
|
if (USE_REWRITE) { |
|
$fullCronUrl = "$baseUrl/cron/" . CRON_TOKEN; |
|
} else { |
|
$fullCronUrl = "$baseUrl/index.php?cron=" . CRON_TOKEN; |
|
} |
|
|
|
// 刷新成功消息 |
|
$refreshMessage = ''; |
|
if (isset($_GET['refresh_success'])) { |
|
$count = intval($_GET['refresh_success']); |
|
$refreshMessage = '<div class="alert success">Successfully refreshed ' . $count . ' cached files.</div>'; |
|
} |
|
|
|
echo <<<HTML |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Cache URL Manager</title> |
|
<style> |
|
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } |
|
.header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; } |
|
.logout { background: #f44336; color: white; padding: 8px 15px; border-radius: 4px; text-decoration: none; display: inline-block; } |
|
.logout:hover { background: #d32f2f; } |
|
.refresh-btn { background: #ff9800; color: white; padding: 8px 15px; border-radius: 4px; text-decoration: none; display: inline-block; margin-right: 10px; } |
|
.refresh-btn:hover { background: #f57c00; } |
|
.url-form { background: #f9f9f9; padding: 20px; border-radius: 5px; margin-bottom: 20px; } |
|
.url-input { width: 70%; padding: 8px; margin-right: 10px; } |
|
.submit-btn { background: #4CAF50; color: white; padding: 8px 15px; border: none; border-radius: 4px; cursor: pointer; } |
|
.submit-btn:hover { background: #45a049; } |
|
.url-list { list-style: none; padding: 0; } |
|
.url-item { background: white; padding: 10px 15px; margin-bottom: 5px; border-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); } |
|
.delete-btn { color: #f44336; text-decoration: none; margin-left: 15px; } |
|
.copy-btn { color: #2196F3; text-decoration: none; margin-left: 10px; } |
|
.cache-info { font-size: 0.8em; color: #666; margin-left: 10px; } |
|
.cron-section { background: #f0f7ff; padding: 15px; border-radius: 5px; margin: 20px 0; } |
|
.cron-url { background: white; padding: 10px; border-radius: 4px; font-family: monospace; margin: 10px 0; word-break: break-all; } |
|
.copy-btn-main { background: #2196F3; color: white; border: none; padding: 5px 10px; border-radius: 4px; cursor: pointer; margin-left: 10px; } |
|
.copy-btn-main:hover { background: #0b7dda; } |
|
.info-grid { display: grid; grid-template-columns: 100px 1fr; gap: 10px; margin: 10px 0; } |
|
.info-label { font-weight: bold; color: #555; } |
|
.alert { padding: 10px; margin: 10px 0; border-radius: 4px; } |
|
.alert.success { background: #dff0d8; color: #3c763d; } |
|
.url-actions { margin-top: 8px; } |
|
</style> |
|
<script> |
|
function copyToClipboard(text) { |
|
navigator.clipboard.writeText(text).then(function() { |
|
alert('URL copied to clipboard!'); |
|
}, function(err) { |
|
console.error('Could not copy text: ', err); |
|
}); |
|
} |
|
</script> |
|
</head> |
|
<body> |
|
<div class="header"> |
|
<h1>URL Manager</h1> |
|
<div> |
|
<a href="?refresh_all" class="refresh-btn" onclick="return confirm('Refresh all cached files now?')">Refresh All Cache</a> |
|
<a href="?logout" class="logout">Logout</a> |
|
</div> |
|
</div> |
|
|
|
{$refreshMessage} |
|
|
|
<div class="url-form"> |
|
<form method="post"> |
|
<input type="url" name="new_url" class="url-input" placeholder="Enter URL to add" required> |
|
<input type="submit" value="Add URL" class="submit-btn"> |
|
</form> |
|
</div> |
|
|
|
<h2>Managed URLs</h2> |
|
<ul class="url-list"> |
|
HTML; |
|
|
|
foreach ($urls as $hash => $url) { |
|
$cacheFile = CACHE_DIR . $hash . '.tmp'; |
|
// 根据是否启用伪静态设置不同的URL格式 |
|
if (USE_REWRITE) { |
|
$downloadUrl = "$baseUrl/url/" . urlencode($url); |
|
} else { |
|
$downloadUrl = "$baseUrl/index.php?url=" . urlencode($url); |
|
} |
|
$cacheStatus = file_exists($cacheFile) ? |
|
'<span class="cache-info">(cached: ' . date('Y-m-d H:i:s', filemtime($cacheFile)) . ')</span>' : |
|
'<span class="cache-info">(not cached)</span>'; |
|
|
|
echo <<<HTML |
|
<li class="url-item"> |
|
<div> |
|
<a href="?url={$url}" target="_blank">{$url}</a> |
|
{$cacheStatus} |
|
</div> |
|
<div class="url-actions"> |
|
<a href="{$downloadUrl}" target="_blank">Download</a> |
|
<a href="#" class="copy-btn" onclick="copyToClipboard('{$downloadUrl}'); return false;">Copy Download Link</a> |
|
<a href="?delete={$hash}" class="delete-btn" onclick="return confirm('Are you sure you want to delete this URL?')">Delete</a> |
|
</div> |
|
</li> |
|
HTML; |
|
} |
|
|
|
echo <<<HTML |
|
</ul> |
|
|
|
<div class="cron-section"> |
|
<h2>Cron Job Information</h2> |
|
|
|
<div class="info-grid"> |
|
<div class="info-label">Scheme:</div> |
|
<div>{$scheme}</div> |
|
|
|
<div class="info-label">Host:</div> |
|
<div>{$host}</div> |
|
|
|
<div class="info-label">Port:</div> |
|
<div>{$port}</div> |
|
|
|
<div class="info-label">Path:</div> |
|
<div>{$path}</div> |
|
</div> |
|
|
|
<h3>Cron Job URL:</h3> |
|
<div class="cron-url" id="cronUrl">{$fullCronUrl}</div> |
|
<button class="copy-btn-main" onclick="copyToClipboard('{$fullCronUrl}')">Copy URL</button> |
|
|
|
<p style="margin-top: 10px; font-size: 0.9em;"> |
|
Use this URL to set up a cron job that will periodically check and update cached files. |
|
Example cron command (runs every day at 3am):<br> |
|
<code>0 3 * * * curl -s "{$fullCronUrl}" >/dev/null 2>&1</code> |
|
</p> |
|
</div> |
|
</body> |
|
</html> |
|
HTML; |
|
} |
|
?> |