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);
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 print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login');
254 } elsif ($script eq 'admin') {
255 ::run($session_result);
258 $self->redirect_to_login($script) if SL::Auth::SESSION_EXPIRED == $session_result;
260 my %auth_result = $self->{auth_handler}->handle(
261 routing_type => $routing_type,
263 controller => $script_name,
267 ::end_of_request() unless $auth_result{auth_ok};
269 delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
272 $::instance_conf->init if $auth_result{auth_level} eq 'user';
274 map { $::form->{$_} = $::myconfig{$_} } qw(charset)
275 unless $action eq 'save' && $::form->{type} eq 'preferences';
277 $::form->set_standard_title;
278 if ($routing_type eq 'old') {
279 ::call_sub('::' . $::locale->findsub($action));
281 _run_controller($script_name, $action);
284 $::form->error($::locale->text('action= not defined!'));
290 if (substr($EVAL_ERROR, 0, length(END_OF_REQUEST())) ne END_OF_REQUEST()) {
291 my $error = $EVAL_ERROR;
294 if ($::request->is_ajax) {
295 eval { render_error_ajax($error) };
297 $::form->{label_error} = $::request->{cgi}->pre($error);
298 chdir SL::System::Process::exe_dir;
299 eval { show_error('generic/error') };
307 $::auth->save_session;
308 $::auth->expire_sessions;
315 Form::disconnect_standard_dbh;
317 $::lxdebug->end_request;
319 $self->_watch_for_changed_files;
321 $::lxdebug->leave_sub;
324 sub redirect_to_login {
325 my ($self, $script) = @_;
326 my $action = $script =~ m/^admin/i ? 'Admin/login' : 'LoginScreen/user_login&error=session';
327 print $::request->cgi->redirect("controller.pl?action=${action}");
331 sub unrequire_bin_mozilla {
333 return unless $self->_interface_is_fcgi;
336 next unless m#^bin/mozilla/#;
337 next if /\bcommon.pl$/;
338 next if /\binstallationcheck.pl$/;
343 sub _interface_is_fcgi {
345 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
349 my $script_name = shift;
351 return $script_name =~ m/dispatcher\.pl$/ ? (type => 'old', _route_dispatcher_request())
352 : $script_name =~ m/controller\.pl/ ? (type => 'controller', _route_controller_request())
353 : (type => 'old', controller => $script_name, action => $::form->{action});
356 sub _route_dispatcher_request {
357 my $name_re = qr{[a-z]\w*};
358 my ($script_name, $action);
361 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
362 $script_name = $::form->{M} . '.pl';
365 $action = $::form->{A};
368 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
369 die "Unroutable request -- inavlid action name.\n" if !$action;
371 delete $::form->{$action};
372 $action = substr $action, 2;
375 delete @{$::form}{qw(M A)};
379 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
380 show_error('generic/error');
383 return (controller => $script_name, action => $action);
386 sub _route_controller_request {
387 my ($controller, $action, $request_type);
390 $::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";
391 ($controller, $action) = ($1, $2);
392 delete $::form->{action};
394 $request_type = $3 ? lc(substr($3, 1)) : 'html';
398 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
399 show_error('generic/error');
402 return (controller => $controller, action => $action, request_type => $request_type);
405 sub _cache_file_modification_times {
408 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
414 return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
415 $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
418 my $cwd = POSIX::getcwd();
419 File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
420 map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
423 sub _watch_for_changed_files {
426 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
428 my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
430 $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
434 sub get_standard_filehandles {
437 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
440 sub _check_for_old_config_files {
441 my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
442 return unless @old_files;
444 $::form->{title} = $::locale->text('Old configuration files');
446 print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
456 die SL::Dispatcher->END_OF_REQUEST;