|
1 | 1 | <?php |
2 | | -require_once ("connectionsnark.php"); |
| 2 | +require_once("connectionsnark.php"); |
3 | 3 |
|
4 | | -if (! (isset($_GET['pageNumber']))) { |
5 | | - $pageNumber = 1; |
6 | | -} else { |
7 | | - $pageNumber = $_GET['pageNumber']; |
8 | | -} |
| 4 | +// Validate and sanitize pageNumber from GET |
| 5 | +$pageNumber = isset($_GET['pageNumber']) ? filter_var($_GET['pageNumber'], FILTER_VALIDATE_INT, ['options' => ['default' => 1, 'min_range' => 1]]) : 1; |
9 | 6 |
|
10 | 7 | $perPageCount = 120; |
11 | 8 |
|
12 | 9 | // Check if IGNORE_APPLICATION_STATUS is set to 1 |
13 | 10 | $ignoreApplicationStatus = getenv('IGNORE_APPLICATION_STATUS') == 1; |
14 | 11 |
|
15 | 12 | // Modify SQL query based on IGNORE_APPLICATION_STATUS |
16 | | -$sqlCondition = $ignoreApplicationStatus ? "score is not null" : "application_status = true and score is not null"; |
| 13 | +$sqlCondition = $ignoreApplicationStatus ? "score IS NOT NULL" : "application_status = TRUE AND score IS NOT NULL"; |
17 | 14 |
|
18 | 15 | $sql = "SELECT COUNT(*) FROM nodes WHERE {$sqlCondition}"; |
19 | | - |
20 | 16 | if ($result = pg_query($conn, $sql)) { |
21 | 17 | $row = pg_fetch_row($result); |
22 | | - $rowCount = $row[0]; |
| 18 | + $rowCount = (int)$row[0]; |
23 | 19 | pg_free_result($result); |
24 | 20 | } |
25 | 21 |
|
26 | 22 | $pagesCount = ceil($rowCount / $perPageCount); |
27 | | - |
28 | 23 | $lowerLimit = ($pageNumber - 1) * $perPageCount; |
29 | 24 |
|
30 | 25 | // Use the modified SQL condition for the main query as well |
31 | 26 | $sqlQuery = "SELECT block_producer_key, score, score_percent FROM nodes WHERE {$sqlCondition} ORDER BY score DESC"; |
32 | 27 |
|
| 28 | +// Execute the main query and sanitize the results |
33 | 29 | $results = pg_query($conn, $sqlQuery); |
34 | | -$row = pg_fetch_all($results); |
| 30 | +$row = pg_fetch_all($results); |
35 | 31 |
|
36 | | -$maxScoreSnark= " WITH recentone as ( |
37 | | - SELECT batch_end_epoch end_epoch, extract('epoch' FROM (to_timestamp(batch_end_epoch) - interval '90' day )) start_epoch |
| 32 | +$maxScoreSnark = " |
| 33 | + WITH recentone AS ( |
| 34 | + SELECT batch_end_epoch end_epoch, |
| 35 | + extract('epoch' FROM (to_timestamp(batch_end_epoch) - interval '90' day)) start_epoch |
38 | 36 | FROM bot_logs b |
39 | | - where file_timestamps <= CURRENT_TIMESTAMP |
40 | | - ORDER BY batch_end_epoch DESC LIMIT 1 |
41 | | - ) |
42 | | - SELECT COUNT(1), to_char(to_timestamp(end_epoch), 'DD-MM-YYYY hh24:mi') as last_modified |
43 | | - FROM bot_logs , recentone |
44 | | - WHERE batch_start_epoch >= start_epoch and batch_end_epoch <= end_epoch |
45 | | - AND files_processed > -1 |
46 | | - group by 2 "; |
47 | | - |
48 | | -$maxScoreSnarkresult = pg_query($conn, $maxScoreSnark); |
49 | | -$maxScoreRow = pg_fetch_row($maxScoreSnarkresult); |
50 | | -$maxScore = $maxScoreRow[0]; |
51 | | -$last_modified=$maxScoreRow[1]; |
52 | | - |
53 | | -echo json_encode(array('row' => $row, 'rowCount' => $rowCount, 'maxScore' => $maxScore, 'last_modified'=>$last_modified)); |
| 37 | + WHERE file_timestamps <= CURRENT_TIMESTAMP |
| 38 | + ORDER BY batch_end_epoch DESC |
| 39 | + LIMIT 1 |
| 40 | + ) |
| 41 | + SELECT COUNT(1), to_char(to_timestamp(end_epoch), 'DD-MM-YYYY hh24:mi') AS last_modified |
| 42 | + FROM bot_logs, recentone |
| 43 | + WHERE batch_start_epoch >= start_epoch |
| 44 | + AND batch_end_epoch <= end_epoch |
| 45 | + AND files_processed > -1 |
| 46 | + GROUP BY 2 |
| 47 | +"; |
| 48 | + |
| 49 | +// Execute the query |
| 50 | +$maxScoreSnarkResult = pg_query($conn, $maxScoreSnark); |
| 51 | +$maxScoreRow = pg_fetch_row($maxScoreSnarkResult); |
| 52 | + |
| 53 | +// Sanitize database output before using it |
| 54 | +$maxScore = (int)$maxScoreRow[0]; |
| 55 | +$last_modified = htmlspecialchars($maxScoreRow[1], ENT_QUOTES, 'UTF-8'); |
| 56 | + |
| 57 | +// Ensure to sanitize the JSON output |
| 58 | +foreach ($row as &$r) { |
| 59 | + $r['block_producer_key'] = htmlspecialchars($r['block_producer_key'], ENT_QUOTES, 'UTF-8'); |
| 60 | + $r['score'] = (float)$r['score']; |
| 61 | + $r['score_percent'] = (float)$r['score_percent']; |
| 62 | +} |
| 63 | + |
| 64 | +echo json_encode(array('row' => $row, 'rowCount' => $rowCount, 'maxScore' => $maxScore, 'last_modified' => $last_modified)); |
54 | 65 |
|
55 | 66 | ?> |
0 commit comments