1 package SL::Dispatcher;
5 # Force scripts/locales.pl to parse these templates:
6 # parse_html_template('login_screen/auth_db_unreachable')
7 # parse_html_template('login_screen/user_login')
8 # parse_html_template('generic/error')
11 use SL::System::Process;
12 my $exe_dir = SL::System::Process::exe_dir;
14 unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML).
15 push @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version.
16 unshift @INC, $exe_dir;
19 use CGI qw( -no_xhtml);
23 use English qw(-no_match_vars);
25 use List::MoreUtils qw(all);
26 use List::Util qw(first);
29 use SL::Dispatcher::AuthHandler;
35 use SL::Helper::DateTime;
36 use SL::InstanceConfiguration;
37 use SL::Template::Plugin::HTMLFixes;
38 use SL::Controller::Layout::None;
40 # Trailing new line is added so that Perl will not add the line
41 # number 'die' was called in.
42 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
47 my ($class, $interface) = @_;
49 my $self = bless {}, $class;
50 $self->{interface} = lc($interface || 'cgi');
51 $self->{auth_handler} = SL::Dispatcher::AuthHandler->new;
58 return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
61 sub pre_request_checks {
62 _check_for_old_config_files();
64 if (!$::auth->session_tables_present) {
65 if ($::form->{script} eq 'admin.pl') {
69 show_error('login_screen/auth_db_unreachable');
75 $::lxdebug->enter_sub;
77 my $error_type = shift || '';
80 $::myconfig{countrycode} = delete($params{countrycode}) || $::lx_office_conf{system}->{language};
81 $::locale = Locale->new($::myconfig{countrycode});
82 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
83 $::form->{error} = $::locale->text('Incorrect password!') if ($error_type eq 'password');
85 $::form->header(no_menu => 1);
86 print $::form->parse_html_template($template, \%params);
87 $::lxdebug->leave_sub;
92 sub pre_startup_setup {
95 SL::LxOfficeConf->read;
99 require "bin/mozilla/common.pl";
100 require "bin/mozilla/installationcheck.pl";
101 } or die $EVAL_ERROR;
103 # canonial globals. if it's not here, chances are it will get refactored someday.
106 $::lxdebug = LXDebug->new;
107 $::auth = SL::Auth->new;
113 $SIG{__WARN__} = sub {
114 $::lxdebug->warn(@_);
117 $self->_cache_file_modification_times;
120 sub pre_startup_checks {
121 ::verify_installation();
126 $self->pre_startup_setup;
127 $self->pre_startup_checks;
130 sub require_main_code {
131 $::lxdebug->enter_sub;
132 my ($script, $suffix) = @_;
136 require "bin/mozilla/$script$suffix";
137 } or die $EVAL_ERROR;
139 if (-f "bin/mozilla/custom_$script$suffix") {
142 require "bin/mozilla/custom_$script$suffix";
144 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
146 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
149 require "bin/mozilla/$::form->{login}_$script";
151 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
153 $::lxdebug->leave_sub;
156 sub _require_controller {
157 my $controller = shift;
158 $controller =~ s|[^A-Za-z0-9_]||g;
159 $controller = "SL/Controller/${controller}";
163 require "${controller}.pm";
164 } or die $EVAL_ERROR;
167 sub _run_controller {
168 "SL::Controller::$_[0]"->new->_run_action($_[1]);
173 $self->{request} = shift;
175 $::lxdebug->enter_sub;
176 $::lxdebug->begin_request;
178 my ($script, $path, $suffix, $script_name, $action, $routing_type);
180 $self->unrequire_bin_mozilla;
182 $::locale = Locale->new($::lx_office_conf{system}->{language});
184 $::instance_conf = SL::InstanceConfiguration->new;
187 layout => SL::Controller::Layout::None->new,
190 my $session_result = $::auth->restore_session;
191 $::auth->create_or_refresh_session;
193 $::form->read_cgi_input;
195 eval { ($routing_type, $script_name, $action) = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
197 if ($routing_type eq 'old') {
198 $::form->{action} = lc $::form->{action};
199 $::form->{action} =~ s/( |-|,|\#)/_/g;
201 ($script, $path, $suffix) = fileparse($script_name, ".pl");
202 require_main_code($script, $suffix);
204 $::form->{script} = $script . $suffix;
207 _require_controller($script_name);
208 $::form->{script} = "controller.pl";
212 pre_request_checks();
214 $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
216 # For compatibility with a lot of database upgrade scripts etc:
217 # Re-write request to old 'login.pl?action=login' to new
218 # 'LoginScreen' controller. Make sure to load its code!
219 if (($script eq 'login') && ($action eq 'login')) {
220 ($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login);
221 _require_controller('LoginScreen');
224 if (($script eq 'login') && !$action) {
225 print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login');
227 } elsif ($script eq 'admin') {
228 $::form->{titlebar} = "kivitendo " . $::locale->text('Version') . " $::form->{version}";
229 ::run($session_result);
232 if (SL::Auth::SESSION_EXPIRED == $session_result) {
233 print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login&error=session');
236 my %auth_result = $self->{auth_handler}->handle(
237 routing_type => $routing_type,
239 controller => $script_name,
243 delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
246 $::instance_conf->init if $auth_result{auth_level} eq 'user';
248 map { $::form->{$_} = $::myconfig{$_} } qw(charset)
249 unless $action eq 'save' && $::form->{type} eq 'preferences';
251 $::form->set_standard_title;
252 if ($routing_type eq 'old') {
253 ::call_sub('::' . $::locale->findsub($action));
255 _run_controller($script_name, $action);
258 $::form->error($::locale->text('action= not defined!'));
264 if ($EVAL_ERROR ne END_OF_REQUEST) {
265 print STDERR $EVAL_ERROR;
266 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
267 chdir SL::System::Process::exe_dir;
268 eval { show_error('generic/error') };
273 $::auth->save_session;
274 $::auth->expire_sessions;
281 Form::disconnect_standard_dbh;
283 $::lxdebug->end_request;
285 $self->_watch_for_changed_files;
287 $::lxdebug->leave_sub;
290 sub unrequire_bin_mozilla {
292 return unless $self->_interface_is_fcgi;
295 next unless m#^bin/mozilla/#;
296 next if /\bcommon.pl$/;
297 next if /\binstallationcheck.pl$/;
302 sub _interface_is_fcgi {
304 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
308 my $script_name = shift;
310 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
311 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
312 : ('old', $script_name, $::form->{action});
315 sub _route_dispatcher_request {
316 my $name_re = qr{[a-z]\w*};
317 my ($script_name, $action);
320 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
321 $script_name = $::form->{M} . '.pl';
324 $action = $::form->{A};
327 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
328 die "Unroutable request -- inavlid action name.\n" if !$action;
330 delete $::form->{$action};
331 $action = substr $action, 2;
334 delete @{$::form}{qw(M A)};
338 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
339 show_error('generic/error');
342 return ($script_name, $action);
345 sub _route_controller_request {
346 my ($controller, $action);
349 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
350 ($controller, $action) = ($1, $2);
351 delete $::form->{action};
355 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
356 show_error('generic/error');
359 return ($controller, $action);
362 sub _cache_file_modification_times {
365 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
371 return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
372 $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
375 my $cwd = POSIX::getcwd();
376 File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
377 map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
380 sub _watch_for_changed_files {
383 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
385 my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
387 $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
391 sub get_standard_filehandles {
394 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
397 sub _check_for_old_config_files {
398 my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
399 return unless @old_files;
401 $::form->{title} = $::locale->text('Old configuration files');
403 print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
413 die SL::Dispatcher->END_OF_REQUEST;