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