]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Handler.pm
Better behavior with failure
[pciids.git] / PciIds / Html / Handler.pm
1 package PciIds::Html::Handler;
2 use strict;
3 use warnings;
4 use PciIds::Db;
5 use PciIds::Html::Tables;
6 use PciIds::Html::Util;
7 use PciIds::Html::List;
8 use PciIds::Html::Users;
9 use PciIds::Html::Debug;
10 use PciIds::Html::Changes;
11 use PciIds::Html::Admin;
12 use PciIds::Html::Notifications;
13 use PciIds::Html::Help;
14 use Apache2::Const qw(:common :http);
15
16 $ENV{'PATH'} = '';
17 my $dbh = connectDb();
18 my $tables = PciIds::Html::Tables::new( $dbh );
19
20 my %handlers = (
21         'GET' => {
22                 'list' => \&PciIds::Html::List::list,#List items
23                 '' => \&PciIds::Html::List::list,
24                 #Database changes
25                 'newitem' => \&PciIds::Html::Changes::newItemForm,
26                 'newcomment' => \&PciIds::Html::Changes::newCommentForm,
27                 #Registering users
28                 'register' => \&PciIds::Html::Users::registerForm,
29                 'register-confirm' => \&PciIds::Html::Users::confirmForm,
30                 #Logins
31                 'login' => \&PciIds::Html::Users::loginForm,
32                 'logout' => \&PciIds::Html::Users::logout,
33                 'respass' => \&PciIds::Html::Users::resetPasswdForm,
34                 'respass-confirm' => \&PciIds::Html::Users::resetPasswdConfirmForm,
35                 #User profile
36                 'profile' => \&PciIds::Html::Users::profileForm,
37                 #Admin
38                 'admin' => \&PciIds::Html::Admin::adminForm,
39                 #Some debug
40                 'test' => \&PciIds::Html::Debug::test,
41                 #Notifications
42                 'notifications' => \&PciIds::Html::Notifications::notifForm
43         },
44         'POST' => {
45                 'newitem' => \&PciIds::Html::Changes::newItemSubmit,
46                 'newcomment' => \&PciIds::Html::Changes::newCommentSubmit,
47                 'register' => \&PciIds::Html::Users::registerSubmit,
48                 'register-confirm' => \&PciIds::Html::Users::confirmSubmit,
49                 'login' => \&PciIds::Html::Users::loginSubmit,
50                 'respass' => \&PciIds::Html::Users::resetPasswdFormSubmit,
51                 'respass-confirm' => \&PciIds::Html::Users::resetPasswdConfirmFormSubmit,
52                 'profile' => \&PciIds::Html::Users::profileFormSubmit,
53                 'admin' => \&PciIds::Html::Admin::submitAdminForm,
54                 'notifications' => \&PciIds::Html::Notifications::notifFormSubmit
55         }
56 );
57
58 sub handler( $$ ) {
59         my( $req, $hasSSL ) = @_;
60         return HTTPRedirect( $req, $req->uri()."index.html" ) if( $req->uri() eq '/' );
61         return DECLINED if( $req->uri() =~ /^\/((static)\/|robots.txt|index.html)/ );
62         return getHelp( $req ) if( $req->uri() =~ /^\/help/ );
63         my $args = parseArgs( $req->args() );
64         my $action = $args->{'action'};
65         $action = '' unless( defined $action );
66         my $method = $handlers{$req->method()};
67         return HTTP_METHOD_NOT_ALLOWED unless( defined $method );#Can't handle this method
68         my $sub = $method->{$action};
69         return HTTP_BAD_REQUEST unless( defined $sub );#I do not know this action for given method
70         my $auth = checkLogin( $req, $tables );#Check if logged in
71         $auth->{'ssl'} = $hasSSL;
72         my( $result );
73         eval {
74                 $result = &{$sub}( $req, $args, $tables, $auth );#Just do the right thing
75                 $tables->commit();
76         };
77         if( $@ ) {
78                 $tables->rollback();
79                 die $@;
80         }
81         return $result;
82 }
83
84 1;