Update chatsnap site to handle new token system

This commit is contained in:
Keir Nellyer 2016-10-11 00:45:33 +01:00
parent 5c5c159ee3
commit f39ee7c247
2 changed files with 69 additions and 45 deletions

View File

@ -15,9 +15,6 @@
/** @var Int */
private $category;
/** @var Snapshot */
private $snapshot;
/**
* Report constructor.
* @param Int $id
@ -25,16 +22,14 @@
* @param User $suspect
* @param UserReport[] $reporters
* @param Int $category
* @param Snapshot $snapshot
*/
function __construct($id, $handler, $suspect, $reporters, $category, $snapshot)
function __construct($id, $handler, $suspect, $reporters, $category)
{
$this->id = $id;
$this->handler = $handler;
$this->suspect = $suspect;
$this->reporters = $reporters;
$this->category = $category;
$this->snapshot = $snapshot;
}
/**
@ -103,12 +98,4 @@
{
return $this->category;
}
/**
* @return Snapshot
*/
public function getSnapshot()
{
return $this->snapshot;
}
}

View File

@ -89,7 +89,7 @@
function getReport($reportId)
{
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare('SELECT reports.suspectId, reports.categoryId, reports.snapshotId, reportHandlers.handlerId FROM reports
$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 = ?;');
@ -97,26 +97,20 @@
$statement->bind_param('i', $reportId);
$statement->execute();
$statement->store_result();
$statement->bind_result($suspectId, $categoryId, $snapshotId, $handlerId);
$statement->bind_result($suspectId, $categoryId, $handlerId);
if ($statement->fetch())
{
$suspectUser = getUser($suspectId);
$reportReasons = getReporters($reportId);
$snapshot = null;
$handlerUser = null;
if (!is_null($snapshotId))
{
$snapshot = getSnapshot($snapshotId);
}
if (!is_null($handlerId))
{
$handlerUser = getUser($handlerId);
}
return new Report($reportId, $handlerUser, $suspectUser, $reportReasons, $categoryId, $snapshot);
return new Report($reportId, $handlerUser, $suspectUser, $reportReasons, $categoryId);
}
$statement->close();
@ -124,7 +118,39 @@
return null;
}
function getSnapshot($messageId)
/**
* @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');
$statement = $connection->prepare('SELECT reportId 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)
{
/** @var $messages Message[] */
$messages = array();
@ -135,14 +161,14 @@ WHERE snapshotMessageMap.snapshotId = snapshots.id
AND snapshotMessages.id = snapshotMessageMap.messageId
AND snapshots.id = ?;");
$statement->bind_param('i', $messageId);
$statement->bind_param('i', $snapshotId);
$statement->execute();
$statement->bind_result($messageId, $senderId, $snapshotType, $server, $time, $message);
$statement->bind_result($snapshotId, $senderId, $snapshotType, $server, $time, $message);
$statement->store_result();
while ($statement->fetch())
{
$recipients = getUsers(getMessageRecipients($messageId));
$recipients = getUsers(getMessageRecipients($snapshotId));
$message = new Message(getUser($senderId), $recipients, $time, $snapshotType, $message, $server);
array_push($messages, $message);
}
@ -161,20 +187,20 @@ WHERE snapshotMessageMap.snapshotId = snapshots.id
}
}
return new Snapshot($messageId, $messages, $snapshotUsers);
return new Snapshot($snapshotId, $messages, $snapshotUsers);
}
/**
* @param $messageId
* @param $snapshotId
* @return Integer[] array
*/
function getMessageRecipients($messageId)
function getMessageRecipients($snapshotId)
{
$recipientIds = array();
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare("SELECT recipientId FROM snapshotRecipients WHERE messageId = ?");
$statement->bind_param('i', $messageId);
$statement->bind_param('i', $snapshotId);
$statement->execute();
$statement->bind_result($recipientId);
@ -355,28 +381,39 @@ WHERE snapshotMessageMap.snapshotId = snapshots.id
return '?' . http_build_query($vars);
}
$validId = isset($_GET['id']);
$validToken = isset($_GET['token']);
$idError = "";
$id = null;
$token = null;
$expanded = null;
$report = null;
$snapshot = null;
if ($validId)
if ($validToken)
{
$id = $_GET['id'];
$token = $_GET['token'];
$expanded = isset($_GET['expanded']) && $_GET['expanded'];
$report = getReport($id);
$snapshotId = getSnapshotId($token);
if ($report)
if ($snapshotId != null)
{
$snapshot = $report->getSnapshot();
$snapshot = getSnapshot($snapshotId);
$reportId = getSnapshotReportId($snapshotId);
if ($reportId)
{
$report = getReport($reportId);
}
else
{
$validId = false;
$idError = "Invalid id.";
$validToken = false;
$idError = 'Associated report not found.'; // TODO: Allow snapshots without reports in future
}
}
else
{
$validToken = false;
$idError = 'Invalid token.';
}
}
?>
@ -389,7 +426,7 @@ WHERE snapshotMessageMap.snapshotId = snapshots.id
<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>
<?php if ($validId): ?>
<?php if ($validToken): ?>
Report #<?= $report->getId() ?>
<?php else: ?>
Report System
@ -405,17 +442,17 @@ WHERE snapshotMessageMap.snapshotId = snapshots.id
<h1>Report System</h1>
</div>
<div id="search">
<form id="id-input" name="id-input" action="view.php" method="get">
<form id="token-input" name="token-input" action="view.php" method="get">
<div class="input-group">
<input name="id" type="text" class="form-control" placeholder="Enter snapshot id...">
<input name="token" type="text" class="form-control" placeholder="Enter snapshot token...">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit" form="id-input"><i class="fa fa-search"></i> Search</button>
<button class="btn btn-secondary" type="submit" form="token-input"><i class="fa fa-search"></i> Search</button>
</span>
</div>
</form>
</div>
<?php if (isset($_GET['id']) && !$validId && !empty($idError)): ?>
<?php if (isset($_GET['id']) && !$validToken && !empty($idError)): ?>
<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" />