#! /usr/bin/perl
# wrtten by Yuichi Koike ($Id: match.pl,v 1.13 2000/07/24 19:27:52 koike Exp $)

use strict;
require "mylib.pl";
require "appel.pl";

#
# Main routine
#
my %buffers;
my %urls;
&parse_input(\%buffers, \%urls);
my ($p_url, $p_con, $a_url, $a_con);

# get P3P policy
if($buffers{"p_url"} eq "\n") {
	if($buffers{"p_file"} eq "\n") {
		print "Status: 302
Location: http://www.w3.org/P3P/validator/appel.html

<html><body>Please specify P3P policy.</body></html>
";
	}
	$p_con = $buffers{"p_file"};
	$p_url = $urls{"p_file"};
} else {
	$p_url = $buffers{"p_url"};
	chop($p_url);
	my %ret = &retrieve($p_url);
	$p_con = $ret{"response"}->content;
}

# get APPEL ruleset
if($buffers{"a_url"} eq "\n") {
	if($buffers{"a_file"} eq "\n") {
		print "Status: 302
Location: http://www.w3.org/P3P/validator/appel.html

<html><body>Please specify APPEL ruleset.</body></html>
";
	}
	$a_con = $buffers{"a_file"};
	$a_url = $urls{"a_file"};
} else {
	$a_url = $buffers{"a_url"};
	chop($a_url);
	my %ret = &retrieve($a_url);
	$a_con = $ret{"response"}->content;
}

# compare APPEL ruleset and P3P policy
my $xml = new P3P::XML;

# P3P Policy object
my $pdoc = $xml->parse($p_con);
my $policy = new P3P::Policy(XML => $pdoc);

# APPEL ruleset object
my $adoc = $xml->parse($a_con);
my $appel = new P3P::APPEL(XML => $adoc);

# Compare them
$appel->evaluate($policy);
my $rule = $appel->{match_rule};
my $error = $appel->{Error};

# Print result
&init;

# Print basic info
print "<p><span class='big'>P3P Policy:</span><br>
<a target='pwin' href='$p_url'>
<tt>$p_url</tt></a></p>
<p><span class='big'>APPEL Ruleset:</span><br>
<a target='pwin' href='$a_url'>
<tt>$a_url</tt></a></p>
<hr>
";

# No matching
unless($rule) {
	print "<p class='big'>No matching rule.</p>\n";
	print $error;
	&pre_exit;
}

# Print Matching Rule
print "<h2>Matching APPEL Rule:</h2>\n";
my $ruleprint = $xml->print($rule);
print "<pre>$ruleprint</pre>\n<hr>\n";

# Print Matched Policy
print "<h2>Matched Part of P3P Policy:</h2>\n";
print "<p style='color: #606060; font-size: 9pt'>
* Red elements are matched with the APPEL rule</p>";
$ruleprint = $xml->print($policy->{XML}->getDocumentElement);
print "<pre>$ruleprint</pre>\n<hr>\n";

&pre_exit;

sub pre_exit {
	print "\n</body></html>";
	exit;
}


print "<h2>Matched Part of Policy</h2>\n";



sub init {
	print "Content-type: text/html

<!DOCTYPE HTML PUBLIC '-//IETF//DTD HTML//EN'>
<html>
<head>
<title>P3P Validator</title>
<style type='text/css'>
BODY {
	margin: 2em 1em 2em 30px;
	font-family: 'Arial', 'Helvetica', sans-serif;
	font-size: 11pt;
	color: black;
	background: white;
	background-position: top left;
	background-attachment: fixed;
	background-repeat: no-repeat;
}

A:hover {
	background: #FFFF90
} 

H1 {
	font-family: 'Arial', 'Helvetica', sans-serif;
	font-size: 24pt;
	font-weight: normal;
	color: #0050B2; 
}

H2 {
	font-family: 'Arial', 'Helvetica', sans-serif;
	font-size: 18pt;
	font-weight: normal;
	color: #0050B2; 
}

H3 {
	font-family: 'Arial', 'Helvetica', sans-serif;
	font-size: 14pt;
	font-weight: normal;
	color: #0050B2; 
}

H4{
	font-family: 'Arial', 'Helvetica', sans-serif;
	font-size: 12pt;
	font-weight: normal;
	color: #0050B2; 
}

DL DT {
	font-weight: bold;
}

DL DD {
	margin-bottom: 0.7em;
}

pre {
	font-family: 'Arial';
}

TT {
	font-family: 'Courier New', 'Courier';
	color: brown;
}

.big {
	font-size: 15pt;
	font-weight: bold;
}

.step {
	font-size: 15pt;
	color: #0050B2; 
}

.result {
	font-size: 13pt;
	font-weight: bold;
}

.result2 {
	font-size: 12pt;
	font-weight: normal;
}

.mes {
	font-size: 10pt;
	margin-left: 30px;
}
</style>
</head>

<body>
<a href='http://www.w3.org/'>
<img border='0' src='http://www.w3.org/Icons/WWW/w3c_home'></a>

<h1>Result of APPEL Checker</h1>";
}

sub parse_input {
	my ($buffers, $urls) = @_;

	my $count = 1;
	my ($name, $url, $buffer, $delim);

	while(<STDIN>) {
		#line feed
		s/\015\012/\n/g;
		s/\015/\n/g;

		if($count eq 1) {
			# recognizing delimiter
			$delim = $_;
			chop($delim);
			$count = 2;
		} elsif($count eq 2) {
			# dealing with URL and FileName
			if(/name=\"(.*?)\"/) {
				$name = $1;
			}
			if(/filename=\"(.*?)\"/) {
				$url = $1;
				$url =~ s/\\/\//g;
				$url = "file:///".$url;
			}
			$count = 3;
		} elsif($count eq 3) {
			if($_ eq "\n") {
				$count = 4;
			}
		} else {
			if(/$delim/) {
				$$buffers{$name} = $buffer;
				$$urls{$name} = $url;
				$buffer = "";
				$count = 2;
			} else {
				$buffer = $buffer.$_;
			}
		}
	}
}

1;
