X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDispatcher.pm;h=f9ad09a874bac259cb63707dbe7048ed80783f84;hb=fab47672ea774d7218190a2a536876727be5a5de;hp=c6f3b5e10d186b4580079f817d726d7319084313;hpb=e7d52be303c9251fa2a46dee0d9a6c45fb0e2d54;p=kivitendo-erp.git diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index c6f3b5e10..f9ad09a87 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -2,6 +2,11 @@ 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 { use SL::System::Process; my $exe_dir = SL::System::Process::exe_dir; @@ -11,6 +16,7 @@ BEGIN { unshift @INC, $exe_dir; } +use Carp; use CGI qw( -no_xhtml); use Config::Std; use DateTime; @@ -20,11 +26,13 @@ use File::Basename; use List::MoreUtils qw(all); use List::Util qw(first); use POSIX; +use SL::ArchiveZipFixes; use SL::Auth; 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; @@ -44,6 +52,8 @@ sub new { $self->{interface} = lc($interface || 'cgi'); $self->{auth_handler} = SL::Dispatcher::AuthHandler->new; + SL::ArchiveZipFixes->apply_fixes; + return $self; } @@ -52,17 +62,31 @@ 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) { + 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 { @@ -76,6 +100,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; @@ -108,6 +134,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; } @@ -176,21 +204,29 @@ 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}; $::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; @@ -200,9 +236,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 @@ -212,52 +251,53 @@ 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); - - } else { - show_error('login_screen/user_login', 'session') if SL::Auth::SESSION_EXPIRED == $session_result; + if ( (($script eq 'login') && !$action) + || ($script eq 'admin') + || (SL::Auth::SESSION_EXPIRED() == $session_result)) { + $self->redirect_to_login($script); - my %auth_result = $self->{auth_handler}->handle( - routing_type => $routing_type, - script => $script, - controller => $script_name, - action => $action, - ); + } - 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') }; + } } }; + $::form->footer; + # cleanup $::auth->save_session; $::auth->expire_sessions; @@ -276,6 +316,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; @@ -296,9 +343,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 { @@ -306,7 +353,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}) { @@ -314,7 +361,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; @@ -328,24 +375,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 -- invalid 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 {