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);
 
  21 use SL::Helper::DateTime;
 
  22 use List::Util qw(first);
 
  25 # Trailing new line is added so that Perl will not add the line
 
  26 # number 'die' was called in.
 
  27 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
 
  30   my ($class, $interface) = @_;
 
  32   my $self           = bless {}, $class;
 
  33   $self->{interface} = lc($interface || 'cgi');
 
  40   return $self->{interface} eq 'cgi' ? 'CGI' : 'FastCGI';
 
  43 sub pre_request_checks {
 
  44   if (!$::auth->session_tables_present) {
 
  45     if ($::form->{script} eq 'admin.pl') {
 
  49       show_error('login/auth_db_unreachable');
 
  52   $::auth->expire_sessions;
 
  56   $::lxdebug->enter_sub;
 
  58   my $error_type           = shift || '';
 
  60   $::locale                = Locale->new($::lx_office_conf{system}->{language});
 
  61   $::form->{error}         = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
 
  62   $::form->{error}         = $::locale->text('Incorrect password!.')                   if ($error_type eq 'password');
 
  63   $::myconfig{countrycode} = $::lx_office_conf{system}->{language};
 
  64   $::form->{stylesheet}    = 'css/lx-office-erp.css';
 
  67   print $::form->parse_html_template($template);
 
  68   $::lxdebug->leave_sub;
 
  73 sub pre_startup_setup {
 
  74   SL::LxOfficeConf->read;
 
  79     require "bin/mozilla/common.pl";
 
  80     require "bin/mozilla/installationcheck.pl";
 
  83   # canonial globals. if it's not here, chances are it will get refactored someday.
 
  86     $::lxdebug     = LXDebug->new;
 
  87     $::auth        = SL::Auth->new;
 
  90     %::called_subs = (); # currently used for recursion detection
 
  93   $SIG{__WARN__} = sub {
 
  98 sub pre_startup_checks {
 
  99   ::verify_installation();
 
 104   pre_startup_checks();
 
 107 sub require_main_code {
 
 108   $::lxdebug->enter_sub;
 
 109   my ($script, $suffix) = @_;
 
 113     require "bin/mozilla/$script$suffix";
 
 114   } or die $EVAL_ERROR;
 
 116   if (-f "bin/mozilla/custom_$script$suffix") {
 
 119       require "bin/mozilla/custom_$script$suffix";
 
 121     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
 
 123   if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
 
 126       require "bin/mozilla/$::form->{login}_$script";
 
 128     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
 
 130   $::lxdebug->leave_sub;
 
 133 sub _require_controller {
 
 134   my $controller =  shift;
 
 135   $controller    =~ s|[^A-Za-z0-9_]||g;
 
 139     require "SL/Controller/${controller}.pm";
 
 140   } or die $EVAL_ERROR;
 
 143 sub _run_controller {
 
 144   "SL::Controller::$_[0]"->new->_run_action($_[1]);
 
 149   $self->{request} = shift;
 
 151   $::lxdebug->enter_sub;
 
 152   $::lxdebug->begin_request;
 
 154   my ($script, $path, $suffix, $script_name, $action, $routing_type);
 
 156   $script_name = $ENV{SCRIPT_NAME};
 
 158   $self->unrequire_bin_mozilla;
 
 160   $::cgi         = CGI->new('');
 
 161   $::locale      = Locale->new($::lx_office_conf{system}->{language});
 
 165   eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
 
 167   if ($routing_type eq 'old') {
 
 168     $::form->{action}  =  lc $::form->{action};
 
 169     $::form->{action}  =~ s/( |-|,|\#)/_/g;
 
 171    ($script, $path, $suffix) = fileparse($script_name, ".pl");
 
 172     require_main_code($script, $suffix);
 
 174     $::form->{script} = $script . $suffix;
 
 177     _require_controller($script_name);
 
 178     $::form->{script} = "controller.pl";
 
 181   pre_request_checks();
 
 184     my $session_result = $::auth->restore_session;
 
 185     $::auth->create_or_refresh_session;
 
 187     $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
 
 189     if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
 
 190       $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
 
 191       ::run($session_result);
 
 194       show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
 
 195       %::myconfig = $::auth->read_user($::form->{login});
 
 197       show_error('login/password_error', 'password') unless $::myconfig{login};
 
 199       $::locale = Locale->new($::myconfig{countrycode});
 
 201       show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
 
 203       $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
 
 204       $::auth->create_or_refresh_session;
 
 205       $::auth->delete_session_value('FLASH')->save_session();
 
 206       delete $::form->{password};
 
 209         map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
 
 210           unless $action eq 'save' && $::form->{type} eq 'preferences';
 
 212         $::form->set_standard_title;
 
 213         if ($routing_type eq 'old') {
 
 214           ::call_sub('::' . $::locale->findsub($action));
 
 216           _run_controller($script_name, $action);
 
 219         $::form->error($::locale->text('action= not defined!'));
 
 225     if ($EVAL_ERROR ne END_OF_REQUEST) {
 
 226       $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
 
 227       eval { show_error('generic/error') };
 
 235   Form::disconnect_standard_dbh unless $self->_interface_is_fcgi;
 
 237   $::lxdebug->end_request;
 
 238   $::lxdebug->leave_sub;
 
 241 sub unrequire_bin_mozilla {
 
 243   return unless $self->_interface_is_fcgi;
 
 246     next unless m#^bin/mozilla/#;
 
 247     next if /\bcommon.pl$/;
 
 248     next if /\binstallationcheck.pl$/;
 
 253 sub _interface_is_fcgi {
 
 255   return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
 
 259   my $script_name = shift;
 
 261   return $script_name =~ m/dispatcher\.pl$/ ? ('old',        _route_dispatcher_request())
 
 262        : $script_name =~ m/controller\.pl/  ? ('controller', _route_controller_request())
 
 263        :                                      ('old',        $script_name, $::form->{action});
 
 266 sub _route_dispatcher_request {
 
 267   my $name_re = qr{[a-z]\w*};
 
 268   my ($script_name, $action);
 
 271     die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
 
 272     $script_name = $::form->{M} . '.pl';
 
 275       $action = $::form->{A};
 
 278       $action = first { m/^A_${name_re}$/ } keys %{ $::form };
 
 279       die "Unroutable request -- inavlid action name.\n" if !$action;
 
 281       delete $::form->{$action};
 
 282       $action = substr $action, 2;
 
 285     delete @{$::form}{qw(M A)};
 
 289     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
 
 290     show_error('generic/error');
 
 293   return ($script_name, $action);
 
 296 sub _route_controller_request {
 
 297   my ($controller, $action);
 
 300     $::form->{action}      =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
 
 301     ($controller, $action) =  ($1, $2);
 
 302     delete $::form->{action};
 
 306     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
 
 307     show_error('generic/error');
 
 310   return ($controller, $action);
 
 313 sub get_standard_filehandles {
 
 316   return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
 
 319 sub _init_environment {
 
 320   my %key_map = ( lib  => { name => 'PERL5LIB', append_path => 1 },
 
 321                   path => { name => 'PATH',     append_path => 1 },
 
 323   my $cfg     = $::lx_office_conf{environment} || {};
 
 325   while (my ($key, $value) = each %{ $cfg }) {
 
 328     my $info = $key_map{$key} || {};
 
 329     $key     = $info->{name}  || $key;
 
 331     if ($info->{append_path}) {
 
 332       $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
 
 333       $value = $ENV{$key} . $value;
 
 345   die SL::Dispatcher->END_OF_REQUEST;