Imported Upstream version 1.4.1
[routino] / web / www / routino / noscript.cgi
1 #!/usr/bin/perl
2 #
3 # Routino non-Javascript 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               "submit"          => "(shortest|quickest|link)",
53               "format"          => "(html|gpx-route|gpx-track|text|text-all|form)"
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 $submit=$cgiparams{submit};
78 delete $cgiparams{submit};
79
80 $format=$cgiparams{format};
81 delete $cgiparams{format};
82
83 $format="form" if(!$format || ($submit ne "shortest" && $submit ne "quickest"));
84
85 # Generate a custom URL
86
87 $customurl="";
88
89 if($submit)
90   {
91    $customurl="noscript.cgi?";
92
93    foreach $key (sort (keys %cgiparams))
94      {
95       $customurl.="$key=$cgiparams{$key};";
96      }
97
98    $customurl.="submit=custom";
99   }
100
101 # Fill in the default parameters
102
103 %fullparams=FillInDefaults(%cgiparams);
104
105 # Open template file before running the router (including changing directory)
106
107 open(TEMPLATE,"<noscript.template.html");
108
109 # Run the router
110
111 if($submit eq "shortest" || $submit eq "quickest")
112   {
113    ($router_uuid,$router_time,$router_result,$router_message)=RunRouter($submit,%fullparams);
114
115    $router_type=$submit;
116    $router_Type="Shortest" if($submit eq "shortest");
117    $router_Type="Quickest" if($submit eq "quickest");
118
119    if($format ne "form")
120      {
121       ReturnOutput($router_uuid,$submit,$format);
122       exit;
123      }
124   }
125
126 # Generate the form output
127
128 print header('text/html');
129
130 # Parse the template and fill in the parameters
131
132 while(<TEMPLATE>)
133   {
134    if (m%<input% && m%<!-- ([^ ]+) *-->%)
135      {
136       $key=$1;
137
138       m%type="([a-z]+)"%;
139       $type=$1;
140
141       m%value="([a-z]+)"%;
142       $value=$1;
143
144       if ($type eq "radio")
145         {
146          $checked="";
147          $checked="checked" if($fullparams{$key} eq $value);
148
149          s%><!-- .+? *-->% $checked>%;
150         }
151       elsif ($type eq "checkbox")
152         {
153          $checked="";
154          $checked="checked" if($fullparams{$key});
155
156          s%><!-- .+? *-->% $checked>%;
157         }
158       elsif ($type eq "text")
159         {
160          s%><!-- .+? *-->% value="$fullparams{$key}">%;
161         }
162
163       print;
164      }
165    elsif (m%<!-- custom-url -->%)
166      {
167       s%<!-- custom-url -->%$customurl%;
168
169       print if($submit);
170      }
171    elsif (m%<!-- result-start -->%)
172      {
173       $results_section=1;
174      }
175    elsif (m%<!-- result-finish -->%)
176      {
177       $results_section=0;
178      }
179    elsif ($results_section)
180      {
181       s%<!-- result-Type -->%$router_Type%;
182       s%<!-- result-type -->%$router_type%;
183       s%<!-- result-uuid -->%$router_uuid%;
184       s%<!-- result-time -->%$router_time%;
185       s%<!-- result-result -->%$router_result%;
186       s%<!-- result-message -->%$router_message%;
187
188       print if($router_uuid);
189      }
190    else
191      {
192       print;
193      }
194   }
195
196 close(TEMPLATE);