0, 'window' => $now]; if (is_file($file)) { $raw = @file_get_contents($file); if ($raw) $data = json_decode($raw, true) ?: $data; } // 新窗口 if ($now - ($data['window'] ?? 0) >= 60) { $data = ['count' => 1, 'window' => $now]; } else { $data['count'] = ($data['count'] ?? 0) + 1; } @file_put_contents($file, json_encode($data)); if ($data['count'] > $rate_limit_per_minute) { json_resp(['code' => 0, 'msg' => '请求过于频繁,请稍后再试'], 429); } } /** * 内容审核(关键词过滤) * 返回true表示通过,false表示违规 */ function content_audit($text) { if (empty($text)) return true; $blocked = [ '/赌博/i', '/色情/i', '/代开发票/i', '/办证/i', '/贷款/i', '/信用卡套现/i', '/刷单/i', ]; foreach ($blocked as $pattern) { if (preg_match($pattern, $text)) { return false; } } return true; } // 执行速率限制检查 check_rate_limit(); /** * 保存笔记内容 * 返回: 'ok' | 'size_exceeded' | 'audit_failed' */ function save($path, $text) { global $max_note_size; if (strlen($text) > $max_note_size) { return 'size_exceeded'; } if (!content_audit($text)) { return 'audit_failed'; } file_put_contents($path, $text); if (!strlen($text)) { @unlink($path); } return 'ok'; } /** * 返回JSON响应 */ function json_resp($data, $code = 200) { header('Content-Type: application/json; charset=utf-8'); http_response_code($code); echo json_encode($data, JSON_UNESCAPED_UNICODE); die; } /** * 获取笔记信息(大小、修改时间) */ function get_note_info($path, $key) { if (!is_file($path)) { return null; } $stat = stat($path); return [ 'key' => $key, 'size' => $stat['size'], 'mtime' => $stat['mtime'], 'exists' => true, ]; } /** * 从REQUEST_URI解析钥匙 * 支持 /key?params 和 /index.php?key?params */ function parse_key_from_uri() { $uri = $_SERVER['REQUEST_URI'] ?? ''; // 去掉query string $path = parse_url($uri, PHP_URL_PATH); // 去掉前导/和index.php $path = preg_replace('#^/index\.php#', '', $path); $path = ltrim($path, '/'); return $path ?: null; } // ========== API 路由 ========== // 新建随机地址笔记 if (isset($_GET['new'])) { $path_url = substr(str_shuffle('234579abcdefghjkmnpqrstwxyz'), -5); $note_path = $save_path . '/' . $path_url; $url = $base_url . '/' . $path_url; $text = ''; if (isset($_GET['text'])) $text = $_GET['text']; if (isset($_POST['text'])) $text = $_POST['text']; $result = save($note_path, $text); if ($result === 'size_exceeded') { json_resp(['code' => 0, 'msg' => '内容超过1MB限制'], 400); } if ($result === 'audit_failed') { json_resp(['code' => 0, 'msg' => '内容包含违规信息'], 400); } // JSON API模式 if (isset($_GET['json'])) { json_resp([ 'code' => 1, 'msg' => 'created', 'data' => [ 'key' => $path_url, 'url' => $url, 'size' => strlen($text), ] ]); } echo($url); die; } // 检查笔记是否存在及变更(轻量接口) if (isset($_GET['check']) && isset($_GET['keys'])) { $keys = explode(',', $_GET['keys']); $results = []; foreach ($keys as $key) { $key = trim($key); if (!validate_key($key)) continue; $note_path = $save_path . '/' . $key; $info = get_note_info($note_path, $key); $results[$key] = $info; } json_resp(['code' => 1, 'data' => $results]); } // 获取笔记信息(单个) if (isset($_GET['info']) && isset($_GET['note'])) { $note = $_GET['note']; if (!validate_key($note)) { json_resp(['code' => 0, 'msg' => '无效的钥匙格式'], 400); } $note_path = $save_path . '/' . $note; $info = get_note_info($note_path, $note); if (!$info) { json_resp(['code' => 0, 'msg' => '笔记不存在'], 404); } json_resp(['code' => 1, 'data' => $info]); } // 删除笔记 if (isset($_GET['delete']) && isset($_GET['note'])) { $note = $_GET['note']; if (!validate_key($note)) { json_resp(['code' => 0, 'msg' => '无效的钥匙格式'], 400); } $note_path = $save_path . '/' . $note; if (is_file($note_path)) { @unlink($note_path); json_resp(['code' => 1, 'msg' => 'deleted']); } json_resp(['code' => 0, 'msg' => '笔记不存在'], 404); } // ========== 笔记路由(通过URI路径解析key) ========== // 从URI解析钥匙 $route_key = parse_key_from_uri(); // 如果没有有效钥匙,重定向到随机笔记 if (!$route_key || !validate_key($route_key)) { // 无效钥匙格式 → 如果是API请求返回400,否则重定向 $is_api = isset($_GET['json']) || isset($_GET['raw']) || isset($_GET['info']); if ($is_api) { json_resp(['code' => 0, 'msg' => '无效的钥匙格式'], 400); } header("Location: $base_url/" . substr(str_shuffle('234579abcdefghjkmnpqrstwxyz'), -5)); die; } $note_path = $save_path . '/' . $route_key; // 写入/修改笔记 if (isset($_POST['text']) || isset($_GET['text'])) { $text = isset($_POST['text']) ? $_POST['text'] : $_GET['text']; $result = save($note_path, $text); if ($result === 'size_exceeded') { json_resp(['code' => 0, 'msg' => '内容超过1MB限制'], 400); } if ($result === 'audit_failed') { json_resp(['code' => 0, 'msg' => '内容包含违规信息'], 400); } // JSON API模式 if (isset($_GET['json'])) { $info = get_note_info($note_path, $route_key); json_resp([ 'code' => 1, 'msg' => 'saved', 'data' => $info ]); } echo("saved"); die; } // 获取笔记原始内容 if (isset($_GET['raw'])) { if (is_file($note_path)) { header('Content-Type: text/plain; charset=utf-8'); echo(file_get_contents($note_path)); } else { http_response_code(404); echo 'Not found'; } die; } // ========== 默认:渲染Web编辑页面 ========== ?>