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);
21 use SL::Helper::DateTime;
23 use List::Util qw(first);
26 # Trailing new line is added so that Perl will not add the line
27 # number 'die' was called in.
28 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
31 my ($class, $interface) = @_;
33 my $self = bless {}, $class;
34 $self->{interface} = lc($interface || 'cgi');
39 sub pre_request_checks {
40 if (!$::auth->session_tables_present) {
41 if ($::form->{script} eq 'admin.pl') {
45 show_error('login/auth_db_unreachable');
48 $::auth->expire_sessions;
52 $::lxdebug->enter_sub;
54 my $error_type = shift || '';
56 $::locale = Locale->new($::lx_office_conf{system}->{language});
57 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
58 $::form->{error} = $::locale->text('Incorrect password!.') if ($error_type eq 'password');
59 $::myconfig{countrycode} = $::lx_office_conf{system}->{language};
60 $::form->{stylesheet} = 'css/lx-office-erp.css';
63 print $::form->parse_html_template($template);
64 $::lxdebug->leave_sub;
69 sub pre_startup_setup {
70 SL::LxOfficeConf->read;
75 require "bin/mozilla/common.pl";
76 require "bin/mozilla/installationcheck.pl";
79 # canonial globals. if it's not here, chances are it will get refactored someday.
82 $::lxdebug = LXDebug->new;
83 $::auth = SL::Auth->new;
86 %::called_subs = (); # currently used for recursion detection
89 $SIG{__WARN__} = sub {
94 sub pre_startup_checks {
95 ::verify_installation();
100 pre_startup_checks();
103 sub require_main_code {
104 $::lxdebug->enter_sub;
105 my ($script, $suffix) = @_;
109 require "bin/mozilla/$script$suffix";
110 } or die $EVAL_ERROR;
112 if (-f "bin/mozilla/custom_$script$suffix") {
115 require "bin/mozilla/custom_$script$suffix";
117 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
119 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
122 require "bin/mozilla/$::form->{login}_$script";
124 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
126 $::lxdebug->leave_sub;
129 sub _require_controller {
130 my $controller = shift;
131 $controller =~ s|[^A-Za-z0-9_]||g;
135 require "SL/Controller/${controller}.pm";
136 } or die $EVAL_ERROR;
139 sub _run_controller {
140 "SL::Controller::$_[0]"->new->_run_action($_[1]);
145 $self->{request} = shift;
147 $::lxdebug->enter_sub;
148 $::lxdebug->begin_request;
150 my ($script, $path, $suffix, $script_name, $action, $routing_type);
152 $script_name = $ENV{SCRIPT_NAME};
154 $self->unrequire_bin_mozilla;
156 $::cgi = CGI->new('');
157 $::locale = Locale->new($::lx_office_conf{system}->{language});
161 eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
163 if ($routing_type eq 'old') {
164 $::form->{action} = lc $::form->{action};
165 $::form->{action} =~ s/( |-|,|\#)/_/g;
167 ($script, $path, $suffix) = fileparse($script_name, ".pl");
168 require_main_code($script, $suffix);
170 $::form->{script} = $script . $suffix;
173 _require_controller($script_name);
174 $::form->{script} = "controller.pl";
177 pre_request_checks();
180 my $session_result = $::auth->restore_session;
181 $::auth->create_or_refresh_session;
183 $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
185 if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
186 $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
187 ::run($session_result);
190 show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
191 %::myconfig = $::auth->read_user($::form->{login});
193 show_error('login/password_error', 'password') unless $::myconfig{login};
195 $::locale = Locale->new($::myconfig{countrycode});
197 show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
199 $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
200 $::auth->create_or_refresh_session;
201 $::auth->delete_session_value('FLASH')->save_session();
202 delete $::form->{password};
205 map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
206 unless $action eq 'save' && $::form->{type} eq 'preferences';
208 $::form->set_standard_title;
209 if ($routing_type eq 'old') {
210 ::call_sub('::' . $::locale->findsub($action));
212 _run_controller($script_name, $action);
215 $::form->error($::locale->text('action= not defined!'));
221 if ($EVAL_ERROR ne END_OF_REQUEST) {
222 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
223 eval { show_error('generic/error') };
231 Form::disconnect_standard_dbh unless $self->_interface_is_fcgi;
233 $::lxdebug->end_request;
234 $::lxdebug->leave_sub;
237 sub unrequire_bin_mozilla {
239 return unless $self->_interface_is_fcgi;
242 next unless m#^bin/mozilla/#;
243 next if /\bcommon.pl$/;
244 next if /\binstallationcheck.pl$/;
249 sub _interface_is_fcgi {
251 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
255 my $script_name = shift;
257 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
258 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
259 : ('old', $script_name, $::form->{action});
262 sub _route_dispatcher_request {
263 my $name_re = qr{[a-z]\w*};
264 my ($script_name, $action);
267 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
268 $script_name = $::form->{M} . '.pl';
271 $action = $::form->{A};
274 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
275 die "Unroutable request -- inavlid action name.\n" if !$action;
277 delete $::form->{$action};
278 $action = substr $action, 2;
281 delete @{$::form}{qw(M A)};
285 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
286 show_error('generic/error');
289 return ($script_name, $action);
292 sub _route_controller_request {
293 my ($controller, $action);
296 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
297 ($controller, $action) = ($1, $2);
298 delete $::form->{action};
302 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
303 show_error('generic/error');
306 return ($controller, $action);
309 sub get_standard_filehandles {
312 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
315 sub _init_environment {
316 my %key_map = ( lib => { name => 'PERL5LIB', append_path => 1 },
317 path => { name => 'PATH', append_path => 1 },
319 my $cfg = $::lx_office_conf{environment} || {};
321 while (my ($key, $value) = each %{ $cfg }) {
324 my $info = $key_map{$key} || {};
325 $key = $info->{name} || $key;
327 if ($info->{append_path}) {
328 $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
329 $value = $ENV{$key} . $value;
341 die SL::Dispatcher->END_OF_REQUEST;