]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Handler.pm
Login gives a real page now
[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 use base 'Exporter';
16
17 our @EXPORT = qw(&callHandler);
18
19 $ENV{'PATH'} = '';
20 my $dbh = connectDb();
21 my $tables = PciIds::Html::Tables::new( $dbh );
22
23 my %handlers = (
24         'GET' => {
25                 'list' => \&PciIds::Html::List::list,#List items
26                 '' => \&PciIds::Html::List::list,
27                 #Database changes
28                 'newitem' => \&PciIds::Html::Changes::newItemForm,
29                 'newhistory' => \&PciIds::Html::Changes::newHistoryForm,
30                 #Registering users
31                 'register' => \&PciIds::Html::Users::registerForm,
32                 'register-confirm' => \&PciIds::Html::Users::confirmForm,
33                 #Logins
34                 'login' => \&PciIds::Html::Users::loginForm,
35                 'logout' => \&PciIds::Html::Users::logout,
36                 'respass' => \&PciIds::Html::Users::resetPasswdForm,
37                 'respass-confirm' => \&PciIds::Html::Users::resetPasswdConfirmForm,
38                 #User profile
39                 'profile' => \&PciIds::Html::Users::profileForm,
40                 #Admin
41                 'admin' => \&PciIds::Html::Admin::adminForm,
42                 #Some debug
43                 'test' => \&PciIds::Html::Debug::test,
44                 #Notifications
45                 'notifications' => \&PciIds::Html::Notifications::notifForm,
46                 'help' => \&PciIds::Html::Help::getHelp
47         },
48         'POST' => {
49                 'newitem' => \&PciIds::Html::Changes::newItemSubmit,
50                 'newhistory' => \&PciIds::Html::Changes::newHistorySubmit,
51                 'register' => \&PciIds::Html::Users::registerSubmit,
52                 'register-confirm' => \&PciIds::Html::Users::confirmSubmit,
53                 'login' => \&PciIds::Html::Users::loginSubmit,
54                 'respass' => \&PciIds::Html::Users::resetPasswdFormSubmit,
55                 'respass-confirm' => \&PciIds::Html::Users::resetPasswdConfirmFormSubmit,
56                 'profile' => \&PciIds::Html::Users::profileFormSubmit,
57                 'admin' => \&PciIds::Html::Admin::submitAdminForm,
58                 'notifications' => \&PciIds::Html::Notifications::notifFormSubmit
59         }
60 );
61
62 sub callHandler( $$$$$$ ) {
63         my( $req, $args, $tables, $auth, $hasSSL, $meth ) = @_;
64         my $action = $args->{'action'};
65         $action = '' unless( defined $action );
66         my $method = $handlers{$meth};
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         $auth = checkLogin( $req, $tables ) unless defined $auth;#Check if logged in
71         $auth->{'ssl'} = $hasSSL;
72         return &{$sub}( $req, $args, $tables, $auth );#Just do the right thing
73 }
74
75 sub handler( $$ ) {
76         my( $req, $hasSSL ) = @_;
77         my $args = parseArgs( $req->args() );
78         return HTTPRedirect( $req, $req->uri()."index.html" ) if( $req->uri() eq '/' && ( !defined $args->{'action'} || $args->{'action'} ne 'help' ) );
79         return DECLINED if( $req->uri() =~ /^\/((static)\/|robots.txt|index.html)/ );
80         my $result;
81         eval {
82                 $result = callHandler( $req, $args, $tables, undef, $hasSSL, $req->method() );
83                 $tables->commit();
84         };
85         if( $@ ) {
86                 $tables->rollback();
87                 die $@;
88         }
89         return $result;
90 }
91
92 1;