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);
13 use English qw(-no_match_vars);
18 use SL::Helper::DateTime;
20 use List::Util qw(first);
23 # Trailing new line is added so that Perl will not add the line
24 # number 'die' was called in.
25 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
28 my ($class, $interface) = @_;
30 my $self = bless {}, $class;
31 $self->{interface} = lc($interface || 'cgi');
36 sub pre_request_checks {
37 if (!$::auth->session_tables_present) {
38 if ($::form->{script} eq 'admin.pl') {
42 show_error('login/auth_db_unreachable');
45 $::auth->expire_sessions;
49 $::lxdebug->enter_sub;
51 my $error_type = shift || '';
53 $::locale = Locale->new($::language);
54 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
55 $::form->{error} = $::locale->text('Incorrect password!.') if ($error_type eq 'password');
56 $::myconfig{countrycode} = $::language;
57 $::form->{stylesheet} = 'css/lx-office-erp.css';
60 print $::form->parse_html_template($template);
61 $::lxdebug->leave_sub;
66 sub pre_startup_setup {
69 require "config/lx-erp.conf";
73 require "config/lx-erp-local.conf";
74 } if -f "config/lx-erp-local.conf";
78 require "bin/mozilla/common.pl";
79 require "bin/mozilla/installationcheck.pl";
82 # canonial globals. if it's not here, chances are it will get refactored someday.
85 $::userspath = "users";
86 $::templates = "templates";
87 $::memberfile = "users/members";
88 $::menufile = "menu.ini";
89 $::sendmail = "| /usr/sbin/sendmail -t";
90 $::lxdebug = LXDebug->new;
91 $::auth = SL::Auth->new;
94 %::called_subs = (); # currently used for recursion detection
97 $SIG{__WARN__} = sub {
102 sub pre_startup_checks {
103 ::verify_installation();
108 pre_startup_checks();
111 sub require_main_code {
112 $::lxdebug->enter_sub;
113 my ($script, $suffix) = @_;
117 require "bin/mozilla/$script$suffix";
118 } or die $EVAL_ERROR;
120 if (-f "bin/mozilla/custom_$script$suffix") {
123 require "bin/mozilla/custom_$script$suffix";
125 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
127 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
130 require "bin/mozilla/$::form->{login}_$script";
132 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
134 $::lxdebug->leave_sub;
137 sub _require_controller {
138 my $controller = shift;
139 $controller =~ s|[^A-Za-z0-9_]||g;
143 require "SL/Controller/${controller}.pm";
144 } or die $EVAL_ERROR;
147 sub _run_controller {
148 "SL::Controller::$_[0]"->new->_run_action($_[1]);
153 $self->{request} = shift;
155 $::lxdebug->enter_sub;
156 $::lxdebug->begin_request;
158 my ($script, $path, $suffix, $script_name, $action, $routing_type);
160 $script_name = $ENV{SCRIPT_NAME};
162 $self->unrequire_bin_mozilla;
164 $::cgi = CGI->new('');
165 $::locale = Locale->new($::language);
169 eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
171 if ($routing_type eq 'old') {
172 $::form->{action} = lc $::form->{action};
173 $::form->{action} =~ s/( |-|,|\#)/_/g;
175 ($script, $path, $suffix) = fileparse($script_name, ".pl");
176 require_main_code($script, $suffix);
178 $::form->{script} = $script . $suffix;
181 _require_controller($script_name);
182 $::form->{script} = "controller.pl";
185 pre_request_checks();
188 my $session_result = $::auth->restore_session;
189 $::auth->create_or_refresh_session;
191 $::form->error($::locale->text('System currently down for maintenance!')) if -e "$::userspath/nologin" && $script ne 'admin';
193 if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
194 $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
195 ::run($session_result);
198 show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
199 %::myconfig = $::auth->read_user($::form->{login});
201 show_error('login/password_error', 'password') unless $::myconfig{login};
203 $::locale = Locale->new($::myconfig{countrycode});
205 show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
207 $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
208 $::auth->create_or_refresh_session;
209 $::auth->delete_session_value('FLASH')->save_session();
210 delete $::form->{password};
213 map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
214 unless $action eq 'save' && $::form->{type} eq 'preferences';
216 $::form->set_standard_title;
217 if ($routing_type eq 'old') {
218 ::call_sub('::' . $::locale->findsub($action));
220 _run_controller($script_name, $action);
223 $::form->error($::locale->text('action= not defined!'));
229 if ($EVAL_ERROR ne END_OF_REQUEST) {
230 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
231 eval { show_error('generic/error') };
239 Form::disconnect_standard_dbh();
241 $::lxdebug->end_request;
242 $::lxdebug->leave_sub;
245 sub unrequire_bin_mozilla {
247 return unless $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
250 next unless m#^bin/mozilla/#;
251 next if /\bcommon.pl$/;
252 next if /\binstallationcheck.pl$/;
258 my $script_name = shift;
260 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
261 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
262 : ('old', $script_name, $::form->{action});
265 sub _route_dispatcher_request {
266 my $name_re = qr{[a-z]\w*};
267 my ($script_name, $action);
270 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
271 $script_name = $::form->{M} . '.pl';
274 $action = $::form->{A};
277 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
278 die "Unroutable request -- inavlid action name.\n" if !$action;
280 delete $::form->{$action};
281 $action = substr $action, 2;
284 delete @{$::form}{qw(M A)};
288 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
289 show_error('generic/error');
292 return ($script_name, $action);
295 sub _route_controller_request {
296 my ($controller, $action);
299 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
300 ($controller, $action) = ($1, $2);
301 delete $::form->{action};
305 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
306 show_error('generic/error');
309 return ($controller, $action);
312 sub get_standard_filehandles {
315 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
323 die SL::Dispatcher->END_OF_REQUEST;