X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDispatcher.pm;h=fe992ed2aef58c2e9b20b96a4ef3e193d7b3dc13;hb=c8473408202bb3b821a14cee9f8945405d8eeffc;hp=5282369fd758eb52d9bd9a3a04ebb96974e623b3;hpb=7c9fc25b6893a1ea508185a0f1a197d6b46f187a;p=kivitendo-erp.git diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index 5282369fd..fe992ed2a 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -16,6 +16,7 @@ BEGIN { unshift @INC, $exe_dir; } +use Carp; use CGI qw( -no_xhtml); use Config::Std; use DateTime; @@ -30,6 +31,7 @@ use SL::Dispatcher::AuthHandler; use SL::LXDebug; use SL::LxOfficeConf; use SL::Locale; +use SL::ClientJS; use SL::Common; use SL::Form; use SL::Helper::DateTime; @@ -57,16 +59,18 @@ sub interface_type { return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI'; } +sub is_admin_request { + my %params = @_; + return ($params{script} eq 'admin.pl') || (($params{routing_type} eq 'controller') && ($params{script_name} eq 'Admin')); +} + sub pre_request_checks { + my (%params) = @_; + _check_for_old_config_files(); - if (!$::auth->session_tables_present) { - if ($::form->{script} eq 'admin.pl') { - ::run(); - ::end_of_request(); - } else { - show_error('login_screen/auth_db_unreachable'); - } + if (!$::auth->session_tables_present && !is_admin_request(%params)) { + show_error('login_screen/auth_db_unreachable'); } if ($::request->type !~ m/^ (?: html | js | json ) $/x) { @@ -74,6 +78,14 @@ sub pre_request_checks { } } +sub render_error_ajax { + my ($error) = @_; + + SL::ClientJS->new + ->error($error) + ->render(SL::Controller::Base->new); +} + sub show_error { $::lxdebug->enter_sub; my $template = shift; @@ -85,6 +97,8 @@ sub show_error { $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session'); $::form->{error} = $::locale->text('Incorrect password!') if ($error_type eq 'password'); + return render_error_ajax($::form->{error}) if $::request->is_ajax; + $::form->header; print $::form->parse_html_template($template, \%params); $::lxdebug->leave_sub; @@ -117,6 +131,8 @@ sub pre_startup_setup { $::lxdebug->warn(@_); }; + $SIG{__DIE__} = sub { Carp::confess( @_ ) } if $::lx_office_conf{debug}->{backtrace_on_die}; + $self->_cache_file_modification_times; } @@ -198,6 +214,7 @@ sub handle_request { my %routing; eval { %routing = _route_request($ENV{SCRIPT_NAME}); 1; } or return; ($routing_type, $script_name, $action) = @routing{qw(type controller action)}; + $::lxdebug->log_request($routing_type, $script_name, $action); $::request->type(lc($routing{request_type} || 'html')); @@ -206,7 +223,7 @@ sub handle_request { $::form->{action} =~ s/( |-|,|\#)/_/g; ($script, $path, $suffix) = fileparse($script_name, ".pl"); - require_main_code($script, $suffix); + require_main_code($script, $suffix) unless $script eq 'admin'; $::form->{script} = $script . $suffix; @@ -216,9 +233,12 @@ sub handle_request { } eval { - pre_request_checks(); + pre_request_checks(script => $script, action => $action, routing_type => $routing_type, script_name => $script_name); - $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin'; + if ( SL::System::InstallationLock->is_locked + && !is_admin_request(script => $script, script_name => $script_name, routing_type => $routing_type)) { + $::form->error($::locale->text('System currently down for maintenance!')); + } # For compatibility with a lot of database upgrade scripts etc: # Re-write request to old 'login.pl?action=login' to new @@ -228,54 +248,48 @@ sub handle_request { _require_controller('LoginScreen'); } - 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}"; - ::run($session_result); + if ( (($script eq 'login') && !$action) + || ($script eq 'admin') + || (SL::Auth::SESSION_EXPIRED() == $session_result)) { + $self->redirect_to_login($script); - } else { - if (SL::Auth::SESSION_EXPIRED == $session_result) { - print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login&error=session'); - ::end_of_request(); - } - - my %auth_result = $self->{auth_handler}->handle( - routing_type => $routing_type, - script => $script, - controller => $script_name, - action => $action, - ); - - ::end_of_request() unless $auth_result{auth_ok}; + } - delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars}; + my %auth_result = $self->{auth_handler}->handle( + routing_type => $routing_type, + script => $script, + controller => $script_name, + action => $action, + ); - if ($action) { - $::instance_conf->init if $auth_result{auth_level} eq 'user'; + ::end_of_request() unless $auth_result{auth_ok}; - map { $::form->{$_} = $::myconfig{$_} } qw(charset) - unless $action eq 'save' && $::form->{type} eq 'preferences'; + delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars}; - $::form->set_standard_title; - if ($routing_type eq 'old') { - ::call_sub('::' . $::locale->findsub($action)); - } else { - _run_controller($script_name, $action); - } + if ($action) { + $::form->set_standard_title; + if ($routing_type eq 'old') { + ::call_sub('::' . $::locale->findsub($action)); } else { - $::form->error($::locale->text('action= not defined!')); + _run_controller($script_name, $action); } + } else { + $::form->error($::locale->text('action= not defined!')); } 1; } or do { - if ($EVAL_ERROR ne END_OF_REQUEST) { - print STDERR $EVAL_ERROR; - $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR); - chdir SL::System::Process::exe_dir; - eval { show_error('generic/error') }; + if (substr($EVAL_ERROR, 0, length(END_OF_REQUEST())) ne END_OF_REQUEST()) { + 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') }; + } } }; @@ -299,6 +313,13 @@ sub handle_request { $::lxdebug->leave_sub; } +sub redirect_to_login { + my ($self, $script) = @_; + my $action = $script =~ m/^admin/i ? 'Admin/login' : 'LoginScreen/user_login&error=session'; + print $::request->cgi->redirect("controller.pl?action=${action}"); + ::end_of_request(); +} + sub unrequire_bin_mozilla { my $self = shift; return unless $self->_interface_is_fcgi; @@ -329,7 +350,7 @@ sub _route_dispatcher_request { my ($script_name, $action); eval { - die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/); + die "Unroutable request -- invalid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/); $script_name = $::form->{M} . '.pl'; if ($::form->{A}) { @@ -337,7 +358,7 @@ sub _route_dispatcher_request { } else { $action = first { m/^A_${name_re}$/ } keys %{ $::form }; - die "Unroutable request -- inavlid action name.\n" if !$action; + die "Unroutable request -- invalid action name.\n" if !$action; delete $::form->{$action}; $action = substr $action, 2; @@ -358,7 +379,7 @@ sub _route_controller_request { my ($controller, $action, $request_type); eval { - $::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"; + $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) ( \. [a-zA-Z]+ )? $|x || die "Unroutable request -- invalid controller/action.\n"; ($controller, $action) = ($1, $2); delete $::form->{action};