]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Handler.pm
Merge branch 'master' of /home/vorner/pciids
[pciids.git] / PciIds / Html / Handler.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::Html::Handler;
20 use strict;
21 use warnings;
22 use PciIds::Db;
23 use PciIds::Html::Tables;
24 use PciIds::Html::Util;
25 use PciIds::Html::List;
26 use PciIds::Html::Users;
27 use PciIds::Html::Debug;
28 use PciIds::Html::Changes;
29 use PciIds::Html::Admin;
30 use PciIds::Html::Notifications;
31 use PciIds::Html::Help;
32 use PciIds::Html::Jump;
33 use Apache2::Const qw(:common :http);
34
35 $ENV{'PATH'} = '';
36 my $dbh = connectDb();
37 my $tables = PciIds::Html::Tables::new( $dbh );
38
39 my %handlers = (
40         'GET' => {
41                 'list' => \&PciIds::Html::List::list,#List items
42                 '' => \&PciIds::Html::List::list,
43                 #Database changes
44                 'newitem' => \&PciIds::Html::Changes::newItemForm,
45                 'newhistory' => \&PciIds::Html::Changes::newHistoryForm,
46                 #Registering users
47                 'register' => \&PciIds::Html::Users::registerForm,
48                 'register-confirm' => \&PciIds::Html::Users::confirmForm,
49                 #Logins
50                 'login' => \&PciIds::Html::Users::loginForm,
51                 'logout' => \&PciIds::Html::Users::logout,
52                 'respass' => \&PciIds::Html::Users::resetPasswdForm,
53                 'respass-confirm' => \&PciIds::Html::Users::resetPasswdConfirmForm,
54                 #User profile
55                 'profile' => \&PciIds::Html::Users::profileForm,
56                 #Admin
57                 'admin' => \&PciIds::Html::Admin::adminForm,
58                 #Some debug
59                 'test' => \&PciIds::Html::Debug::test,
60                 #Notifications
61                 'notifications' => \&PciIds::Html::Notifications::notifForm,
62                 'help' => \&PciIds::Html::Help::getHelp
63         },
64         'POST' => {
65                 'newitem' => \&PciIds::Html::Changes::newItemSubmit,
66                 'newhistory' => \&PciIds::Html::Changes::newHistorySubmit,
67                 'register' => \&PciIds::Html::Users::registerSubmit,
68                 'register-confirm' => \&PciIds::Html::Users::confirmSubmit,
69                 'login' => \&PciIds::Html::Users::loginSubmit,
70                 'respass' => \&PciIds::Html::Users::resetPasswdFormSubmit,
71                 'respass-confirm' => \&PciIds::Html::Users::resetPasswdConfirmFormSubmit,
72                 'profile' => \&PciIds::Html::Users::profileFormSubmit,
73                 'admin' => \&PciIds::Html::Admin::submitAdminForm,
74                 'notifications' => \&PciIds::Html::Notifications::notifFormSubmit,
75                 'jump' => \&PciIds::Html::Jump::jump,
76                 'help' => \&PciIds::Html::Help::getHelp
77         }
78 );
79
80 sub handler( $$ ) {
81         my( $req, $hasSSL ) = @_;
82         my $args = parseArgs( $req->args() );
83         return HTTPRedirect( $req, protoName( $hasSSL ).'://'.$req->hostname().'/index.html' ) if( $req->uri() eq '/' && ( !defined $args->{'action'} || $args->{'action'} ne 'help' ) );
84         return DECLINED if( $req->uri() =~ /^\/((static)\/|robots.txt|index.html)/ );
85         my $action = $args->{'action'};
86         $action = '' unless( defined $action );
87         return HTTPRedirect( $req, protoName( $hasSSL ).'://'.$req->hostname().'/' ) if $req->uri() =~ /^\/(read|mods)\/?$/  && ( $action eq '' || $action eq 'list' );
88         my $method = $handlers{$req->method()};
89         return HTTP_METHOD_NOT_ALLOWED unless( defined $method );#Can't handle this method
90         my $sub = $method->{$action};
91         return HTTP_BAD_REQUEST unless( defined $sub );#I do not know this action for given method
92         my $auth = checkLogin( $req, $tables );#Check if logged in
93         $auth->{'ssl'} = $hasSSL;
94         my( $result );
95         eval {
96                 $result = &{$sub}( $req, $args, $tables, $auth );#Just do the right thing
97                 $tables->commit();
98         };
99         if( $@ ) {
100                 $tables->rollback();
101                 die $@;
102         }
103         return $result;
104 }
105
106 1;