9d968ad0ec29335c584131c98b70c752dfa039ab
[kivitendo-erp.git] / SL / Dispatcher.pm
1 package SL::Dispatcher;
2
3 use strict;
4
5 use CGI qw( -no_xhtml);
6 use English qw(-no_match_vars);
7 use SL::Auth;
8 use SL::LXDebug;
9 use SL::Locale;
10 use SL::Common;
11 use Form;
12 use Moose;
13 use Rose::DB;
14 use Rose::DB::Object;
15 use File::Basename;
16
17 sub pre_request_checks {
18   show_error('login/auth_db_unreachable') unless $::auth->session_tables_present;
19   $::auth->expire_sessions;
20 }
21
22 sub show_error {
23   $::lxdebug->enter_sub;
24   my $template           = shift;
25   my $error_type         = shift || '';
26   my $locale             = Locale->new($::language, 'all');
27   $::form->{error}       = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
28   $::form->{error}       = $::locale->text('Incorrect password!.')                   if ($error_type eq 'password');
29   $::myconfig{countrycode} = $::language;
30   $::form->{stylesheet}    = 'css/lx-office-erp.css';
31
32   $::form->header;
33   print $::form->parse_html_template($template);
34   $::lxdebug->leave_sub;
35
36   exit;
37 }
38
39 sub pre_startup_setup {
40   eval {
41     package main;
42     require "config/lx-erp.conf";
43   };
44   eval {
45     package main;
46     require "config/lx-erp-local.conf";
47   } if -f "config/lx-erp-local.conf";
48
49   eval {
50     package main;
51     require "bin/mozilla/common.pl";
52     require "bin/mozilla/installationcheck.pl";
53   } or die $EVAL_ERROR;
54
55   # dummy globals
56   {
57     no warnings 'once';
58     $::userspath  = "users";
59     $::templates  = "templates";
60     $::memberfile = "users/members";
61     $::sendmail   = "| /usr/sbin/sendmail -t";
62     $::lxdebug    = LXDebug->new;
63     $::auth       = SL::Auth->new;
64     %::myconfig   = ();
65   }
66 }
67
68 sub pre_startup_checks {
69   ::verify_installation();
70 }
71
72 sub pre_startup {
73   pre_startup_setup();
74   pre_startup_checks();
75 }
76
77 sub require_main_code {
78   my ($script, $suffix) = @_;
79
80   eval {
81     package main;
82     require "bin/mozilla/$script$suffix";
83   } or die $EVAL_ERROR;
84
85   if (-f "bin/mozilla/custom_$script$suffix") {
86     eval {
87       package main;
88       require "bin/mozilla/custom_$script$suffix";
89     };
90     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
91   }
92   if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$::form->{script}") {
93     eval {
94       package main;
95       require "bin/mozilla/$::form->{login}_$::form->{script}";
96     };
97     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
98   }
99 }
100
101 sub handle_request {
102   $::lxdebug->enter_sub;
103   $::lxdebug->begin_request;
104
105   my $interface = lc(shift || 'cgi');
106   my $script_name;
107
108   if ($interface =~ m/^(?:fastcgi|fcgid|fcgi)$/) {
109     $script_name = $ENV{SCRIPT_NAME};
110     unrequire_bin_mozilla();
111
112   } else {
113     $script_name = $0;
114   }
115
116   my ($script, $path, $suffix) = fileparse($script_name, ".pl");
117   require_main_code($script, $suffix);
118
119   $::cgi            = CGI->new('');
120   $::locale         = Locale->new($::language, $script);
121   $::form           = Form->new;
122   $::form->{script} = $script . $suffix;
123
124   pre_request_checks();
125
126   eval {
127     if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
128       $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
129       ::run($::auth->restore_session);
130
131     } elsif ($::form->{action}) {
132       # copy from am.pl routines
133       $::form->error($::locale->text('System currently down for maintenance!')) if -e "$main::userspath/nologin" && $script ne 'admin';
134
135       my $session_result = $::auth->restore_session;
136
137       show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
138       %::myconfig = $::auth->read_user($::form->{login});
139
140       show_error('login/password_error', 'password') unless $::myconfig{login};
141
142       $::locale = Locale->new($::myconfig{countrycode}, $script);
143
144       show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
145
146       $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
147       $::auth->create_or_refresh_session;
148       delete $::form->{password};
149
150       map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
151         unless $::form->{action} eq 'save' && $::form->{type} eq 'preferences';
152
153       $::form->set_standard_title;
154       ::call_sub('::' . $::locale->findsub($::form->{action}));
155
156     } else {
157       $::form->error($::locale->text('action= not defined!'));
158     }
159
160     1;
161   } or do {
162     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
163     show_error('generic/error');
164   };
165
166   # cleanup
167   $::locale   = undef;
168   $::form     = undef;
169   $::myconfig = ();
170
171   $::lxdebug->end_request;
172   $::lxdebug->leave_sub;
173 }
174
175 sub unrequire_bin_mozilla {
176   for (keys %INC) {
177     next unless m#^bin/mozilla/#;
178     next if /\bcommon.pl$/;
179     next if /\binstallationcheck.pl$/;
180     delete $INC{$_};
181   }
182 }
183
184 1;