Repositories | Polyback | Log in to see private repositories


polyback-website / 7e551d223f42aecd154514fc9f0a7f04b918903f / api/gameserver/report-abuse.php

2,099 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

// In-game Report Abuse, forwarded by the game server (X-Polygon-Gameserver-Key):
// {"jobId","reporterId","targetId","reason","comment","chat":[{"userId","name","text"},...]}

use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Gameservers;

Gameservers::RequireAuthorized();
$data = Gameservers::Body();

$reporter = (int) ($data["reporterId"] ?? 0);
$target = (int) ($data["targetId"] ?? 0);
$jobId = (string) ($data["jobId"] ?? "");
$reason = mb_substr(trim((string) ($data["reason"] ?? "")), 0, 200);
$comment = mb_substr(trim((string) ($data["comment"] ?? "")), 0, 1000);
if ($reporter <= 0 || $reason === "") die(json_encode(["success" => false, "message" => "Bad report"]));

// the reporter must be in this game (their ticket was redeemed for it)
$job = Database::singleton()->run("SELECT PlaceID FROM GameJobs WHERE JobID = :j", [":j" => $jobId])->fetch(PDO::FETCH_OBJ);
if (!$job) die(json_encode(["success" => false, "message" => "Unknown game"]));

// at most 10 reports per user per hour
$recent = Database::singleton()->run(
	"SELECT COUNT(*) FROM AbuseReports WHERE ReporterID = :u AND Created + 3600 > UNIX_TIMESTAMP()", [":u" => $reporter]
)->fetchColumn();
if ($recent >= 10) die(json_encode(["success" => false, "message" => "Too many reports"]));

$chat = [];
foreach (array_slice(is_array($data["chat"] ?? null) ? $data["chat"] : [], -40) as $line)
{
	if (!is_array($line)) continue;
	$chat[] = [
		"userId" => (int) ($line["userId"] ?? 0),
		"name" => mb_substr((string) ($line["name"] ?? ""), 0, 50),
		"text" => mb_substr((string) ($line["text"] ?? ""), 0, 200),
	];
}

Database::singleton()->run(
	"INSERT INTO AbuseReports (ReporterID, TargetID, PlaceID, JobID, Reason, Comment, ChatLog, Created)
	 VALUES (:r, :t, :p, :j, :reason, :comment, :chat, UNIX_TIMESTAMP())",
	[":r" => $reporter, ":t" => $target > 0 ? $target : null, ":p" => $job->PlaceID, ":j" => $jobId,
	 ":reason" => $reason, ":comment" => $comment, ":chat" => json_encode($chat)]
);
echo json_encode(["success" => true]);