X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDispatcher.pm;h=01b6aff2ea860f1b4267dd3541e72f20a607a70a;hb=13fc241edb1b1a658eb818a106ec06e1e9dac005;hp=f6c9f60cf951f228fe7f0270b06428185b5d7e3c;hpb=540c0b5e9a38158e6f7a6efacd20b2477efad7ac;p=kivitendo-erp.git diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index f6c9f60cf..01b6aff2e 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -2,11 +2,21 @@ package SL::Dispatcher; use strict; +# 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') + BEGIN { - unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML). - push @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version. + 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; } +use Carp; use CGI qw( -no_xhtml); use Config::Std; use DateTime; @@ -21,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; @@ -59,6 +70,18 @@ sub pre_request_checks { show_error('login_screen/auth_db_unreachable'); } } + + if ($::request->type !~ m/^ (?: html | js | json ) $/x) { + die $::locale->text("Invalid request type '#1'", $::request->type); + } +} + +sub render_error_ajax { + my ($error) = @_; + + SL::ClientJS->new + ->error($error) + ->render(SL::Controller::Base->new); } sub show_error { @@ -72,6 +95,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; @@ -104,6 +129,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; } @@ -172,14 +199,22 @@ sub handle_request { $::locale = Locale->new($::lx_office_conf{system}->{language}); $::form = Form->new; $::instance_conf = SL::InstanceConfiguration->new; - $::request = { cgi => CGI->new({}) }; + $::request = SL::Request->new( + cgi => CGI->new({}), + layout => SL::Layout::None->new, + ); my $session_result = $::auth->restore_session; $::auth->create_or_refresh_session; $::form->read_cgi_input; - eval { ($routing_type, $script_name, $action) = _route_request($ENV{SCRIPT_NAME}); 1; } or return; + 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')); if ($routing_type eq 'old') { $::form->{action} = lc $::form->{action}; @@ -200,7 +235,13 @@ sub handle_request { $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin'; - ($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login) if ($script eq 'login') && ($action eq 'login'); + # 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'); + } if (($script eq 'login') && !$action) { print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login'); @@ -210,7 +251,10 @@ sub handle_request { ::run($session_result); } else { - show_error('login_screen/user_login', 'session') if SL::Auth::SESSION_EXPIRED == $session_result; + 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, @@ -219,6 +263,8 @@ sub handle_request { action => $action, ); + ::end_of_request() unless $auth_result{auth_ok}; + delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars}; if ($action) { @@ -240,13 +286,22 @@ sub handle_request { 1; } or do { - if ($EVAL_ERROR ne END_OF_REQUEST) { - print STDERR $EVAL_ERROR; - $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR); - 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') }; + } } }; + $::form->footer; + # cleanup $::auth->save_session; $::auth->expire_sessions; @@ -285,9 +340,9 @@ sub _interface_is_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}); + 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}); } sub _route_dispatcher_request { @@ -317,24 +372,26 @@ sub _route_dispatcher_request { show_error('generic/error'); }; - return ($script_name, $action); + return (controller => $script_name, action => $action); } sub _route_controller_request { - my ($controller, $action); + my ($controller, $action, $request_type); eval { - $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|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 -- inavlid controller/action.\n"; ($controller, $action) = ($1, $2); delete $::form->{action}; + $request_type = $3 ? lc(substr($3, 1)) : 'html'; + 1; } or do { $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR); show_error('generic/error'); }; - return ($controller, $action); + return (controller => $controller, action => $action, request_type => $request_type); } sub _cache_file_modification_times {