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