Imported Upstream version 1.5.1
[routino] / web / www / routino / customrouter.cgi
1 #!/usr/bin/perl
2 #
3 # Routino router custom link 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"             => "[-0-9.]+",
39               "lat"             => "[-0-9.]+",
40               "zoom"            => "[0-9]+",
41
42               "lon[1-9]"        => "[-0-9.]+",
43               "lat[1-9]"        => "[-0-9.]+",
44               "transport"       => "[a-z]+",
45               "highway-[a-z]+"  => "[0-9.]+",
46               "speed-[a-z]+"    => "[0-9.]+",
47               "property-[a-z]+" => "[0-9.]+",
48               "oneway"          => "(1|0|true|false|on|off)",
49               "weight"          => "[0-9.]+",
50               "height"          => "[0-9.]+",
51               "width"           => "[0-9.]+",
52               "length"          => "[0-9.]+",
53
54               "language"        => "[-a-zA-Z]+"
55              );
56
57 # Validate the CGI parameters, ignore invalid ones
58
59 foreach $key (@rawparams)
60   {
61    foreach $test (keys (%legalparams))
62      {
63       if($key =~ m%^$test$%)
64         {
65          $value=$query->param($key);
66
67          if($value =~ m%^$legalparams{$test}$%)
68            {
69             $cgiparams{$key}=$value;
70             last;
71            }
72         }
73      }
74   }
75
76 # Fill in the default parameters
77
78 %fullparams=FillInDefaults(%cgiparams);
79
80 # Open template file and output it
81
82 $lang=$cgiparams{'language'};
83
84 if( -f "router.html.$lang")
85   {
86    open(TEMPLATE,"<router.html.$lang");
87   }
88 else
89   {
90    open(TEMPLATE,"<router.html");
91   }
92
93 # Parse the template and fill in the parameters
94
95 print header('text/html');
96
97 while(<TEMPLATE>)
98   {
99    if(m%^<BODY.+>%)
100      {
101       s/'lat'/$cgiparams{'lat'}/   if(defined $cgiparams{'lat'});
102       s/'lon'/$cgiparams{'lon'}/   if(defined $cgiparams{'lon'});
103       s/'zoom'/$cgiparams{'zoom'}/ if(defined $cgiparams{'zoom'});
104       print;
105      }
106    elsif(m%<input% && m%<!-- ([^ ]+) *-->%)
107      {
108       $key=$1;
109
110       m%type="([a-z]+)"%;
111       $type=$1;
112
113       m%value="([a-z]+)"%;
114       $value=$1;
115
116       if($type eq "radio")
117         {
118          $checked="";
119          $checked="checked" if($fullparams{$key} eq $value);
120
121          s%><!-- .+? *-->% $checked>%;
122         }
123       elsif($type eq "checkbox")
124         {
125          $checked="";
126          $checked="checked" if($fullparams{$key});
127
128          s%><!-- .+? *-->% $checked>%;
129         }
130       elsif($type eq "text")
131         {
132          s%><!-- .+? *-->% value="$fullparams{$key}">%;
133         }
134
135       print;
136      }
137    else
138      {
139       print;
140      }
141   }
142
143 close(TEMPLATE);