From 0de4a58f61b9b32ff370ca7344ccedbc71d83fbe Mon Sep 17 00:00:00 2001 From: xiaoz Date: Fri, 7 Mar 2025 18:46:12 +0800 Subject: [PATCH] fix bug --- class/Api.php | 82 ++++++++++++++++++++++++--- controller/api.php | 2 +- data/update.log | 3 + templates/admin/setting/subscribe.php | 4 +- version.txt | 2 +- 5 files changed, 82 insertions(+), 11 deletions(-) diff --git a/class/Api.php b/class/Api.php index 47fa6e0..e89c763 100755 --- a/class/Api.php +++ b/class/Api.php @@ -10,15 +10,25 @@ require("./functions/helper.php"); class Api { protected $db; public function __construct($db){ - // 设置API地址(xiaoz) - $api_url = base64_decode("aHR0cHM6Ly9vbmVuYXYueGlhb3oudG9w"); - // 获取API服务器状态码,超时时间为2s - $http_code = $this->GetHeadCode($api_url,2); - // 如果状态码为0、301、302均视为失败 - if( $http_code === 0 || $http_code === 301 || $http_code === 302 ) { - // 失败了则设置备用API地址(rss) - $api_url = base64_decode("aHR0cHM6Ly9vbmVuYXYucnNzLmluaw=="); + // 获取API服务器缓存 + $api_url = $this->getFileCache("api_url"); + + // 如果API地址为空,则设置API地址 + if ($api_url == ""){ + // 设置API地址(xiaoz) + $api_url = base64_decode("aHR0cHM6Ly9vbmVuYXYueGlhb3oudG9w"); + // 获取API服务器状态码,超时时间为2s + $http_code = $this->GetHeadCode($api_url,2); + // 如果状态码为0、301、302均视为失败 + if( $http_code === 0 || $http_code === 301 || $http_code === 302 ) { + // 失败了则设置备用API地址(rss) + $api_url = base64_decode("aHR0cHM6Ly9vbmVuYXYucnNzLmluaw=="); + } + // 设置API地址缓存 + $this->setFileCache("api_url",$api_url,60 * 60 * 24); } + + // $api_url = base64_decode("aHR0cHM6Ly9vbmVuYXYucnNzLmluaw=="); // 设置常量 define("API_URL",$api_url); @@ -3108,7 +3118,63 @@ class Api { return $httpCode; } + // 读取缓存 + private function getFileCache($key) { + // 定义缓存文件的路径 + $cacheDir = __DIR__ . '/cache/'; + $filePath = $cacheDir . md5($key) . '.cache'; + + // 检查文件是否存在 + if (file_exists($filePath)) { + // 读取文件内容 + $data = unserialize(file_get_contents($filePath)); + + // 检查是否过期 + if ($data['expire'] > time()) { + return $data['value']; + } else { + // 如果过期,删除文件 + unlink($filePath); + } + } + + // 如果文件不存在或已过期,返回空字符串 + return ''; + } + // 设置缓存 + private function setFileCache($key, $value, $ttl) { + // 定义缓存文件的路径 + $cacheDir = __DIR__ . '/cache/'; + if (!is_dir($cacheDir)) { + mkdir($cacheDir, 0755, true); + } + $filePath = $cacheDir . md5($key) . '.cache'; + + // 计算过期时间戳 + $expireTime = time() + $ttl; + // 将数据和过期时间一起写入文件 + file_put_contents($filePath, serialize(['value' => $value, 'expire' => $expireTime])); + } + + // 获取相临的版本 + public function getNextVersion(){ + $this->auth($token); + // 获取当前版本 + $version = trim($_GET['version']); + // 正则判断版本是否合法,只能是数组或.组成 + $pattern = "/^[0-9.]+$/"; + if( !preg_match($pattern,$version) ) { + echo "0.0.0"; + exit; + } + // 请求:https://onenav.xiaoz.top/v1/get_version.php?version=1.1.1 + $url = API_URL . '/v1/get_version.php?version=' . $version; + // 设置响应头为text类型 + header('Content-Type: text/plain'); + // 输出结果 + echo curl_get($url); + } } diff --git a/controller/api.php b/controller/api.php index 73b52db..dd89994 100755 --- a/controller/api.php +++ b/controller/api.php @@ -458,7 +458,7 @@ function create_sk($api) { //获取onenav最新版本号 function get_latest_version() { try { - $curl = curl_init("https://git.xiaoz.me/xiaoz/onenav/raw/branch/main/version.txt"); + $curl = curl_init("https://git.xiaoz.org/xiaoz/onenav/raw/branch/main/version.txt"); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50"); curl_setopt($curl, CURLOPT_FAILONERROR, true); diff --git a/data/update.log b/data/update.log index d0bd4d5..ed4ee20 100755 --- a/data/update.log +++ b/data/update.log @@ -1,5 +1,8 @@ 2025.03.07 1. 搜索支持Ctrl + Enter提交 +2. 新增缓存函数 +3. 修复一处BUG +4. 版本检测改为后端获取 2025.03.06 1. 去除default2主题点击输入框自动清空关键词 diff --git a/templates/admin/setting/subscribe.php b/templates/admin/setting/subscribe.php index 2dac2aa..4f6512e 100755 --- a/templates/admin/setting/subscribe.php +++ b/templates/admin/setting/subscribe.php @@ -144,9 +144,11 @@ //获取可更新版本 function available_version() { var current_version = $("#current_version").val(); - $.get("https://onenav.xiaoz.top/v1/get_version.php",{version:current_version},function(data,status){ + $.get("/index.php?c=api&method=getNextVersion",{version:current_version},function(data,status){ + // console.log(data); $("#new_version").val(data); }); + } available_version(); //立即更新按钮 diff --git a/version.txt b/version.txt index 9f1cf89..47ff858 100755 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.1.1-20250306 \ No newline at end of file +v1.1.2-20250307 \ No newline at end of file