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