2020-10-25 13:59:02 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-09 17:24:40 +01:00
|
|
|
* @param string $reporter The NamelessMC ID of the user who is creating the report
|
|
|
|
|
* @param string $reported The NamelessMC ID of the user who is getting reported (optional, required if reported_username/reported_uid not provided)
|
2020-10-25 13:59:02 -07:00
|
|
|
* @param string $content The content of the report
|
2021-10-09 17:24:40 +01:00
|
|
|
* @param string $reported_username The username of the reported user (optional, required if reported not provided)
|
|
|
|
|
* @param string $reported_uid A unique ID for the reported user (optional, required if reported not provided)
|
2024-10-18 06:55:18 +01:00
|
|
|
* @param string $server_id A server ID the report is associated with
|
2020-12-27 14:24:57 +01:00
|
|
|
*
|
2020-10-25 13:59:02 -07:00
|
|
|
* @return string JSON Array
|
|
|
|
|
*/
|
2022-01-15 11:48:52 -08:00
|
|
|
class CreateReportEndpoint extends KeyAuthEndpoint {
|
2020-10-25 13:59:02 -07:00
|
|
|
|
|
|
|
|
public function __construct() {
|
2022-01-15 12:06:46 -08:00
|
|
|
$this->_route = 'reports/create';
|
2020-10-25 13:59:02 -07:00
|
|
|
$this->_module = 'Core';
|
|
|
|
|
$this->_description = 'Create a report';
|
2021-02-21 13:15:07 -08:00
|
|
|
$this->_method = 'POST';
|
2020-10-25 13:59:02 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-15 15:18:05 -08:00
|
|
|
public function execute(Nameless2API $api): void {
|
2021-10-09 17:24:40 +01:00
|
|
|
$api->validateParams($_POST, ['reporter', 'content']);
|
|
|
|
|
|
|
|
|
|
// Ensure either reported OR reported_username AND reported_uid are provided
|
|
|
|
|
if (!$_POST['reported'] && !($_POST['reported_username'] && $_POST['reported_uid'])) {
|
2022-05-24 23:05:07 -06:00
|
|
|
$api->throwError(Nameless2API::ERROR_CANNOT_FIND_USER);
|
2021-10-09 17:24:40 +01:00
|
|
|
}
|
2020-10-25 13:59:02 -07:00
|
|
|
|
2020-12-27 14:42:47 +01:00
|
|
|
// Ensure content is correct length
|
|
|
|
|
if (strlen($_POST['content']) > 255) {
|
2022-05-24 23:05:07 -06:00
|
|
|
$api->throwError(CoreApiErrors::ERROR_REPORT_CONTENT_TOO_LONG);
|
2020-12-27 14:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure user reporting has website account, and has not been banned
|
2022-06-25 09:37:09 +02:00
|
|
|
$user_reporting = $api->getUser('id', $_POST['reporter']);
|
2022-02-19 23:20:45 -08:00
|
|
|
$user_reporting_data = $user_reporting->data();
|
2020-10-25 13:59:02 -07:00
|
|
|
|
2022-02-19 23:20:45 -08:00
|
|
|
if ($user_reporting_data->isbanned) {
|
2022-05-24 23:05:07 -06:00
|
|
|
$api->throwError(CoreApiErrors::ERROR_BANNED_FROM_WEBSITE);
|
2020-12-27 14:42:47 +01:00
|
|
|
}
|
2020-10-25 13:59:02 -07:00
|
|
|
|
2020-12-27 14:42:47 +01:00
|
|
|
// See if reported user exists
|
2022-05-01 23:22:19 -07:00
|
|
|
$user_reported_id = $api->getDb()->get('users', ['id', (int)$_POST['reported']]);
|
2021-02-21 16:42:24 -08:00
|
|
|
if (!$user_reported_id->count()) {
|
2022-06-02 17:51:59 +01:00
|
|
|
$user_reported_id = null;
|
2021-02-21 16:42:24 -08:00
|
|
|
} else {
|
|
|
|
|
$user_reported_id = $user_reported_id->first()->id;
|
2020-12-27 14:42:47 +01:00
|
|
|
}
|
2020-10-25 13:59:02 -07:00
|
|
|
|
2022-02-19 23:20:45 -08:00
|
|
|
if ($user_reporting_data->id == $user_reported_id) {
|
2022-05-25 11:00:34 +02:00
|
|
|
$api->throwError(CoreApiErrors::ERROR_CANNOT_REPORT_YOURSELF);
|
2020-12-27 14:42:47 +01:00
|
|
|
}
|
2020-10-25 13:59:02 -07:00
|
|
|
|
2020-12-27 14:42:47 +01:00
|
|
|
// Ensure user has not already reported the same player, and the report is open
|
2022-05-01 23:22:19 -07:00
|
|
|
$user_reports = $api->getDb()->get('reports', ['reporter_id', $user_reporting_data->id])->results();
|
2022-02-19 23:20:45 -08:00
|
|
|
foreach ($user_reports as $report) {
|
|
|
|
|
if ($report->status == 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ((
|
2022-06-02 17:51:59 +01:00
|
|
|
$report->reported_id != null && $report->reported_id == $user_reported_id)
|
2022-06-25 09:37:09 +02:00
|
|
|
|| (isset($_POST['reported_uid']) && $report->reported_uuid == $_POST['reported_uid'])
|
2022-02-19 23:20:45 -08:00
|
|
|
) {
|
2022-05-24 23:05:07 -06:00
|
|
|
$api->throwError(CoreApiErrors::ERROR_OPEN_REPORT_ALREADY);
|
2020-12-27 14:24:57 +01:00
|
|
|
}
|
2020-12-27 14:42:47 +01:00
|
|
|
}
|
2020-10-25 13:59:02 -07:00
|
|
|
|
2022-04-24 20:31:04 +02:00
|
|
|
$reported_user = new User($user_reported_id);
|
|
|
|
|
if ($reported_user->exists()) {
|
|
|
|
|
$integrationUser = $reported_user->getIntegration('Minecraft');
|
|
|
|
|
if ($integrationUser != null) {
|
|
|
|
|
$reported_uuid = $integrationUser->data()->identifier;
|
|
|
|
|
}
|
2022-04-16 18:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-19 23:20:45 -08:00
|
|
|
Report::create($api->getLanguage(), $user_reporting, $reported_user, [
|
|
|
|
|
'type' => Report::ORIGIN_API,
|
|
|
|
|
'reporter_id' => $user_reporting_data->id,
|
|
|
|
|
'reported_id' => $user_reported_id,
|
2022-06-25 09:37:09 +02:00
|
|
|
'report_reason' => $_POST['content'],
|
2022-02-19 23:20:45 -08:00
|
|
|
'updated_by' => $user_reporting_data->id,
|
2024-10-18 06:55:18 +01:00
|
|
|
'reported_mcname' => $_POST['reported_username'] ?: $reported_user->getDisplayname(),
|
|
|
|
|
'reported_uuid' => $_POST['reported_uid'] ?: $reported_uuid ?? 'none',
|
|
|
|
|
'server_id' => $_POST['server_id'],
|
2022-02-19 23:20:45 -08:00
|
|
|
]);
|
2022-02-19 10:12:20 -08:00
|
|
|
|
2022-02-19 23:20:45 -08:00
|
|
|
$api->returnArray(['message' => $api->getLanguage()->get('api', 'report_created')], 201);
|
2020-10-25 13:59:02 -07:00
|
|
|
}
|
2020-12-27 14:24:57 +01:00
|
|
|
}
|