#!/usr/bin/perl
use strict;

my $lat = 0;
my $lon = 0;
my $zoom = 13;
my $text = '10px';
my $name = '';
my $address = '';
my $parameter_error = 0;
my $map_width = 350;
my $map_heigth = 500;
my $body_style = 'margin:0; padding:0;';

sub url_decode {
	my $uri = shift;
	$uri =~ s/%[cC]2%([a-bA-B][0-9a-fA-F])/pack("c",hex($1))/ge;	# UTF-8 Umlaute dekodieren (1)
	$uri =~ s/%[cC]3%([8-9a-bA-B][0-9a-fA-F])/pack("c",hex($1)+0x40)/ge;	# UTF-8 Umlaute dekodieren (2)
	$uri =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;	# ISO Umlaute dekodieren
	$uri =~ s/\\x([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
	$uri =~ s/\+/ /g;	# decode space
	return $uri;
}

# use pathinfo
my $uri = $ENV{REQUEST_URI};
$uri = url_decode($uri);
# /map2/lat/lon/ad_code/zoom/name/address
$uri =~ s#^/route/##;
$uri =~ s#\.html$##;
my $ad_code;	# 1 = ads, 2 = adlinks, 3 = both
($lat, $lon, $ad_code, $zoom, $name, $address) = split(/\//, $uri);
$lat = 0 if (!defined($lat));
$lon = 0 if (!defined($lon));
$zoom = 13 if (!defined($zoom));
$name = '' if (!defined($name));
$address = '' if (!defined($address));
$name = url_decode($name);
$address = url_decode($address);
$text = "<b>$name</b><br>$address";
$parameter_error = 1 if (length($address) == 0);

# clean up parameters
$lat =~ s/[^\-\.\d]//g;
$lon =~ s/[^\-\.\d]//g;
$zoom =~ s/[^\.\d]//g;
$text =~ s/"/&quot;/g;

$parameter_error = 1 if (!defined($lat) || ($lat !~ /^\-*\d+\.\d+$/) || ($lat == 0));
$parameter_error = 1 if (!defined($lon) || ($lon !~ /^\-*\d+\.\d+$/) || ($lon == 0));
$parameter_error = 1 if (length($text) == 0);
if ($parameter_error) {
	print "Content-Type: text/html\nStatus: 404\n\n";
	print "<html><body>Route: Not Found<p><a href='/'>Homepage</a></body></html>\n";
	exit;
}

my $domain = $ENV{HTTP_HOST} || $ENV{SERVER_NAME};
my $locale = 'de';
my $start_label = 'Von';
my $button_label = 'Route berechnen';
my $drucktext = 'Drucken';

print "Content-Type: text/html\nStatus: 200 OK\nCache-Control: max-age=" . 7*24*60*60 ."\n\n";
print<<END_HEAD;
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>$name - $address</title><meta name="ICBM" content="$lat, $lon"><base target=_top><script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBoIlsX4SAgcF8xIYiINUUojjykLB0fm6Q" type="text/javascript"></script><script type="text/javascript"> var directionDisplay; var directionsService = new google.maps.DirectionsService(); function initialize() { directionsDisplay = new google.maps.DirectionsRenderer(); var pos = new google.maps.LatLng($lat, $lon); var myOptions = { zoom: $zoom, mapTypeId: google.maps.MapTypeId.ROADMAP, center: pos }; var map = new google.maps.Map(document.getElementById('map'), myOptions); var marker = new google.maps.Marker({ position: pos, map: map, title: '$name' }); directionsDisplay.setMap(map); directionsDisplay.setPanel(document.getElementById('directions')); } function setDirections(fromAddress) { var request = { origin: fromAddress, destination: '$address', travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); } google.maps.event.addDomListener(window, 'load', initialize); </script><style type="text/css"> body, p, th, td, a { font:10pt Arial,sans-serif; } body { $body_style } \@media print { body, p, th, td, a { font:8pt Arial,sans-serif; } #map { width:320px !important; } #directions { width:270px !important; } } </style></head><body">
END_HEAD

print "<br><span style='border:2px solid #ffffff; font-size:10pt; padding:4px;'>Anfahrt zu Walpurgis in Wolfshagen i.H.<!-- WebGoTec Werbung hier --></span><br><br>";

print <<END_MAP;
<form onsubmit="setDirections(document.getElementById('fromAddress').value); return false;" method=post>$start_label <input type="text" size="10pt" id="fromAddress" name="from" value="" /><input name="submit" type="submit" value="$button_label" onclick="javascript:setDirections(document.getElementById('fromAddress').value); return false;"/>&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' onclick='print(); return false;'>$drucktext</a></form><table><tr><td valign="top"><br/><div id="map" style="width:${map_width}px;height:${map_heigth}px;"></div><p>$text<p><td valign="top"><div id="directions" style="width:${map_width}px;"></div><p>$text<p></td>
</tr></table>
END_MAP

