e6aa5477487b6015dcfc89a979a20ead4b7909b8
[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 English qw(-no_match_vars);
13 use SL::Auth;
14 use SL::LXDebug;
15 use SL::Locale;
16 use SL::Common;
17 use Form;
18 use List::Util qw(first);
19 use File::Basename;
20
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";
24
25 sub new {
26   my ($class, $interface) = @_;
27
28   my $self           = bless {}, $class;
29   $self->{interface} = lc($interface || 'cgi');
30
31   return $self;
32 }
33
34 sub pre_request_checks {
35   if (!$::auth->session_tables_present) {
36     if ($::form->{script} eq 'admin.pl') {
37       ::run();
38       ::end_of_request();
39     } else {
40       show_error('login/auth_db_unreachable');
41     }
42   }
43   $::auth->expire_sessions;
44 }
45
46 sub show_error {
47   $::lxdebug->enter_sub;
48   my $template             = shift;
49   my $error_type           = shift || '';
50
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';
56
57   $::form->header;
58   print $::form->parse_html_template($template);
59   $::lxdebug->leave_sub;
60
61   ::end_of_request();
62 }
63
64 sub pre_startup_setup {
65   eval {
66     package main;
67     require "config/lx-erp.conf";
68   };
69   eval {
70     package main;
71     require "config/lx-erp-local.conf";
72   } if -f "config/lx-erp-local.conf";
73
74   eval {
75     package main;
76     require "bin/mozilla/common.pl";
77     require "bin/mozilla/installationcheck.pl";
78   } or die $EVAL_ERROR;
79
80   # canonial globals. if it's not here, chances are it will get refactored someday.
81   {
82     no warnings 'once';
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;
90     $::form        = undef;
91     %::myconfig    = ();
92     %::called_subs = (); # currently used for recursion detection
93   }
94
95   $SIG{__WARN__} = sub {
96     $::lxdebug->warn(@_);
97   }
98 }
99
100 sub pre_startup_checks {
101   ::verify_installation();
102 }
103
104 sub pre_startup {
105   pre_startup_setup();
106   pre_startup_checks();
107 }
108
109 sub require_main_code {
110   $::lxdebug->enter_sub;
111   my ($script, $suffix) = @_;
112
113   eval {
114     package main;
115     require "bin/mozilla/$script$suffix";
116   } or die $EVAL_ERROR;
117
118   if (-f "bin/mozilla/custom_$script$suffix") {
119     eval {
120       package main;
121       require "bin/mozilla/custom_$script$suffix";
122     };
123     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
124   }
125   if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
126     eval {
127       package main;
128       require "bin/mozilla/$::form->{login}_$script";
129     };
130     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
131   }
132   $::lxdebug->leave_sub;
133 }
134
135 sub _require_controller {
136   my $controller =  shift;
137   $controller    =~ s|[^A-Za-z0-9_]||g;
138
139   eval {
140     package main;
141     require "SL/Controller/${controller}.pm";
142   } or die $EVAL_ERROR;
143 }
144
145 sub _run_controller {
146   "SL::Controller::$_[0]"->new->_run_action($_[1]);
147 }
148
149 sub handle_request {
150   my $self = 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 package main;
310
311 use strict;
312
313 sub end_of_request {
314   die SL::Dispatcher->END_OF_REQUEST;
315 }
316
317 1;