#! /usr/bin/perl
######################################################################
#
# main module
#
######################################################################
#
# $Id: policy.pl,v 1.3 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;
use CGI;
require "common.pl";
require "xml.pl";
require "dataschema.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 $xml = new P3P::XML;
my $uri;
my $buffer;
my $q = new CGI;
if($q->param("uri")) {
	$uri = P3P::Common::correctURI($q->param("uri"));
	$xml->setURI($uri);
	$xml->access;
	$buffer = $xml->{content};
} elsif($q->param("file")) {
	my $f = $q->param('file');
	$uri = scalar($f);
	my $h = $q->uploadInfo($f);
	while (not eof $f) {$buffer .= <$f>};
	$xml->setURI($uri);
	$xml->{access} = 1;
	$xml->{code} = 200;
}
$xml->{content} = $buffer;

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

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

#
#  Does policy file exist?
#

if(!$xml->{content}	|| $xml->{content} eq "\n"
	|| $xml->{code} ne 200) {
	$pr->pri("<hr>
<p class='result'>The specified policy 
<font color='red'>does not</font> exist. 
Validation aborted. 
[<tt>".$uri."</tt>]");

	&pre_exit;
}

#
#  Check policy file
#
my $policy = new P3P::PolicyCheck;
$policy->uri($uri);
$policy->{xml} = $xml;
$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/file.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 Policy validation</h2>
<p><span class='big'>Target URI:</span>
<a target='tarwin' href='$uri'><tt>$uri</tt></a></p>
");
}
