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;
 
  19 use CGI qw( -no_xhtml);
 
  23 use English qw(-no_match_vars);
 
  25 use List::MoreUtils qw(all);
 
  26 use List::Util qw(first);
 
  29 use SL::Dispatcher::AuthHandler;
 
  35 use SL::Helper::DateTime;
 
  36 use SL::InstanceConfiguration;
 
  37 use SL::Template::Plugin::HTMLFixes;
 
  39 # Trailing new line is added so that Perl will not add the line
 
  40 # number 'die' was called in.
 
  41 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
 
  46   my ($class, $interface) = @_;
 
  48   my $self           = bless {}, $class;
 
  49   $self->{interface} = lc($interface || 'cgi');
 
  50   $self->{auth_handler} = SL::Dispatcher::AuthHandler->new;
 
  57   return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
 
  60 sub pre_request_checks {
 
  61   _check_for_old_config_files();
 
  63   if (!$::auth->session_tables_present) {
 
  64     if ($::form->{script} eq 'admin.pl') {
 
  68       show_error('login_screen/auth_db_unreachable');
 
  74   $::lxdebug->enter_sub;
 
  76   my $error_type           = shift || '';
 
  79   $::myconfig{countrycode} = delete($params{countrycode}) || $::lx_office_conf{system}->{language};
 
  80   $::locale                = Locale->new($::myconfig{countrycode});
 
  81   $::form->{error}         = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
 
  82   $::form->{error}         = $::locale->text('Incorrect password!')                    if ($error_type eq 'password');
 
  85   print $::form->parse_html_template($template, \%params);
 
  86   $::lxdebug->leave_sub;
 
  91 sub pre_startup_setup {
 
  94   SL::LxOfficeConf->read;
 
  98     require "bin/mozilla/common.pl";
 
  99     require "bin/mozilla/installationcheck.pl";
 
 100   } or die $EVAL_ERROR;
 
 102   # canonial globals. if it's not here, chances are it will get refactored someday.
 
 105     $::lxdebug     = LXDebug->new;
 
 106     $::auth        = SL::Auth->new;
 
 112   $SIG{__WARN__} = sub {
 
 113     $::lxdebug->warn(@_);
 
 116   $self->_cache_file_modification_times;
 
 119 sub pre_startup_checks {
 
 120   ::verify_installation();
 
 125   $self->pre_startup_setup;
 
 126   $self->pre_startup_checks;
 
 129 sub require_main_code {
 
 130   $::lxdebug->enter_sub;
 
 131   my ($script, $suffix) = @_;
 
 135     require "bin/mozilla/$script$suffix";
 
 136   } or die $EVAL_ERROR;
 
 138   if (-f "bin/mozilla/custom_$script$suffix") {
 
 141       require "bin/mozilla/custom_$script$suffix";
 
 143     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
 
 145   if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
 
 148       require "bin/mozilla/$::form->{login}_$script";
 
 150     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
 
 152   $::lxdebug->leave_sub;
 
 155 sub _require_controller {
 
 156   my $controller =  shift;
 
 157   $controller    =~ s|[^A-Za-z0-9_]||g;
 
 158   $controller    =  "SL/Controller/${controller}";
 
 162     require "${controller}.pm";
 
 163   } or die $EVAL_ERROR;
 
 166 sub _run_controller {
 
 167   "SL::Controller::$_[0]"->new->_run_action($_[1]);
 
 172   $self->{request} = shift;
 
 174   $::lxdebug->enter_sub;
 
 175   $::lxdebug->begin_request;
 
 177   my ($script, $path, $suffix, $script_name, $action, $routing_type);
 
 179   $self->unrequire_bin_mozilla;
 
 181   $::locale        = Locale->new($::lx_office_conf{system}->{language});
 
 183   $::instance_conf = SL::InstanceConfiguration->new;
 
 184   $::request       = { cgi => CGI->new({}) };
 
 186   my $session_result = $::auth->restore_session;
 
 187   $::auth->create_or_refresh_session;
 
 189   $::form->read_cgi_input;
 
 191   eval { ($routing_type, $script_name, $action) = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
 
 193   if ($routing_type eq 'old') {
 
 194     $::form->{action}  =  lc $::form->{action};
 
 195     $::form->{action}  =~ s/( |-|,|\#)/_/g;
 
 197    ($script, $path, $suffix) = fileparse($script_name, ".pl");
 
 198     require_main_code($script, $suffix);
 
 200     $::form->{script} = $script . $suffix;
 
 203     _require_controller($script_name);
 
 204     $::form->{script} = "controller.pl";
 
 208     pre_request_checks();
 
 210     $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
 
 212     # For compatibility with a lot of database upgrade scripts etc:
 
 213     # Re-write request to old 'login.pl?action=login' to new
 
 214     # 'LoginScreen' controller. Make sure to load its code!
 
 215     if (($script eq 'login') && ($action eq 'login')) {
 
 216       ($routing_type, $script, $script_name, $action) = qw(controller controller LoginScreen login);
 
 217       _require_controller('LoginScreen');
 
 220     if (($script eq 'login') && !$action) {
 
 221       print $::request->{cgi}->redirect('controller.pl?action=LoginScreen/user_login');
 
 223     } elsif ($script eq 'admin') {
 
 224       $::form->{titlebar} = "kivitendo " . $::locale->text('Version') . " $::form->{version}";
 
 225       ::run($session_result);
 
 228       show_error('login_screen/user_login', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
 
 230       my %auth_result = $self->{auth_handler}->handle(
 
 231         routing_type => $routing_type,
 
 233         controller   => $script_name,
 
 237       delete @{ $::form }{ grep { m/^\{AUTH\}/ } keys %{ $::form } } unless $auth_result{keep_auth_vars};
 
 240         $::instance_conf->init if $auth_result{auth_level} eq 'user';
 
 242         map { $::form->{$_} = $::myconfig{$_} } qw(charset)
 
 243           unless $action eq 'save' && $::form->{type} eq 'preferences';
 
 245         $::form->set_standard_title;
 
 246         if ($routing_type eq 'old') {
 
 247           ::call_sub('::' . $::locale->findsub($action));
 
 249           _run_controller($script_name, $action);
 
 252         $::form->error($::locale->text('action= not defined!'));
 
 258     if ($EVAL_ERROR ne END_OF_REQUEST) {
 
 259       print STDERR $EVAL_ERROR;
 
 260       $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
 
 261       chdir SL::System::Process::exe_dir;
 
 262       eval { show_error('generic/error') };
 
 267   $::auth->save_session;
 
 268   $::auth->expire_sessions;
 
 275   Form::disconnect_standard_dbh;
 
 277   $::lxdebug->end_request;
 
 279   $self->_watch_for_changed_files;
 
 281   $::lxdebug->leave_sub;
 
 284 sub unrequire_bin_mozilla {
 
 286   return unless $self->_interface_is_fcgi;
 
 289     next unless m#^bin/mozilla/#;
 
 290     next if /\bcommon.pl$/;
 
 291     next if /\binstallationcheck.pl$/;
 
 296 sub _interface_is_fcgi {
 
 298   return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
 
 302   my $script_name = shift;
 
 304   return $script_name =~ m/dispatcher\.pl$/ ? ('old',        _route_dispatcher_request())
 
 305        : $script_name =~ m/controller\.pl/  ? ('controller', _route_controller_request())
 
 306        :                                      ('old',        $script_name, $::form->{action});
 
 309 sub _route_dispatcher_request {
 
 310   my $name_re = qr{[a-z]\w*};
 
 311   my ($script_name, $action);
 
 314     die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
 
 315     $script_name = $::form->{M} . '.pl';
 
 318       $action = $::form->{A};
 
 321       $action = first { m/^A_${name_re}$/ } keys %{ $::form };
 
 322       die "Unroutable request -- inavlid action name.\n" if !$action;
 
 324       delete $::form->{$action};
 
 325       $action = substr $action, 2;
 
 328     delete @{$::form}{qw(M A)};
 
 332     $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
 
 333     show_error('generic/error');
 
 336   return ($script_name, $action);
 
 339 sub _route_controller_request {
 
 340   my ($controller, $action);
 
 343     $::form->{action}      =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
 
 344     ($controller, $action) =  ($1, $2);
 
 345     delete $::form->{action};
 
 349     $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
 
 350     show_error('generic/error');
 
 353   return ($controller, $action);
 
 356 sub _cache_file_modification_times {
 
 359   return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
 
 365     return unless $File::Find::name =~ m/\.(?:pm|f?pl|html|conf|conf\.default)$/;
 
 366     $fcgi_file_cache{ $File::Find::name } = (stat $File::Find::name)[9];
 
 369   my $cwd = POSIX::getcwd();
 
 370   File::Find::find($wanted, map { "${cwd}/${_}" } qw(config bin SL templates/webpages));
 
 371   map { my $name = "${cwd}/${_}"; $fcgi_file_cache{$name} = (stat $name)[9] } qw(admin.pl dispatcher.fpl);
 
 374 sub _watch_for_changed_files {
 
 377   return unless $self->_interface_is_fcgi && $::lx_office_conf{debug}->{restart_fcgi_process_on_changes};
 
 379   my $ok = all { (stat($_))[9] == $fcgi_file_cache{$_} } keys %fcgi_file_cache;
 
 381   $::lxdebug->message(LXDebug::DEBUG1(), "Program modifications detected. Restarting.");
 
 385 sub get_standard_filehandles {
 
 388   return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
 
 391 sub _check_for_old_config_files {
 
 392   my @old_files = grep { -f "config/${_}" } qw(authentication.pl console.conf lx-erp.conf lx-erp-local.conf);
 
 393   return unless @old_files;
 
 395   $::form->{title} = $::locale->text('Old configuration files');
 
 397   print $::form->parse_html_template('login_screen/old_configuration_files', { FILES => \@old_files });
 
 407   die SL::Dispatcher->END_OF_REQUEST;