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