]> git.deb.at Git - deb/packages.git/blob - cgi-bin/dispatcher.pl
Backwards compatibility mode for reportbug
[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 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 if ($ENV{is_reportbug}) {
87     $input->param('exact', 1);
88     debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
89 }
90
91 my $what_to_do = 'show';
92 my $source = 0;
93 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
94     my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
95
96     push @components, 'index' if @components && $path =~ m,/$,;
97
98     my %LANGUAGES = map { $_ => 1 } @LANGUAGES;
99     if (@components > 0 and $LANGUAGES{$components[0]}) {
100         $input->param( 'lang', shift(@components) );
101     }
102     if (@components > 0 and $components[0] eq 'source') {
103         shift @components;
104         $input->param( 'source', 1 );
105     }
106     if (@components > 0 and $components[0] eq 'search') {
107         shift @components;
108         $what_to_do = 'search';
109         # Done
110         fatal_error( _g( "search doesn't take any more path elements" ) )
111             if @components;
112     } elsif (@components == 0) {
113         fatal_error( _g( "We're supposed to display the homepage here, instead of getting dispatch.pl" ) );
114     } elsif (@components == 1) {
115         $what_to_do = 'search';
116     } else {
117
118         for ($components[-1]) {
119             /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
120                 pop @components;
121                 $what_to_do = $1;
122                 last;
123             };
124         }
125
126         my %SUITES = map { $_ => 1 } @SUITES;
127         my %SUITES_ALIAS = ( sarge => 'oldstable',
128                              etch => 'stable',
129                              lenny => 'testing',
130                              sid => 'unstable', );
131         my %SECTIONS = map { $_ => 1 } @SECTIONS;
132         my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
133         my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
134         my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
135         my %params_set;
136         sub set_param_once {
137             my ($cgi, $params_set, $key, $val) = @_;
138             debug("set_param_once key=$key val=$val",4) if DEBUG;
139             if ($params_set->{$key}++) {
140                 fatal_error( sprintf( _g( "%s set more than once in path" ), $key ) );
141             } else {
142                 $cgi->param( $key, $val );
143             }
144         }
145
146         my (@pkg, $need_pkg);
147         foreach (reverse @components) {
148             $need_pkg = !@pkg
149                 && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
150             debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
151             if (!$need_pkg && $SUITES{$_}) {
152                 set_param_once( $input, \%params_set, 'suite', $_);
153             } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
154                 set_param_once( $input, \%params_set, 'suite', $s);
155             } elsif (!$need_pkg && $SECTIONS{$_}) {
156                 set_param_once( $input, \%params_set, 'section', $_);
157             } elsif (!$need_pkg && $ARCHIVES{$_}) {
158                 set_param_once( $input, \%params_set, 'archive', $_);
159             } elsif (!$need_pkg && $sections_descs{$_}) {
160                 set_param_once( $input, \%params_set, 'subsection', $_);
161             } elsif (!$need_pkg && ($_ eq 'non-us')) { # non-US hack
162                 set_param_once( $input, \%params_set, 'subsection', 'non-US');
163             } elsif (!$need_pkg && ($_ eq 'source')) {
164                 set_param_once( $input, \%params_set, 'source', 1);
165             } elsif ($ARCHITECTURES{$_}) {
166                 set_param_once( $input, \%params_set, 'arch', $_)
167                     unless $_ eq 'any';
168             } elsif ($PRIORITIES{$_}) {
169                 set_param_once( $input, \%params_set, 'priority', $_);
170             } else {
171                 push @pkg, $_;
172             }
173         }
174         @components = @pkg;
175
176         if (@components > 1) {
177             fatal_error( sprintf( _g( "two or more packages specified (%s)" ), "@components" ) );
178         }
179     } # else if (@components == 1)
180     
181     if (@components) {
182         $input->param( 'keywords', $components[0] );
183         $input->param( 'package', $components[0] );
184     }
185 }
186
187 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
188
189 my %params_def = ( keywords => { default => undef,
190                                  array => '\s+',
191                                  match => '^([-+\@\w\/.:]+)$',
192                              },
193                    package => { default => undef,
194                                 match => '^([\w.+-]+)$',
195                                 var => \$pkg },
196                    suite => { default => 'default', match => '^([\w-]+)$',
197                               array => ',', var => \@suites,
198                               replace => { all => \@SUITES,
199                                            default => \@SUITES } },
200                    archive => { default => ($what_to_do eq 'search') ?
201                                     'all' : 'default',
202                                     match => '^([\w-]+)$',
203                                     array => ',', var => \@archives,
204                                     replace => { all => \@ARCHIVES,
205                                                  default => \@ARCHIVES} },
206                    exact => { default => 0, match => '^(\w+)$',  },
207                    lang => { default => $http_lang, match => '^(\w+)$',  },
208                    source => { default => 0, match => '^(\d+)$',  },
209                    debug => { default => 0, match => '^(\d+)$',  },
210                    searchon => { default => 'names', match => '^(\w+)$', },
211                    section => { default => 'all', match => '^([\w-]+)$',
212                                 alias => 'release', array => ',',
213                                 var => \@sections,
214                                 replace => { all => \@SECTIONS } },
215                    subsection => { default => 'default', match => '^([\w-]+)$',
216                                    array => ',', var => \@subsections,
217                                    replace => { default => [] } },
218                    priority => { default => 'default', match => '^([\w-]+)$',
219                                  array => ',',
220                                  replace => { default => [] } },
221                    arch => { default => 'any', match => '^([\w-]+)$',
222                              array => ',', var => \@archs, replace =>
223                              { any => \@ARCHITECTURES } },
224                    format => { default => 'html', match => '^([\w.]+)$',  },
225                    mode => { default => undef, match => '^(\w+)$',  },
226                    sort_by => { default => 'file', match => '^(\w+)$', },
227                    );
228 my %opts;
229 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
230 Packages::CGI::init_url( $input, \%params, \%opts );
231
232 my $locale = get_locale($opts{lang});
233 my $charset = get_charset($opts{lang});
234 setlocale ( LC_ALL, $locale )
235     or do { debug( "couldn't set locale $locale, using default" ) if DEBUG;
236             setlocale( LC_ALL, get_locale() )
237                 or do {
238                     debug( "couldn't set default locale either" ) if DEBUG;
239                     setlocale( LC_ALL, "C" );
240                 };
241         };
242 debug( "locale=$locale charset=$charset", 2 ) if DEBUG;
243
244 $opts{h_suites} = { map { $_ => 1 } @suites };
245 $opts{h_sections} = { map { $_ => 1 } @sections };
246 $opts{h_archives} = { map { $_ => 1 } @archives };
247 $opts{h_archs} = { map { $_ => 1 } @archs };
248
249 if ((($opts{searchon} eq 'names') && $opts{source}) ||
250     ($opts{searchon} eq 'sourcenames')) {
251     $opts{source} = 1;
252     $opts{searchon} = 'names',
253     $opts{searchon_form} = 'sourcenames';
254 } else {
255     $opts{searchon_form} = $opts{searchon};
256 }
257 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
258     $what_to_do = 'search_contents';
259 }
260
261 my $pet1 = new Benchmark;
262 my $petd = timediff($pet1, $pet0);
263 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
264
265 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
266
267 unless (-e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
268     fatal_error( "requested format not available for this document",
269                  "406 requested format not available");
270 }
271
272 my (%html_header, %page_content);
273 unless (@Packages::CGI::fatal_errors) {
274     no strict 'refs';
275     &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
276                          \%page_content );
277 }
278
279 $page_content{opts} = \%opts;
280 $page_content{params} = \%params;
281
282 $page_content{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
283 $page_content{make_url} = sub { return &Packages::CGI::make_url(@_) };
284 # needed to work around the limitations of the the FILTER syntax
285 $page_content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
286 $page_content{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
287 $page_content{quotemeta} = sub { return quotemeta($_[0]) };
288 $page_content{string2id} = sub { return &Packages::CGI::string2id(@_) };
289
290 unless (@Packages::CGI::fatal_errors) {
291     print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
292     #use Data::Dumper;
293     #print '<pre>'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'</pre>';
294     print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
295     my $tet1 = new Benchmark;
296     my $tetd = timediff($tet1, $tet0);
297     print $template->trailer( undef, undef, undef, $tetd );
298 } elsif ($Packages::CGI::http_code) {
299     print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
300 } else {
301     # We currently have only an error page in html
302     # so no format support here
303     print $input->header( -charset => $charset );
304     print $template->error_page( get_all_messages() );
305     print $template->trailer();;
306 }
307
308
309 # vim: ts=8 sw=4