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);
12 use English qw(-no_match_vars);
18 use List::Util qw(first);
21 # Trailing new line is added so that Perl will not add the line
22 # number 'die' was called in.
23 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
25 sub pre_request_checks {
26 if (!$::auth->session_tables_present) {
27 if ($::form->{script} eq 'admin.pl') {
31 show_error('login/auth_db_unreachable');
34 $::auth->expire_sessions;
38 $::lxdebug->enter_sub;
40 my $error_type = shift || '';
42 $::locale = Locale->new($::language);
43 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
44 $::form->{error} = $::locale->text('Incorrect password!.') if ($error_type eq 'password');
45 $::myconfig{countrycode} = $::language;
46 $::form->{stylesheet} = 'css/lx-office-erp.css';
49 print $::form->parse_html_template($template);
50 $::lxdebug->leave_sub;
55 sub pre_startup_setup {
58 require "config/lx-erp.conf";
62 require "config/lx-erp-local.conf";
63 } if -f "config/lx-erp-local.conf";
67 require "bin/mozilla/common.pl";
68 require "bin/mozilla/installationcheck.pl";
71 # canonial globals. if it's not here, chances are it will get refactored someday.
74 $::userspath = "users";
75 $::templates = "templates";
76 $::memberfile = "users/members";
77 $::menufile = "menu.ini";
78 $::sendmail = "| /usr/sbin/sendmail -t";
79 $::lxdebug = LXDebug->new;
80 $::auth = SL::Auth->new;
83 %::called_subs = (); # currently used for recursion detection
86 $SIG{__WARN__} = sub {
91 sub pre_startup_checks {
92 ::verify_installation();
100 sub require_main_code {
101 $::lxdebug->enter_sub;
102 my ($script, $suffix) = @_;
106 require "bin/mozilla/$script$suffix";
107 } or die $EVAL_ERROR;
109 if (-f "bin/mozilla/custom_$script$suffix") {
112 require "bin/mozilla/custom_$script$suffix";
114 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
116 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
119 require "bin/mozilla/$::form->{login}_$script";
121 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
123 $::lxdebug->leave_sub;
126 sub _require_controller {
127 my $controller = shift;
128 $controller =~ s|[^A-Za-z0-9_]||g;
132 require "SL/Controller/${controller}.pm";
133 } or die $EVAL_ERROR;
136 sub _run_controller {
137 "SL::Controller::$_[0]"->new->_run_action($_[1]);
141 $::lxdebug->enter_sub;
142 $::lxdebug->begin_request;
144 my $interface = lc(shift || 'cgi');
145 my ($script, $path, $suffix, $script_name, $action, $routing_type);
147 $script_name = $ENV{SCRIPT_NAME};
149 unrequire_bin_mozilla($interface);
151 $::cgi = CGI->new('');
152 $::locale = Locale->new($::language);
156 eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
158 if ($routing_type eq 'old') {
159 $::form->{action} = lc $::form->{action};
160 $::form->{action} =~ s/( |-|,|\#)/_/g;
162 ($script, $path, $suffix) = fileparse($script_name, ".pl");
163 require_main_code($script, $suffix);
165 $::form->{script} = $script . $suffix;
168 _require_controller($script_name);
169 $::form->{script} = "controller.pl";
172 pre_request_checks();
175 my $session_result = $::auth->restore_session;
176 $::auth->create_or_refresh_session;
178 $::form->error($::locale->text('System currently down for maintenance!')) if -e "$::userspath/nologin" && $script ne 'admin';
180 if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
181 $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
182 ::run($session_result);
185 show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
186 %::myconfig = $::auth->read_user($::form->{login});
188 show_error('login/password_error', 'password') unless $::myconfig{login};
190 $::locale = Locale->new($::myconfig{countrycode});
192 show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
194 $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
195 $::auth->create_or_refresh_session;
196 $::auth->delete_session_value('FLASH')->save_session();
197 delete $::form->{password};
200 map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
201 unless $action eq 'save' && $::form->{type} eq 'preferences';
203 $::form->set_standard_title;
204 if ($routing_type eq 'old') {
205 ::call_sub('::' . $::locale->findsub($action));
207 _run_controller($script_name, $action);
210 $::form->error($::locale->text('action= not defined!'));
216 if ($EVAL_ERROR ne END_OF_REQUEST) {
217 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
218 eval { show_error('generic/error') };
226 Form::disconnect_standard_dbh();
228 $::lxdebug->end_request;
229 $::lxdebug->leave_sub;
232 sub unrequire_bin_mozilla {
233 return unless $_[0] =~ m/^(?:fastcgi|fcgid|fcgi)$/;
236 next unless m#^bin/mozilla/#;
237 next if /\bcommon.pl$/;
238 next if /\binstallationcheck.pl$/;
244 my $script_name = shift;
246 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
247 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
248 : ('old', $script_name, $::form->{action});
251 sub _route_dispatcher_request {
252 my $name_re = qr{[a-z]\w*};
253 my ($script_name, $action);
256 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
257 $script_name = $::form->{M} . '.pl';
260 $action = $::form->{A};
263 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
264 die "Unroutable request -- inavlid action name.\n" if !$action;
266 delete $::form->{$action};
267 $action = substr $action, 2;
270 delete @{$::form}{qw(M A)};
274 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
275 show_error('generic/error');
278 return ($script_name, $action);
281 sub _route_controller_request {
282 my ($controller, $action);
285 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
286 ($controller, $action) = ($1, $2);
287 delete $::form->{action};
291 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
292 show_error('generic/error');
295 return ($controller, $action);
303 die SL::Dispatcher->END_OF_REQUEST;