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