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.
10 use CGI qw( -no_xhtml);
11 use English qw(-no_match_vars);
17 use List::Util qw(first);
20 # Trailing new line is added so that Perl will not add the line
21 # number 'die' was called in.
22 use constant END_OF_REQUEST => "END-OF-REQUEST\n";
25 my ($class, $interface) = @_;
27 my $self = bless {}, $class;
28 $self->{interface} = lc($interface || 'cgi');
33 sub pre_request_checks {
34 if (!$::auth->session_tables_present) {
35 if ($::form->{script} eq 'admin.pl') {
39 show_error('login/auth_db_unreachable');
42 $::auth->expire_sessions;
46 $::lxdebug->enter_sub;
48 my $error_type = shift || '';
50 $::locale = Locale->new($::language);
51 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
52 $::form->{error} = $::locale->text('Incorrect password!.') if ($error_type eq 'password');
53 $::myconfig{countrycode} = $::language;
54 $::form->{stylesheet} = 'css/lx-office-erp.css';
57 print $::form->parse_html_template($template);
58 $::lxdebug->leave_sub;
63 sub pre_startup_setup {
66 require "config/lx-erp.conf";
70 require "config/lx-erp-local.conf";
71 } if -f "config/lx-erp-local.conf";
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 $::userspath = "users";
83 $::templates = "templates";
84 $::memberfile = "users/members";
85 $::menufile = "menu.ini";
86 $::sendmail = "| /usr/sbin/sendmail -t";
87 $::lxdebug = LXDebug->new;
88 $::auth = SL::Auth->new;
91 %::called_subs = (); # currently used for recursion detection
94 $SIG{__WARN__} = sub {
99 sub pre_startup_checks {
100 ::verify_installation();
105 pre_startup_checks();
108 sub require_main_code {
109 $::lxdebug->enter_sub;
110 my ($script, $suffix) = @_;
114 require "bin/mozilla/$script$suffix";
115 } or die $EVAL_ERROR;
117 if (-f "bin/mozilla/custom_$script$suffix") {
120 require "bin/mozilla/custom_$script$suffix";
122 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
124 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
127 require "bin/mozilla/$::form->{login}_$script";
129 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
131 $::lxdebug->leave_sub;
134 sub _require_controller {
135 my $controller = shift;
136 $controller =~ s|[^A-Za-z0-9_]||g;
140 require "SL/Controller/${controller}.pm";
141 } or die $EVAL_ERROR;
144 sub _run_controller {
145 "SL::Controller::$_[0]"->new->_run_action($_[1]);
150 $self->{request} = shift;
152 $::lxdebug->enter_sub;
153 $::lxdebug->begin_request;
155 my ($script, $path, $suffix, $script_name, $action, $routing_type);
157 $script_name = $ENV{SCRIPT_NAME};
159 $self->unrequire_bin_mozilla;
161 $::cgi = CGI->new('');
162 $::locale = Locale->new($::language);
166 eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
168 if ($routing_type eq 'old') {
169 $::form->{action} = lc $::form->{action};
170 $::form->{action} =~ s/( |-|,|\#)/_/g;
172 ($script, $path, $suffix) = fileparse($script_name, ".pl");
173 require_main_code($script, $suffix);
175 $::form->{script} = $script . $suffix;
178 _require_controller($script_name);
179 $::form->{script} = "controller.pl";
182 pre_request_checks();
185 my $session_result = $::auth->restore_session;
186 $::auth->create_or_refresh_session;
188 $::form->error($::locale->text('System currently down for maintenance!')) if -e "$::userspath/nologin" && $script ne 'admin';
190 if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
191 $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
192 ::run($session_result);
195 show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
196 %::myconfig = $::auth->read_user($::form->{login});
198 show_error('login/password_error', 'password') unless $::myconfig{login};
200 $::locale = Locale->new($::myconfig{countrycode});
202 show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
204 $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
205 $::auth->create_or_refresh_session;
206 $::auth->delete_session_value('FLASH')->save_session();
207 delete $::form->{password};
210 map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
211 unless $action eq 'save' && $::form->{type} eq 'preferences';
213 $::form->set_standard_title;
214 if ($routing_type eq 'old') {
215 ::call_sub('::' . $::locale->findsub($action));
217 _run_controller($script_name, $action);
220 $::form->error($::locale->text('action= not defined!'));
226 if ($EVAL_ERROR ne END_OF_REQUEST) {
227 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
228 eval { show_error('generic/error') };
236 Form::disconnect_standard_dbh();
238 $::lxdebug->end_request;
239 $::lxdebug->leave_sub;
242 sub unrequire_bin_mozilla {
244 return unless $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
247 next unless m#^bin/mozilla/#;
248 next if /\bcommon.pl$/;
249 next if /\binstallationcheck.pl$/;
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);
320 die SL::Dispatcher->END_OF_REQUEST;