Imported Upstream version 1.4.1
[routino] / web / www / routino / router.cgi
1 #!/usr/bin/perl
2 #
3 # Routino interactive router CGI
4 #
5 # Part of the Routino routing software.
6 #
7 # This file Copyright 2008-2010 Andrew M. Bishop
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU Affero General Public License for more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 # Use the generic router script
24 require "router.pl";
25
26 # Use the perl CGI module
27 use CGI ':cgi';
28
29 # Create the query and get the parameters
30
31 $query=new CGI;
32
33 @rawparams=$query->param;
34
35 # Legal CGI parameters with regexp validity check
36
37 %legalparams=(
38               "lon[1-9]"        => "[-0-9.]+",
39               "lat[1-9]"        => "[-0-9.]+",
40               "transport"       => "[a-z]+",
41               "highway-[a-z]+"  => "[0-9.]+",
42               "speed-[a-z]+"    => "[0-9.]+",
43               "property-[a-z]+" => "[0-9.]+",
44               "oneway"          => "(1|0|true|false|on|off)",
45               "weight"          => "[0-9.]+",
46               "height"          => "[0-9.]+",
47               "width"           => "[0-9.]+",
48               "length"          => "[0-9.]+",
49               "length"          => "[0-9.]+",
50
51               "language"        => "[-a-zA-Z]+",
52               "type"            => "(shortest|quickest)",
53               "format"          => "(html|gpx-route|gpx-track|text|text-all)"
54              );
55
56 # Validate the CGI parameters, ignore invalid ones
57
58 foreach $key (@rawparams)
59   {
60    foreach $test (keys (%legalparams))
61      {
62       if($key =~ m%^$test$%)
63         {
64          $value=$query->param($key);
65
66          if($value =~ m%^$legalparams{$test}$%)
67            {
68             $cgiparams{$key}=$value;
69             last;
70            }
71         }
72      }
73   }
74
75 # Get the important parameters
76
77 $type=$cgiparams{type};
78 delete $cgiparams{type};
79
80 $format=$cgiparams{format};
81 delete $cgiparams{format};
82
83 # Fill in the default parameters
84
85 %fullparams=FillInDefaults(%cgiparams);
86
87 # Run the router
88
89 ($router_uuid,$router_time,$router_result,$router_message)=RunRouter($type,%fullparams);
90
91 # Return the output
92
93 if($format)
94   {
95    ReturnOutput($router_uuid,$type,$format);
96   }
97 else
98   {
99    print header('text/plain');
100
101    print "$router_uuid\n";
102    print "$router_time\n";
103    print "$router_result\n";
104    print "$router_message\n";
105   }