Dispatcher bruacht kein SL/ include, wenn Form in der korrekten Form "SL::Form" einge...
[kivitendo-erp.git] / SL / Dispatcher.pm
1 package SL::Dispatcher;
2
3 use strict;
4
5 BEGIN {
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 }
9
10 use CGI qw( -no_xhtml);
11 use English qw(-no_match_vars);
12 use SL::Auth;
13 use SL::LXDebug;
14 use SL::Locale;
15 use SL::Common;
16 use SL::Form;
17 use List::Util qw(first);
18 use File::Basename;
19
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";
23
24 sub new {
25   my ($class, $interface) = @_;
26
27   my $self           = bless {}, $class;
28   $self->{interface} = lc($interface || 'cgi');
29
30   return $self;
31 }
32
33 sub pre_request_checks {
34   if (!$::auth->session_tables_present) {
35     if ($::form->{script} eq 'admin.pl') {
36       ::run();
37       ::end_of_request();
38     } else {
39       show_error('login/auth_db_unreachable');
40     }
41   }
42   $::auth->expire_sessions;
43 }
44
45 sub show_error {
46   $::lxdebug->enter_sub;
47   my $template             = shift;
48   my $error_type           = shift || '';
49
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';
55
56   $::form->header;
57   print $::form->parse_html_template($template);
58   $::lxdebug->leave_sub;
59
60   ::end_of_request();
61 }
62
63 sub pre_startup_setup {
64   eval {
65     package main;
66     require "config/lx-erp.conf";
67   };
68   eval {
69     package main;
70     require "config/lx-erp-local.conf";
71   } if -f "config/lx-erp-local.conf";
72
73   eval {
74     package main;
75     require "bin/mozilla/common.pl";
76     require "bin/mozilla/installationcheck.pl";
77   } or die $EVAL_ERROR;
78
79   # canonial globals. if it's not here, chances are it will get refactored someday.
80   {
81     no warnings 'once';
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;
89     $::form        = undef;
90     %::myconfig    = ();
91     %::called_subs = (); # currently used for recursion detection
92   }
93
94   $SIG{__WARN__} = sub {
95     $::lxdebug->warn(@_);
96   }
97 }
98
99 sub pre_startup_checks {
100   ::verify_installation();
101 }
102
103 sub pre_startup {
104   pre_startup_setup();
105   pre_startup_checks();
106 }
107
108 sub require_main_code {
109   $::lxdebug->enter_sub;
110   my ($script, $suffix) = @_;
111
112   eval {
113     package main;
114     require "bin/mozilla/$script$suffix";
115   } or die $EVAL_ERROR;
116
117   if (-f "bin/mozilla/custom_$script$suffix") {
118     eval {
119       package main;
120       require "bin/mozilla/custom_$script$suffix";
121     };
122     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
123   }
124   if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
125     eval {
126       package main;
127       require "bin/mozilla/$::form->{login}_$script";
128     };
129     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
130   }
131   $::lxdebug->leave_sub;
132 }
133
134 sub _require_controller {
135   my $controller =  shift;
136   $controller    =~ s|[^A-Za-z0-9_]||g;
137
138   eval {
139     package main;
140     require "SL/Controller/${controller}.pm";
141   } or die $EVAL_ERROR;
142 }
143
144 sub _run_controller {
145   "SL::Controller::$_[0]"->new->_run_action($_[1]);
146 }
147
148 sub handle_request {
149   my $self         = shift;
150   $self->{request} = shift;
151
152   $::lxdebug->enter_sub;
153   $::lxdebug->begin_request;
154
155   my ($script, $path, $suffix, $script_name, $action, $routing_type);
156
157   $script_name = $ENV{SCRIPT_NAME};
158
159   $self->unrequire_bin_mozilla;
160
161   $::cgi         = CGI->new('');
162   $::locale      = Locale->new($::language);
163   $::form        = Form->new;
164   %::called_subs = ();
165
166   eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
167
168   if ($routing_type eq 'old') {
169     $::form->{action}  =  lc $::form->{action};
170     $::form->{action}  =~ s/( |-|,|\#)/_/g;
171
172    ($script, $path, $suffix) = fileparse($script_name, ".pl");
173     require_main_code($script, $suffix);
174
175     $::form->{script} = $script . $suffix;
176
177   } else {
178     _require_controller($script_name);
179     $::form->{script} = "controller.pl";
180   }
181
182   pre_request_checks();
183
184   eval {
185     my $session_result = $::auth->restore_session;
186     $::auth->create_or_refresh_session;
187
188     $::form->error($::locale->text('System currently down for maintenance!')) if -e "$::userspath/nologin" && $script ne 'admin';
189
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);
193
194     } else {
195       show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
196       %::myconfig = $::auth->read_user($::form->{login});
197
198       show_error('login/password_error', 'password') unless $::myconfig{login};
199
200       $::locale = Locale->new($::myconfig{countrycode});
201
202       show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
203
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};
208
209       if ($action) {
210         map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
211           unless $action eq 'save' && $::form->{type} eq 'preferences';
212
213         $::form->set_standard_title;
214         if ($routing_type eq 'old') {
215           ::call_sub('::' . $::locale->findsub($action));
216         } else {
217           _run_controller($script_name, $action);
218         }
219       } else {
220         $::form->error($::locale->text('action= not defined!'));
221       }
222     }
223
224     1;
225   } or do {
226     if ($EVAL_ERROR ne END_OF_REQUEST) {
227       $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
228       eval { show_error('generic/error') };
229     }
230   };
231
232   # cleanup
233   $::locale   = undef;
234   $::form     = undef;
235   $::myconfig = ();
236   Form::disconnect_standard_dbh();
237
238   $::lxdebug->end_request;
239   $::lxdebug->leave_sub;
240 }
241
242 sub unrequire_bin_mozilla {
243   my $self = shift;
244   return unless $self->{interface} =~ m/^(?:fastcgi|fcgid|fcgi)$/;
245
246   for (keys %INC) {
247     next unless m#^bin/mozilla/#;
248     next if /\bcommon.pl$/;
249     next if /\binstallationcheck.pl$/;
250     delete $INC{$_};
251   }
252 }
253
254 sub _route_request {
255   my $script_name = shift;
256
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});
260 }
261
262 sub _route_dispatcher_request {
263   my $name_re = qr{[a-z]\w*};
264   my ($script_name, $action);
265
266   eval {
267     die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
268     $script_name = $::form->{M} . '.pl';
269
270     if ($::form->{A}) {
271       $action = $::form->{A};
272
273     } else {
274       $action = first { m/^A_${name_re}$/ } keys %{ $::form };
275       die "Unroutable request -- inavlid action name.\n" if !$action;
276
277       delete $::form->{$action};
278       $action = substr $action, 2;
279     }
280
281     delete @{$::form}{qw(M A)};
282
283     1;
284   } or do {
285     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
286     show_error('generic/error');
287   };
288
289   return ($script_name, $action);
290 }
291
292 sub _route_controller_request {
293   my ($controller, $action);
294
295   eval {
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};
299
300     1;
301   } or do {
302     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
303     show_error('generic/error');
304   };
305
306   return ($controller, $action);
307 }
308
309 sub get_standard_filehandles {
310   my $self = shift;
311
312   return $self->{interface} =~ m/f(?:ast)cgi/i ? $self->{request}->GetHandles() : (\*STDIN, \*STDOUT, \*STDERR);
313 }
314
315 package main;
316
317 use strict;
318
319 sub end_of_request {
320   die SL::Dispatcher->END_OF_REQUEST;
321 }
322
323 1;