kivitendo/SL/Dispatcher.pm @ ee71d315
94899fc9 | Moritz Bunkus | package SL::Dispatcher;
|
||
use strict;
|
||||
bbb67c4a | Moritz Bunkus | # Force scripts/locales.pl to parse these templates:
|
||
# parse_html_template('login_screen/auth_db_unreachable')
|
||||
# parse_html_template('login_screen/user_login')
|
||||
# parse_html_template('generic/error')
|
||||
0ea2ddad | Sven Schöling | BEGIN {
|
||
32822bba | Moritz Bunkus | use SL::System::Process;
|
||
my $exe_dir = SL::System::Process::exe_dir;
|
||||
unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML).
|
||||
push @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version.
|
||||
unshift @INC, $exe_dir;
|
||||
0ea2ddad | Sven Schöling | }
|
||
ee71d315 | Moritz Bunkus | use Carp;
|
||
94899fc9 | Moritz Bunkus | use CGI qw( -no_xhtml);
|
||
f04a7950 | Moritz Bunkus | use Config::Std;
|
||
cad439ef | Moritz Bunkus | use DateTime;
|
||
f04a7950 | Moritz Bunkus | use Encode;
|
||
94899fc9 | Moritz Bunkus | use English qw(-no_match_vars);
|
||
77ee93fc | Moritz Bunkus | use File::Basename;
|
||
use List::MoreUtils qw(all);
|
||||
use List::Util qw(first);
|
||||
use POSIX;
|
||||
94899fc9 | Moritz Bunkus | use SL::Auth;
|
||
6afd06ad | Moritz Bunkus | use SL::Dispatcher::AuthHandler;
|
||
94899fc9 | Moritz Bunkus | use SL::LXDebug;
|
||
67b21d42 | Moritz Bunkus | use SL::LxOfficeConf;
|
||
94899fc9 | Moritz Bunkus | use SL::Locale;
|
||
b9740e8a | Moritz Bunkus | use SL::ClientJS;
|
||
94899fc9 | Moritz Bunkus | use SL::Common;
|
||
12dadc47 | Sven Schöling | use SL::Form;
|
||
cad439ef | Moritz Bunkus | use SL::Helper::DateTime;
|
||
891411c1 | Moritz Bunkus | use SL::InstanceConfiguration;
|
||
e257fa36 | Moritz Bunkus | use SL::Template::Plugin::HTMLFixes;
|
||
94899fc9 | Moritz Bunkus | |||
b2945bf6 | Sven Schöling | # Trailing new line is added so that Perl will not add the line
|
||
# number 'die' was called in.
|
||||
use constant END_OF_REQUEST => "END-OF-REQUEST\n";
|
||||
77ee93fc | Moritz Bunkus | my %fcgi_file_cache;
|
||
23f79a87 | Moritz Bunkus | sub new {
|
||
my ($class, $interface) = @_;
|
||||
my $self = bless {}, $class;
|
||||
$self->{interface} = lc($interface || 'cgi');
|
||||
6afd06ad | Moritz Bunkus | $self->{auth_handler} = SL::Dispatcher::AuthHandler->new;
|
||
23f79a87 | Moritz Bunkus | |||
return $self;
|
||||
}
|
||||
bb800c52 | Moritz Bunkus | sub interface_type {
|
||
my ($self) = @_;
|
||||
return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
|
||||
}
|
||||
94899fc9 | Moritz Bunkus | sub pre_request_checks {
|
||
f416a998 | Moritz Bunkus | _check_for_old_config_files();
|
||
879abbad | Sven Schöling | if (!$::auth->session_tables_present) {
|
||
if ($::form->{script} eq 'admin.pl') {
|
||||
::run();
|
||||
::end_of_request();
|
||||
} else {
|
||||
540c0b5e | Moritz Bunkus | show_error('login_screen/auth_db_unreachable');
|
||
879abbad | Sven Schöling | }
|
||
}
|
||||
8c6871be | Moritz Bunkus | |||
if ($::request->type !~ m/^ (?: html | js | json ) $/x) {
|
||||
die $::locale->text("Invalid request type '#1'", $::request->type);
|
||||
}
|
||||
94899fc9 | Moritz Bunkus | }
|
||
b9740e8a | Moritz Bunkus | sub render_error_ajax {
|
||
my ($error) = @_;
|
||||
SL::ClientJS->new
|
||||
->error($error)
|
||||
->render(SL::Controller::Base->new);
|
||||
}
|
||||
94899fc9 | Moritz Bunkus | sub show_error {
|
||
$::lxdebug->enter_sub;
|
||||
881cc205 | Moritz Bunkus | my $template = shift;
|
||
my $error_type = shift || '';
|
||||
6afd06ad | Moritz Bunkus | my %params = @_;
|
||
881cc205 | Moritz Bunkus | |||
540c0b5e | Moritz Bunkus | $::myconfig{countrycode} = delete($params{countrycode}) || $::lx_office_conf{system}->{language};
|
||
$::locale = Locale->new($::myconfig{countrycode});
|
||||
881cc205 | Moritz Bunkus | $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
|
||
540c0b5e | Moritz Bunkus | $::form->{error} = $::locale->text('Incorrect password!') if ($error_type eq 'password');
|
||
94899fc9 | Moritz Bunkus | |||
b9740e8a | Moritz Bunkus | return render_error_ajax($::form->{error}) if $::request->is_ajax;
|
||
7647d46a | Moritz Bunkus | $::form->header;
|
||
6afd06ad | Moritz Bunkus | print $::form->parse_html_template($template, \%params);
|
||
94899fc9 | Moritz Bunkus | $::lxdebug->leave_sub;
|
||
b2945bf6 | Sven Schöling | ::end_of_request();
|
||
94899fc9 | Moritz Bunkus | }
|
||
sub pre_startup_setup {
|
||||
77ee93fc | Moritz Bunkus | my ($self) = @_;
|
||
67b21d42 | Moritz Bunkus | SL::LxOfficeConf->read;
|
||
ed7bfc4a | Moritz Bunkus | |||
94899fc9 | Moritz Bunkus | eval {
|
||
package main;
|
||||
require "bin/mozilla/common.pl";
|
||||
require "bin/mozilla/installationcheck.pl";
|
||||
} or die $EVAL_ERROR;
|
||||
1c8bbb4e | Sven Schöling | # canonial globals. if it's not here, chances are it will get refactored someday.
|
||
94899fc9 | Moritz Bunkus | {
|
||
no warnings 'once';
|
||||
1c8bbb4e | Sven Schöling | $::lxdebug = LXDebug->new;
|
||
a0945527 | Sven Schöling | $::auth = SL::Auth->new;
|
||
1c8bbb4e | Sven Schöling | $::form = undef;
|
||
%::myconfig = ();
|
||||
772f08d0 | Sven Schöling | $::request = undef;
|
||
94899fc9 | Moritz Bunkus | }
|
||
847d924b | Sven Schöling | |||
$SIG{__WARN__} = sub {
|
||||
$::lxdebug->warn(@_);
|
||||
77ee93fc | Moritz Bunkus | };
|
||
ee71d315 | Moritz Bunkus | $SIG{__DIE__} = sub { Carp::confess( @_ ) } if $::lx_office_conf{debug}->{backtrace_on_die};
|
||
77ee93fc | Moritz Bunkus | $self->_cache_file_modification_times;
|
||
94899fc9 | Moritz Bunkus | }
|
||
sub pre_startup_checks {
|
||||
::verify_installation();
|
||||
}
|
||||
sub pre_startup {
|
||||
77ee93fc | Moritz Bunkus | my ($self) = @_;
|
||
$self->pre_startup_setup;
|
||||
$self->pre_startup_checks;
|
||||
94899fc9 | Moritz Bunkus | }
|
||
sub require_main_code {
|
||||
193b1e4e | Sven Schöling | $::lxdebug->enter_sub;
|
||
94899fc9 | Moritz Bunkus | my ($script, $suffix) = @_;
|
||
eval {
|
||||
package main;
|
||||
require "bin/mozilla/$script$suffix";
|
||||
} or die $EVAL_ERROR;
|
||||
if (-f "bin/mozilla/custom_$script$suffix") {
|
||||
eval {
|
||||
package main;
|
||||
require "bin/mozilla/custom_$script$suffix";
|
||||
};
|
||||
$::form->error($EVAL_ERROR) if ($EVAL_ERROR);
|
||||
}
|
||||
d4f9d559 | Moritz Bunkus | if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
|
||
94899fc9 | Moritz Bunkus | eval {
|
||
package main;
|
||||
d4f9d559 | Moritz Bunkus | require "bin/mozilla/$::form->{login}_$script";
|
||
94899fc9 | Moritz Bunkus | };
|
||
$::form->error($EVAL_ERROR) if ($EVAL_ERROR);
|
||||
}
|
||||
193b1e4e | Sven Schöling | $::lxdebug->leave_sub;
|
||
94899fc9 | Moritz Bunkus | }
|
||
41400107 | Moritz Bunkus | sub _require_controller {
|
||
my $controller = shift;
|
||||
$controller =~ s|[^A-Za-z0-9_]||g;
|
||||
6afd06ad | Moritz Bunkus | $controller = "SL/Controller/${controller}";
|
||
41400107 | Moritz Bunkus | |||
eval {
|
||||
package main;
|
||||
6afd06ad | Moritz Bunkus | require "${controller}.pm";
|
||
41400107 | Moritz Bunkus | } or die $EVAL_ERROR;
|
||
}
|
||||
sub _run_controller {
|
||||
"SL::Controller::$_[0]"->new->_run_action($_[1]);
|
||||
}
|
||||
94899fc9 | Moritz Bunkus | sub handle_request {
|
||
7a0da5ac | Moritz Bunkus | my $self = shift;
|
||
$self->{request} = shift;
|
||||
23f79a87 | Moritz Bunkus | |||
94899fc9 | Moritz Bunkus | $::lxdebug->enter_sub;
|
||
$::lxdebug->begin_request;
|
||||
41400107 | Moritz Bunkus | my ($script, $path, $suffix, $script_name, $action, $routing_type);
|
||
94899fc9 | Moritz Bunkus | |||
23f79a87 | Moritz Bunkus | $self->unrequire_bin_mozilla;
|
||
94899fc9 | Moritz Bunkus | |||
27e80751 | Moritz Bunkus | $::locale = Locale->new($::lx_office_conf{system}->{language});
|
||
$::form = Form->new;
|
||||
891411c1 | Moritz Bunkus | $::instance_conf = SL::InstanceConfiguration->new;
|
||
8c6871be | Moritz Bunkus | $::request = SL::Request->new(
|
||
4a12c839 | Sven Schöling | cgi => CGI->new({}),
|
||
b6fd15a8 | Sven Schöling | layout => SL::Layout::None->new,
|
||
8c6871be | Moritz Bunkus | );
|
||
7a604472 | Moritz Bunkus | |||
75f69249 | Moritz Bunkus | my $session_result = $::auth->restore_session;
|
||
$::auth->create_or_refresh_session;
|
||||
$::form->read_cgi_input;
|
||||
8c6871be | Moritz Bunkus | my %routing;
|
||
eval { %routing = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
|
||||
($routing_type, $script_name, $action) = @routing{qw(type controller action)};
|
||||
e8557567 | Sven Schöling | $::lxdebug->log_request($routing_type, $script_name, $action);
|
||
8c6871be | Moritz Bunkus | |||
$::request->type(lc($routing{request_type} || 'html'));
|
||||
7a604472 | Moritz Bunkus | |||
41400107 | Moritz Bunkus | if ($routing_type eq 'old') {
|
||
$::form->{action} = lc $::form->{action};
|
||||
$::form->{action} =~ s/( |-|,|\#)/_/g;
|
||||
94899fc9 | Moritz Bunkus | |||
41400107 | Moritz Bunkus | ($script, $path, $suffix) = fileparse($script_name, ".pl");
|
||
require_main_code($script, $suffix);
|
||||
$::form->{script} = $script . $suffix;
|
||||
} else {
|
||||
_require_controller($script_name);
|
||||
$::form->{script} = "controller.pl";
|
||||
}
|
||||
94899fc9 | Moritz Bunkus | |||
eval {
|
||||
579d651f | Moritz Bunkus | pre_request_checks();
|
||
8cd05ad6 | Moritz Bunkus | $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
|
||
cb114b28 | Moritz Bunkus | |||
a54282a9 | Moritz Bunkus | # For compatibility with a lot of database upgrade scripts etc:
|
||
# Re-write request to old 'login.pl?action=login' to new
|
||||
# 'LoginScreen' controller. Make sure to load its code!
|
||||
if (($script eq 'login') && ($action eq 'login')) {
|
||||
($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login);
|
||||
_require_controller('LoginScreen');
|
||||
}
|
||||
540c0b5e | Moritz Bunkus | |||
if (($script eq 'login') && !$action) {
|
||||
print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login');
|
||||
} elsif ($script eq 'admin') {
|
||||
$::form->{titlebar} = "kivitendo " . $::locale->text('Version') . " $::form->{version}";
|
||||
fa438d50 | Moritz Bunkus | ::run($session_result);
|
||
94899fc9 | Moritz Bunkus | |||
aa2bdd23 | Moritz Bunkus | } else {
|
||
3ab26ffc | Sven Schöling | if (SL::Auth::SESSION_EXPIRED == $session_result) {
|
||
print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login&error=session');
|
||||
c3bbf6d3 | Moritz Bunkus | ::end_of_request();
|
||
3ab26ffc | Sven Schöling | }
|
||
eb8ba476 | Sven Schöling | |||
540c0b5e | Moritz Bunkus | my %auth_result = $self->{auth_handler}->handle(
|
||
6afd06ad | Moritz Bunkus | routing_type => $routing_type,
|
||
script => $script,
|
||||
controller => $script_name,
|
||||
action => $action,
|
||||
);
|
||||
94899fc9 | Moritz Bunkus | |||
0e451e1b | Moritz Bunkus | ::end_of_request() unless $auth_result{auth_ok};
|
||
540c0b5e | Moritz Bunkus | delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
|
||
670f76a8 | Moritz Bunkus | |||
aa2bdd23 | Moritz Bunkus | if ($action) {
|
||
540c0b5e | Moritz Bunkus | $::instance_conf->init if $auth_result{auth_level} eq 'user';
|
||
891411c1 | Moritz Bunkus | |||
01b3bcb9 | Sven Schöling | map { $::form->{$_} = $::myconfig{$_} } qw(charset)
|
||
aa2bdd23 | Moritz Bunkus | unless $action eq 'save' && $::form->{type} eq 'preferences';
|
||
94899fc9 | Moritz Bunkus | |||
aa2bdd23 | Moritz Bunkus | $::form->set_standard_title;
|
||
41400107 | Moritz Bunkus | if ($routing_type eq 'old') {
|
||
::call_sub('::' . $::locale->findsub($action));
|
||||
} else {
|
||||
_run_controller($script_name, $action);
|
||||
}
|
||||
aa2bdd23 | Moritz Bunkus | } else {
|
||
$::form->error($::locale->text('action= not defined!'));
|
||||
}
|
||||
94899fc9 | Moritz Bunkus | }
|
||
1;
|
||||
} or do {
|
||||
b2945bf6 | Sven Schöling | if ($EVAL_ERROR ne END_OF_REQUEST) {
|
||
b9740e8a | Moritz Bunkus | my $error = $EVAL_ERROR;
|
||
print STDERR $error;
|
||||
if ($::request->is_ajax) {
|
||||
eval { render_error_ajax($error) };
|
||||
} else {
|
||||
$::form->{label_error} = $::request->{cgi}->pre($error);
|
||||
chdir SL::System::Process::exe_dir;
|
||||
eval { show_error('generic/error') };
|
||||
}
|
||||
b2945bf6 | Sven Schöling | }
|
||
94899fc9 | Moritz Bunkus | };
|
||
2219d158 | Sven Schöling | $::form->footer;
|
||
94899fc9 | Moritz Bunkus | # cleanup
|
||
e0ba33ff | Moritz Bunkus | $::auth->save_session;
|
||
91c07c66 | Sven Schöling | $::auth->expire_sessions;
|
||
$::auth->reset;
|
||||
94899fc9 | Moritz Bunkus | $::locale = undef;
|
||
$::form = undef;
|
||||
$::myconfig = ();
|
||||
772f08d0 | Sven Schöling | $::request = undef;
|
||
6b9cf44d | Moritz Bunkus | Form::disconnect_standard_dbh;
|
||
94899fc9 | Moritz Bunkus | |||
$::lxdebug->end_request;
|
||||
77ee93fc | Moritz Bunkus | |||
$self->_watch_for_changed_files;
|
||||
94899fc9 | Moritz Bunkus | $::lxdebug->leave_sub;
|
||
}
|
||||
sub unrequire_bin_mozilla {
|
||||
23f79a87 | Moritz Bunkus | my $self = shift;
|
||
2a19f220 | Moritz Bunkus | return unless $self->_interface_is_fcgi;
|
||
9ea6d163 | Sven Schöling | |||
94899fc9 | Moritz Bunkus | for (keys %INC) {
|
||
next unless m#^bin/mozilla/#;
|
||||
next if /\bcommon.pl$/;
|
||||
next if /\binstallationcheck.pl$/;
|
||||
delete $INC{$_};
|
||||
}
|
||||
}
|
||||
2a19f220 | Moritz Bunkus | sub _interface_is_fcgi {
|
||
my $self = shift;
|
||||
return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
|
||||
}
|
||||
7a604472 | Moritz Bunkus | sub _route_request {
|
||
my $script_name = shift;
|
||||
8c6871be | Moritz Bunkus | return $script_name =~ m/dispatcher\.pl$/ ? (type => 'old', _route_dispatcher_request())
|
||
: $script_name =~ m/controller\.pl/ ? (type => 'controller', _route_controller_request())
|
||||
: (type => 'old', controller => $script_name, action => $::form->{action});
|
||||
7a604472 | Moritz Bunkus | }
|
||
sub _route_dispatcher_request {
|
||||
d8b7e4d0 | Moritz Bunkus | my $name_re = qr{[a-z]\w*};
|
||
7a604472 | Moritz Bunkus | my ($script_name, $action);
|
||
eval {
|
||||
d8b7e4d0 | Moritz Bunkus | die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
|
||
7a604472 | Moritz Bunkus | $script_name = $::form->{M} . '.pl';
|
||
if ($::form->{A}) {
|
||||
$action = $::form->{A};
|
||||
} else {
|
||||
d8b7e4d0 | Moritz Bunkus | $action = first { m/^A_${name_re}$/ } keys %{ $::form };
|
||
7a604472 | Moritz Bunkus | die "Unroutable request -- inavlid action name.\n" if !$action;
|
||
delete $::form->{$action};
|
||||
$action = substr $action, 2;
|
||||
}
|
||||
delete @{$::form}{qw(M A)};
|
||||
1;
|
||||
} or do {
|
||||
5494f687 | Sven Schöling | $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
|
||
7a604472 | Moritz Bunkus | show_error('generic/error');
|
||
};
|
||||
8c6871be | Moritz Bunkus | return (controller => $script_name, action => $action);
|
||
7a604472 | Moritz Bunkus | }
|
||
41400107 | Moritz Bunkus | sub _route_controller_request {
|
||
8c6871be | Moritz Bunkus | my ($controller, $action, $request_type);
|
||
41400107 | Moritz Bunkus | |||
eval {
|
||||
8c6871be | Moritz Bunkus | $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) ( \. [a-zA-Z]+ )? $|x || die "Unroutable request -- inavlid controller/action.\n";
|
||
41400107 | Moritz Bunkus | ($controller, $action) = ($1, $2);
|
||
delete $::form->{action};
|
||||
8c6871be | Moritz Bunkus | $request_type = $3 ? lc(substr($3, 1)) : 'html';
|
||
41400107 | Moritz Bunkus | 1;
|
||
} or do {
|
||||
5494f687 | Sven Schöling | $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
|
||
41400107 | Moritz Bunkus | show_error('generic/error');
|
||
};
|
||||
8c6871be | Moritz Bunkus | return (controller => $controller, action => $action, request_type => $request_type);
|
||
41400107 | Moritz Bunkus | }
|
||
77ee93fc | Moritz Bunkus | 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;
|
||||
}
|
||||
7a0da5ac | Moritz Bunkus | sub get_standard_filehandles {
|
||
my $self = shift;
|
||||
return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
|
||||
}
|
||||
f416a998 | Moritz Bunkus | 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;
|
||||
540c0b5e | Moritz Bunkus | $::form->{title} = $::locale->text('Old configuration files');
|
||
f416a998 | Moritz Bunkus | $::form->header;
|
||
540c0b5e | Moritz Bunkus | print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
|
||
f416a998 | Moritz Bunkus | |||
::end_of_request();
|
||||
}
|
||||
b2945bf6 | Sven Schöling | package main;
|
||
use strict;
|
||||
sub end_of_request {
|
||||
die SL::Dispatcher->END_OF_REQUEST;
|
||||
}
|
||||
94899fc9 | Moritz Bunkus | 1;
|