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