forked from ledgersmb/LedgerSMB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsmb-request.pl
More file actions
194 lines (138 loc) · 4.78 KB
/
Copy pathlsmb-request.pl
File metadata and controls
194 lines (138 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
=head1 NAME
lsmb-request.pl - The LedgerSMB Request Handler
=head1 SYNOPSYS
This file receives the web request, instantiates the proper objects, and passes
execution off to the appropriate workflow scripts. This is for use with new
code only and should not be used with old SQL-Ledger(TM) code as it is
architecturally dissimilar.
=head1 COPYRIGHT
Copyright (C) 2007 The LedgerSMB Core Team
This file is licensed under the GNU General Public License (GPL) version 2 or
at your option any later version. A copy of the GNU GPL has been included with
this software.
=cut
package LedgerSMB::Handler;
use LedgerSMB::Sysconfig;
use LedgerSMB::Locale;
use Digest::MD5;
use Try::Tiny;
$| = 1;
binmode (STDIN, ':bytes');
binmode (STDOUT, ':utf8');
use LedgerSMB::User;
use LedgerSMB::App_State;
use LedgerSMB;
use LedgerSMB::Locale;
use Data::Dumper;
use Log::Log4perl;
use strict;
my $logger;
sub get_script {
my ($locale, $request) = @_;
$ENV{SCRIPT_NAME} =~ m/([^\/\\]*.pl)\?*.*$/;
my $script = $1;
$logger->debug("\$ENV{SCRIPT_NAME}=$ENV{SCRIPT_NAME} "
. "\$request->{action}=$request->{action} "
. "\$script=$script");
if (!$script){
$request->error($locale->text('No workflow script specified'));
}
return $script;
}
sub get_locale {
my ($request) = @_;
my $locale;
if ($request->{_user}){
$LedgerSMB::App_State::User = $request->{_user};
$locale = LedgerSMB::Locale->get_handle($request->{_user}->{language});
$LedgerSMB::App_State::Locale = $locale;
} else {
$locale =
LedgerSMB::Locale->get_handle( $LedgerSMB::Sysconfig::language );
$request->error( __FILE__ . ':' . __LINE__ .
": Locale ($LedgerSMB::Sysconfig::language) "
. "not loaded: $!\n"
) unless $locale;
$LedgerSMB::App_State::Locale = $locale;
}
return $locale;
}
$ENV{SCRIPT_NAME} =~ m/([^\/\\]*.pl)\?*.*$/;
my $script = $1;
$script = '' unless defined $script;
$logger->debug("\$ENV{SCRIPT_NAME}=$ENV{SCRIPT_NAME} \$request->{action}=$request->{action} \$script=$script");
sub app_initialize {
LedgerSMB::App_State->cleanup();
Log::Log4perl::init(\$LedgerSMB::Sysconfig::log4perl_config);
$logger = Log::Log4perl->get_logger('LedgerSMB::Handler');
$logger->debug("Begin");
}
sub request_instantiate {
my $request;
$logger->debug("getting new LedgerSMB");
my $request = new LedgerSMB;
$logger->debug("Got \$request=$request");
$logger->trace("\$request=".Data::Dumper::Dumper($request));
$request->{action} = '__default' if (!$request->{action});
return $request;
}
sub call_script {
my $script = shift @_;
my $request = shift @_;
my $locale = shift @_;
try {
$request->{script} = $script;
$script =~ s/\.pl$//;
$script = "LedgerSMB::Scripts::$script";
$request->{_script_handle} = $script;
eval "require $script;"
|| die $locale->text('Unable to open script') .
": $script : $!: $@";
my @no_db_actions =
$script->can('no_db_actions')->()
if $script->can('no_db_actions');
my $no_db = 0;
foreach my $action (@no_db_actions) {
$no_db = 1
if $action eq $request->{action};
}
if (! ($no_db || $script->can('no_db'))) {
$request->_db_init();
$request->initialize_with_db();
}
$script->can($request->{action})
|| die $locale->text("Action Not Defined: ") . $request->{action};
$script->can( $request->{action} )->($request);
$request->{dbh}->commit if defined $request->{dbh};
LedgerSMB::App_State->cleanup();
}
catch {
# We have an exception here because otherwise we always get an exception
# when output terminates. A mere 'die' will no longer trigger an
# automatic error, but die 'foo' will map to $request->error('foo')
# -- CT
$LedgerSMB::App_State::DBH->rollback if ($LedgerSMB::App_State::DBH and $_ eq 'Died');
LedgerSMB::App_State->cleanup();
$request->_error($_) unless $_ =~ 'Died at' or $_ =~ /^exit at/;
};
}
sub request_cleanup {
my ($request) = @_;
# Prevent flooding the error logs with undestroyed connection warnings
$request->{dbh}->disconnect()
if defined $request->{dbh};
$logger->debug("End");
}
&app_initialize();
# for custom preprocessing logic
eval { require "custom.pl"; };
my $request = request_instantiate();
my $locale = get_locale($request);
$request->{_locale} = $locale;
my $script = get_script($locale, $request);
$logger->debug("calling $script");
&call_script( $script, $request, $locale);
$logger->debug("after calling script=$script action=$request->{action} "
. "\$request->{dbh}=$request->{dbh}");
&request_cleanup();
1;