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($::lx_office_conf{system}->{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} = $::lx_office_conf{system}->{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";
78 read_config 'config/lx_office.conf' => %::lx_office_conf;
79 _decode_recursively(\%::lx_office_conf);
84 require "bin/mozilla/common.pl";
85 require "bin/mozilla/installationcheck.pl";
88 # canonial globals. if it's not here, chances are it will get refactored someday.
91 $::lxdebug = LXDebug->new;
92 $::auth = SL::Auth->new;
95 %::called_subs = (); # currently used for recursion detection
98 $SIG{__WARN__} = sub {
103 sub pre_startup_checks {
104 ::verify_installation();
109 pre_startup_checks();
112 sub require_main_code {
113 $::lxdebug->enter_sub;
114 my ($script, $suffix) = @_;
118 require "bin/mozilla/$script$suffix";
119 } or die $EVAL_ERROR;
121 if (-f "bin/mozilla/custom_$script$suffix") {
124 require "bin/mozilla/custom_$script$suffix";
126 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
128 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
131 require "bin/mozilla/$::form->{login}_$script";
133 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
135 $::lxdebug->leave_sub;
138 sub _require_controller {
139 my $controller = shift;
140 $controller =~ s|[^A-Za-z0-9_]||g;
144 require "SL/Controller/${controller}.pm";
145 } or die $EVAL_ERROR;
148 sub _run_controller {
149 "SL::Controller::$_[0]"->new->_run_action($_[1]);
154 $self->{request} = shift;
156 $::lxdebug->enter_sub;
157 $::lxdebug->begin_request;
159 my ($script, $path, $suffix, $script_name, $action, $routing_type);
161 $script_name = $ENV{SCRIPT_NAME};
163 $self->unrequire_bin_mozilla;
165 $::cgi = CGI->new('');
166 $::locale = Locale->new($::lx_office_conf{system}->{language});
170 eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
172 if ($routing_type eq 'old') {
173 $::form->{action} = lc $::form->{action};
174 $::form->{action} =~ s/( |-|,|\#)/_/g;
176 ($script, $path, $suffix) = fileparse($script_name, ".pl");
177 require_main_code($script, $suffix);
179 $::form->{script} = $script . $suffix;
182 _require_controller($script_name);
183 $::form->{script} = "controller.pl";
186 pre_request_checks();
189 my $session_result = $::auth->restore_session;
190 $::auth->create_or_refresh_session;
192 $::form->error($::locale->text('System currently down for maintenance!')) if -e ($::lx_office_conf{paths}->{userspath} . "/nologin") && $script ne 'admin';
194 if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
195 $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
196 ::run($session_result);
199 show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
200 %::myconfig = $::auth->read_user($::form->{login});
202 show_error('login/password_error', 'password') unless $::myconfig{login};
204 $::locale = Locale->new($::myconfig{countrycode});
206 show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
208 $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
209 $::auth->create_or_refresh_session;
210 $::auth->delete_session_value('FLASH')->save_session();
211 delete $::form->{password};
214 map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
215 unless $action eq 'save' && $::form->{type} eq 'preferences';
217 $::form->set_standard_title;
218 if ($routing_type eq 'old') {
219 ::call_sub('::' . $::locale->findsub($action));
221 _run_controller($script_name, $action);
224 $::form->error($::locale->text('action= not defined!'));
230 if ($EVAL_ERROR ne END_OF_REQUEST) {
231 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
232 eval { show_error('generic/error') };
240 Form::disconnect_standard_dbh unless $self->_interface_is_fcgi;
242 $::lxdebug->end_request;
243 $::lxdebug->leave_sub;
246 sub unrequire_bin_mozilla {
248 return unless $self->_interface_is_fcgi;
251 next unless m#^bin/mozilla/#;
252 next if /\bcommon.pl$/;
253 next if /\binstallationcheck.pl$/;
258 sub _interface_is_fcgi {
260 return $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
264 my $script_name = shift;
266 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
267 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
268 : ('old', $script_name, $::form->{action});
271 sub _route_dispatcher_request {
272 my $name_re = qr{[a-z]\w*};
273 my ($script_name, $action);
276 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
277 $script_name = $::form->{M} . '.pl';
280 $action = $::form->{A};
283 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
284 die "Unroutable request -- inavlid action name.\n" if !$action;
286 delete $::form->{$action};
287 $action = substr $action, 2;
290 delete @{$::form}{qw(M A)};
294 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
295 show_error('generic/error');
298 return ($script_name, $action);
301 sub _route_controller_request {
302 my ($controller, $action);
305 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
306 ($controller, $action) = ($1, $2);
307 delete $::form->{action};
311 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
312 show_error('generic/error');
315 return ($controller, $action);
318 sub get_standard_filehandles {
321 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
324 sub _decode_recursively {
327 while (my ($key, $value) = each %{ $obj }) {
328 if (ref($value) eq 'HASH') {
329 _decode_recursively($value);
331 $obj->{$key} = decode('UTF-8', $value);
336 sub _init_environment {
337 my %key_map = ( lib => { name => 'PERL5LIB', append_path => 1 },
338 path => { name => 'PATH', append_path => 1 },
340 my $cfg = $::lx_office_conf{environment} || {};
342 while (my ($key, $value) = each %{ $cfg }) {
345 my $info = $key_map{$key} || {};
346 $key = $info->{name} || $key;
348 if ($info->{append_path}) {
349 $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
350 $value = $ENV{$key} . $value;
362 die SL::Dispatcher->END_OF_REQUEST;