1 package SL::Dispatcher;
6 use SL::System::Process;
7 my $exe_dir = SL::System::Process::exe_dir;
9 unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML).
10 push @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version.
11 unshift @INC, $exe_dir;
14 use CGI qw( -no_xhtml);
18 use English qw(-no_match_vars);
20 use List::MoreUtils qw(all);
21 use List::Util qw(first);
24 use SL::Dispatcher::AuthHandler;
30 use SL::Helper::DateTime;
31 use SL::InstanceConfiguration;
32 use SL::Template::Plugin::HTMLFixes;
34 # Trailing new line is added so that Perl will not add the line
35 # number 'die' was called in.
36 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
41 my ($class, $interface) = @_;
43 my $self = bless {}, $class;
44 $self->{interface} = lc($interface || 'cgi');
45 $self->{auth_handler} = SL::Dispatcher::AuthHandler->new;
52 return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
55 sub pre_request_checks {
56 _check_for_old_config_files();
58 if (!$::auth->session_tables_present) {
59 if ($::form->{script} eq 'admin.pl') {
63 show_error('login_screen/auth_db_unreachable');
69 $::lxdebug->enter_sub;
71 my $error_type = shift || '';
74 $::myconfig{countrycode} = delete($params{countrycode}) || $::lx_office_conf{system}->{language};
75 $::locale = Locale->new($::myconfig{countrycode});
76 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
77 $::form->{error} = $::locale->text('Incorrect password!') if ($error_type eq 'password');
80 print $::form->parse_html_template($template, \%params);
81 $::lxdebug->leave_sub;
86 sub pre_startup_setup {
89 SL::LxOfficeConf->read;
93 require "bin/mozilla/common.pl";
94 require "bin/mozilla/installationcheck.pl";
97 # canonial globals. if it's not here, chances are it will get refactored someday.
100 $::lxdebug = LXDebug->new;
101 $::auth = SL::Auth->new;
107 $SIG{__WARN__} = sub {
108 $::lxdebug->warn(@_);
111 $self->_cache_file_modification_times;
114 sub pre_startup_checks {
115 ::verify_installation();
120 $self->pre_startup_setup;
121 $self->pre_startup_checks;
124 sub require_main_code {
125 $::lxdebug->enter_sub;
126 my ($script, $suffix) = @_;
130 require "bin/mozilla/$script$suffix";
131 } or die $EVAL_ERROR;
133 if (-f "bin/mozilla/custom_$script$suffix") {
136 require "bin/mozilla/custom_$script$suffix";
138 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
140 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
143 require "bin/mozilla/$::form->{login}_$script";
145 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
147 $::lxdebug->leave_sub;
150 sub _require_controller {
151 my $controller = shift;
152 $controller =~ s|[^A-Za-z0-9_]||g;
153 $controller = "SL/Controller/${controller}";
157 require "${controller}.pm";
158 } or die $EVAL_ERROR;
161 sub _run_controller {
162 "SL::Controller::$_[0]"->new->_run_action($_[1]);
167 $self->{request} = shift;
169 $::lxdebug->enter_sub;
170 $::lxdebug->begin_request;
172 my ($script, $path, $suffix, $script_name, $action, $routing_type);
174 $self->unrequire_bin_mozilla;
176 $::locale = Locale->new($::lx_office_conf{system}->{language});
178 $::instance_conf = SL::InstanceConfiguration->new;
179 $::request = { cgi => CGI->new({}) };
181 my $session_result = $::auth->restore_session;
182 $::auth->create_or_refresh_session;
184 $::form->read_cgi_input;
186 eval { ($routing_type, $script_name, $action) = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
188 if ($routing_type eq 'old') {
189 $::form->{action} = lc $::form->{action};
190 $::form->{action} =~ s/( |-|,|\#)/_/g;
192 ($script, $path, $suffix) = fileparse($script_name, ".pl");
193 require_main_code($script, $suffix);
195 $::form->{script} = $script . $suffix;
198 _require_controller($script_name);
199 $::form->{script} = "controller.pl";
203 pre_request_checks();
205 $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
207 # For compatibility with a lot of database upgrade scripts etc:
208 # Re-write request to old 'login.pl?action=login' to new
209 # 'LoginScreen' controller. Make sure to load its code!
210 if (($script eq 'login') && ($action eq 'login')) {
211 ($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login);
212 _require_controller('LoginScreen');
215 if (($script eq 'login') && !$action) {
216 print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login');
218 } elsif ($script eq 'admin') {
219 $::form->{titlebar} = "kivitendo " . $::locale->text('Version') . " $::form->{version}";
220 ::run($session_result);
223 show_error('login_screen/user_login', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
225 my %auth_result = $self->{auth_handler}->handle(
226 routing_type => $routing_type,
228 controller => $script_name,
232 delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
235 $::instance_conf->init if $auth_result{auth_level} eq 'user';
237 map { $::form->{$_} = $::myconfig{$_} } qw(charset)
238 unless $action eq 'save' && $::form->{type} eq 'preferences';
240 $::form->set_standard_title;
241 if ($routing_type eq 'old') {
242 ::call_sub('::' . $::locale->findsub($action));
244 _run_controller($script_name, $action);
247 $::form->error($::locale->text('action= not defined!'));
253 if ($EVAL_ERROR ne END_OF_REQUEST) {
254 print STDERR $EVAL_ERROR;
255 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
256 chdir SL::System::Process::exe_dir;
257 eval { show_error('generic/error') };
262 $::auth->save_session;
263 $::auth->expire_sessions;
270 Form::disconnect_standard_dbh;
272 $::lxdebug->end_request;
274 $self->_watch_for_changed_files;
276 $::lxdebug->leave_sub;
279 sub unrequire_bin_mozilla {
281 return unless $self->_interface_is_fcgi;
284 next unless m#^bin/mozilla/#;
285 next if /\bcommon.pl$/;
286 next if /\binstallationcheck.pl$/;
291 sub _interface_is_fcgi {
293 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
297 my $script_name = shift;
299 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
300 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
301 : ('old', $script_name, $::form->{action});
304 sub _route_dispatcher_request {
305 my $name_re = qr{[a-z]\w*};
306 my ($script_name, $action);
309 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
310 $script_name = $::form->{M} . '.pl';
313 $action = $::form->{A};
316 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
317 die "Unroutable request -- inavlid action name.\n" if !$action;
319 delete $::form->{$action};
320 $action = substr $action, 2;
323 delete @{$::form}{qw(M A)};
327 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
328 show_error('generic/error');
331 return ($script_name, $action);
334 sub _route_controller_request {
335 my ($controller, $action);
338 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
339 ($controller, $action) = ($1, $2);
340 delete $::form->{action};
344 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
345 show_error('generic/error');
348 return ($controller, $action);
351 sub _cache_file_modification_times {
354 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
360 return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
361 $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
364 my $cwd = POSIX::getcwd();
365 File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
366 map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
369 sub _watch_for_changed_files {
372 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
374 my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
376 $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
380 sub get_standard_filehandles {
383 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
386 sub _check_for_old_config_files {
387 my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
388 return unless @old_files;
390 $::form->{title} = $::locale->text('Old configuration files');
392 print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
402 die SL::Dispatcher->END_OF_REQUEST;