+sub _interface_is_fcgi {
+ my $self = shift;
+ return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
+}
+
+sub _route_request {
+ my $script_name = shift;
+
+ return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
+ : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
+ : ('old', $script_name, $::form->{action});
+}
+
+sub _route_dispatcher_request {
+ my $name_re = qr{[a-z]\w*};
+ my ($script_name, $action);
+
+ eval {
+ die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
+ $script_name = $::form->{M} . '.pl';
+
+ if ($::form->{A}) {
+ $action = $::form->{A};
+
+ } else {
+ $action = first { m/^A_${name_re}$/ } keys %{ $::form };
+ die "Unroutable request -- inavlid action name.\n" if !$action;
+
+ delete $::form->{$action};
+ $action = substr $action, 2;
+ }
+
+ delete @{$::form}{qw(M A)};
+
+ 1;
+ } or do {
+ $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
+ show_error('generic/error');
+ };
+
+ return ($script_name, $action);
+}
+
+sub _route_controller_request {
+ my ($controller, $action);
+
+ eval {
+ $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
+ ($controller, $action) = ($1, $2);
+ delete $::form->{action};
+
+ 1;
+ } or do {
+ $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
+ show_error('generic/error');
+ };
+
+ return ($controller, $action);
+}
+
+sub _cache_file_modification_times {
+ my ($self) = @_;
+
+ return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
+
+ require File::Find;
+ require POSIX;
+
+ my $wanted = sub {
+ return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
+ $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
+ };
+
+ my $cwd = POSIX::getcwd();
+ File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
+ map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
+}
+
+sub _watch_for_changed_files {
+ my ($self) = @_;
+
+ return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
+
+ my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
+ return if $ok;
+ $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
+ exit;
+}
+
+sub get_standard_filehandles {
+ my $self = shift;
+
+ return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
+}
+
+sub _check_for_old_config_files {
+ my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
+ return unless @old_files;
+
+ $::form->{title} = $::locale->text('Old configuration files');
+ $::form->{stylesheet} = 'lx-office-erp.css';
+ $::form->header;
+ print $::form->parse_html_template('login/old_configuration_files', { FILES => \@old_files });
+
+ ::end_of_request();
+}
+
+package main;
+
+use strict;
+
+sub end_of_request {
+ die SL::Dispatcher->END_OF_REQUEST;
+}
+