diff options
Diffstat (limited to 'modules/private/websites/tools/tools/dmarc_reports/api.php')
-rw-r--r-- | modules/private/websites/tools/tools/dmarc_reports/api.php | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/modules/private/websites/tools/tools/dmarc_reports/api.php b/modules/private/websites/tools/tools/dmarc_reports/api.php new file mode 100644 index 0000000..9b7f0c0 --- /dev/null +++ b/modules/private/websites/tools/tools/dmarc_reports/api.php | |||
@@ -0,0 +1,87 @@ | |||
1 | <?php | ||
2 | |||
3 | require(getenv("SECRETS_FILE")); | ||
4 | |||
5 | $response = array( | ||
6 | "status" => "ok", | ||
7 | ); | ||
8 | $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport); | ||
9 | |||
10 | function error_die($text, $number) { | ||
11 | http_response_code("500"); | ||
12 | $message = array( | ||
13 | "status" => "error", | ||
14 | "message" => $text, | ||
15 | "code" => $number | ||
16 | ); | ||
17 | |||
18 | die(json_encode($message)); | ||
19 | } | ||
20 | |||
21 | if ($mysqli->connect_errno) { | ||
22 | error_die($mysqli->connect_error, $mysqli->connect_errno); | ||
23 | } | ||
24 | |||
25 | if (!isset($_GET['serial'])) { | ||
26 | $response["domains"] = array(); | ||
27 | $query = $mysqli->query("SELECT DISTINCT domain FROM `report` ORDER BY domain"); | ||
28 | if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } | ||
29 | while($row = $query->fetch_assoc()) { | ||
30 | $response["domains"][] = $row['domain']; | ||
31 | } | ||
32 | |||
33 | $response["orgs"] = array(); | ||
34 | $query = $mysqli->query("SELECT DISTINCT org FROM `report` ORDER BY org"); | ||
35 | if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } | ||
36 | while($row = $query->fetch_assoc()) { | ||
37 | $response["orgs"][] = $row['org']; | ||
38 | } | ||
39 | |||
40 | $response["dates"] = array(); | ||
41 | $query = $mysqli->query("SELECT DISTINCT DISTINCT year(mindate) as year, month(mindate) as month FROM `report` ORDER BY year DESC,month DESC"); | ||
42 | if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } | ||
43 | while($row = $query->fetch_assoc()) { | ||
44 | $response["dates"][] = sprintf( "%'.04d-%'.02d", $row['year'], $row['month'] ); | ||
45 | } | ||
46 | |||
47 | $response["summaries"] = array(); | ||
48 | if (isset($_GET['errors_only'])) { | ||
49 | $where = " WHERE (spfresult != 'pass' or dkimresult != 'pass')"; | ||
50 | } else { | ||
51 | $where = ""; | ||
52 | } | ||
53 | |||
54 | $sql = "SELECT report.* , sum(rptrecord.rcount) AS rcount, MIN(rptrecord.dkimresult) AS dkimresult, MIN(rptrecord.spfresult) AS spfresult FROM report LEFT JOIN (SELECT rcount, COALESCE(dkimresult, 'neutral') AS dkimresult, COALESCE(spfresult, 'neutral') AS spfresult, serial FROM rptrecord) AS rptrecord ON report.serial = rptrecord.serial$where GROUP BY serial ORDER BY mindate ASC, maxdate ASC, org"; | ||
55 | $query = $mysqli->query($sql); | ||
56 | if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } | ||
57 | while($row = $query->fetch_assoc()) { | ||
58 | unset($row["raw_xml"]); | ||
59 | $response["summaries"][] = $row; | ||
60 | } | ||
61 | } else { | ||
62 | $response["rptrecord"] = []; | ||
63 | $sql = $mysqli->prepare("SELECT * FROM rptrecord where serial = ?"); | ||
64 | $sql->bind_param("s", $_GET["serial"]); | ||
65 | $sql->execute(); | ||
66 | $query = $sql->get_result(); | ||
67 | if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } | ||
68 | while($row = $query->fetch_assoc()) { | ||
69 | if ($row['ip']) { | ||
70 | $ip = long2ip($row['ip']); | ||
71 | $host = gethostbyaddr($ip); | ||
72 | } elseif ( $row['ip6'] ) { | ||
73 | $ip = inet_ntop($row['ip6']); | ||
74 | $host = gethostbyaddr($ip); | ||
75 | } else { | ||
76 | $ip = "-"; | ||
77 | $host = "-"; | ||
78 | } | ||
79 | $row['ip'] = $ip; | ||
80 | $row['host'] = $host; | ||
81 | unset($row['ip6']); | ||
82 | $response["rptrecord"][] = $row; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | echo json_encode($response, JSON_PRETTY_PRINT); | ||
87 | ?> | ||