1 package SL::Dispatcher;
6 unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
7 push @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
10 use CGI qw( -no_xhtml);
14 use English qw(-no_match_vars);
16 use List::MoreUtils qw(all);
17 use List::Util qw(first);
20 use SL::Dispatcher::AuthHandler;
26 use SL::Helper::DateTime;
27 use SL::InstanceConfiguration;
28 use SL::Template::Plugin::HTMLFixes;
30 # Trailing new line is added so that Perl will not add the line
31 # number 'die' was called in.
32 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
37 my ($class, $interface) = @_;
39 my $self = bless {}, $class;
40 $self->{interface} = lc($interface || 'cgi');
41 $self->{auth_handler} = SL::Dispatcher::AuthHandler->new;
48 return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
51 sub pre_request_checks {
52 _check_for_old_config_files();
54 if (!$::auth->session_tables_present) {
55 if ($::form->{script} eq 'admin.pl') {
59 show_error('login_screen/auth_db_unreachable');
65 $::lxdebug->enter_sub;
67 my $error_type = shift || '';
70 $::myconfig{countrycode} = delete($params{countrycode}) || $::lx_office_conf{system}->{language};
71 $::locale = Locale->new($::myconfig{countrycode});
72 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
73 $::form->{error} = $::locale->text('Incorrect password!') if ($error_type eq 'password');
76 print $::form->parse_html_template($template, \%params);
77 $::lxdebug->leave_sub;
82 sub pre_startup_setup {
85 SL::LxOfficeConf->read;
89 require "bin/mozilla/common.pl";
90 require "bin/mozilla/installationcheck.pl";
93 # canonial globals. if it's not here, chances are it will get refactored someday.
96 $::lxdebug = LXDebug->new;
97 $::auth = SL::Auth->new;
103 $SIG{__WARN__} = sub {
104 $::lxdebug->warn(@_);
107 $self->_cache_file_modification_times;
110 sub pre_startup_checks {
111 ::verify_installation();
116 $self->pre_startup_setup;
117 $self->pre_startup_checks;
120 sub require_main_code {
121 $::lxdebug->enter_sub;
122 my ($script, $suffix) = @_;
126 require "bin/mozilla/$script$suffix";
127 } or die $EVAL_ERROR;
129 if (-f "bin/mozilla/custom_$script$suffix") {
132 require "bin/mozilla/custom_$script$suffix";
134 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
136 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
139 require "bin/mozilla/$::form->{login}_$script";
141 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
143 $::lxdebug->leave_sub;
146 sub _require_controller {
147 my $controller = shift;
148 $controller =~ s|[^A-Za-z0-9_]||g;
149 $controller = "SL/Controller/${controller}";
153 require "${controller}.pm";
154 } or die $EVAL_ERROR;
157 sub _run_controller {
158 "SL::Controller::$_[0]"->new->_run_action($_[1]);
163 $self->{request} = shift;
165 $::lxdebug->enter_sub;
166 $::lxdebug->begin_request;
168 my ($script, $path, $suffix, $script_name, $action, $routing_type);
170 $self->unrequire_bin_mozilla;
172 $::locale = Locale->new($::lx_office_conf{system}->{language});
174 $::instance_conf = SL::InstanceConfiguration->new;
175 $::request = { cgi => CGI->new({}) };
177 my $session_result = $::auth->restore_session;
178 $::auth->create_or_refresh_session;
180 $::form->read_cgi_input;
182 eval { ($routing_type, $script_name, $action) = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
184 if ($routing_type eq 'old') {
185 $::form->{action} = lc $::form->{action};
186 $::form->{action} =~ s/( |-|,|\#)/_/g;
188 ($script, $path, $suffix) = fileparse($script_name, ".pl");
189 require_main_code($script, $suffix);
191 $::form->{script} = $script . $suffix;
194 _require_controller($script_name);
195 $::form->{script} = "controller.pl";
199 pre_request_checks();
201 $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
203 # For compatibility with a lot of database upgrade scripts etc:
204 # Re-write request to old 'login.pl?action=login' to new
205 # 'LoginScreen' controller. Make sure to load its code!
206 if (($script eq 'login') && ($action eq 'login')) {
207 ($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login);
208 _require_controller('LoginScreen');
211 if (($script eq 'login') && !$action) {
212 print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login');
214 } elsif ($script eq 'admin') {
215 $::form->{titlebar} = "kivitendo " . $::locale->text('Version') . " $::form->{version}";
216 ::run($session_result);
219 show_error('login_screen/user_login', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
221 my %auth_result = $self->{auth_handler}->handle(
222 routing_type => $routing_type,
224 controller => $script_name,
228 delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
231 $::instance_conf->init if $auth_result{auth_level} eq 'user';
233 map { $::form->{$_} = $::myconfig{$_} } qw(charset)
234 unless $action eq 'save' && $::form->{type} eq 'preferences';
236 $::form->set_standard_title;
237 if ($routing_type eq 'old') {
238 ::call_sub('::' . $::locale->findsub($action));
240 _run_controller($script_name, $action);
243 $::form->error($::locale->text('action= not defined!'));
249 if ($EVAL_ERROR ne END_OF_REQUEST) {
250 print STDERR $EVAL_ERROR;
251 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
252 eval { show_error('generic/error') };
257 $::auth->save_session;
258 $::auth->expire_sessions;
265 Form::disconnect_standard_dbh;
267 $::lxdebug->end_request;
269 $self->_watch_for_changed_files;
271 $::lxdebug->leave_sub;
274 sub unrequire_bin_mozilla {
276 return unless $self->_interface_is_fcgi;
279 next unless m#^bin/mozilla/#;
280 next if /\bcommon.pl$/;
281 next if /\binstallationcheck.pl$/;
286 sub _interface_is_fcgi {
288 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
292 my $script_name = shift;
294 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
295 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
296 : ('old', $script_name, $::form->{action});
299 sub _route_dispatcher_request {
300 my $name_re = qr{[a-z]\w*};
301 my ($script_name, $action);
304 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
305 $script_name = $::form->{M} . '.pl';
308 $action = $::form->{A};
311 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
312 die "Unroutable request -- inavlid action name.\n" if !$action;
314 delete $::form->{$action};
315 $action = substr $action, 2;
318 delete @{$::form}{qw(M A)};
322 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
323 show_error('generic/error');
326 return ($script_name, $action);
329 sub _route_controller_request {
330 my ($controller, $action);
333 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
334 ($controller, $action) = ($1, $2);
335 delete $::form->{action};
339 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
340 show_error('generic/error');
343 return ($controller, $action);
346 sub _cache_file_modification_times {
349 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
355 return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
356 $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
359 my $cwd = POSIX::getcwd();
360 File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
361 map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
364 sub _watch_for_changed_files {
367 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
369 my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
371 $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
375 sub get_standard_filehandles {
378 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
381 sub _check_for_old_config_files {
382 my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
383 return unless @old_files;
385 $::form->{title} = $::locale->text('Old configuration files');
387 print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
397 die SL::Dispatcher->END_OF_REQUEST;