diff --git a/config/common.json b/config/common.json new file mode 100644 index 0000000..99fb03e --- /dev/null +++ b/config/common.json @@ -0,0 +1,3 @@ +{ + "web": "https://demo.winxg.com" +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index 6b09ab5..f924ab4 100644 --- a/public/index.php +++ b/public/index.php @@ -248,8 +248,7 @@ function api_test() function api_upload() { - $web = "https://demo.winxg.com"; - + $web = get_config('common', 'web'); $url = "https://telegra.ph/upload"; $filePath = $_FILES["file"]["tmp_name"]; $ch = curl_init(); @@ -298,32 +297,7 @@ function init() check_api(); - if (str_starts_with($_SERVER['REQUEST_URI'], "/file")) { - $id = explode("/", $_SERVER['REQUEST_URI'])[2] ?? ""; - - $stmt = get_db()->prepare("SELECT * FROM files WHERE uuid=:uuid LIMIT 1"); - $stmt->bindParam(':uuid', $id); - $stmt->execute(); - - $row = $stmt->fetch(PDO::FETCH_ASSOC); - - if (empty($row['src'])) { - echo_json([ - "code" => 1, - "msg" => "图片不存在", - "data" => [] - ]); - } - - $imgUrl = "https://telegra.ph{$row['src']}"; - - $content = file_get_contents($imgUrl); - header('Content-Type: image/jpeg'); - header('Content-Length: ' . strlen($content)); - - echo $content; - die; - } + check_file(); } function get_page_title() @@ -377,5 +351,54 @@ function uniqidReal($lenght = 13) return substr(bin2hex($bytes), 0, $lenght); } +function check_file() +{ + if (str_starts_with($_SERVER['REQUEST_URI'], "/file")) { + $id = explode("/", $_SERVER['REQUEST_URI'])[2] ?? ""; + + $stmt = get_db()->prepare("SELECT * FROM files WHERE uuid=:uuid LIMIT 1"); + $stmt->bindParam(':uuid', $id); + $stmt->execute(); + + $row = $stmt->fetch(PDO::FETCH_ASSOC); + + if (empty($row['src'])) { + echo_json([ + "code" => 1, + "msg" => "图片不存在", + "data" => [] + ]); + } + + $imgUrl = "https://telegra.ph{$row['src']}"; + + $content = file_get_contents($imgUrl); + header('Content-Type: image/jpeg'); + header('Content-Length: ' . strlen($content)); + + echo $content; + die; + } +} + +function get_config($file, $path) +{ + if (!file_exists(CONFIG_PATH . $file . ".json")) { + return ""; + } + $conf = file_get_contents(CONFIG_PATH . $file . ".json"); + + $arr = json_decode($conf, true); + + $value = $arr; + + foreach(explode(".", $path) as $p){ + $value = $value[$p] ?? ""; + if (!$value) break; + } + + return $value; +} + // common func end ?> \ No newline at end of file