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";
26 my ($class, $interface) = @_;
28 my $self = bless {}, $class;
29 $self->{interface} = lc($interface || 'cgi');
34 sub pre_request_checks {
35 if (!$::auth->session_tables_present) {
36 if ($::form->{script} eq 'admin.pl') {
40 show_error('login/auth_db_unreachable');
43 $::auth->expire_sessions;
47 $::lxdebug->enter_sub;
49 my $error_type = shift || '';
51 $::locale = Locale->new($::language);
52 $::form->{error} = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
53 $::form->{error} = $::locale->text('Incorrect password!.') if ($error_type eq 'password');
54 $::myconfig{countrycode} = $::language;
55 $::form->{stylesheet} = 'css/lx-office-erp.css';
58 print $::form->parse_html_template($template);
59 $::lxdebug->leave_sub;
64 sub pre_startup_setup {
67 require "config/lx-erp.conf";
71 require "config/lx-erp-local.conf";
72 } if -f "config/lx-erp-local.conf";
76 require "bin/mozilla/common.pl";
77 require "bin/mozilla/installationcheck.pl";
80 # canonial globals. if it's not here, chances are it will get refactored someday.
83 $::userspath = "users";
84 $::templates = "templates";
85 $::memberfile = "users/members";
86 $::menufile = "menu.ini";
87 $::sendmail = "| /usr/sbin/sendmail -t";
88 $::lxdebug = LXDebug->new;
89 $::auth = SL::Auth->new;
92 %::called_subs = (); # currently used for recursion detection
95 $SIG{__WARN__} = sub {
100 sub pre_startup_checks {
101 ::verify_installation();
106 pre_startup_checks();
109 sub require_main_code {
110 $::lxdebug->enter_sub;
111 my ($script, $suffix) = @_;
115 require "bin/mozilla/$script$suffix";
116 } or die $EVAL_ERROR;
118 if (-f "bin/mozilla/custom_$script$suffix") {
121 require "bin/mozilla/custom_$script$suffix";
123 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
125 if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
128 require "bin/mozilla/$::form->{login}_$script";
130 $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
132 $::lxdebug->leave_sub;
135 sub _require_controller {
136 my $controller = shift;
137 $controller =~ s|[^A-Za-z0-9_]||g;
141 require "SL/Controller/${controller}.pm";
142 } or die $EVAL_ERROR;
145 sub _run_controller {
146 "SL::Controller::$_[0]"->new->_run_action($_[1]);
151 $self->{request} = shift;
153 $::lxdebug->enter_sub;
154 $::lxdebug->begin_request;
156 my ($script, $path, $suffix, $script_name, $action, $routing_type);
158 $script_name = $ENV{SCRIPT_NAME};
160 $self->unrequire_bin_mozilla;
162 $::cgi = CGI->new('');
163 $::locale = Locale->new($::language);
167 eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
169 if ($routing_type eq 'old') {
170 $::form->{action} = lc $::form->{action};
171 $::form->{action} =~ s/( |-|,|\#)/_/g;
173 ($script, $path, $suffix) = fileparse($script_name, ".pl");
174 require_main_code($script, $suffix);
176 $::form->{script} = $script . $suffix;
179 _require_controller($script_name);
180 $::form->{script} = "controller.pl";
183 pre_request_checks();
186 my $session_result = $::auth->restore_session;
187 $::auth->create_or_refresh_session;
189 $::form->error($::locale->text('System currently down for maintenance!')) if -e "$::userspath/nologin" && $script ne 'admin';
191 if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
192 $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
193 ::run($session_result);
196 show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
197 %::myconfig = $::auth->read_user($::form->{login});
199 show_error('login/password_error', 'password') unless $::myconfig{login};
201 $::locale = Locale->new($::myconfig{countrycode});
203 show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
205 $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
206 $::auth->create_or_refresh_session;
207 $::auth->delete_session_value('FLASH')->save_session();
208 delete $::form->{password};
211 map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
212 unless $action eq 'save' && $::form->{type} eq 'preferences';
214 $::form->set_standard_title;
215 if ($routing_type eq 'old') {
216 ::call_sub('::' . $::locale->findsub($action));
218 _run_controller($script_name, $action);
221 $::form->error($::locale->text('action= not defined!'));
227 if ($EVAL_ERROR ne END_OF_REQUEST) {
228 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
229 eval { show_error('generic/error') };
237 Form::disconnect_standard_dbh();
239 $::lxdebug->end_request;
240 $::lxdebug->leave_sub;
243 sub unrequire_bin_mozilla {
245 return unless $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
248 next unless m#^bin/mozilla/#;
249 next if /\bcommon.pl$/;
250 next if /\binstallationcheck.pl$/;
256 my $script_name = shift;
258 return $script_name =~ m/dispatcher\.pl$/ ? ('old', _route_dispatcher_request())
259 : $script_name =~ m/controller\.pl/ ? ('controller', _route_controller_request())
260 : ('old', $script_name, $::form->{action});
263 sub _route_dispatcher_request {
264 my $name_re = qr{[a-z]\w*};
265 my ($script_name, $action);
268 die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
269 $script_name = $::form->{M} . '.pl';
272 $action = $::form->{A};
275 $action = first { m/^A_${name_re}$/ } keys %{ $::form };
276 die "Unroutable request -- inavlid action name.\n" if !$action;
278 delete $::form->{$action};
279 $action = substr $action, 2;
282 delete @{$::form}{qw(M A)};
286 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
287 show_error('generic/error');
290 return ($script_name, $action);
293 sub _route_controller_request {
294 my ($controller, $action);
297 $::form->{action} =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
298 ($controller, $action) = ($1, $2);
299 delete $::form->{action};
303 $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
304 show_error('generic/error');
307 return ($controller, $action);
310 sub get_standard_filehandles {
313 return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
321 die SL::Dispatcher->END_OF_REQUEST;