Neuer Routingmechanismus für Controller, die als Package implementiert sind
[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 pre_request_checks {
26   if (!$::auth->session_tables_present) {
27     if ($::form->{script} eq 'admin.pl') {
28       ::run();
29       ::end_of_request();
30     } else {
31       show_error('login/auth_db_unreachable');
32     }
33   }
34   $::auth->expire_sessions;
35 }
36
37 sub show_error {
38   $::lxdebug->enter_sub;
39   my $template             = shift;
40   my $error_type           = shift || '';
41
42   $::locale                = Locale->new($::language);
43   $::form->{error}         = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
44   $::form->{error}         = $::locale->text('Incorrect password!.')                   if ($error_type eq 'password');
45   $::myconfig{countrycode} = $::language;
46   $::form->{stylesheet}    = 'css/lx-office-erp.css';
47
48   $::form->header;
49   print $::form->parse_html_template($template);
50   $::lxdebug->leave_sub;
51
52   ::end_of_request();
53 }
54
55 sub pre_startup_setup {
56   eval {
57     package main;
58     require "config/lx-erp.conf";
59   };
60   eval {
61     package main;
62     require "config/lx-erp-local.conf";
63   } if -f "config/lx-erp-local.conf";
64
65   eval {
66     package main;
67     require "bin/mozilla/common.pl";
68     require "bin/mozilla/installationcheck.pl";
69   } or die $EVAL_ERROR;
70
71   # canonial globals. if it's not here, chances are it will get refactored someday.
72   {
73     no warnings 'once';
74     $::userspath   = "users";
75     $::templates   = "templates";
76     $::memberfile  = "users/members";
77     $::menufile    = "menu.ini";
78     $::sendmail    = "| /usr/sbin/sendmail -t";
79     $::lxdebug     = LXDebug->new;
80     $::auth        = SL::Auth->new;
81     $::form        = undef;
82     %::myconfig    = ();
83     %::called_subs = (); # currently used for recursion detection
84   }
85
86   $SIG{__WARN__} = sub {
87     $::lxdebug->warn(@_);
88   }
89 }
90
91 sub pre_startup_checks {
92   ::verify_installation();
93 }
94
95 sub pre_startup {
96   pre_startup_setup();
97   pre_startup_checks();
98 }
99
100 sub require_main_code {
101   $::lxdebug->enter_sub;
102   my ($script, $suffix) = @_;
103
104   eval {
105     package main;
106     require "bin/mozilla/$script$suffix";
107   } or die $EVAL_ERROR;
108
109   if (-f "bin/mozilla/custom_$script$suffix") {
110     eval {
111       package main;
112       require "bin/mozilla/custom_$script$suffix";
113     };
114     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
115   }
116   if ($::form->{login} && -f "bin/mozilla/$::form->{login}_$script") {
117     eval {
118       package main;
119       require "bin/mozilla/$::form->{login}_$script";
120     };
121     $::form->error($EVAL_ERROR) if ($EVAL_ERROR);
122   }
123   $::lxdebug->leave_sub;
124 }
125
126 sub _require_controller {
127   my $controller =  shift;
128   $controller    =~ s|[^A-Za-z0-9_]||g;
129
130   eval {
131     package main;
132     require "SL/Controller/${controller}.pm";
133   } or die $EVAL_ERROR;
134 }
135
136 sub _run_controller {
137   "SL::Controller::$_[0]"->new->_run_action($_[1]);
138 }
139
140 sub handle_request {
141   $::lxdebug->enter_sub;
142   $::lxdebug->begin_request;
143
144   my $interface = lc(shift || 'cgi');
145   my ($script, $path, $suffix, $script_name, $action, $routing_type);
146
147   $script_name = $ENV{SCRIPT_NAME};
148
149   unrequire_bin_mozilla($interface);
150
151   $::cgi         = CGI->new('');
152   $::locale      = Locale->new($::language);
153   $::form        = Form->new;
154   %::called_subs = ();
155
156   eval { ($routing_type, $script_name, $action) = _route_request($script_name); 1; } or return;
157
158   if ($routing_type eq 'old') {
159     $::form->{action}  =  lc $::form->{action};
160     $::form->{action}  =~ s/( |-|,|\#)/_/g;
161
162    ($script, $path, $suffix) = fileparse($script_name, ".pl");
163     require_main_code($script, $suffix);
164
165     $::form->{script} = $script . $suffix;
166
167   } else {
168     _require_controller($script_name);
169     $::form->{script} = "controller.pl";
170   }
171
172   pre_request_checks();
173
174   eval {
175     my $session_result = $::auth->restore_session;
176     $::auth->create_or_refresh_session;
177
178     $::form->error($::locale->text('System currently down for maintenance!')) if -e "$::userspath/nologin" && $script ne 'admin';
179
180     if ($script eq 'login' or $script eq 'admin' or $script eq 'kopf') {
181       $::form->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $::form->{version}";
182       ::run($session_result);
183
184     } else {
185       show_error('login/password_error', 'session') if SL::Auth::SESSION_EXPIRED == $session_result;
186       %::myconfig = $::auth->read_user($::form->{login});
187
188       show_error('login/password_error', 'password') unless $::myconfig{login};
189
190       $::locale = Locale->new($::myconfig{countrycode});
191
192       show_error('login/password_error', 'password') if SL::Auth::OK != $::auth->authenticate($::form->{login}, $::form->{password}, 0);
193
194       $::auth->set_session_value('login', $::form->{login}, 'password', $::form->{password});
195       $::auth->create_or_refresh_session;
196       $::auth->delete_session_value('FLASH')->save_session();
197       delete $::form->{password};
198
199       if ($action) {
200         map { $::form->{$_} = $::myconfig{$_} } qw(stylesheet charset)
201           unless $action eq 'save' && $::form->{type} eq 'preferences';
202
203         $::form->set_standard_title;
204         if ($routing_type eq 'old') {
205           ::call_sub('::' . $::locale->findsub($action));
206         } else {
207           _run_controller($script_name, $action);
208         }
209       } else {
210         $::form->error($::locale->text('action= not defined!'));
211       }
212     }
213
214     1;
215   } or do {
216     if ($EVAL_ERROR ne END_OF_REQUEST) {
217       $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
218       eval { show_error('generic/error') };
219     }
220   };
221
222   # cleanup
223   $::locale   = undef;
224   $::form     = undef;
225   $::myconfig = ();
226   Form::disconnect_standard_dbh();
227
228   $::lxdebug->end_request;
229   $::lxdebug->leave_sub;
230 }
231
232 sub unrequire_bin_mozilla {
233   return unless $_[0] =~ m/^(?:fastcgi|fcgid|fcgi)$/;
234
235   for (keys %INC) {
236     next unless m#^bin/mozilla/#;
237     next if /\bcommon.pl$/;
238     next if /\binstallationcheck.pl$/;
239     delete $INC{$_};
240   }
241 }
242
243 sub _route_request {
244   my $script_name = shift;
245
246   return $script_name =~ m/dispatcher\.pl$/ ? ('old',        _route_dispatcher_request())
247        : $script_name =~ m/controller\.pl/  ? ('controller', _route_controller_request())
248        :                                      ('old',        $script_name, $::form->{action});
249 }
250
251 sub _route_dispatcher_request {
252   my $name_re = qr{[a-z]\w*};
253   my ($script_name, $action);
254
255   eval {
256     die "Unroutable request -- inavlid module name.\n" if !$::form->{M} || ($::form->{M} !~ m/^${name_re}$/);
257     $script_name = $::form->{M} . '.pl';
258
259     if ($::form->{A}) {
260       $action = $::form->{A};
261
262     } else {
263       $action = first { m/^A_${name_re}$/ } keys %{ $::form };
264       die "Unroutable request -- inavlid action name.\n" if !$action;
265
266       delete $::form->{$action};
267       $action = substr $action, 2;
268     }
269
270     delete @{$::form}{qw(M A)};
271
272     1;
273   } or do {
274     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
275     show_error('generic/error');
276   };
277
278   return ($script_name, $action);
279 }
280
281 sub _route_controller_request {
282   my ($controller, $action);
283
284   eval {
285     $::form->{action}      =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) :: ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
286     ($controller, $action) =  ($1, $2);
287     delete $::form->{action};
288
289     1;
290   } or do {
291     $::form->{label_error} = $::cgi->pre($EVAL_ERROR);
292     show_error('generic/error');
293   };
294
295   return ($controller, $action);
296 }
297
298 package main;
299
300 use strict;
301
302 sub end_of_request {
303   die SL::Dispatcher->END_OF_REQUEST;
304 }
305
306 1;