#!/usr/bin/perl
# (C) 2005 Dave Hansen <dave@sr71.net>
# 
# This code is licenced under the GPL.
#

use lib "/var/www/gpsdb";
use gps_db;

print "Content-type: application/xml\n\n";
#print "Content-type: text/plain\n\n";

sub fixup_formdata
{
	$var = shift;
	$var =~ tr/+/ /;
	$var =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$var =~ s/[\'\;\"\)\(]//g;
	return $var;
}

my %formdata;
my @attrs = split(/&/, $ENV{QUERY_STRING});
foreach $attr (@attrs) {
	(my $var, my $value) = split (/=/, $attr);
	dprint "attr: '$attr' var: '$var' val: '$value'\n";
	$var = fixup_formdata($var);
	$value = fixup_formdata($value);
	$formdata{$var} = $value;
	dprint "formdata{$var} = '$value'\n";
}
#dprint "ENV{QUERY_STRING}: $ENV{QUERY_STRING}\n";

my $dbh = db_connect();

$id_array_ref = get_coordinates_ids_with_attributes($dbh, \%formdata);
print "<points>\n";
my $n = 0;
foreach $coord_id (@$id_array_ref) {
	print_coordinate_xml($dbh, "point", $coord_id);
}
print "</points>\n";

$dbh->disconnect();
