Commit 4a0a76e9 authored by Bastien Durel's avatar Bastien Durel
Browse files

record reports in database

parent f5c9af28
/vendor/
/config.inc
/pdo.config.php
......@@ -7,6 +7,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if ($_SERVER['REQUEST_METHOD'] != 'POST') die("unknown method");
include_once 'vendor/autoload.php';
include_once 'pdo.config.php'; // get $db object
$data = json_decode(file_get_contents('php://input'));
......@@ -14,8 +15,45 @@ $validator = new JsonSchema\Validator;
$validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
if ($validator->isValid()) {
// TODO...
//var_dump($data);
$stmt = $db->prepare("INSERT INTO `report` (`organization_name`, `start_datetime`, `end_datetime`, `contact_info`, `report_id`) VALUES (:n, :sd, :ed, :c, :i);");
$stmt_policy = $db->prepare("INSERT INTO `policy` (`organization_name`, `report_id`, `policy_type`, `policy_string`, `policy_domain`, `mx_host`, `total_successful_session`, `total_failure_session`) VALUES (:n, :i, :t, :s, :d, :mx, :tss, :tsf)");
$stmt_detail = $db->prepare("INSERT INTO `failure_detail` (`policy_id`, `result_type`, `sending_mta_ip`, `receiving_mx_hostname`, `receiving_mx_helo`, `receiving_ip`, `failed_session_count`, `additional_information`, `failure_reason_code`) VALUES (:pid, :t, :sip, :rmx, :rh, :rip, :fs, :add, :c)");
$db->beginTransaction();
$stmt->bindParam(':n', $data->{'organization-name'});
$stmt->bindParam(':sd', $data->{'date-range'}->{'start-datetime'});
$stmt->bindParam(':ed', $data->{'date-range'}->{'end-datetime'});
$stmt->bindParam(':c', $data->{'contact-info'});
$stmt->bindParam(':i', $data->{'report-id'});
$stmt->execute();
$stmt_policy->bindParam(':n', $data->{'organization-name'});
$stmt_policy->bindParam(':i', $data->{'report-id'});
foreach ($data->{'policies'} as $pol) {
$stmt_policy->bindParam(':t', $pol->{'policy'}->{'policy-type'});
$stmt_policy->bindParam(':s', $pol->{'policy'}->{'policy-string'});
$stmt_policy->bindParam(':d', $pol->{'policy'}->{'policy-domain'});
$stmt_policy->bindParam(':mx', $pol->{'policy'}->{'mx-host'});
$stmt_policy->bindParam(':tss', $pol->{'summary'}->{'total-successful-session-count'});
$stmt_policy->bindParam(':tsf', $pol->{'summary'}->{'total-failure-session-count'});
$stmt_policy->execute();
$id = $db->lastInsertId();
$stmt_detail->bindParam(':pid', $id);
foreach ($pol->{'failure-details'} as $fail) {
$stmt_detail->bindParam(':t', $fail->{'result-type'});
$stmt_detail->bindParam(':sip', $fail->{'sending-mta-ip'});
$stmt_detail->bindParam(':rmx', $fail->{'receiving-mx-hostname'});
$stmt_detail->bindParam(':rh', $fail->{'receiving-mx-helo'});
$stmt_detail->bindParam(':rip', $fail->{'receiving-ip'});
$stmt_detail->bindParam(':fs', $fail->{'failed-session-count'});
$stmt_detail->bindParam(':add', $fail->{'additional-information'});
$stmt_detail->bindParam(':c', $fail->{'failure-reason-code'});
$stmt_detail->execute();
}
}
$db->commit();
} else {
echo "JSON does not validate. Violations:\n";
foreach ($validator->getErrors() as $error) {
......
<?php // -*- php -*-
$user = 'foo';
$pass = 'bar';
$db = new PDO('mysql:host=localhost;dbname=reports;charset=UTF8', $user, $pass);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment