1 package SL::Dispatcher;
5 # Force scripts/locales.pl to parse these templates:
6 # parse_html_template('login_screen/auth_db_unreachable')
7 # parse_html_template('login_screen/user_login')
8 # parse_html_template('generic/error')
11 use SL::System::Process;
12 my $exe_dir = SL::System::Process::exe_dir;
14 unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML).
15 push @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version.
16 unshift @INC, $exe_dir;
20 use CGI qw( -no_xhtml);
24 use English qw(-no_match_vars);
26 use List::MoreUtils qw(all);
27 use List::Util qw(first);
30 use SL::Dispatcher::AuthHandler;
37 use SL::Helper::DateTime;
38 use SL::InstanceConfiguration;
39 use SL::Template::Plugin::HTMLFixes;
41 # Trailing new line is added so that Perl will not add the line
42 # number 'die' was called in.
43 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
48 my ($class, $interface) = @_;
50 my $self = bless {}, $class;
51 $self->{interface} = lc($interface || 'cgi');
52 $self->{auth_handler} = SL::Dispatcher::AuthHandler->new;
59 return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
62 sub is_admin_request {
64 return ($params{script} eq 'admin.pl') || (($params{routing_type} eq 'controller') && ($params{script_name} eq 'Admin'));
67 sub pre_request_checks {
70 _check_for_old_config_files();
72 if (!$::auth->session_tables_present && !is_admin_request(%params)) {
73 show_error('login_screen/auth_db_unreachable');
76 if ($::request->type !~ m/^ (?: html | js | json ) $/x) {
77 die $::locale->text("Invalid request type '#1'", $::request->type);
81 sub render_error_ajax {
86 ->render(SL::Controller::Base->new);
90 $::lxdebug->enter_sub;
92 my $error_type = shift || '';
95 $::myconfig{countrycode} = delete($params{countrycode}) || $::lx_office_conf{system}->{language};
96 $::locale = Locale->new($::myconfig{countrycode});
97 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
98 $::form->{error} = $::locale->text('Incorrect password!') if ($error_type eq 'password');
100 return render_error_ajax($::form->{error}) if $::request->is_ajax;
103 print $::form->parse_html_template($template, \%params);
104 $::lxdebug->leave_sub;
109 sub pre_startup_setup {
112 SL::LxOfficeConf->read;
116 require "bin/mozilla/common.pl";
117 require "bin/mozilla/installationcheck.pl";
118 } or die $EVAL_ERROR;
120 # canonial globals. if it's not here, chances are it will get refactored someday.
123 $::lxdebug = LXDebug->new;
124 $::auth = SL::Auth->new;
130 $SIG{__WARN__} = sub {
131 $::lxdebug->warn(@_);
134 $SIG{__DIE__} = sub { Carp::confess( @_ ) } if $::lx_office_conf{debug}->{backtrace_on_die};
136 $self->_cache_file_modification_times;
139 sub pre_startup_checks {
140 ::verify_installation();
145 $self->pre_startup_setup;
146 $self->pre_startup_checks;
149 sub require_main_code {
150 $::lxdebug->enter_sub;
151 my ($script, $suffix) = @_;
155 require "bin/mozilla/$script$suffix";
156 } or die $EVAL_ERROR;
158 if (-f "bin/mozilla/custom_$script$suffix") {
161 require "bin/mozilla/custom_$script$suffix";
163 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
165 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
168 require "bin/mozilla/$::form->{login}_$script";
170 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
172 $::lxdebug->leave_sub;
175 sub _require_controller {
176 my $controller = shift;
177 $controller =~ s|[^A-Za-z0-9_]||g;
178 $controller = "SL/Controller/${controller}";
182 require "${controller}.pm";
183 } or die $EVAL_ERROR;
186 sub _run_controller {
187 "SL::Controller::$_[0]"->new->_run_action($_[1]);
192 $self->{request} = shift;
194 $::lxdebug->enter_sub;
195 $::lxdebug->begin_request;
197 my ($script, $path, $suffix, $script_name, $action, $routing_type);
199 $self->unrequire_bin_mozilla;
201 $::locale = Locale->new($::lx_office_conf{system}->{language});
203 $::instance_conf = SL::InstanceConfiguration->new;
204 $::request = SL::Request->new(
206 layout => SL::Layout::None->new,
209 my $session_result = $::auth->restore_session;
210 $::auth->create_or_refresh_session;
212 $::form->read_cgi_input;
215 eval { %routing = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
216 ($routing_type, $script_name, $action) = @routing{qw(type controller action)};
217 $::lxdebug->log_request($routing_type, $script_name, $action);
219 $::request->type(lc($routing{request_type} || 'html'));
221 if ($routing_type eq 'old') {
222 $::form->{action} = lc $::form->{action};
223 $::form->{action} =~ s/( |-|,|\#)/_/g;
225 ($script, $path, $suffix) = fileparse($script_name, ".pl");
226 require_main_code($script, $suffix) unless $script eq 'admin';
228 $::form->{script} = $script . $suffix;
231 _require_controller($script_name);
232 $::form->{script} = "controller.pl";
236 pre_request_checks(script => $script, action => $action, routing_type => $routing_type, script_name => $script_name);
238 if ( SL::System::InstallationLock->is_locked
239 && !is_admin_request(script => $script, script_name => $script_name, routing_type => $routing_type)) {
240 $::form->error($::locale->text('System currently down for maintenance!'));
243 # For compatibility with a lot of database upgrade scripts etc:
244 # Re-write request to old 'login.pl?action=login' to new
245 # 'LoginScreen' controller. Make sure to load its code!
246 if (($script eq 'login') && ($action eq 'login')) {
247 ($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login);
248 _require_controller('LoginScreen');
251 if ( (($script eq 'login') && !$action)
252 || ($script eq 'admin')
253 || (SL::Auth::SESSION_EXPIRED() == $session_result)) {
254 $self->redirect_to_login($script);
258 my %auth_result = $self->{auth_handler}->handle(
259 routing_type => $routing_type,
261 controller => $script_name,
265 ::end_of_request() unless $auth_result{auth_ok};
267 delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
270 $::form->set_standard_title;
271 if ($routing_type eq 'old') {
272 ::call_sub('::' . $::locale->findsub($action));
274 _run_controller($script_name, $action);
277 $::form->error($::locale->text('action= not defined!'));
282 if (substr($EVAL_ERROR, 0, length(END_OF_REQUEST())) ne END_OF_REQUEST()) {
283 my $error = $EVAL_ERROR;
286 if ($::request->is_ajax) {
287 eval { render_error_ajax($error) };
289 $::form->{label_error} = $::request->{cgi}->pre($error);
290 chdir SL::System::Process::exe_dir;
291 eval { show_error('generic/error') };
299 $::auth->save_session;
300 $::auth->expire_sessions;
307 Form::disconnect_standard_dbh;
309 $::lxdebug->end_request;
311 $self->_watch_for_changed_files;
313 $::lxdebug->leave_sub;
316 sub redirect_to_login {
317 my ($self, $script) = @_;
318 my $action = $script =~ m/^admin/i ? 'Admin/login' : 'LoginScreen/user_login&error=session';
319 print $::request->cgi->redirect("controller.pl?action=${action}");
323 sub unrequire_bin_mozilla {
325 return unless $self->_interface_is_fcgi;
328 next unless m#^bin/mozilla/#;
329 next if /\bcommon.pl$/;
330 next if /\binstallationcheck.pl$/;
335 sub _interface_is_fcgi {
337 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
341 my $script_name = shift;
343 return $script_name =~ m/dispatcher\.pl$/ ? (type => 'old', _route_dispatcher_request())
344 : $script_name =~ m/controller\.pl/ ? (type => 'controller', _route_controller_request())
345 : (type => 'old', controller => $script_name, action => $::form->{action});
348 sub _route_dispatcher_request {
349 my $name_re = qr{[a-z]\w*};
350 my ($script_name, $action);
353 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
354 $script_name = $::form->{M} . '.pl';
357 $action = $::form->{A};
360 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
361 die "Unroutable request -- inavlid action name.\n" if !$action;
363 delete $::form->{$action};
364 $action = substr $action, 2;
367 delete @{$::form}{qw(M A)};
371 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
372 show_error('generic/error');
375 return (controller => $script_name, action => $action);
378 sub _route_controller_request {
379 my ($controller, $action, $request_type);
382 $::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";
383 ($controller, $action) = ($1, $2);
384 delete $::form->{action};
386 $request_type = $3 ? lc(substr($3, 1)) : 'html';
390 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
391 show_error('generic/error');
394 return (controller => $controller, action => $action, request_type => $request_type);
397 sub _cache_file_modification_times {
400 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
406 return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
407 $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
410 my $cwd = POSIX::getcwd();
411 File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
412 map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
415 sub _watch_for_changed_files {
418 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
420 my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
422 $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
426 sub get_standard_filehandles {
429 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
432 sub _check_for_old_config_files {
433 my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
434 return unless @old_files;
436 $::form->{title} = $::locale->text('Old configuration files');
438 print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
448 die SL::Dispatcher->END_OF_REQUEST;