#!/usr/bin/perl
# csp-report v1.0  (c) 11.1.2019 by Andreas Ley  (u) 19.1.2019
# Handle reports for Content Security Policy violations

my $root = '/var/www';

use strict;

use File::Spec::Functions;
use CGI ':standard';
use JSON;

my $log = catfile($root,$ENV{'SERVER_NAME'},'log','csp-report.log');

print header('-type'=>'','-status'=>'204 No response');

my $json = param('POSTDATA');
# Shitty Microsoft BingBot issues a GET request on a report-uri!!!
exit unless (length($json));

my $report = decode_json($json);
$report->{'user-agent'} = user_agent();

if (open(LOG,'>>',$log)) {
	#my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
	#$report->{'dtg'} = sprintf('%04d-%02d-%02dT%02d:%02d:%02d',$year+1900,$mon+1,$mday,$hour,$min,$sec);
	print LOG encode_json($report),"\n";
	close(LOG);
}
