Skip to content

Commit c1a46a6

Browse files
committed
Retrieve reports using archive status
1 parent 03583f7 commit c1a46a6

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

app/controllers/Api.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ private function parseCookies($cookies)
277277
public function reports()
278278
{
279279
$id = $this->getPostValue('id');
280+
$archive = $this->getPostValue('archive') == '1' ? 1 : 0;
280281

281282
// Check payload permissions
282283
$payloadList = $this->payloadList();
@@ -288,7 +289,7 @@ public function reports()
288289
if (+$id === 0) {
289290
if ($this->isAdmin()) {
290291
// Show all reports
291-
$reports = $this->model('Report')->getAll();
292+
$reports = $this->model('Report')->getAllByArchive($archive);
292293
} else {
293294
// Show all reports of allowed payloads
294295
$reports = [];
@@ -299,7 +300,7 @@ public function reports()
299300
if (strpos($payload['payload'], '/') === false) {
300301
$payloadUri .= '/%';
301302
}
302-
$reports = array_merge($reports, $this->model('Report')->getAllByPayload($payloadUri));
303+
$reports = array_merge($reports, $this->model('Report')->getAllByPayload($payloadUri, $archive));
303304
}
304305
}
305306
}
@@ -311,20 +312,7 @@ public function reports()
311312
if (strpos($payload['payload'], '/') === false) {
312313
$payloadUri .= '/%';
313314
}
314-
$reports = $this->model('Report')->getAllByPayload($payloadUri);
315-
}
316-
317-
// Remove or keep reports depending the requested archive value
318-
$archive = $this->getPostValue('archive') == '1' ? true : false;
319-
foreach ($reports as $key => $value) {
320-
$reports[$key]['ip'] = substr($reports[$key]['ip'], 0, 25);
321-
$reports[$key]['payload'] = substr($reports[$key]['payload'], 0, 50);
322-
323-
if (($reports[$key]['archive'] == '0' && $archive) ||
324-
($reports[$key]['archive'] == '1' && !$archive)
325-
) {
326-
unset($reports[$key]);
327-
}
315+
$reports = $this->model('Report')->getAllByPayload($payloadUri, $archive);
328316
}
329317

330318
return json_encode(["data" => array_values($reports)]);

app/models/Report.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ public function getAll()
2525
return $data;
2626
}
2727

28+
/**
29+
* Get all reports by archive status
30+
*
31+
* @param string $archive Archive status
32+
* @return array
33+
*/
34+
public function getAllByArchive($archive)
35+
{
36+
$database = Database::openConnection();
37+
$database->prepare('SELECT id,uri,ip,payload,shareid FROM reports WHERE archive = :archive ORDER BY id DESC');
38+
$database->bindValue(':archive', $archive);
39+
$database->execute();
40+
41+
$data = $database->fetchAll();
42+
43+
return $data;
44+
}
45+
2846
/**
2947
* Get report by share id
3048
*
@@ -51,13 +69,15 @@ public function getByShareId($id)
5169
/**
5270
* Get report by payload
5371
* @param string $payload The payload
72+
* @param string $archive Archive status
5473
* @throws Exception
5574
* @return array
5675
*/
57-
public function getAllByPayload($payload)
76+
public function getAllByPayload($payload, $archive)
5877
{
5978
$database = Database::openConnection();
60-
$database->prepare('SELECT id,uri,ip,payload,archive,shareid FROM reports WHERE payload LIKE :payload ORDER BY id DESC');
79+
$database->prepare('SELECT id,uri,ip,payload,shareid FROM reports WHERE payload LIKE :payload AND archive = :archive ORDER BY id DESC');
80+
$database->bindValue(':archive', $archive);
6181
$database->bindValue(':payload', $payload);
6282

6383
if (!$database->execute()) {

0 commit comments

Comments
 (0)