X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FConfig%2FNix.git;a=blobdiff_plain;f=modules%2Fprivate%2Fwebsites%2Ftools%2Ftools%2Fdmarc_reports%2Fapi.php;fp=modules%2Fprivate%2Fwebsites%2Ftools%2Ftools%2Fdmarc_reports%2Fapi.php;h=0000000000000000000000000000000000000000;hp=850f9ce4994d7a24908ff8947abf757ffab7694c;hb=1a64deeb894dc95e2645a75771732c6cc53a79ad;hpb=fa25ffd4583cc362075cd5e1b4130f33306103f0 diff --git a/modules/private/websites/tools/tools/dmarc_reports/api.php b/modules/private/websites/tools/tools/dmarc_reports/api.php deleted file mode 100644 index 850f9ce..0000000 --- a/modules/private/websites/tools/tools/dmarc_reports/api.php +++ /dev/null @@ -1,122 +0,0 @@ - "ok", -); -$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport); - -function error_die($text, $number) { - http_response_code("500"); - $message = array( - "status" => "error", - "message" => $text, - "code" => $number - ); - - die(json_encode($message)); -} - -$anonymous = isset($_GET['anonymous']) && $_GET['anonymous']; -function maybe_anonymize($string, $long = false) { - global $anonymous_key; - global $anonymous; - if ($anonymous) { - if ($long) { - return md5($anonymous_key . ":" . $string); - } else { - return substr(md5($anonymous_key . ":" . $string), 0, 6); - } - } else { - return $string; - } -} - -if (!$anonymous && (!isset($_SERVER['HTTP_AUTHORIZATION']) || $_SERVER['HTTP_AUTHORIZATION'] === "")) { - header('WWW-Authenticate: Basic realm="Immae"'); - header('HTTP/1.0 401 Unauthorized'); - echo "You need to be authenticated to access private information"; - exit; -} - -if ($mysqli->connect_errno) { - error_die($mysqli->connect_error, $mysqli->connect_errno); -} - -if (!isset($_GET['serial'])) { - $response["domains"] = array(); - $query = $mysqli->query("SELECT DISTINCT domain FROM `report` ORDER BY domain"); - if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } - while($row = $query->fetch_assoc()) { - $response["domains"][] = maybe_anonymize($row['domain']); - } - - $response["orgs"] = array(); - $query = $mysqli->query("SELECT DISTINCT org FROM `report` ORDER BY org"); - if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } - while($row = $query->fetch_assoc()) { - $response["orgs"][] = maybe_anonymize($row['org']); - } - - $response["dates"] = array(); - $query = $mysqli->query("SELECT DISTINCT DISTINCT year(mindate) as year, month(mindate) as month FROM `report` ORDER BY year DESC,month DESC"); - if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } - while($row = $query->fetch_assoc()) { - $response["dates"][] = sprintf( "%'.04d-%'.02d", $row['year'], $row['month'] ); - } - - $response["summaries"] = array(); - if (isset($_GET['errors_only'])) { - $where = " WHERE (spfresult != 'pass' or dkimresult != 'pass')"; - } else { - $where = ""; - } - - $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"; - $query = $mysqli->query($sql); - if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } - while($row = $query->fetch_assoc()) { - $wanted_keys = array( - 'domain', 'org', 'reportid', 'mindate', 'maxdate', 'rcount', 'serial', 'policy_adkim', 'policy_aspf', 'policy_none', 'policy_sp', 'policy_pct', 'spfresult', 'dkimresult' - ); - $row = array_intersect_key($row, array_fill_keys($wanted_keys, '1')); - $row["domain"] = maybe_anonymize($row["domain"]); - $row["org"] = maybe_anonymize($row["org"]); - $row["reportid"] = maybe_anonymize($row["reportid"], true); - $response["summaries"][] = $row; - } -} else { - $response["rptrecord"] = []; - $sql = $mysqli->prepare("SELECT * FROM rptrecord where serial = ?"); - $sql->bind_param("s", $_GET["serial"]); - $sql->execute(); - $query = $sql->get_result(); - if ($mysqli->error) { error_die($mysqli->error, $mysqli->errno); } - while($row = $query->fetch_assoc()) { - if ($row['ip']) { - $ip = long2ip($row['ip']); - $host = gethostbyaddr($ip); - } elseif ( $row['ip6'] ) { - $ip = inet_ntop($row['ip6']); - $host = gethostbyaddr($ip); - } else { - $ip = "-"; - $host = "-"; - } - $wanted_keys = array( - 'ip', 'host', 'rcount', 'disposition', 'reason', 'dkimdomain', 'dkimresult', 'spfdomain', 'spfresult' - ); - $row = array_intersect_key($row, array_fill_keys($wanted_keys, '1')); - $row['ip'] = maybe_anonymize($ip); - $row['host'] = maybe_anonymize($host); - $row['dkimdomain'] = maybe_anonymize($row['dkimdomain']); - $row['spfdomain'] = maybe_anonymize($row['spfdomain']); - $response["rptrecord"][] = $row; - } -} - -header("Content-Type: application/json"); - -echo json_encode($response, JSON_PRETTY_PRINT); -?>