]> git.deb.at Git - deb/packages.git/blob - cgi-bin/dispatcher.pl
dispatcher.pl: Add some backwards compatibility handling
[deb/packages.git] / cgi-bin / dispatcher.pl
1 #!/usr/bin/perl -T
2 # $Id: search_packages.pl 91 2006-02-10 22:18:31Z jeroen $
3 # dispatcher.pl -- CGI interface for packages.debian.org
4 #
5 # Copyright (C) 2004-2006 Frank Lichtenheld
6 #
7 # use is allowed under the terms of the GNU Public License (GPL)                              
8 # see http://www.fsf.org/copyleft/gpl.html for a copy of the license
9
10 use strict;
11 use warnings;
12
13 use lib '../lib';
14 use CGI;
15 use POSIX;
16 use File::Basename;
17 use URI::Escape;
18 use HTML::Entities;
19 use Template;
20 use DB_File;
21 use Benchmark ':hireswallclock';
22 use I18N::AcceptLanguage;
23 use Locale::gettext;
24
25 use Deb::Versions;
26 use Packages::Config qw( $DBDIR $ROOT $TEMPLATEDIR $CACHEDIR
27                          @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES @PRIORITIES
28                          @LANGUAGES $LOCALES );
29 use Packages::CGI qw( :DEFAULT error get_all_messages );
30 use Packages::DB;
31 use Packages::Search qw( :all );
32 use Packages::Template ();
33 use Packages::Sections;
34 use Packages::I18N::Locale;
35
36 use Packages::DoSearch;
37 use Packages::DoSearchContents;
38 use Packages::DoShow;
39 use Packages::DoIndex;
40 use Packages::DoNewPkg;
41 use Packages::DoDownload;
42 use Packages::DoFilelist;
43
44 &Packages::CGI::reset;
45 $Packages::Search::too_many_hits = 0;
46
47 # clean up env
48 $ENV{PATH} = "/bin:/usr/bin";
49 delete $ENV{'LANGUAGE'};
50 delete $ENV{'LANG'};
51 delete $ENV{'LC_ALL'};
52 delete $ENV{'LC_MESSAGES'};
53
54 # Read in all the variables set by the form
55 my $input;
56 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
57         $input = new CGI(\*STDIN);
58 } else {
59         $input = new CGI;
60 }
61 my $cgi_error = $input->cgi_error;
62 if ($cgi_error) {
63     fatal_error( "Error parsing the request", $cgi_error );
64 }
65
66
67 my $pet0 = new Benchmark;
68 my $tet0 = new Benchmark;
69 my $debug = DEBUG && $input->param("debug");
70 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
71 $Packages::CGI::debug = $debug;
72
73 my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
74 &Packages::Config::init( $homedir );
75 &Packages::DB::init();
76
77 my $acc = I18N::AcceptLanguage->new();
78 my $http_lang = $acc->accepts( $input->http("Accept-Language"),
79                                \@LANGUAGES ) || 'en';
80 debug( "LANGUAGES=@LANGUAGES header=".
81        ($input->http("Accept-Language")||'').
82        " http_lang=$http_lang", 2 ) if DEBUG;
83 bindtextdomain ( 'pdo', $LOCALES );
84 textdomain( 'pdo' );
85
86 # backwards compatibility stuff
87 debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
88
89 if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_|) {
90     error( "You reached this site over an old URL. ".
91            "Depending on the exact parameters your search might work or not." );
92     # contents search changed a lot
93     if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_contents|) {
94         $input->param('keywords',$input->param('word')) if $input->param('word');
95         $input->param('searchon','contents');
96         for ($input->param('searchmode')) {
97             /^searchfiles/ && do {
98                 $input->param('mode','filename');
99                 last;
100             };
101             /^filelist/ && do {
102                 $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
103                                                 $input->param('keywords'),
104                                                 $input->param('arch')||'i386',
105                                                 'filelist' ));
106                 $input->delete('searchon','version','keywords','arch');
107                 last;
108             };
109         }
110     }
111 }
112 if ($ENV{is_reportbug}) {
113     $input->param('exact', 1);
114     debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
115 }
116
117 my $what_to_do = 'show';
118 my $source = 0;
119 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
120     my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
121
122     debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
123
124     push @components, 'index' if @components && $path =~ m,/$,;
125
126     my %LANGUAGES = map { $_ => 1 } @LANGUAGES;
127     if (@components > 0 and $LANGUAGES{$components[0]}) {
128         $input->param( 'lang', shift(@components) );
129     }
130     if (@components > 0 and $components[0] eq 'source') {
131         shift @components;
132         $input->param( 'source', 1 );
133     }
134     if (@components > 0 and $components[0] eq 'search') {
135         shift @components;
136         $what_to_do = 'search';
137         # Done
138         fatal_error( _g( "search doesn't take any more path elements" ) )
139             if @components;
140     } elsif (@components == 0) {
141         fatal_error( _g( "We're supposed to display the homepage here, instead of getting dispatch.pl" ) );
142     } elsif (@components == 1) {
143         $what_to_do = 'search';
144     } else {
145
146         for ($components[-1]) {
147             /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
148                 pop @components;
149                 $what_to_do = $1;
150                 last;
151             };
152         }
153
154         my %SUITES = map { $_ => 1 } @SUITES;
155         my %SUITES_ALIAS = ( sarge => 'oldstable',
156                              etch => 'stable',
157                              lenny => 'testing',
158                              sid => 'unstable', );
159         my %SECTIONS = map { $_ => 1 } @SECTIONS;
160         my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
161         my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
162         my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
163         my %params_set;
164         sub set_param_once {
165             my ($cgi, $params_set, $key, $val) = @_;
166             debug("set_param_once key=$key val=$val",4) if DEBUG;
167             if ($params_set->{$key}++) {
168                 fatal_error( sprintf( _g( "%s set more than once in path" ), $key ) );
169             } else {
170                 $cgi->param( $key, $val );
171             }
172         }
173
174         my (@pkg, $need_pkg);
175         foreach (reverse @components) {
176             $need_pkg = !@pkg
177                 && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
178             debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
179             if (!$need_pkg && $SUITES{$_}) {
180                 set_param_once( $input, \%params_set, 'suite', $_);
181             } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
182                 set_param_once( $input, \%params_set, 'suite', $s);
183             } elsif (!$need_pkg && $SECTIONS{$_}) {
184                 set_param_once( $input, \%params_set, 'section', $_);
185             } elsif (!$need_pkg && $ARCHIVES{$_}) {
186                 set_param_once( $input, \%params_set, 'archive', $_);
187             } elsif (!$need_pkg && $sections_descs{$_}) {
188                 set_param_once( $input, \%params_set, 'subsection', $_);
189             } elsif (!$need_pkg && ($_ eq 'non-us')) { # non-US hack
190                 set_param_once( $input, \%params_set, 'subsection', 'non-US');
191             } elsif (!$need_pkg && ($_ eq 'source')) {
192                 set_param_once( $input, \%params_set, 'source', 1);
193             } elsif ($ARCHITECTURES{$_}) {
194                 set_param_once( $input, \%params_set, 'arch', $_)
195                     unless $_ eq 'any';
196             } elsif ($PRIORITIES{$_}) {
197                 set_param_once( $input, \%params_set, 'priority', $_);
198             } else {
199                 push @pkg, $_;
200             }
201         }
202         @components = @pkg;
203
204         if (@components > 1) {
205             fatal_error( sprintf( _g( "two or more packages specified (%s)" ), "@components" ) );
206         }
207     } # else if (@components == 1)
208     
209     if (@components) {
210         $input->param( 'keywords', $components[0] );
211         $input->param( 'package', $components[0] );
212     }
213 }
214
215 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
216
217 my %params_def = ( keywords => { default => undef,
218                                  array => '\s+',
219                                  match => '^([-+\@\w\/.:]+)$',
220                              },
221                    package => { default => undef,
222                                 match => '^([\w.+-]+)$',
223                                 var => \$pkg },
224                    suite => { default => 'default', match => '^([\w-]+)$',
225                               array => ',', var => \@suites,
226                               replace => { all => \@SUITES,
227                                            default => \@SUITES } },
228                    archive => { default => ($what_to_do eq 'search') ?
229                                     'all' : 'default',
230                                     match => '^([\w-]+)$',
231                                     array => ',', var => \@archives,
232                                     replace => { all => \@ARCHIVES,
233                                                  default => \@ARCHIVES} },
234                    exact => { default => 0, match => '^(\w+)$',  },
235                    lang => { default => $http_lang, match => '^(\w+)$',  },
236                    source => { default => 0, match => '^(\d+)$',  },
237                    debug => { default => 0, match => '^(\d+)$',  },
238                    searchon => { default => 'names', match => '^(\w+)$', },
239                    section => { default => 'all', match => '^([\w-]+)$',
240                                 alias => 'release', array => ',',
241                                 var => \@sections,
242                                 replace => { all => \@SECTIONS } },
243                    subsection => { default => 'default', match => '^([\w-]+)$',
244                                    array => ',', var => \@subsections,
245                                    replace => { default => [] } },
246                    priority => { default => 'default', match => '^([\w-]+)$',
247                                  array => ',',
248                                  replace => { default => [] } },
249                    arch => { default => 'any', match => '^([\w-]+)$',
250                              array => ',', var => \@archs, replace =>
251                              { any => \@ARCHITECTURES } },
252                    format => { default => 'html', match => '^([\w.]+)$',  },
253                    mode => { default => undef, match => '^(\w+)$',  },
254                    sort_by => { default => 'file', match => '^(\w+)$', },
255                    );
256 my %opts;
257 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
258 Packages::CGI::init_url( $input, \%params, \%opts );
259
260 my $locale = get_locale($opts{lang});
261 my $charset = get_charset($opts{lang});
262 setlocale ( LC_ALL, $locale )
263     or do { debug( "couldn't set locale $locale, using default" ) if DEBUG;
264             setlocale( LC_ALL, get_locale() )
265                 or do {
266                     debug( "couldn't set default locale either" ) if DEBUG;
267                     setlocale( LC_ALL, "C" );
268                 };
269         };
270 debug( "locale=$locale charset=$charset", 2 ) if DEBUG;
271
272 $opts{h_suites} = { map { $_ => 1 } @suites };
273 $opts{h_sections} = { map { $_ => 1 } @sections };
274 $opts{h_archives} = { map { $_ => 1 } @archives };
275 $opts{h_archs} = { map { $_ => 1 } @archs };
276
277 if ((($opts{searchon} eq 'names') && $opts{source}) ||
278     ($opts{searchon} eq 'sourcenames')) {
279     $opts{source} = 1;
280     $opts{searchon} = 'names',
281     $opts{searchon_form} = 'sourcenames';
282 } else {
283     $opts{searchon_form} = $opts{searchon};
284 }
285 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
286     $what_to_do = 'search_contents';
287 }
288
289 my $pet1 = new Benchmark;
290 my $petd = timediff($pet1, $pet0);
291 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
292
293 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
294
295 unless (-e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
296     fatal_error( "requested format not available for this document",
297                  "406 requested format not available");
298 }
299
300 my (%html_header, %page_content);
301 unless (@Packages::CGI::fatal_errors) {
302     no strict 'refs';
303     &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
304                          \%page_content );
305 }
306
307 $page_content{opts} = \%opts;
308 $page_content{params} = \%params;
309
310 $page_content{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
311 $page_content{make_url} = sub { return &Packages::CGI::make_url(@_) };
312 # needed to work around the limitations of the the FILTER syntax
313 $page_content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
314 $page_content{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
315 $page_content{quotemeta} = sub { return quotemeta($_[0]) };
316 $page_content{string2id} = sub { return &Packages::CGI::string2id(@_) };
317
318 unless (@Packages::CGI::fatal_errors) {
319     print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
320     #use Data::Dumper;
321     #print '<pre>'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'</pre>';
322     print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
323     my $tet1 = new Benchmark;
324     my $tetd = timediff($tet1, $tet0);
325     print $template->trailer( undef, undef, undef, $tetd );
326 } elsif ($Packages::CGI::http_code && $Packages::CGI::http_code !~ /^2\d\d/) {
327     print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
328 } else {
329     # We currently have only an error page in html
330     # so no format support here
331     print $input->header( -charset => $charset );
332     print $template->error_page( get_all_messages() );
333     print $template->trailer();;
334 }
335
336
337 # vim: ts=8 sw=4