X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDispatcher.pm;h=fcb3aeaf16962bd8ff3dc07104bb8d40ad72c5bb;hb=4c8c615a3268dc328f748cc8efe20fc2dc6cb8c5;hp=f9ad09a874bac259cb63707dbe7048ed80783f84;hpb=fab47672ea774d7218190a2a536876727be5a5de;p=kivitendo-erp.git diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index f9ad09a87..fcb3aeaf1 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -7,25 +7,18 @@ use strict; # 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; - - 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; use Encode; use English qw(-no_match_vars); +use FCGI; use File::Basename; +use IO::File; use List::MoreUtils qw(all); use List::Util qw(first); -use POSIX; +use POSIX qw(setlocale); use SL::ArchiveZipFixes; use SL::Auth; use SL::Dispatcher::AuthHandler; @@ -38,6 +31,11 @@ use SL::Form; use SL::Helper::DateTime; use SL::InstanceConfiguration; use SL::Template::Plugin::HTMLFixes; +use SL::User; + +use Rose::Object::MakeMethods::Generic ( + scalar => [ qw(restart_after_request) ], +); # Trailing new line is added so that Perl will not add the line # number 'die' was called in. @@ -54,6 +52,11 @@ sub new { SL::ArchiveZipFixes->apply_fixes; + # Initialize character type locale to be UTF-8 instead of C: + foreach my $locale (qw(de_DE.UTF-8 en_US.UTF-8)) { + last if setlocale('LC_CTYPE', $locale); + } + return $self; } @@ -81,6 +84,36 @@ sub pre_request_checks { } } +sub pre_request_initialization { + my ($self, %params) = @_; + + $self->unrequire_bin_mozilla; + + $::locale = Locale->new($::lx_office_conf{system}->{language}); + $::form = Form->new; + $::instance_conf = SL::InstanceConfiguration->new; + $::request = SL::Request->new( + cgi => CGI->new({}), + layout => SL::Layout::None->new, + ); + + my $session_result = $::auth->restore_session; + $::auth->create_or_refresh_session; + + if ($params{client}) { + $::auth->set_client($params{client}) || die("cannot find client " . $params{client}); + + if ($params{login}) { + die "cannot find user " . $params{login} unless %::myconfig = $::auth->read_user(login => $params{login}); + die "cannot find locale for user " . $params{login} unless $::locale = Locale->new($::myconfig{countrycode}); + + $::form->{login} = $params{login}; # normaly implicit at login + } + } + + return $session_result; +} + sub render_error_ajax { my ($error) = @_; @@ -99,6 +132,7 @@ sub show_error { $::locale = Locale->new($::myconfig{countrycode}); $::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->{error} = $::locale->text('The action is missing or invalid.') if ($error_type eq 'action'); return render_error_ajax($::form->{error}) if $::request->is_ajax; @@ -106,7 +140,7 @@ sub show_error { print $::form->parse_html_template($template, \%params); $::lxdebug->leave_sub; - ::end_of_request(); + end_request(); } sub pre_startup_setup { @@ -126,8 +160,8 @@ sub pre_startup_setup { $::lxdebug = LXDebug->new; $::auth = SL::Auth->new; $::form = undef; - %::myconfig = (); $::request = undef; + %::myconfig = User->get_default_myconfig; } $SIG{__WARN__} = sub { @@ -190,6 +224,20 @@ sub _run_controller { "SL::Controller::$_[0]"->new->_run_action($_[1]); } +sub handle_all_requests { + my ($self) = @_; + + my $request = FCGI::Request(); + while ($request->Accept() >= 0) { + $self->handle_request($request); + + $self->restart_after_request(1) if $self->_interface_is_fcgi && SL::System::Process::memory_usage_is_too_high(); + $request->LastCall if $self->restart_after_request; + } + + exec $0 if $self->restart_after_request; +} + sub handle_request { my $self = shift; $self->{request} = shift; @@ -199,23 +247,12 @@ sub handle_request { my ($script, $path, $suffix, $script_name, $action, $routing_type); - $self->unrequire_bin_mozilla; - - $::locale = Locale->new($::lx_office_conf{system}->{language}); - $::form = Form->new; - $::instance_conf = SL::InstanceConfiguration->new; - $::request = SL::Request->new( - cgi => CGI->new({}), - layout => SL::Layout::None->new, - ); - - my $session_result = $::auth->restore_session; - $::auth->create_or_refresh_session; + my $session_result = $self->pre_request_initialization; $::form->read_cgi_input; my %routing; - eval { %routing = _route_request($ENV{SCRIPT_NAME}); 1; } or return; + eval { %routing = $self->_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); @@ -254,7 +291,7 @@ sub handle_request { if ( (($script eq 'login') && !$action) || ($script eq 'admin') || (SL::Auth::SESSION_EXPIRED() == $session_result)) { - $self->redirect_to_login($script); + $self->redirect_to_login(script => $script, error => 'session'); } @@ -265,7 +302,7 @@ sub handle_request { action => $action, ); - ::end_of_request() unless $auth_result{auth_ok}; + $self->end_request unless $auth_result{auth_ok}; delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars}; @@ -298,6 +335,14 @@ sub handle_request { $::form->footer; + if ($self->_interface_is_fcgi) { + # fcgi? send send reponse on its way before cleanup. + $self->{request}->Flush; + $self->{request}->Finish; + } + + $::lxdebug->end_request(routing_type => $routing_type, script_name => $script_name, action => $action); + # cleanup $::auth->save_session; $::auth->expire_sessions; @@ -305,11 +350,10 @@ sub handle_request { $::locale = undef; $::form = undef; - $::myconfig = (); + %::myconfig = (); $::request = undef; - Form::disconnect_standard_dbh; - $::lxdebug->end_request; + SL::DBConnect::Cache->reset_all; $self->_watch_for_changed_files; @@ -317,10 +361,12 @@ sub handle_request { } sub redirect_to_login { - my ($self, $script) = @_; - my $action = $script =~ m/^admin/i ? 'Admin/login' : 'LoginScreen/user_login&error=session'; + my ($self, %params) = @_; + my $action = ($params{script} // '') =~ m/^admin/i ? 'Admin/login' : 'LoginScreen/user_login'; + $action .= '&error=' . $params{error} if $params{error}; + print $::request->cgi->redirect("controller.pl?action=${action}"); - ::end_of_request(); + $self->end_request; } sub unrequire_bin_mozilla { @@ -341,14 +387,15 @@ sub _interface_is_fcgi { } sub _route_request { - my $script_name = shift; + my ($self, $script_name) = @_; - return $script_name =~ m/dispatcher\.pl$/ ? (type => 'old', _route_dispatcher_request()) - : $script_name =~ m/controller\.pl/ ? (type => 'controller', _route_controller_request()) + return $script_name =~ m/dispatcher\.pl$/ ? (type => 'old', $self->_route_dispatcher_request) + : $script_name =~ m/controller\.pl/ ? (type => 'controller', $self->_route_controller_request) : (type => 'old', controller => $script_name, action => $::form->{action}); } sub _route_dispatcher_request { + my ($self) = @_; my $name_re = qr{[a-z]\w*}; my ($script_name, $action); @@ -379,9 +426,16 @@ sub _route_dispatcher_request { } sub _route_controller_request { + my ($self) = @_; my ($controller, $action, $request_type); eval { + # Redirect simple requests to controller.pl without any GET/POST + # param to the login page. + $self->redirect_to_login(error => 'action') if !$::form->{action}; + + # Show an error if the »action« parameter doesn't match the + # pattern »Controller/action«. $::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}; @@ -423,7 +477,7 @@ sub _watch_for_changed_files { my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache; return if $ok; $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting."); - exit; + $self->restart_after_request(1); } sub get_standard_filehandles { @@ -440,14 +494,10 @@ sub _check_for_old_config_files { $::form->header; print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files }); - ::end_of_request(); + end_request(); } -package main; - -use strict; - -sub end_of_request { +sub end_request { die SL::Dispatcher->END_OF_REQUEST; }