#!/usr/bin/perl
#
# wrtten by Yuichi Koike ($Id: p3p.pl,v 1.2 2000/10/06 16:02:56 koike Exp $)
#

# Modules
use strict;
require "step1.pl";
require "step2.pl";
require "step3.pl";
require "step4.pl";

#
# Main routine
#

# Auto Flash
$| = 1;

# Init
my %params = &read_params("GET");
my $pr = new P3P::Print;
$pr->set_warn($params{"warn"});

# URL correction
my $u = $params{"url"};
if($u eq "") {
	print "Status: 302
Location: http://www.w3.org/P3P/validator.html
Content-type: text/html

<html><body>Please input URL</body></html>
"
}

unless($u =~ /^http:/) {
	$u = "http://".$u;
}

unless ($u =~ /^http:\/\/.*\//) {
	$u = $u."/";
}
$params{"url"} = $u;

&init($params{"url"});
my $step = 1;

# Well Known Location
my $step0 = new P3P::RefCheck;
my $wellknown = $step0->wellknown($params{"url"});
my $result0 = $step0->check($wellknown, $params{"url"}, $pr, "well", $step, 1);
$step ++;
my $policy_uri = $step0->policy_uri;

# HTTP header checking
my $step1 = new P3P::HTTPCheck;
my $result1 = $step1->check($params{"url"}, $policy_uri, $pr, $step);
if($result1 eq 0) {
	&pre_exit;
}
$step ++;

# HTML header checking
my $step2 = new P3P::HTMLCheck;
my $result2 = $step2->check($params{"url"}, $step1, $policy_uri, $pr, $step);
if($result2 eq 0) {
	&pre_exit;
}
$step ++;

# Ready to go to step3 ?
my $pr_uri;
if($policy_uri eq "") {
	$pr_uri = &get_pr_uri($step1->uri, $step2->uri);
	if($pr_uri eq "") {
		&pre_exit;
	}
}

# Policy Reference File
$pr->clear_gerr;
my $step3; my $result3;
if($policy_uri eq "") {
	$step3 = new P3P::RefCheck;
	$result3 = $step3->check($pr_uri, $params{"url"}, $pr, "normal", $step, 0);
	if($result3 eq 0) {
		&pre_exit;
	}
	$policy_uri = $step3->policy_uri;
	$step ++;
}

# Policy Checking
my $step4 = new P3P::PolicyCheck;
my $result4 = $step4->check($policy_uri, $pr, $step);
$step++;
if($result4 eq 0) {
	&pre_exit;
}

&pre_exit;
exit;

sub pre_exit {
	if(0) {
		$pr->pri("<hr>
<p><span class='step'>Step $step: </span>
<span class='big'>Register to the compliant site list</span></p>
<p>
The URL is P3P compliant. If you would like to add your site 
to <a href='http://www.w3.org/P3P/compliant_sites'>W3C's compliant 
sites list</a>, please fill the below form and submit.
</p>
<form action='add.pl' method='GET'>
<input type='hidden' name='url' value='$u'>
<table border='0'>
<tr><td><b>Site URL:</b></td>
<td><tt>$u</tt></td></tr>
<tr><td><b>Site Name:</b></td>
<td><input type='text' name='name' size='40' maxlength='100' ></td></tr>
<tr><td><b>E-mail:</b></td>
<td><input type='text' name='email' size='40' maxlength='100' >
<input type='submit' value='regist'></td></tr>
</table>
</form>");
	}
	$pr->pri("<hr>\n</body>\n</html>\n");
	exit;
}

sub init {
	my $url = $_[0];

	open AAA, ">> /home/koike/validator/20000510/p3p.log";
	print AAA scalar(localtime()), " ", $url, "\n";
	close AAA;

	# HTTP headers
	$pr->pri("Content-type: text/html\n\n");

	# HTML headers
	$pr->pri("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\
                      \"http://www.w3.org/TR/REC-html40/loose.dtd\">");
	$pr->pri("<html>\n");
	$pr->pri("<head>\n");
	$pr->pri("<title>P3P Validator</title>\n");
	$pr->pri("<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: 'Courier New', 'Courier';
}

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>\n");
	$pr->pri("</head>\n");
	$pr->pri("<body>\n");

	# Title
	$pr->pri("<h2>Results of P3P validation</h2>\n");
	$pr->pri("<p><span class='big'>Target URL:</span>\n");
	$pr->pri("<a target='tarwin' href='$url'>\n");
	$pr->pri("<tt>$url</tt></a></p>\n");
}

sub get_pr_uri {
	my ($uri1, $uri2) = @_;

	if($uri1 eq "" && $uri2 eq "") {
		$pr->pri("<hr>\n<p class='big'>");
		$pr->pri("Validation <font color='red'>canceled.</font></p>\n");
		$pr->pri("<p class='result'>Could not retrieve ");
		$pr->pri("valid policy reference file URI.</p>");
		return "";
	} else {
		return ($uri1 ne "") ? $uri1 : $uri2;
	}
}
