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