Mineplex2018-withcommit/Plugins/Mineplex.ChatSnapManager/web/report.php

187 lines
3.9 KiB
PHP
Raw Normal View History

2015-12-07 18:05:58 +01:00
<?php
require_once('snapshot.php');
require_once('message.php');
require_once('player.php');
2015-12-08 19:37:05 +01:00
$dataDir = 'data/';
2015-12-07 18:05:58 +01:00
// ID corresponds to the Report ID
if (!isset($_GET['identifier']) || empty($_GET['identifier'])) {
exit('No identifier supplied.');
2015-12-07 18:05:58 +01:00
}
$identifier = removeBadCharacters($_GET['identifier']); // prevents escaping
$reportNumber = explode("-", $identifier)[0];
$filePath = $dataDir . $identifier . '.json';
2015-12-07 18:05:58 +01:00
if (!file_exists($filePath))
{
exit('Invalid identifier.');
2015-12-07 18:05:58 +01:00
}
$snapshot = toSnapshot($identifier, toDataArray($filePath));
/**
* @param String $filename
* @return array JSON data array.
*/
function toDataArray($filename)
{
2015-12-07 18:05:58 +01:00
return json_decode(file_get_contents($filename), true);
}
/**
* @param String $identifier
* @param Snapshot $snapshot
*/
function displayMessages($identifier, $snapshot)
{
echo '<p>';
foreach ($snapshot->getMessages() as $message) {
echo getMessageHTML($message) . "<br />\n";
}
echo '</p>';
}
/**
* @param Message $message
* @return string
*/
function getMessageHTML($message)
{
$sender = $message->getSender();
return '<span data-uuid=\"' . $sender->getUUID() . '\">' . $sender->getUsername() . '</span> - ' . $message->getMessage();
}
/**
* @param String $identifier
* @param array $snapshotData Snapshot data array.
* @return Snapshot
*/
function toSnapshot($identifier, $snapshotData)
{
$players = toPlayers($snapshotData['usernames']);
$messages = toMessages($players, $snapshotData['snapshots']);
$timeGenerated = $snapshotData["time_generated"];
return new Snapshot($identifier, $messages, $players, $timeGenerated);
}
/**
* @param Player[] $players
* @param array $messagesData Messages data array.
* @return Message[]
*/
function toMessages($players, $messagesData)
{
$messages = array();
for ($i = 0; $i < count($messagesData); $i++)
{
$messages[$i] = getMessage($players, $messagesData[$i]);
}
return $messages;
}
/**
* @param Player[] $players
* @param array $messageData Message data array.
* @return Message
*/
function getMessage($players, $messageData)
{
2015-12-08 19:37:05 +01:00
$sender = getPlayer($players, $messageData['sender']);
$recipients = getPlayersFromUUIDs($players, $messageData['recipients']);
$timestamp = $messageData['time'];
$message = $messageData['message'];
return new Message($sender, $recipients, $timestamp, $message);
}
/**
* @param Player[] $players
* @param String[] $uuidArray the UUIDs of the players to fetch
* @return Player[]
*/
function getPlayersFromUUIDs($players, $uuidArray)
{
$matchedPlayers = array();
for ($i = 0; $i < count($uuidArray); $i++)
{
$matchedPlayers[$i] = getPlayer($players, $uuidArray[$i]);
}
return $matchedPlayers;
}
/**
* @param Player[] $playersArray
* @return Player[]
*/
function toPlayers($playersArray) // String UUID as Key
{
$players = array();
foreach ($playersArray as $uuid => $username)
{
$players[$uuid] = new Player($uuid, $username);
}
return $players;
}
/**
* @param Player[] $players
* @param String $uuid
* @return Player
*/
function getPlayer($players, $uuid)
{
$player = $players[$uuid];
if ($player != null)
{
return $player;
}
else
{
2015-12-08 19:37:05 +01:00
throw new RuntimeException('Player for UUID not found.');
}
}
2015-12-08 19:37:05 +01:00
/**
* @param String $input
* @return String
*/
function removeBadCharacters($input)
2015-12-08 19:37:05 +01:00
{
return preg_replace('/[^A-Za-z0-9_\-]/', '_', $input);
}
?>
<html>
<head>
<title>Snapshot for Report #<?php echo $reportNumber ?></title>
</head>
<body>
<?php
2015-12-08 19:37:05 +01:00
displayMessages($identifier, $snapshot);
// convert to friendly date time
$seconds = $snapshot->getTimeGenerated() / 1000;
$friendlyDateTime = date('m/d/y \a\t h:ia', $seconds);
echo '<p>';
echo 'Snapshot taken at: ' . $friendlyDateTime;
echo '<br />';
echo 'Player names were valid at this time.';
echo '</p>';
?>
</body>
</html>