#! /usr/bin/perl
######################################################################
#
# main module
#
######################################################################
#
# $Id: p3p.pl,v 1.4 2002/03/14 02:24:40 koike Exp $
# Created by Yuichi Koike (koike@w3.org)
#

unshift(@INC,"/home/koike/validator/lib");
#unshift(@INC,"/home/sushi/koike/work/unix/w3clib/site_perl/");
#$ENV{"http_proxy"} = "http://terra.mmp.cl.nec.co.jp:3128";
#$ENV{"https_proxy"} = "http://terra.mmp.cl.nec.co.jp:3128";

use strict;
require "common.pl";
require "xml.pl";
require "dataschema.pl";
require "policyref.pl";
require "http.pl";
require "html.pl";
require "policycheck.pl";

#
# Main routine
#

######################################################################
#  Initialization
######################################################################

# Dealing with parameters
$| = 1;

my %params = P3P::Common::read_params("GET");
my $pr = new P3P::Print;

# URI correction
my $target_uri = P3P::Common::correctURI($params{"uri"});

if($target_uri eq "") {
	print "Status: 302
Location: http://www.w3.org/P3P/validator.html
Content-type: text/html

<html><body>Please input URI</body></html>
";
	exit;
}

# Initial message
&init($target_uri);
my $step = 1;


######################################################################
#  Well known location file check
######################################################################

my $prf = new P3P::PolicyRef;
$prf->targetURI($target_uri);
$prf->prf_uri(P3P::Common::wellknownURI($target_uri));
$prf->check($step, $pr);
$step++;


######################################################################
#  HTTP header check
######################################################################

my $http = new P3P::HTTPCheck;
$http->targetURI($target_uri);
my $res1 = $http->check($step, $pr, ($prf->policy_uri ? $prf->prf_uri : ""));
if(! $res1) {
	# fatal error
}
$step++;

######################################################################
#  HTML header check
######################################################################

my $html = new P3P::HTMLCheck;
if($http->content_type eq "text/html") {
	$html->targetURI($target_uri);
	$html->content($http->content);
	$html->check($step, $pr, ($prf->policy_uri ? $prf->prf_uri : ""), $http->prf_uri);
	$step++;
}


######################################################################
#  Policy Reference File check
######################################################################

if($prf->policy_uri eq "") {
	# Determine Policy Reference File URI (HTTP overcomes HTML)
	my $prf_uri = ($http->prf_uri ? $http->prf_uri : $html->prf_uri);

	# Can't find PRF
	if(! $prf_uri) {
		$pr->pri("<hr><p class='result'>Validator 
<font color='red'>could not</font> find valid 
<font color='green'>policy reference file</font> 
URI. Validation aborted.</p>");
		&pre_exit;
	}

	$prf->prf_uri($prf_uri);
	$prf->targetURI($target_uri);
	$prf->fromHTML($http->prf_uri eq "");
	$prf->check($step, $pr);
	$step++;
}

######################################################################
#  Policy File check
######################################################################

#
#  Do I have valid Policy File URI?
#
if($prf->policy_uri eq "") {
	$pr->pri("<hr><p class='result'>Validator 
<font color='red'>could not</font> find 
<font color='brown'>P3P policy file</font>. 
Validation aborted.</p>");
	&pre_exit;
}


#
#  Does policy file exist?
#
my $eres = $prf->exist($prf->policy_uri);
if($eres ne "yes" && $eres ne "yes_but") {
	$pr->pri("<hr>
<p class='result'>The specified policy 
<font color='red'>does not</font> exist. 
Validation aborted. 
[<tt>".$prf->policy_uri."</tt>]");
	&pre_exit;
}

#
#  Check policy file
#
my $policy = new P3P::PolicyCheck;
$policy->uri($prf->policy_uri);
$policy->{xml} = $prf->getPolicy($prf->policy_uri);
$policy->prf_uri($prf->prf_uri);
$policy->check($step, $pr);

&pre_exit;
exit;

sub pre_exit {
	$pr->pri("<hr>\n</body>\n</html>\n");
	exit;
}

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

	open AAA, ">> /home/koike/validator/20020128/url.log";
	print AAA scalar(localtime()), " ", $ENV{"REMOTE_ADDR"}, " ", $uri, "\n";
	close AAA;

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

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN' 
     'http://www.w3.org/TR/REC-html40/loose.dtd'>
<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;
}

A:hover {
	background: #FFFF90
} 

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

PRE {
	font-family: 'Courier New', 'Courier';
	color: #808080; 
}

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

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

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

.step2 {
	font-size: 13pt;
	color: #0050B2; 
	font-weight: bold;
}

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

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

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

</head>
<body>

<h2>Results of P3P validation</h2>
<p><span class='big'>Target URI:</span>
<a target='tarwin' href='$uri'><tt>$uri</tt></a></p>
");
}

