This commit is contained in:
2025-09-09 22:48:57 -07:00
parent fa7503b5aa
commit b3463ef5ec
4 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/../incl/util.php';
setPlainHeader();
checkClientDatabaseVersion();
$conn = newConnection();
$post = getPostData();
$targetId = (int)$post['targetId'] ?? 0;
$token = $post['token'] ?? '';
$username = $post['username'] ?? '';
$stmt = $conn->prepare("SELECT * FROM users WHERE token = ? AND username = ?");
$stmt->bind_param("ss", $token, $username);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if (!$row) {
echo encrypt(json_encode(["success" => false, "message" => 'User info not found']));
exit;
}
$stmt->close();
$user_id = $row["id"];
$time = time();
$stmt = $conn->prepare("UPDATE userposts SET deleted_at = ? WHERE id = ? AND userId = ? AND deleted_at = 0");
$stmt->bind_param("iii", $time, $targetId, $user_id);
$stmt->execute();
$success = $stmt->affected_rows > 0;
$stmt->close();
echo encrypt(json_encode(["success" => $success]));
$conn->close();

View File

@@ -0,0 +1,22 @@
<?php
require __DIR__ . '/../incl/util.php';
setPlainHeader();
checkClientDatabaseVersion();
$conn = newConnection();
$post = getPostData();
$targetId = (int)$post['targetId'] ?? 0;
$stmt = $conn->prepare("
SELECT p.id, p.content, p.timestamp, p.likes, u.id as userId
FROM userposts p
JOIN users u ON p.userId = u.id
WHERE u.banned = 0 AND p.deleted_at = 0
ORDER BY p.id DESC
");
$stmt->execute();
$result = $stmt->get_result();
echo encrypt(json_encode(array_map(fn($row) => ['id' => $row['id'], 'userId' => $row['userId'], 'content' => $row['content'], 'timestamp' => genTimestamp($row['timestamp']) . " ago", 'likes' => $row['likes']], $result->fetch_all(MYSQLI_ASSOC))));
$conn->close();

View File

@@ -0,0 +1,36 @@
<?php
require __DIR__ . '/../incl/util.php';
setPlainHeader();
checkClientDatabaseVersion();
$post = getPostData();
$request_content = $post['content'] ?? '';
$token = $post['token'] ?? '';
$username = $post['username'] ?? '';
if (!preg_match('/^[ a-zA-Z0-9!@#\$%\^&\*\(\)_\+\-=\[\]\{\};\':",\.<>\/\?\\\\|`~]+$/', $request_content)) {
exitWithMessage(json_encode(["success" => false]));
}
$conn = newConnection();
$stmt = $conn->prepare("SELECT * FROM users WHERE token = ? AND username = ?");
$stmt->bind_param("ss", $token, $username);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if (!$row) exitWithMessage(json_encode(["success" => false]));
$stmt->close();
$id = $row["id"];
$content = base64_encode($request_content);
$time = time();
$stmt = $conn->prepare("INSERT INTO userposts (userId, content, timestamp) VALUES (?, ?, ?)");
$stmt->bind_param("isi", $id, $content, $time);
$stmt->execute();
$stmt->close();
echo encrypt(json_encode(["success" => true]));
$conn->close();

View File

@@ -0,0 +1,59 @@
<?php
require __DIR__ . '/../incl/util.php';
setPlainHeader();
checkClientDatabaseVersion();
$conn = newConnection();
$post = getPostData();
$targetId = (int)$post['targetId'] ?? 0;
$liked = (int)$post['liked'] ?? -1;
$token = $post['token'] ?? '';
$username = $post['username'] ?? '';
if ($liked !== 0 && $liked !== 1) {
echo encrypt(json_encode(["success" => false, "message" => 'Invalid type']));
exit;
}
$stmt = $conn->prepare("SELECT * FROM users WHERE token = ? AND username = ?");
$stmt->bind_param("ss", $token, $username);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if (!$row) {
echo encrypt(json_encode(["success" => false, "message" => 'User info not found']));
exit;
}
$stmt->close();
$user_id = $row["id"];
$stmt = $conn->prepare("SELECT votes, likes FROM userposts WHERE id = ?");
$stmt->bind_param("i", $targetId);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if (!$row) {
echo encrypt(json_encode(["success" => false, "message" => 'Post info not found']));
exit;
}
$stmt->close();
$votes = json_decode($row["votes"], true) ?? [];
$likes = (int)$row["likes"];
if (isset($votes[$user_id])) {
echo encrypt(json_encode(["success" => false, "message" => 'You have already voted']));
exit;
}
$votes[$user_id] = $liked === 0 ? false : true;
$likes += $liked ? 1 : -1;
$votes = json_encode($votes);
$stmt = $conn->prepare("UPDATE userposts SET likes = ?, votes = ? WHERE id = ?");
$stmt->bind_param("isi", $likes, $votes, $targetId);
$stmt->execute();
$stmt->close();
echo encrypt(json_encode(["success" => true, "likes" => $likes]));
$conn->close();