Mineplex2018-withcommit/Plugins/Mineplex.ChatSnapManager/web/report.php
2015-12-08 18:37:05 +00:00

300 lines
5.6 KiB
PHP

<?php
$dataDir = 'data/';
// ID corresponds to the Report ID
if (!isset($_GET['id']) || empty($_GET['id'])) {
echo 'No identifier supplied.';
return;
}
$id = removeNonAlphaNumeric($_GET['id']); // prevents escaping
$filePath = $dataDir . $id . '.json';
if (file_exists($filePath)) {
displayMessages(toDataArray($filePath));
} else {
echo 'Invalid identifier.';
}
/**
* @param array $dataArray root JSON data array.
*/
function displayMessages($dataArray)
{
$snapshot = toSnapshot($dataArray);
foreach ($snapshot->getMessages() as $message) {
echo getMessageLine($message);
echo '<br \>';
}
}
/**
* @param String $filename
* @return array JSON data array.
*/
function toDataArray($filename)
{
return json_decode(file_get_contents($filename), true);
}
/**
* @param Message $message
* @return string
*/
function getMessageLine($message)
{
return $message->getSender()->getUsername() . ' - ' . $message->getMessage();
}
/**
* @param array $snapshotData Snapshot data array.
* @return Snapshot
*/
function toSnapshot($snapshotData)
{
$identifier = $snapshotData['token'];
$players = getPlayers($snapshotData['usernames']);
$messages = getMessages($players, $snapshotData['snapshots']);
return new Snapshot($identifier, $messages, $players);
}
/**
* @param Player[] $players
* @param array $messagesData Messages data array.
* @return Message[]
*/
function getMessages($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)
{
$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 getPlayers($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
{
throw new RuntimeException('Player for UUID not found.');
}
}
/**
* @param String $input
* @return String
*/
function removeNonAlphaNumeric($input)
{
return preg_replace('/[^A-Za-z0-9_\-]/', '_', $input);
}
class Snapshot {
/** @var String */
private $identifier;
/** @var Message[] */
private $messages;
/** @var Player[] */
private $players; // String UUID as Key
/**
* Snapshot constructor.
* @param String $identifier
* @param Message[] $messages
* @param Player[] $players
*/
function Snapshot($identifier, $messages, $players)
{
$this->identifier = $identifier;
$this->messages = $messages;
$this->players = $players;
}
/**
* @return String
*/
public function getIdentifier()
{
return $this->identifier;
}
/**
* @return Message[]
*/
public function getMessages()
{
return $this->messages;
}
/**
* @return Player[]
*/
public function getPlayers()
{
return $this->players;
}
}
class Message
{
/** @var Player */
private $sender;
/** @var Player[] */
private $recipients;
/** @var Int */
private $timestamp;
/** @var String */
private $message;
/**
* Message constructor.
* @param Player $sender
* @param Player[] $recipients
* @param Int $timestamp
* @param String $message
*/
function Message($sender, $recipients, $timestamp, $message)
{
$this->sender = $sender;
$this->recipients = $recipients;
$this->timestamp = $timestamp;
$this->message = $message;
}
/**
* @return Player
*/
public function getSender()
{
return $this->sender;
}
/**
* @return Player[]
*/
public function getRecipients()
{
return $this->recipients;
}
/**
* @return Int
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* @return String
*/
public function getMessage()
{
return $this->message;
}
}
class Player
{
/** @var String */
private $uuid;
/** @var String */
private $username;
/**
* Player constructor.
* @param String $uuid
* @param String $username
*/
function Player($uuid, $username)
{
$this->uuid = $uuid;
$this->username = $username;
}
/**
* @return String
*/
public function getUUID()
{
return $this->uuid;
}
/**
* @return String
*/
public function getUsername()
{
return $this->username;
}
}