上传音乐
++ + 您好,。您已登录,上传将自动记录您的昵称。 + + 您当前未登录,上传后将以“匿名用户”身份显示。登录后可获得更完整的服务。 + +
+ + + + + + +diff --git a/sou_api.php b/sou_api.php new file mode 100644 index 0000000..3a9d23c --- /dev/null +++ b/sou_api.php @@ -0,0 +1,122 @@ + 'error', + 'message' => 'Unknown error', + 'count' => 0, + 'results' => [] +]; + +try { + // 1. 验证请求方法 + if ($_SERVER['REQUEST_METHOD'] !== 'GET') { + http_response_code(405); + $response['message'] = 'Method Not Allowed'; + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; + } + + // 2. 验证并获取必要的参数 + if (!isset($_GET['action']) || $_GET['action'] !== 'getSou') { + http_response_code(400); + $response['message'] = 'Invalid action. Use ?action=getSou'; + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; + } + + // 检查搜索词是否存在 + if (!isset($_GET['s'])) { + http_response_code(400); + $response['message'] = 'Missing search term. Use &s=your_keyword'; + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; + } + + $searchTerm = trim($_GET['s']); + + // 如果搜索词为空,则返回空结果 + if (empty($searchTerm)) { + $response['status'] = 'success'; + $response['message'] = 'Search term is empty, returning no results.'; + $response['count'] = 0; + $response['results'] = []; + http_response_code(200); + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; + } + + // 3. 引入音乐数据 + $musicFile = __DIR__ . '/data/music.php'; + if (!file_exists($musicFile)) { + http_response_code(500); + $response['message'] = 'Music data file not found at ' . $musicFile; + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; + } + + // 使用require确保每次都能获取最新数据 + $musicList = require $musicFile; + + // 验证音乐数据格式 + if (!is_array($musicList)) { + http_response_code(500); + $response['message'] = 'Invalid music data format in ' . $musicFile; + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; + } + + // 4. 定义音乐分类(用于返回更丰富的信息) + $categories = [ + 'all' => ['name' => '全部音乐', 'color' => '#b89e81', 'text_color' => '#5d4037'], + 'cantonese' => ['name' => '粤语歌曲', 'color' => '#c8e6c9', 'text_color' => '#2e7d32'], + 'mandarin' => ['name' => '国语歌曲', 'color' => '#fff3e0', 'text_color' => '#e65100'], + 'waiyu' => ['name' => '外语歌曲', 'color' => '#e3f2fd', 'text_color' => '#0d47a1'], + 'classic' => ['name' => '经典老歌', 'color' => '#efebe9', 'text_color' => '#3e2723'], + 'other' => ['name' => '其他音乐', 'color' => '#f3e5f5', 'text_color' => '#6a1b9a'] + ]; + + // 5. 执行搜索 + $searchResults = []; + $lowerTerm = strtolower($searchTerm); + + foreach ($musicList as $music) { + // 检查音乐条目是否有效 + if (!is_array($music) || !isset($music['title'], $music['artist'])) { + continue; // 跳过无效条目 + } + + if (strpos(strtolower($music['title']), $lowerTerm) !== false || + strpos(strtolower($music['artist']), $lowerTerm) !== false) { + // 为结果添加分类信息,如果分类不存在则使用'other' + $music['category_info'] = $categories[$music['category']] ?? $categories['other']; + $searchResults[] = $music; + } + } + + // 6. 构建成功响应 + $response = [ + 'status' => 'success', + 'action' => 'getSou', + 'search_term' => $searchTerm, + 'count' => count($searchResults), + 'results' => $searchResults + ]; + + http_response_code(200); + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; + +} catch (Exception $e) { + // 捕获所有未预见的异常 + http_response_code(500); + $response['message'] = 'Server error: ' . $e->getMessage(); + echo json_encode($response, JSON_UNESCAPED_UNICODE); + exit; +} +?> \ No newline at end of file diff --git a/submit_recommendation.php b/submit_recommendation.php new file mode 100644 index 0000000..b848b36 --- /dev/null +++ b/submit_recommendation.php @@ -0,0 +1,53 @@ +prepare($sql); + $stmt->bindParam(':song_name', $songName); + $stmt->bindParam(':artist_name', $artistName); + $stmt->bindParam(':reason', $reason); + $stmt->bindParam(':user_id', $userIdentifier); + $stmt->execute(); + + // 跳转回主页并显示成功消息 + header('Location: index.html?status=success'); + exit; + } catch(PDOException $e) { + // 数据库错误 + header('Location: index.html?status=error&msg=' . urlencode($e->getMessage())); + exit; + } + } else { + // 字段验证失败 + header('Location: index.html?status=error&msg=歌曲名称和歌手名称为必填项'); + exit; + } +} else { + // 非POST请求直接跳转回主页 + header('Location: index.html'); + exit; +} +?> \ No newline at end of file diff --git a/update_recommendation_status.php b/update_recommendation_status.php new file mode 100644 index 0000000..7b0c6fc --- /dev/null +++ b/update_recommendation_status.php @@ -0,0 +1,50 @@ +prepare($sql); + $stmt->bindParam(':status', $status, PDO::PARAM_INT); + $stmt->bindParam(':id', $id, PDO::PARAM_INT); + + // 执行更新 + $result = $stmt->execute(); + + if ($result) { + $message = $status == 1 ? "推荐已同意" : "推荐已驳回"; + header("Location: $adminHome?delete_status=success&msg=$message"); + } else { + header("Location: $adminHome?delete_status=error&msg=更新失败,请重试"); + } + } catch(PDOException $e) { + // 记录错误日志 + $logMsg = date('[Y-m-d H:i:s] ') . "状态更新错误: " . $e->getMessage() . "\n"; + file_put_contents('db_error.log', $logMsg, FILE_APPEND); + + header("Location: $adminHome?delete_status=error&msg=数据库错误,请稍后再试"); + } +} else { + header("Location: $adminHome?delete_status=error&msg=数据库连接失败"); +} + +exit; +?> \ No newline at end of file diff --git a/upload.php b/upload.php new file mode 100644 index 0000000..3acd0b0 --- /dev/null +++ b/upload.php @@ -0,0 +1,442 @@ + '粤语歌曲', + 'mandarin' => '国语歌曲', + 'waiyu' => '外语歌曲', + 'classic' => '经典老歌', + 'other' => '其他音乐' +]; + +// 检查用户是否登录 +$isLoggedIn = isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in'] === true; +$uploaderName = $isLoggedIn ? $_SESSION['user_info']['username'] : '匿名用户'; + +// 处理上传 +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload_music'])) { + // 验证表单数据 + $title = trim($_POST['title'] ?? ''); + $artist = trim($_POST['artist'] ?? ''); + // 如果用户已登录,强制使用会话中的用户名,忽略表单提交的值 + $uploader = $isLoggedIn ? $_SESSION['user_info']['username'] : trim($_POST['uploader'] ?? '匿名用户'); + $category = $_POST['category'] ?? 'other'; + $description = trim($_POST['description'] ?? ''); + $bvid = trim($_POST['bvid'] ?? ''); + $duration = trim($_POST['duration'] ?? ''); + + // 验证时长格式 + if (!empty($duration) && !preg_match('/^\d+:\d{2}$/', $duration)) { + $message = "时长格式不正确,请使用 MM:SS 格式(例如 3:45 表示3分45秒)"; + $message_type = "error"; + } elseif (empty($title) || empty($artist)) { + $message = "歌曲名和歌手名不能为空"; + $message_type = "error"; + } elseif (!isset($_FILES['audio_file']) || $_FILES['audio_file']['error'] !== UPLOAD_ERR_OK) { + $message = "请选择要上传的音频文件"; + $message_type = "error"; + } else { + // 验证BV号格式 + if (!empty($bvid) && !preg_match('/^BV[0-9A-Za-z]+$/', $bvid)) { + $message = "BV号格式不正确,应为以BV开头的字符串(如BV1Va4y1n7HN)"; + $message_type = "error"; + } else { + // 处理文件上传 + $upload_dir = 'uploads/'; + if (!is_dir($upload_dir)) { + mkdir($upload_dir, 0755, true); + } + + $file_ext = pathinfo($_FILES['audio_file']['name'], PATHINFO_EXTENSION); + $allowed_ext = ['mp3', 'wav', 'flac', 'm4a']; + + if (!in_array(strtolower($file_ext), $allowed_ext)) { + $message = "不支持的文件格式,仅支持mp3, wav, flac, m4a"; + $message_type = "error"; + } else { + // 检查文件大小(限制30MB) + $max_file_size = 30 * 1024 * 1024; // 30MB + if ($_FILES['audio_file']['size'] > $max_file_size) { + $message = "文件大小超过限制(30MB)"; + $message_type = "error"; + } else { + // 生成唯一文件名 + $file_name = uniqid('music_') . '.' . $file_ext; + $target_path = $upload_dir . $file_name; + + if (move_uploaded_file($_FILES['audio_file']['tmp_name'], $target_path)) { + // 使用用户输入的时长,如果为空则使用默认值 + $final_duration = !empty($duration) ? $duration : "0:00"; + + // 保存到待审核表 + try { + $stmt = $conn->prepare("INSERT INTO pending_music + (uploader_name, title, artist, category, description, file_path, duration, upload_time, bvid) + VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), ?)"); + $stmt->execute([$uploader, $title, $artist, $category, $description, $target_path, $final_duration, $bvid]); + + $message = "上传成功,已进入待审核队列,感谢您的分享!"; + $message_type = "success"; + // 清空表单 + $_POST = []; + } catch (PDOException $e) { + $message = "数据库错误: " . $e->getMessage(); + $message_type = "error"; + unlink($target_path); // 数据库错误时删除已上传文件 + } + } else { + $message = "文件上传失败"; + $message_type = "error"; + } + } + } + } + } +} +?> + + +
+ + +