Mineplex2018-withcommit/Plugins/Mineplex.ReportSite/view.php

616 lines
21 KiB
PHP
Raw Normal View History

<?php
2016-08-09 16:17:19 +02:00
require_once('snapshot.php');
require_once('report.php');
require_once('message.php');
require_once('user.php');
require_once('user_report.php');
2016-08-09 16:17:19 +02:00
const collapsedMessageCount = 20;
2016-08-09 16:17:19 +02:00
// In Java this is "DateTimeFormatter.ISO_LOCAL_DATE_TIME"
const jsonDateTimeFormat = 'Y-m-d\TH:i:s';
2016-08-09 16:17:19 +02:00
$dateTimeZone = new DateTimeZone('America/Chicago');
2016-08-09 16:17:19 +02:00
/** @var mysqli[] $connections */
$connections = array(); // String index = Connection name
2016-08-09 16:17:19 +02:00
/** @var User[] $users */ // Account id index
$users = array();
2016-08-09 16:17:19 +02:00
$categories = array(
1 => 'Hacking',
2 => 'Chat Abuse',
3 => 'Gameplay'
);
2016-08-08 17:51:40 +02:00
2016-08-09 16:17:19 +02:00
/** PARSE DB CONNECTIONS */
2016-08-09 16:17:19 +02:00
$dbConfigFile = new SplFileObject('database-config.dat');
2016-08-09 16:17:19 +02:00
if ($dbConfigFile->isFile())
{
2016-08-09 16:17:19 +02:00
while (!$dbConfigFile->eof())
{
2016-08-09 16:17:19 +02:00
$line = trim($dbConfigFile->fgets());
2016-08-09 16:17:19 +02:00
if ($line) // check not empty line
{
$parts = explode(' ', $line);
$fullUrl = $parts[1];
$urlParts = explode('/', $fullUrl);
$host = $urlParts[0];
$port = 3306;
// check is port has been declared
if (strpos($host, ':') !== false)
{
$hostParts = explode(':', $host);
$host = $hostParts[0];
$port = $hostParts[1];
}
2016-08-09 16:17:19 +02:00
$database = $urlParts[1];
2016-08-09 16:17:19 +02:00
$name = $parts[0];
$username = $parts[2];
$password = $parts[3];
$connection = new mysqli($host, $username, $password, $database, $port);
2016-08-09 16:17:19 +02:00
if ($connection->connect_error) {
die("Connection \"$name\" failed: $connection->connect_error");
}
2016-08-09 16:17:19 +02:00
$connections[$name] = $connection;
}
}
}
2016-08-09 16:17:19 +02:00
else
{
die('database-config.dat does not exist or is not a file.');
}
/**
* @param String $name
* @return mysqli
*/
function getConnection($name)
{
global $connections;
return $connections[$name];
}
/**
* @param Int $reportId
* @return Report
*/
function getReport($reportId)
{
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare('SELECT reports.suspectId, reports.categoryId, reportHandlers.handlerId FROM reports
LEFT JOIN reportHandlers ON reports.id = reportHandlers.reportId AND reportHandlers.aborted IS FALSE
LEFT JOIN reportResults ON reports.id = reportResults.reportId
WHERE reports.id = ?;');
2016-08-09 16:17:19 +02:00
$statement->bind_param('i', $reportId);
$statement->execute();
$statement->store_result();
$statement->bind_result($suspectId, $categoryId, $handlerId);
2016-08-09 16:17:19 +02:00
if ($statement->fetch())
{
2016-08-09 16:17:19 +02:00
$suspectUser = getUser($suspectId);
$reportReasons = getReporters($reportId);
$handlerUser = null;
2016-08-09 16:17:19 +02:00
if (!is_null($handlerId))
{
$handlerUser = getUser($handlerId);
}
return new Report($reportId, $handlerUser, $suspectUser, $reportReasons, $categoryId);
2016-08-09 16:17:19 +02:00
}
2016-08-09 16:17:19 +02:00
$statement->close();
2016-08-09 16:17:19 +02:00
return null;
}
/**
* @param string $token
* @return int|null
*/
function getSnapshotId($token)
{
$connection = getConnection('ACCOUNT');
$statement = $connection->prepare('SELECT id FROM snapshots WHERE token = ?;');
$statement->bind_param('s', $token); // TODO: correct data type
$statement->execute();
$statement->bind_result($snapshotId);
$statement->store_result();
$statement->fetch();
return $snapshotId;
}
/**
* @param int $snapshotId
* @return int|null
*/
function getSnapshotReportId($snapshotId)
{
$connection = getConnection('ACCOUNT');
2016-10-11 02:22:43 +02:00
$statement = $connection->prepare('SELECT id FROM reports WHERE snapshotId = ?;');
$statement->bind_param('i', $snapshotId);
$statement->execute();
$statement->bind_result($reportId);
$statement->store_result();
$statement->fetch();
return $reportId;
}
function getSnapshot($snapshotId)
2016-08-09 16:17:19 +02:00
{
/** @var $messages Message[] */
$messages = array();
2016-08-09 16:17:19 +02:00
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare("SELECT messageId, senderId, snapshotType, `server`, `time`, message FROM snapshots, snapshotMessages, snapshotMessageMap
WHERE snapshotMessageMap.snapshotId = snapshots.id
AND snapshotMessages.id = snapshotMessageMap.messageId
AND snapshots.id = ?;");
$statement->bind_param('i', $snapshotId);
2016-08-09 16:17:19 +02:00
$statement->execute();
$statement->bind_result($snapshotId, $senderId, $snapshotType, $server, $time, $message);
2016-08-09 16:17:19 +02:00
$statement->store_result();
2016-08-09 16:17:19 +02:00
while ($statement->fetch())
{
$recipients = getUsers(getMessageRecipients($snapshotId));
2016-08-09 16:17:19 +02:00
$message = new Message(getUser($senderId), $recipients, $time, $snapshotType, $message, $server);
array_push($messages, $message);
}
2016-08-09 16:17:19 +02:00
$statement->close();
$snapshotUsers = array();
2016-08-09 16:17:19 +02:00
foreach ($messages as $message)
{
2016-08-09 16:17:19 +02:00
$sender = $message->getSender();
$snapshotUsers[$sender->getId()] = $sender;
foreach ($message->getRecipients() as $recipient)
{
$snapshotUsers[$recipient->getId()] = $recipient;
}
}
2016-08-09 16:17:19 +02:00
return new Snapshot($snapshotId, $messages, $snapshotUsers);
}
2016-08-09 16:17:19 +02:00
/**
* @param $snapshotId
2016-08-09 16:17:19 +02:00
* @return Integer[] array
*/
function getMessageRecipients($snapshotId)
{
2016-08-09 16:17:19 +02:00
$recipientIds = array();
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare("SELECT recipientId FROM snapshotRecipients WHERE messageId = ?");
$statement->bind_param('i', $snapshotId);
2016-08-09 16:17:19 +02:00
$statement->execute();
$statement->bind_result($recipientId);
while ($statement->fetch())
{
array_push($recipientIds, $recipientId);
}
2016-08-09 16:17:19 +02:00
$statement->close();
2016-08-09 16:17:19 +02:00
return $recipientIds;
}
2016-08-09 16:17:19 +02:00
/**
* @param Integer[] $ids
* @return User[] array
*/
function getUsers($ids)
{
2016-08-09 16:17:19 +02:00
$users = array();
foreach ($ids as $id)
{
array_push($users, getUser($id));
}
return $users;
}
2016-08-09 16:17:19 +02:00
/**
* @param $id
* @return User
*/
function getUser($id)
{
2016-08-09 16:17:19 +02:00
if (isset($users[$id]))
{
return $users[$id];
}
else
{
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare('SELECT uuid, `name`, rank FROM accounts WHERE id = ?');
2016-08-09 16:17:19 +02:00
$statement->bind_param('i', $id);
$statement->execute();
$statement->bind_result($uuid, $name, $rank);
$statement->fetch();
2016-08-09 16:17:19 +02:00
$user = new User($id, $uuid, $name, parseRank($rank));
$users[$id] = $user;
$statement->close();
2016-08-09 16:17:19 +02:00
return $user;
}
}
2016-08-09 16:17:19 +02:00
/**
* @param int $reportId
* @return UserReport[]
*/
function getReporters($reportId)
{
2016-08-09 16:17:19 +02:00
global $dateTimeZone;
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare("SELECT reporterId, `time`, reason FROM reportReasons WHERE reportId = ?");
$reportReasons = array();
2016-08-09 16:17:19 +02:00
$statement->bind_param('i', $reportId);
$statement->execute();
$statement->bind_result($reporterId, $time, $reason);
$statement->store_result(); // prevents issues with other queries running before this statement is closed
2016-08-09 16:17:19 +02:00
while ($statement->fetch())
{
$reportReasons[$reporterId] = new UserReport(getUser($reporterId), new DateTime($time, $dateTimeZone), $reason);
}
2016-08-09 16:17:19 +02:00
$statement->close();
2016-06-30 04:13:13 +02:00
2016-08-09 16:17:19 +02:00
return $reportReasons;
2016-06-30 04:13:13 +02:00
}
2016-08-09 16:17:19 +02:00
/**
* @param Report $report
* @return User[]
*/
function getInvolvedUsers($report)
2016-08-09 16:17:19 +02:00
{
$involvedUsers[$report->getSuspect()->getId()] = $report->getSuspect();
2016-06-30 04:13:13 +02:00
2016-08-09 16:17:19 +02:00
foreach ($report->getReporters() as $reporterReason) {
$reporter = $reporterReason->getUser();
$involvedUsers[$reporter->getId()] = $reporter;
}
2016-07-27 16:52:22 +02:00
2016-08-09 16:17:19 +02:00
return $involvedUsers;
2016-07-27 16:52:22 +02:00
}
2016-08-09 16:17:19 +02:00
/**
* @param string $dbRank
* @return string
*/
function parseRank($dbRank)
{
2016-08-09 16:17:19 +02:00
$rank = $dbRank;
if ($dbRank == 'ALL')
{
$rank = 'PLAYER';
}
return $rank;
}
2016-08-09 16:17:19 +02:00
/**
* @param Message $messageA
* @param Message $messageB
* @return int
*/
function compareMessageTimes($messageA, $messageB)
{
2016-08-09 16:17:19 +02:00
return $messageA->getTimestamp()->getTimestamp() - $messageB->getTimestamp()->getTimestamp();
}
2016-08-09 16:17:19 +02:00
/**
* @param String $dateTime
* @param DateTimeZone $timezone
* @return DateTime
*/
function parseDateTime($dateTime, $timezone)
{
2016-08-09 16:17:19 +02:00
return DateTime::createFromFormat(jsonDateTimeFormat, $dateTime, $timezone);
}
2016-08-09 16:17:19 +02:00
/**
* Converts an interval to minutes, days or months, depending on the size.
*
* @param DateInterval $interval
* @return string
*/
function approximateHumanInterval($interval)
{
2016-08-09 16:17:19 +02:00
if ($interval->y > 0)
{
$humanString = $interval->y . ' year' . ($interval->y != 1 ? 's' : '');
} else if ($interval->m > 0)
{
$humanString = $interval->m . ' month' . ($interval->m != 1 ? 's' : '');
}
else if ($interval->d > 0)
{
$humanString = $interval->d . ' day' . ($interval->d != 1 ? 's' : '');
}
else if ($interval->h > 0)
{
$humanString = $interval->h . ' hour' . ($interval->h != 1 ? 's' : '');
}
else if ($interval->i > 0)
{
$humanString = $interval->i . ' minute' . ($interval->i != 1 ? 's' : '');
}
else
{
$humanString = $interval->s . ' second' . ($interval->s != 1 ? 's' : '');
}
return $humanString;
}
2016-08-09 16:17:19 +02:00
function getExpandedURL()
{
2016-08-09 16:17:19 +02:00
$vars = $_GET;
$vars['expanded'] = true;
return '?' . http_build_query($vars);
}
2016-01-30 00:12:50 +01:00
$validToken = isset($_GET['token']);
2016-10-11 02:22:43 +02:00
$errorMsg = "";
$title = 'Report & Snapshot System';
$token = null;
2016-08-09 16:17:19 +02:00
$expanded = null;
$report = null;
$snapshot = null;
$messages = null;
if ($validToken)
{
$token = $_GET['token'];
2016-08-09 16:17:19 +02:00
$expanded = isset($_GET['expanded']) && $_GET['expanded'];
$snapshotId = getSnapshotId($token);
2016-08-09 16:17:19 +02:00
if ($snapshotId != null)
2016-08-09 16:17:19 +02:00
{
$snapshot = getSnapshot($snapshotId);
2016-10-11 02:22:43 +02:00
$messages = $snapshot->getMessages();
$reportId = getSnapshotReportId($snapshotId);
$report = null;
if ($reportId)
{
$report = getReport($reportId);
$title = "Report #$reportId";
}
else
{
$title = "Snapshot #$snapshotId";
}
2016-08-09 16:17:19 +02:00
}
else
{
$validToken = false;
2016-10-11 02:22:43 +02:00
$errorMsg = 'Invalid token.';
2016-08-09 16:17:19 +02:00
}
}
?>
2015-12-16 01:22:55 +01:00
<!DOCTYPE html>
<html>
<head>
2015-12-16 01:22:55 +01:00
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="css/tiger.css">
<link href='https://fonts.googleapis.com/css?family=Crete+Round' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<title>
<?= $title ?> &middot; Mineplex
</title>
</head>
<body>
2015-12-16 01:22:55 +01:00
<div id="wrapper">
<div id="header">
<img src="img/logo.png" height="70px" width="70px" />
<h1>Report System</h1>
</div>
<div id="search">
<form id="token-input" name="token-input" action="view.php" method="get">
2015-12-18 16:52:20 +01:00
<div class="input-group">
<input name="token" type="text" class="form-control" placeholder="Enter snapshot token...">
2015-12-18 16:52:20 +01:00
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit" form="token-input"><i class="fa fa-search"></i> Search</button>
2015-12-18 16:52:20 +01:00
</span>
</div>
</form>
2015-12-16 01:22:55 +01:00
</div>
2015-12-18 18:41:03 +01:00
2016-10-11 02:22:43 +02:00
<?php if ((isset($_GET['token']) && !$validToken) || !empty($errorMsg)): ?>
2015-12-18 19:11:25 +01:00
<div id="content" class="center-block" style="text-align: center; background-color: rgba(204, 34, 42, 0.52);">
<p class="error-oh-no" style="font-size: 60px;">What did you do?!?!?</p>
<img src="img/shaun.gif" />
<?php if (!empty($errorMsg)): ?>
<p class="error-oh-no" style="font-size: 40px;">Error: <?= $errorMsg ?></p>
<br />
<?php endif; ?>
2015-12-16 01:22:55 +01:00
</div>
2016-02-27 01:25:48 +01:00
<?php else: ?>
2016-10-11 02:22:43 +02:00
<?php if (!isset($_GET['token'])) exit(); ?>
2016-02-27 01:08:48 +01:00
2015-12-18 19:11:25 +01:00
<div id="content">
<div>
2015-12-16 01:22:55 +01:00
<hr>
2015-12-18 19:11:25 +01:00
<h2 style="font-family: 'Oswald', sans-serif; text-align: center;">
<?= $title ?>
2015-12-18 19:11:25 +01:00
</h2>
<hr>
</div>
<div class="row">
<div id="chat" class="col-lg-7">
<h4><i class="fa fa-comments"></i>&nbsp;&nbsp;&nbsp;Chat Log</h4>
<hr>
2016-08-09 16:20:49 +02:00
<div id="log">
<?php
2016-08-09 16:17:19 +02:00
$messageCount = count($messages);
$displayAmount = $expanded || $messageCount <= collapsedMessageCount ? $messageCount : collapsedMessageCount;
$involvedUsers = null;
2016-08-09 16:17:19 +02:00
if ($report != null)
2016-08-09 16:17:19 +02:00
{
$involvedUsers = getInvolvedUsers($report);
}
else
{
$involvedUsers = array();
2016-08-09 16:17:19 +02:00
}
foreach ($snapshot->getPlayers() as $player)
{
$involvedUsers[$player->getId()] = $player;
}
2016-08-09 16:17:19 +02:00
2016-08-10 17:57:44 +02:00
if($displayAmount == 0): ?>
2016-08-09 16:17:19 +02:00
<span class="black">No chat log available for this report.</span>
<?php else:
2016-08-10 17:57:44 +02:00
for($i = 0; $i < $displayAmount; $i++):
2016-08-09 16:17:19 +02:00
$message = $messages[$i];
$typeId = $message->getType();
$typeDisplayName = Message::$TYPE_DISPLAY_NAMES[$typeId];
$isPM = $typeId == Message::TYPE_PM;
?>
<span class="log-line">
<?php if($isPM): ?>
<span class="label label-primary chat pm"><?= $typeDisplayName ?></span>
<?php elseif($typeId == Message::TYPE_PARTY): ?>
<span class="label label-warning chat"><?= $typeDisplayName ?></span>
<?php else: ?>
<span class="label label-info chat"><?= $typeDisplayName ?></span>
<?php endif; ?>
<span class="remove-whitespace">
<span class="<?= ($report != null && $message->getSender() == $report->getSuspect() ? 'suspect' : 'black') ?>"><?= $message->getSender()->getUsername() ?></span>
<?php if ($isPM): ?>
&nbsp;->&nbsp;<?= $message->getRecipients()[0]->getUsername() ?>
<?php endif; ?>
<span class="message-separator black">: </span>
</span>
<span class="text-muted"><?= $message->getMessage() ?></span>
<?php if ($i < $displayAmount - 1): // Don't break on the last element ?>
<br />
<?php endif; ?>
2016-08-09 16:17:19 +02:00
</span>
<?php endfor; ?>
<?php endif; ?>
2015-12-16 01:22:55 +01:00
</div>
2016-02-27 01:08:48 +01:00
<?php if (!$expanded && $displayAmount < $messageCount): ?>
<br />
<a href="<?= getExpandedURL() ?>">Show All (<?= $messageCount ?> messages)</a>
<?php endif; ?>
2015-12-18 19:11:25 +01:00
</div>
<div id="users" class="col-lg-5">
<?php if ($report != null): ?>
<h4><i class="fa fa-info-circle"></i>&nbsp;&nbsp;&nbsp;Information</h4>
<hr>
<div class="row">
<div class="col-lg-12">
<?php
// Put all reporter usernames in array for easy access later
$reporterUsernames = array();
foreach ($report->getReporters() as $reporterReason)
{
$reporterUsernames[count($reporterUsernames)] = $reporterReason->getUser()->getUsername();
}
$reportCreationTime = $report->getTimeCreated();
$age = approximateHumanInterval($reportCreationTime->diff(new DateTime('now', $reportCreationTime->getTimezone())));
?>
<i class="fa fa-clock-o fa-fw"></i>
<span class="label label-pill label-default" title="Last Report: <?= $reportCreationTime->format('Y/m/d H:i:s T') ?>"><?= $age . ' ago' ?></span>
<br>
<i class="fa fa-sitemap fa-fw"></i>
<span class="label label-pill label-primary"><?= $categories[$report->getCategory()] ?></span>
<br>
<i class="fa fa-user-plus fa-fw"></i>
<span class="label label-pill label-success">Reported by <?= implode(", ", $reporterUsernames) ?></span>
<br>
<i class="fa fa-user-times fa-fw"></i>
<span class="label label-pill label-danger">Suspect is <?= $report->getSuspect()->getUsername() ?></span>
<br>
<i class="fa fa-gavel fa-fw"></i>
<span class="label label-pill label-warning">
<?php if ($report->getHandler() != null): ?>
Staff Member assigned is <?= $report->getHandler()->getUsername() ?>
<?php else: ?>
No Staff Member assigned
<?php endif; ?>
</span>
<br>
<?php endif; ?>
2015-12-18 19:11:25 +01:00
</div>
</div>
<br>
2015-12-18 19:11:25 +01:00
<h4><i class="fa fa-users"></i>&nbsp;&nbsp;&nbsp;Users</h4>
<hr>
2016-06-30 04:13:13 +02:00
<?php foreach($involvedUsers as $user): ?>
<img src="http://cravatar.eu/avatar/<?= $user->getUUID() ?>/55.png" class="pull-left" />
&nbsp;&nbsp;<b class="name"><?= $user->getUsername() ?></b> <span class="label label-staff"><?= ucwords(strtolower($user->getRank())) ?></span><br> <!-- TODO different styling for different ranks -->&nbsp;
2016-06-30 04:13:13 +02:00
<code style="font-size: 11px;"><?= $user->getUUID() ?></code>
2015-12-18 19:11:25 +01:00
<br><br>
<?php endforeach; ?>
2015-12-16 01:22:55 +01:00
</div>
</div>
</div>
2016-02-27 01:25:48 +01:00
<?php endif; ?>
<div id="footer">
2015-12-16 01:22:55 +01:00
<a href="http://www.mineplex.com"><img src="img/logo-full.png" width="225px" /></a>
<div class="btn-group pull-right indent-link" style="font-family: 'Crete Round', serif; padding-top: 10px;">
<a href="http://www.mineplex.com" class="btn btn-link btn-small text-muted">Home</a>
<a href="http://www.mineplex.com/shop/" class="btn btn-link btn-small text-muted">Shop</a>
<a href="http://www.mineplex.com/forums/" class="btn btn-link btn-small text-muted">Forums</a>
<a href="http://www.mineplex.com/supporthub/" class="btn btn-link btn-small text-muted">Support</a>
</div>
</div>
</div>
</body>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js"></script>
</html>
2016-02-27 01:25:48 +01:00
<?php foreach ($connections as $connection) {
2016-02-27 01:08:48 +01:00
$connection->close();
2016-02-27 01:25:48 +01:00
} ?>