X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDispatcher.pm;h=93d3a18e6c491d6455758b5d8a025aaa22c0e401;hb=33d5d38a2873a25f1e9c2c37a907644fdd33f026;hp=35a351877f8f6ef84ac26f530141259780d4a3a0;hpb=075bd42af8885aee3c18fe055a2c82b8b43f4cea;p=kivitendo-erp.git diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index 35a351877..93d3a18e6 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -30,12 +30,12 @@ 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; use SL::InstanceConfiguration; use SL::Template::Plugin::HTMLFixes; -use SL::Layout::None; # Trailing new line is added so that Perl will not add the line # number 'die' was called in. @@ -69,6 +69,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 { @@ -82,7 +94,9 @@ 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'); - $::form->header(no_menu => 1); + return render_error_ajax($::form->{error}) if $::request->is_ajax; + + $::form->header; print $::form->parse_html_template($template, \%params); $::lxdebug->leave_sub; @@ -182,17 +196,22 @@ sub handle_request { $::locale = Locale->new($::lx_office_conf{system}->{language}); $::form = Form->new; $::instance_conf = SL::InstanceConfiguration->new; - $::request = { + $::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}; @@ -231,6 +250,7 @@ sub handle_request { } 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( @@ -240,6 +260,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) { @@ -262,10 +284,16 @@ 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); - chdir SL::System::Process::exe_dir; - eval { show_error('generic/error') }; + 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') }; + } } }; @@ -309,9 +337,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 { @@ -341,24 +369,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 {