-
Notifications
You must be signed in to change notification settings - Fork 3
/
edit_datasource.cgi
293 lines (249 loc) · 7.73 KB
/
edit_datasource.cgi
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/perl
require './jru-lib.pl';
require '../webmin/webmin-lib.pl'; #for OS detection
sub jru_add_entry{
open(my $fh, '>>', get_prop_file()) or die "open:$!";
print $fh "\n";
print $fh '[datasource:'.$in{'name'}."]\n";
foreach my $key (@ds_keys){
print $fh $key.'='.$in{$key}."\n";
}
close $fh;
}
sub jru_update_entry{
my $name = $in{'dsname'};
my $prop_file = get_prop_file();
$lref = &read_file_lines($prop_file);
my $ln=0;
my $ds_line = -1;
foreach $line (@$lref){
if($line =~ /^\[datasource:$name\]/){
$ds_line = $ln;
last; #don't add the line twice
}
$ln=$ln+1;
}
if($ds_line > 0){
@{$lref}[$ds_line] = '[datasource:'.$in{'name'}."]";
$ln = $ds_line;
foreach my $key (@ds_keys){
$ln=$ln+1;
my $line = @{$lref}[$ln];
if($line =~ /^$key=/){
@{$lref}[$ln] = $key.'='.$in{$key};
}
}
}
&flush_file_lines($prop_file);
}
sub jru_rm_entry{
my $name = $in{'datasource'};
my $prop_file = get_prop_file();
$lref = &read_file_lines($prop_file);
my $ln=0;
my $ds_line = -1;
foreach $line (@$lref){
if($line =~ /^\[datasource:$name\]/){
$ds_line = $ln;
last; #don't add the line twice
}
$ln=$ln+1;
}
if($ds_line > 0){
@{$lref}[$ds_line] = '#'.@{$lref}[$ds_line];
$ln = $ds_line;
foreach my $key (@ds_keys){
$ln=$ln+1;
my $line = @{$lref}[$ln];
if($line =~ /^$key=/){
@{$lref}[$ln] = '#'.$line;
}
}
}
&flush_file_lines($prop_file);
}
sub jru_rm_resource_ctx{
my $name = $_[0];
my $ctxxml = get_catalina_home().'/conf/context.xml';
$lref = &read_file_lines($ctxxml);
my $ln=0;
my $sln = -1, $eln = -1;
my $pat = '<Resource name="jdbc/'.$name.'" auth="Container" type="javax.sql.DataSource';
foreach my $line (@$lref){
if(($sln == -1) && (index($line, $pat) >= 0)){
$sln = $ln;
}elsif(($sln >= 0) and ($line =~ /\/>$/)){
$eln = $ln;
last;
}
$ln=$ln+1;
}
if($sln > 0 and $eln > 0){
# remove the section
for (my $i = $sln; $i <= $eln; $i++) {
delete @{$lref}[$i];
}
}
&flush_file_lines($ctxxml);
}
sub jru_rm_resource_web{
my $name = $_[0];
my $webxml = get_catalina_home().'/webapps/JasperReportsIntegration/WEB-INF/web.xml';
$lref = &read_file_lines($webxml);
my $ln=0;
my $sln = -1, $eln = -1;
my $pat = '<res-ref-name>'.$name.'</res-ref-name>';
foreach $line (@$lref){
if(($sln == -1) && (index($line, $pat) >= 0)){
$sln = $ln;
}elsif(($sln >=0) and ($line =~ /<\/resource-ref>$/)){
$eln = $ln;
last;
}
$ln=$ln+1;
}
if($sln > 0 and $eln > 0){
# remove the section
for (my $i = $sln - 2; $i <= $eln; $i++) {
delete @{$lref}[$i];
}
}
&flush_file_lines($webxml);
}
&ReadParse();
&ui_print_header(undef, $text{'jru_title'}, "", "sources", 0, 0);
#Make options for type field
my @jru_types = ('jdbc', 'jndi');
@opt_jru_types = map { [$_, $_]} sort @jru_types;
%datasources = get_jru_datasources();
@opt_datasources = map { [$_, $_]} sort keys %datasources;
if($in{'post_flag'} == 3){ #update
my %ds = %{$datasources{$in{'dsname'}}};
if(!%ds){
print("<b>datasource:".$in{'dsname'}."</b> doesn't exists!");
&ui_print_footer("", $text{'index_return'});
exit;
}else{
#check if all keys exists
foreach my $key (@ds_keys){
if(!$in{$key}){
print("Datasource $key is empty!");
&ui_print_footer("", $text{'index_return'});
exit;
}
}
jru_update_entry();
if($ds{'type'} == 'jndi'){
jru_rm_resource_ctx($ds{'name'});
jru_rm_resource_web($ds{'name'});
if($ds{'url'} =~ /^jdbc:postgresql/){ jri_add_pg_resource( $in{'name'}, $in{'url'}, $in{'username'}, $in{'password'});
}elsif($ds{'url'} =~ /^jdbc:mysql/){ jri_add_mysql_resource($in{'name'}, $in{'url'}, $in{'username'}, $in{'password'});
}elsif($ds{'url'} =~ /^jdbc:sqlserver/){ jri_add_mssql_resource($in{'name'}, $in{'url'}, $in{'username'}, $in{'password'});
}
}
}
}elsif($in{'post_flag'} == 1){ #add
if($datasources{$in{'name'}}){
print("<b>datasource:".$in{'name'}."</b> already exists!");
&ui_print_footer("", $text{'index_return'});
exit;
}else{
#check if all keys exists
foreach my $key (@ds_keys){
if(!$in{$key}){
print("Datasource $key is empty!");
&ui_print_footer("", $text{'index_return'});
exit;
}
}
jru_add_entry();
if($in{'type'} == 'jndi'){
if($in{'url'} =~ /^jdbc:postgresql/){ jri_add_pg_resource( $in{'name'}, $in{'url'}, $in{'username'}, $in{'password'});
}elsif($in{'url'} =~ /^jdbc:mysql/){ jri_add_mysql_resource($in{'name'}, $in{'url'}, $in{'username'}, $in{'password'});
}elsif($in{'url'} =~ /^jdbc:sqlserver/){ jri_add_mssql_resource($in{'name'}, $in{'url'}, $in{'username'}, $in{'password'});
}
}
}
}elsif($in{'post_flag'} == 2){ #remove
my %ds = %{$datasources{$in{'datasource'}}};
if(!%ds){
print("<b>datasource:".$in{'datasource'}."</b> doesn't exists!");
&ui_print_footer("", $text{'index_return'});
exit;
}
jru_rm_entry();
if($ds{'type'} == 'jndi'){
jru_rm_resource_ctx($ds{'name'});
jru_rm_resource_web($ds{'name'});
}
}
if($in{'post_flag'}){
#re-read the file, after we have added/removed a user
%datasources = get_jru_datasources();
@opt_datasources = map { [$_, $_]} sort keys %datasources;
}
# Show tabs
@tabs = ( [ "add",$text{'jru_tabadd'}, "edit_datasource.cgi?mode=add" ],
[ "remove", $text{'jru_tabremove'}, "edit_datasource.cgi?mode=remove" ],
[ "list", $text{'jru_tablist'}, "edit_datasource.cgi?mode=list" ]
);
print &ui_tabs_start(\@tabs, "mode", $in{'mode'} || "list", 1);
my @cols = ('', '', '', '', '');
if($in{'dsname'}){
my %ds_entries = %{$datasources{$in{'dsname'}}};
@cols = map { $ds_entries{$_} } @ds_keys;
}
#Add tab
print &ui_tabs_start_tab("mode", "add");
print "$text{'jru_desc1'}<p>\n";
print &ui_form_start("edit_datasource.cgi", "post");
if($in{'dsname'}){
print &ui_hidden("post_flag", 3); #update
print &ui_hidden("dsname", $in{'dsname'}); #name of the datasource to be edited
}else{
print &ui_hidden("post_flag", 1); #add
}
print &ui_table_start($text{'jru_add'}, undef, 2);
print &ui_table_row($text{'jru_type'}, &ui_select("type", $cols[0], \@opt_jru_types, 1, 0), 2);
print &ui_table_row($text{'jru_name'}, &ui_textbox("name", $cols[1], 40), 2);
print &ui_table_row($text{'jru_url'}, &ui_textbox("url", $cols[2], 40), 2);
print &ui_table_row($text{'jru_username'}, &ui_textbox("username", $cols[3], 40), 2);
print &ui_table_row($text{'jru_password'}, &ui_textbox("password", $cols[4], 40), 2);
print &ui_table_end();
if($in{'dsname'}){
print &ui_form_end([ [ "but_update", $text{'jru_update'} ] ]);
}else{
print &ui_form_end([ [ "but_add", $text{'jru_addok'} ] ]);
}
print &ui_tabs_end_tab();
#End Add tab
#remove tab
print &ui_tabs_start_tab("mode", "remove");
print "$text{'jru_desc2'}<p>\n";
print &ui_form_start("edit_datasource.cgi", "post");
print &ui_hidden("post_flag", 2);
print &ui_table_start($text{'jru_remove'}, undef, 2);
print &ui_table_row($text{'jru_datasource'}, &ui_select("datasource", undef, \@opt_datasources, 1, 0), 2);
print &ui_table_end();
print &ui_form_end([ [ "", $text{'jru_rmok'} ] ]);
print &ui_tabs_end_tab();
#End remove tab
#list tab
print &ui_tabs_start_tab("mode", "list");
print "$text{'jru_desc3'}<p>\n";
local @tds = ( "width=5" );
print &ui_columns_start([ 'Datasource', 'Type', 'Name', 'URL', 'Username', 'Password' ], 100, 0, \@tds);
foreach my $ds_name (sort keys %datasources){
local %ds_entries = %{$datasources{$ds_name}};
my @cols = ($ds_name);
$cols[0] = '<a href="/jri_publisher/edit_datasource.cgi?mode=add&dsname='.&urlize($ds_name).'">'.$ds_name."</a>";
foreach my $key (@ds_keys){
push(@cols, $ds_entries{$key});
}
print &ui_columns_row(\@cols, \@tds);
}
print &ui_columns_end();
print &ui_tabs_end_tab();
#End list tab
print &ui_tabs_end(1);
&ui_print_footer("", $text{'index_return'});