misuzu/src/Users/warning.php

103 lines
3 KiB
PHP
Raw Normal View History

2018-12-24 21:35:25 +01:00
<?php
define('MSZ_WARN_NOTE', 0);
2018-12-25 22:41:28 +01:00
define('MSZ_WARN_WARNING', 1);
define('MSZ_WARN_SILENCE', 2);
define('MSZ_WARN_BAN', 3);
2018-12-24 21:35:25 +01:00
define('MSZ_WARN_TYPES', [
2018-12-25 22:41:28 +01:00
MSZ_WARN_NOTE, MSZ_WARN_WARNING, MSZ_WARN_SILENCE, MSZ_WARN_BAN,
2018-12-24 21:35:25 +01:00
]);
function user_warning_add(
int $userId,
string $userIp,
int $issuerId,
string $issuerIp,
int $type,
string $publicNote,
string $privateNote
): int {
if (!in_array($type, MSZ_WARN_TYPES, true)) {
return -1;
}
$addWarning = db_prepare('
INSERT INTO `msz_user_warnings`
(`user_id`, `user_ip`, `issuer_id`, `issuer_ip`, `warning_type`, `warning_note`, `warning_note_private`)
VALUES
(:user_id, INET6_ATON(:user_ip), :issuer_id, INET6_ATON(:issuer_ip), :type, :note, :note_private)
');
$addWarning->bindValue('user_id', $userId);
$addWarning->bindValue('user_ip', $userIp);
$addWarning->bindValue('issuer_id', $issuerId);
$addWarning->bindValue('issuer_ip', $issuerIp);
$addWarning->bindValue('type', $type);
$addWarning->bindValue('note', $publicNote);
$addWarning->bindValue('note_private', $privateNote);
if (!$addWarning->execute()) {
return 0;
}
return (int)db_last_insert_id();
}
function user_warning_count(int $userId): int
{
if ($userId < 1) {
return 0;
}
$countWarnings = db_prepare('
SELECT COUNT(`warning_id`)
FROM `msz_user_warnings`
WHERE `user_id` = :user_id
');
$countWarnings->bindValue('user_id', $userId);
return (int)($countWarnings->execute() ? $countWarnings->fetchColumn() : 0);
}
function user_warning_remove(int $warningId): bool
{
if ($warningId < 1) {
return false;
}
$removeWarning = db_prepare('
DELETE FROM `msz_user_warnings`
WHERE `warning_id` = :warning_id
');
$removeWarning->bindValue('warning_id', $warningId);
return $removeWarning->execute();
}
function user_warning_fetch(
int $userId,
?int $days = null
): array {
$fetchWarnings = db_prepare(sprintf(
'
SELECT
uw.`warning_id`, uw.`warning_created`, uw.`warning_type`, uw.`warning_note`,
2018-12-25 22:41:28 +01:00
uw.`warning_note_private`, uw.`user_id`, uw.`issuer_id`, uw.`warning_duration`,
TIMESTAMPDIFF(SECOND, uw.`warning_created`, uw.`warning_duration`) AS `warning_duration_secs`,
2018-12-24 21:35:25 +01:00
INET6_NTOA(uw.`user_ip`) AS `user_ip`, INET6_NTOA(uw.`issuer_ip`) AS `issuer_ip`,
iu.`username` AS `issuer_username`
FROM `msz_user_warnings` AS uw
LEFT JOIN `msz_users` AS iu
ON iu.`user_id` = uw.`issuer_id`
WHERE uw.`user_id` = :user_id
%s
',
$days !== null ? 'AND uw.`warning_created` >= NOW() - INTERVAL :days DAY' : ''
));
$fetchWarnings->bindValue('user_id', $userId);
if ($days !== null) {
$fetchWarnings->bindValue('days', $days);
}
$warnings = $fetchWarnings->execute() ? $fetchWarnings->fetchAll(PDO::FETCH_ASSOC) : false;
return $warnings ? $warnings : [];
}