95
square_submit.php
Normal file
95
square_submit.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
// 推送 / 撤回 / 修改标题:用户把自己图库里的一张图推到照片广场,等管理员审核。
|
||||
// 推送到广场不额外扣积分。zone_id 可选:只能选自己可见的分区,否则拒绝。
|
||||
require_once __DIR__ . '/includes/auth.php';
|
||||
require_once __DIR__ . '/includes/features.php';
|
||||
require_once __DIR__ . '/includes/zones.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
function reply($arr, $code = 200)
|
||||
{
|
||||
http_response_code($code);
|
||||
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!SQUARE_ENABLED) {
|
||||
reply(['ok' => false, 'msg' => '照片广场未开放'], 404);
|
||||
}
|
||||
|
||||
$user = current_user($conn);
|
||||
if (!$user) {
|
||||
reply(['ok' => false, 'msg' => '登录已失效,请重新登录'], 401);
|
||||
}
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !csrf_ok()) {
|
||||
reply(['ok' => false, 'msg' => '请求校验失败,请刷新页面重试'], 400);
|
||||
}
|
||||
|
||||
$image_id = (int)($_POST['image_id'] ?? 0);
|
||||
$title = trim((string)($_POST['title'] ?? ''));
|
||||
if (mb_strlen($title, 'UTF-8') > 40) {
|
||||
reply(['ok' => false, 'msg' => '广场标题最多 40 个字']);
|
||||
}
|
||||
|
||||
// 分区:0 或不传 = 默认分区;否则必须是自己能看见的分区(公开/登录/本组)
|
||||
$zone_id = (int)($_POST['zone_id'] ?? 0);
|
||||
if ($zone_id > 0) {
|
||||
$visible = visible_zones($conn, $user);
|
||||
$allowed = [];
|
||||
foreach ($visible as $z) $allowed[] = (int)$z['id'];
|
||||
if (!in_array($zone_id, $allowed, true)) {
|
||||
reply(['ok' => false, 'msg' => '不能推送到你看不到的分区']);
|
||||
}
|
||||
} else {
|
||||
$zone_id = null;
|
||||
}
|
||||
|
||||
// 归属校验:只能操作自己图库里、未删除的图
|
||||
$stmt = $conn->prepare("SELECT image_id FROM images WHERE image_id = ? AND user_id = ? AND is_deleted = 0");
|
||||
$stmt->bind_param("ii", $image_id, $user['user_id']);
|
||||
$stmt->execute();
|
||||
if (!$stmt->get_result()->fetch_assoc()) {
|
||||
$stmt->close();
|
||||
reply(['ok' => false, 'msg' => '图片不存在或不属于你'], 404);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
$stmt = $conn->prepare("SELECT id, status, zone_id FROM photos_square WHERE image_id = ?");
|
||||
$stmt->bind_param("i", $image_id);
|
||||
$stmt->execute();
|
||||
$existing = $stmt->get_result()->fetch_assoc();
|
||||
$stmt->close();
|
||||
|
||||
if (!$existing) {
|
||||
$stmt = $conn->prepare("INSERT INTO photos_square (image_id, user_id, zone_id, title) VALUES (?, ?, ?, ?)");
|
||||
$stmt->bind_param("iiis", $image_id, $user['user_id'], $zone_id, $title);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
reply(['ok' => true, 'status' => 'pending', 'msg' => '已提交,等待审核']);
|
||||
}
|
||||
|
||||
switch ($existing['status']) {
|
||||
case 'pending':
|
||||
case 'rejected':
|
||||
// 驳回后允许改标题/换分区重新提交,回到待审
|
||||
$stmt = $conn->prepare("UPDATE photos_square SET title = ?, zone_id = ?, status = 'pending', reject_reason = '', submitted_at = NOW(), reviewed_at = NULL, reviewed_by = NULL WHERE id = ? AND user_id = ?");
|
||||
$stmt->bind_param("siii", $title, $zone_id, $existing['id'], $user['user_id']);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
reply(['ok' => true, 'status' => 'pending', 'msg' => '已重新提交,等待审核']);
|
||||
case 'approved':
|
||||
// 已上架的只改标题,换了分区就必须重新审核:否则可直接把内容塞进受限分区
|
||||
if ($zone_id !== (int)$existing['zone_id']) {
|
||||
$stmt = $conn->prepare("UPDATE photos_square SET title = ?, zone_id = ?, status = 'pending', reject_reason = '', submitted_at = NOW(), reviewed_at = NULL, reviewed_by = NULL WHERE id = ? AND user_id = ?");
|
||||
$stmt->bind_param("siii", $title, $zone_id, $existing['id'], $user['user_id']);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
reply(['ok' => true, 'status' => 'pending', 'msg' => '已更换分区,等待管理员重新审核']);
|
||||
}
|
||||
$stmt = $conn->prepare("UPDATE photos_square SET title = ? WHERE id = ? AND user_id = ?");
|
||||
$stmt->bind_param("sii", $title, $existing['id'], $user['user_id']);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
reply(['ok' => true, 'status' => 'approved', 'msg' => '标题与分区已更新']);
|
||||
}
|
||||
reply(['ok' => false, 'msg' => '状态异常']);
|
||||
Reference in New Issue
Block a user