1 package SL::Dispatcher;
6 unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
7 push @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
10 use CGI qw( -no_xhtml);
14 use English qw(-no_match_vars);
16 use List::MoreUtils qw(all);
17 use List::Util qw(first);
25 use SL::Helper::DateTime;
26 use SL::InstanceConfiguration;
27 use SL::Template::Plugin::HTMLFixes;
29 # Trailing new line is added so that Perl will not add the line
30 # number 'die' was called in.
31 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
36 my ($class, $interface) = @_;
38 my $self = bless {}, $class;
39 $self->{interface} = lc($interface || 'cgi');
46 return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
49 sub pre_request_checks {
50 _check_for_old_config_files();
52 if (!$::auth->session_tables_present) {
53 if ($::form->{script} eq 'admin.pl') {
57 show_error('login/auth_db_unreachable');
63 $::lxdebug->enter_sub;
65 my $error_type = shift || '';
67 $::locale = Locale->new($::lx_office_conf{system}->{language});
68 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
69 $::form->{error} = $::locale->text('Incorrect password!.') if ($error_type eq 'password');
70 $::myconfig{countrycode} = $::lx_office_conf{system}->{language};
71 $::form->{stylesheet} = 'css/lx-office-erp.css';
74 print $::form->parse_html_template($template);
75 $::lxdebug->leave_sub;
80 sub pre_startup_setup {
83 SL::LxOfficeConf->read;
88 require "bin/mozilla/common.pl";
89 require "bin/mozilla/installationcheck.pl";
92 # canonial globals. if it's not here, chances are it will get refactored someday.
95 $::lxdebug = LXDebug->new;
96 $::auth = SL::Auth->new;
99 %::called_subs = (); # currently used for recursion detection
102 $SIG{__WARN__} = sub {
103 $::lxdebug->warn(@_);
106 $self->_cache_file_modification_times;
109 sub pre_startup_checks {
110 ::verify_installation();
115 $self->pre_startup_setup;
116 $self->pre_startup_checks;
119 sub require_main_code {
120 $::lxdebug->enter_sub;
121 my ($script, $suffix) = @_;
125 require "bin/mozilla/$script$suffix";
126 } or die $EVAL_ERROR;
128 if (-f "bin/mozilla/custom_$script$suffix") {
131 require "bin/mozilla/custom_$script$suffix";
133 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
135 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
138 require "bin/mozilla/$::form->{login}_$script";
140 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
142 $::lxdebug->leave_sub;
145 sub _require_controller {
146 my $controller = shift;
147 $controller =~ s|[^A-Za-z0-9_]||g;
151 require "SL/Controller/${controller}.pm";
152 } or die $EVAL_ERROR;
155 sub _run_controller {
156 "SL::Controller::$_[0]"->new->_run_action($_[1]);
161 $self->{request} = shift;
163 $::lxdebug->enter_sub;
164 $::lxdebug->begin_request;
166 my ($script, $path, $suffix, $script_name, $action, $routing_type);
168 $script_name = $ENV{SCRIPT_NAME};
170 $self->unrequire_bin_mozilla;
172 $::cgi = CGI->new('');
173 $::locale = Locale->new($::lx_office_conf{system}->{language});
176 $::instance_conf = SL::InstanceConfiguration->new;
178 my $session_result = $::auth->restore_session;
179 $::auth->create_or_refresh_session;
181 $::form->read_cgi_input;
183 eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
185 if ($routing_type eq 'old') {
186 $::form->{action} = lc $::form->{action};
187 $::form->{action} =~ s/( |-|,|\#)/_/g;
189 ($script, $path, $suffix) = fileparse($script_name, ".pl");
190 require_main_code($script, $suffix);
192 $::form->{script} = $script . $suffix;
195 _require_controller($script_name);
196 $::form->{script} = "controller.pl";
200 pre_request_checks();
202 $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
204 if ($script eq 'login' or $script eq 'admin') {
205 $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
206 ::run($session_result);
209 show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
210 %::myconfig = $::auth->read_user($::form->{login});
212 show_error('login/password_error', 'password') unless $::myconfig{login};
214 $::locale = Locale->new($::myconfig{countrycode});
216 show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password});
218 $::auth->store_credentials_in_session(login => $::form->{login}, password => $::form->{password});
219 $::auth->create_or_refresh_session;
220 $::auth->delete_session_value('FLASH');
221 delete $::form->{password};
224 $::instance_conf->init;
226 map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
227 unless $action eq 'save' && $::form->{type} eq 'preferences';
229 $::form->set_standard_title;
230 if ($routing_type eq 'old') {
231 ::call_sub('::' . $::locale->findsub($action));
233 _run_controller($script_name, $action);
236 $::form->error($::locale->text('action= not defined!'));
242 if ($EVAL_ERROR ne END_OF_REQUEST) {
243 print STDERR $EVAL_ERROR;
244 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
245 eval { show_error('generic/error') };
250 $::auth->save_session;
251 $::auth->expire_sessions;
257 Form::disconnect_standard_dbh;
259 $::lxdebug->end_request;
261 $self->_watch_for_changed_files;
263 $::lxdebug->leave_sub;
266 sub unrequire_bin_mozilla {
268 return unless $self->_interface_is_fcgi;
271 next unless m#^bin/mozilla/#;
272 next if /\bcommon.pl$/;
273 next if /\binstallationcheck.pl$/;
278 sub _interface_is_fcgi {
280 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
284 my $script_name = shift;
286 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
287 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
288 : ('old', $script_name, $::form->{action});
291 sub _route_dispatcher_request {
292 my $name_re = qr{[a-z]\w*};
293 my ($script_name, $action);
296 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
297 $script_name = $::form->{M} . '.pl';
300 $action = $::form->{A};
303 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
304 die "Unroutable request -- inavlid action name.\n" if !$action;
306 delete $::form->{$action};
307 $action = substr $action, 2;
310 delete @{$::form}{qw(M A)};
314 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
315 show_error('generic/error');
318 return ($script_name, $action);
321 sub _route_controller_request {
322 my ($controller, $action);
325 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
326 ($controller, $action) = ($1, $2);
327 delete $::form->{action};
331 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
332 show_error('generic/error');
335 return ($controller, $action);
338 sub _cache_file_modification_times {
341 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
347 return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
348 $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
351 my $cwd = POSIX::getcwd();
352 File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
353 map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
356 sub _watch_for_changed_files {
359 return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
361 my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
363 $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
367 sub get_standard_filehandles {
370 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
373 sub _init_environment {
374 my %key_map = ( lib => { name => 'PERL5LIB', append_path => 1 },
375 path => { name => 'PATH', append_path => 1 },
377 my $cfg = $::lx_office_conf{environment} || {};
379 while (my ($key, $value) = each %{ $cfg }) {
382 my $info = $key_map{$key} || {};
383 $key = $info->{name} || $key;
385 if ($info->{append_path}) {
386 $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
387 $value = $ENV{$key} . $value;
394 sub _check_for_old_config_files {
395 my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
396 return unless @old_files;
398 $::form->{title} = $::locale->text('Old configuration files');
399 $::form->{stylesheet} = 'lx-office-erp.css';
401 print $::form->parse_html_template('login/old_configuration_files', { FILES => \@old_files });
411 die SL::Dispatcher->END_OF_REQUEST;