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.
 
   8   push    @INC, "SL";               # FCGI won't find modules that are not properly named. Help it by inclduging SL
 
  11 use CGI qw( -no_xhtml);
 
  15 use English qw(-no_match_vars);
 
  20 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');
 
  38 sub pre_request_checks {
 
  39   if (!$::auth->session_tables_present) {
 
  40     if ($::form->{script} eq 'admin.pl') {
 
  44       show_error('login/auth_db_unreachable');
 
  47   $::auth->expire_sessions;
 
  51   $::lxdebug->enter_sub;
 
  53   my $error_type           = shift || '';
 
  55   $::locale                = Locale->new($::language);
 
  56   $::form->{error}         = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
 
  57   $::form->{error}         = $::locale->text('Incorrect password!.')                   if ($error_type eq 'password');
 
  58   $::myconfig{countrycode} = $::language;
 
  59   $::form->{stylesheet}    = 'css/lx-office-erp.css';
 
  62   print $::form->parse_html_template($template);
 
  63   $::lxdebug->leave_sub;
 
  68 sub pre_startup_setup {
 
  71     require "config/lx-erp.conf";
 
  75     require "config/lx-erp-local.conf";
 
  76   } if -f "config/lx-erp-local.conf";
 
  80     require "bin/mozilla/common.pl";
 
  81     require "bin/mozilla/installationcheck.pl";
 
  84   # canonial globals. if it's not here, chances are it will get refactored someday.
 
  87     $::userspath   = "users";
 
  88     $::templates   = "templates";
 
  89     $::memberfile  = "users/members";
 
  90     $::menufile    = "menu.ini";
 
  91     $::sendmail    = "| /usr/sbin/sendmail -t";
 
  92     $::lxdebug     = LXDebug->new;
 
  93     $::auth        = SL::Auth->new;
 
  96     %::called_subs = (); # currently used for recursion detection
 
  98     read_config 'config/lx_office.conf' => %::lx_office_conf if -f "config/lx_office.conf";
 
  99     _decode_recursively(\%::lx_office_conf);
 
 102   $SIG{__WARN__} = sub {
 
 103     $::lxdebug->warn(@_);
 
 107 sub pre_startup_checks {
 
 108   ::verify_installation();
 
 113   pre_startup_checks();
 
 116 sub require_main_code {
 
 117   $::lxdebug->enter_sub;
 
 118   my ($script, $suffix) = @_;
 
 122     require "bin/mozilla/$script$suffix";
 
 123   } or die $EVAL_ERROR;
 
 125   if (-f "bin/mozilla/custom_$script$suffix") {
 
 128       require "bin/mozilla/custom_$script$suffix";
 
 130     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
 
 132   if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
 
 135       require "bin/mozilla/$::form->{login}_$script";
 
 137     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
 
 139   $::lxdebug->leave_sub;
 
 142 sub _require_controller {
 
 143   my $controller =  shift;
 
 144   $controller    =~ s|[^A-Za-z0-9_]||g;
 
 148     require "SL/Controller/${controller}.pm";
 
 149   } or die $EVAL_ERROR;
 
 152 sub _run_controller {
 
 153   "SL::Controller::$_[0]"->new->_run_action($_[1]);
 
 158   $self->{request} = shift;
 
 160   $::lxdebug->enter_sub;
 
 161   $::lxdebug->begin_request;
 
 163   my ($script, $path, $suffix, $script_name, $action, $routing_type);
 
 165   $script_name = $ENV{SCRIPT_NAME};
 
 167   $self->unrequire_bin_mozilla;
 
 169   $::cgi         = CGI->new('');
 
 170   $::locale      = Locale->new($::language);
 
 174   eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
 
 176   if ($routing_type eq 'old') {
 
 177     $::form->{action}  =  lc $::form->{action};
 
 178     $::form->{action}  =~ s/( |-|,|\#)/_/g;
 
 180    ($script, $path, $suffix) = fileparse($script_name, ".pl");
 
 181     require_main_code($script, $suffix);
 
 183     $::form->{script} = $script . $suffix;
 
 186     _require_controller($script_name);
 
 187     $::form->{script} = "controller.pl";
 
 190   pre_request_checks();
 
 193     my $session_result = $::auth->restore_session;
 
 194     $::auth->create_or_refresh_session;
 
 196     $::form->error($::locale->text('System currently down for maintenance!')) if -e "$::userspath/nologin" && $script ne 'admin';
 
 198     if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
 
 199       $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
 
 200       ::run($session_result);
 
 203       show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
 
 204       %::myconfig = $::auth->read_user($::form->{login});
 
 206       show_error('login/password_error', 'password') unless $::myconfig{login};
 
 208       $::locale = Locale->new($::myconfig{countrycode});
 
 210       show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
 
 212       $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
 
 213       $::auth->create_or_refresh_session;
 
 214       $::auth->delete_session_value('FLASH')->save_session();
 
 215       delete $::form->{password};
 
 218         map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
 
 219           unless $action eq 'save' && $::form->{type} eq 'preferences';
 
 221         $::form->set_standard_title;
 
 222         if ($routing_type eq 'old') {
 
 223           ::call_sub('::' . $::locale->findsub($action));
 
 225           _run_controller($script_name, $action);
 
 228         $::form->error($::locale->text('action= not defined!'));
 
 234     if ($EVAL_ERROR ne END_OF_REQUEST) {
 
 235       $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
 
 236       eval { show_error('generic/error') };
 
 244   Form::disconnect_standard_dbh unless $self->_interface_is_fcgi;
 
 246   $::lxdebug->end_request;
 
 247   $::lxdebug->leave_sub;
 
 250 sub unrequire_bin_mozilla {
 
 252   return unless $self->_interface_is_fcgi;
 
 255     next unless m#^bin/mozilla/#;
 
 256     next if /\bcommon.pl$/;
 
 257     next if /\binstallationcheck.pl$/;
 
 262 sub _interface_is_fcgi {
 
 264   return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
 
 268   my $script_name = shift;
 
 270   return $script_name =~ m/dispatcher\.pl$/ ? ('old',        _route_dispatcher_request())
 
 271        : $script_name =~ m/controller\.pl/  ? ('controller', _route_controller_request())
 
 272        :                                      ('old',        $script_name, $::form->{action});
 
 275 sub _route_dispatcher_request {
 
 276   my $name_re = qr{[a-z]\w*};
 
 277   my ($script_name, $action);
 
 280     die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
 
 281     $script_name = $::form->{M} . '.pl';
 
 284       $action = $::form->{A};
 
 287       $action = first { m/^A_${name_re}$/ } keys %{ $::form };
 
 288       die "Unroutable request -- inavlid action name.\n" if !$action;
 
 290       delete $::form->{$action};
 
 291       $action = substr $action, 2;
 
 294     delete @{$::form}{qw(M A)};
 
 298     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
 
 299     show_error('generic/error');
 
 302   return ($script_name, $action);
 
 305 sub _route_controller_request {
 
 306   my ($controller, $action);
 
 309     $::form->{action}      =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
 
 310     ($controller, $action) =  ($1, $2);
 
 311     delete $::form->{action};
 
 315     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
 
 316     show_error('generic/error');
 
 319   return ($controller, $action);
 
 322 sub get_standard_filehandles {
 
 325   return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
 
 328 sub _decode_recursively {
 
 331   while (my ($key, $value) = each %{ $obj }) {
 
 332     if (ref($value) eq 'HASH') {
 
 333       _decode_recursively($value);
 
 335       $obj->{$key} = decode('UTF-8', $value);
 
 345   die SL::Dispatcher->END_OF_REQUEST;