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