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);
29 use SL::ArchiveZipFixes;
31 use SL::Dispatcher::AuthHandler;
38 use SL::Helper::DateTime;
39 use SL::InstanceConfiguration;
40 use SL::Template::Plugin::HTMLFixes;
42 # Trailing new line is added so that Perl will not add the line
43 # number 'die' was called in.
44 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
49 my ($class, $interface) = @_;
51 my $self = bless {}, $class;
52 $self->{interface} = lc($interface || 'cgi');
53 $self->{auth_handler} = SL::Dispatcher::AuthHandler->new;
55 SL::ArchiveZipFixes->apply_fixes;
62 return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
65 sub is_admin_request {
67 return ($params{script} eq 'admin.pl') || (($params{routing_type} eq 'controller') && ($params{script_name} eq 'Admin'));
70 sub pre_request_checks {
73 _check_for_old_config_files();
75 if (!$::auth->session_tables_present && !is_admin_request(%params)) {
76 show_error('login_screen/auth_db_unreachable');
79 if ($::request->type !~ m/^ (?: html | js | json ) $/x) {
80 die $::locale->text("Invalid request type '#1'", $::request->type);
84 sub render_error_ajax {
89 ->render(SL::Controller::Base->new);
93 $::lxdebug->enter_sub;
95 my $error_type = shift || '';
98 $::myconfig{countrycode} = delete($params{countrycode}) || $::lx_office_conf{system}->{language};
99 $::locale = Locale->new($::myconfig{countrycode});
100 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
101 $::form->{error} = $::locale->text('Incorrect password!') if ($error_type eq 'password');
103 return render_error_ajax($::form->{error}) if $::request->is_ajax;
106 print $::form->parse_html_template($template, \%params);
107 $::lxdebug->leave_sub;
112 sub pre_startup_setup {
115 SL::LxOfficeConf->read;
119 require "bin/mozilla/common.pl";
120 require "bin/mozilla/installationcheck.pl";
121 } or die $EVAL_ERROR;
123 # canonial globals. if it's not here, chances are it will get refactored someday.
126 $::lxdebug = LXDebug->new;
127 $::auth = SL::Auth->new;
133 $SIG{__WARN__} = sub {
134 $::lxdebug->warn(@_);
137 $SIG{__DIE__} = sub { Carp::confess( @_ ) } if $::lx_office_conf{debug}->{backtrace_on_die};
139 $self->_cache_file_modification_times;
142 sub pre_startup_checks {
143 ::verify_installation();
148 $self->pre_startup_setup;
149 $self->pre_startup_checks;
152 sub require_main_code {
153 $::lxdebug->enter_sub;
154 my ($script, $suffix) = @_;
158 require "bin/mozilla/$script$suffix";
159 } or die $EVAL_ERROR;
161 if (-f "bin/mozilla/custom_$script$suffix") {
164 require "bin/mozilla/custom_$script$suffix";
166 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
168 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
171 require "bin/mozilla/$::form->{login}_$script";
173 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
175 $::lxdebug->leave_sub;
178 sub _require_controller {
179 my $controller = shift;
180 $controller =~ s|[^A-Za-z0-9_]||g;
181 $controller = "SL/Controller/${controller}";
185 require "${controller}.pm";
186 } or die $EVAL_ERROR;
189 sub _run_controller {
190 "SL::Controller::$_[0]"->new->_run_action($_[1]);
195 $self->{request} = shift;
197 $::lxdebug->enter_sub;
198 $::lxdebug->begin_request;
200 my ($script, $path, $suffix, $script_name, $action, $routing_type);
202 $self->unrequire_bin_mozilla;
204 $::locale = Locale->new($::lx_office_conf{system}->{language});
206 $::instance_conf = SL::InstanceConfiguration->new;
207 $::request = SL::Request->new(
209 layout => SL::Layout::None->new,
212 my $session_result = $::auth->restore_session;
213 $::auth->create_or_refresh_session;
215 $::form->read_cgi_input;
218 eval { %routing = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
219 ($routing_type, $script_name, $action) = @routing{qw(type controller action)};
220 $::lxdebug->log_request($routing_type, $script_name, $action);
222 $::request->type(lc($routing{request_type} || 'html'));
224 if ($routing_type eq 'old') {
225 $::form->{action} = lc $::form->{action};
226 $::form->{action} =~ s/( |-|,|\#)/_/g;
228 ($script, $path, $suffix) = fileparse($script_name, ".pl");
229 require_main_code($script, $suffix) unless $script eq 'admin';
231 $::form->{script} = $script . $suffix;
234 _require_controller($script_name);
235 $::form->{script} = "controller.pl";
239 pre_request_checks(script => $script, action => $action, routing_type => $routing_type, script_name => $script_name);
241 if ( SL::System::InstallationLock->is_locked
242 && !is_admin_request(script => $script, script_name => $script_name, routing_type => $routing_type)) {
243 $::form->error($::locale->text('System currently down for maintenance!'));
246 # For compatibility with a lot of database upgrade scripts etc:
247 # Re-write request to old 'login.pl?action=login' to new
248 # 'LoginScreen' controller. Make sure to load its code!
249 if (($script eq 'login') && ($action eq 'login')) {
250 ($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login);
251 _require_controller('LoginScreen');
254 if ( (($script eq 'login') && !$action)
255 || ($script eq 'admin')
256 || (SL::Auth::SESSION_EXPIRED() == $session_result)) {
257 $self->redirect_to_login($script);
261 my %auth_result = $self->{auth_handler}->handle(
262 routing_type => $routing_type,
264 controller => $script_name,
268 ::end_of_request() unless $auth_result{auth_ok};
270 delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
273 $::form->set_standard_title;
274 if ($routing_type eq 'old') {
275 ::call_sub('::' . $::locale->findsub($action));
277 _run_controller($script_name, $action);
280 $::form->error($::locale->text('action= not defined!'));
285 if (substr($EVAL_ERROR, 0, length(END_OF_REQUEST())) ne END_OF_REQUEST()) {
286 my $error = $EVAL_ERROR;
289 if ($::request->is_ajax) {
290 eval { render_error_ajax($error) };
292 $::form->{label_error} = $::request->{cgi}->pre($error);
293 chdir SL::System::Process::exe_dir;
294 eval { show_error('generic/error') };
302 $::auth->save_session;
303 $::auth->expire_sessions;
310 Form::disconnect_standard_dbh;
312 $::lxdebug->end_request;
314 $self->_watch_for_changed_files;
316 $::lxdebug->leave_sub;
319 sub redirect_to_login {
320 my ($self, $script) = @_;
321 my $action = $script =~ m/^admin/i ? 'Admin/login' : 'LoginScreen/user_login&error=session';
322 print $::request->cgi->redirect("controller.pl?action=${action}");
326 sub unrequire_bin_mozilla {
328 return unless $self->_interface_is_fcgi;
331 next unless m#^bin/mozilla/#;
332 next if /\bcommon.pl$/;
333 next if /\binstallationcheck.pl$/;
338 sub _interface_is_fcgi {
340 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
344 my $script_name = shift;
346 return $script_name =~ m/dispatcher\.pl$/ ? (type => 'old', _route_dispatcher_request())
347 : $script_name =~ m/controller\.pl/ ? (type => 'controller', _route_controller_request())
348 : (type => 'old', controller => $script_name, action => $::form->{action});
351 sub _route_dispatcher_request {
352 my $name_re = qr{[a-z]\w*};
353 my ($script_name, $action);
356 die "Unroutable request -- invalid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
357 $script_name = $::form->{M} . '.pl';
360 $action = $::form->{A};
363 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
364 die "Unroutable request -- invalid action name.\n" if !$action;
366 delete $::form->{$action};
367 $action = substr $action, 2;
370 delete @{$::form}{qw(M A)};
374 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
375 show_error('generic/error');
378 return (controller => $script_name, action => $action);
381 sub _route_controller_request {
382 my ($controller, $action, $request_type);
385 $::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";
386 ($controller, $action) = ($1, $2);
387 delete $::form->{action};
389 $request_type = $3 ? lc(substr($3, 1)) : 'html';
393 $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
394 show_error('generic/error');
397 return (controller => $controller, action => $action, request_type => $request_type);
400 sub _cache_file_modification_times {
403 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
409 return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
410 $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
413 my $cwd = POSIX::getcwd();
414 File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
415 map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
418 sub _watch_for_changed_files {
421 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
423 my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
425 $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
429 sub get_standard_filehandles {
432 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
435 sub _check_for_old_config_files {
436 my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
437 return unless @old_files;
439 $::form->{title} = $::locale->text('Old configuration files');
441 print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
451 die SL::Dispatcher->END_OF_REQUEST;