Add support for Apache config indent formatting

This commit is contained in:
Ilia Rostovtsev
2021-11-03 21:21:19 +03:00
parent ce765d28bb
commit 9cb874c605
72 changed files with 173 additions and 2 deletions

View File

@ -24,7 +24,17 @@ $found || &error($text{'manual_efile'});
print &ui_form_start("allmanual_save.cgi", "form-data");
print &ui_hidden("file", $in{'file'}),"\n";
$data = &read_file_contents($in{'file'});
# Display nicely too
if ($config{'format_config'}) {
$data = &read_file_lines($in{'file'}, 1);
&format_config($data);
$data = join("\n", @{$data});
}
else {
$data = &read_file_contents($in{'file'});
}
print &ui_textarea("data", $data, 20, 80, undef, undef,
"style='width:100%'"),"<br>\n";
print &ui_form_end([ [ "save", $text{'save'} ] ]);

View File

@ -27,6 +27,7 @@ if ($config{'test_manual'}) {
}
}
unlink($temp);
&format_config_file($in{'file'}) if ($config{'format_config'});
&webmin_log("manual", undef, undef, { 'file' => $in{'file'} });
&redirect("index.cgi?mode=global");

View File

@ -2124,5 +2124,90 @@ if (@cst && @rst && $cst[9] > $rst[9]) {
return 0;
}
# format_config(conf-lines-ref, [indent])
# Formats Apache config lines with default
# four spaces of indent for each block
sub format_config
{
my ($conf_lref, $indent) = @_;
# Default single indent equals 4 spaces
$indent ||= 4;
$indent = " " x $indent;
# At first check if Apache blocks are ballanced
my $conf_block_opening;
my $conf_block_closing;
foreach my $l (@{$conf_lref}) {
# If line doesn't start with # disregard of trailing spaces
if ($l !~ /^\s*#/) {
# This is a new block, count it
if ($l =~ /(<[a-zA-Z]+).*>/) {
$conf_block_opening++;
}
# This is a new closing block, count it
if ($l =~ /(<\/[a-zA-Z]+).*>/) {
$conf_block_closing++;
}
}
}
# If the number of closing and opening blocks
# is the same then generate proper indents
if ($conf_block_opening == $conf_block_closing) {
my $conf_lvl = 0;
foreach my $l (@{$conf_lref}) {
my $indent_current = $indent x $conf_lvl;
# If line doesn't start with # disregard of trailing spaces
if ($l !~ /^\s*#/) {
# Indent up next line if a new block
if ($l =~ /(<[a-zA-Z]+).*>/) {
$conf_lvl++;
}
# Indent down next line if a closing block
if ($l =~ /(<\/[a-zA-Z]+).*>/) {
$conf_lvl--;
# Change current indent right now as it is a closing block
$indent_current = $indent x $conf_lvl;
}
}
# Replace beginning spaces with needed indent (which we could make configurable)
$l =~ s/^\s*/$indent_current/
if($l);
}
}
}
# format_config(filename, [indent])
# Formats Apache config given file
sub format_config_file
{
my ($file, $indent) = @_;
# Lock file
&lock_file($file);
# Open file
my $conf_lref = &read_file_lines($file);
# Format
&format_config($conf_lref, $indent);
# Write file
&flush_file_lines($file);
# Unlock file
&unlock_file($file);
}
1;

View File

@ -16,3 +16,4 @@ auto_mods=1
show_names=0
allow_virtualmin=0
pid_file=/run/httpd/httpd.pid
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
show_names=0
allow_virtualmin=0
pid_file=/run/httpd/httpd.pid
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
show_names=0
allow_virtualmin=0
pid_file=/run/httpd/httpd.pid
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
show_names=0
allow_virtualmin=0
pid_file=/etc/httpd/run/httpd.pid
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
show_names=0
allow_virtualmin=0
pid_file=/run/httpd/httpd.pid
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
show_names=0
allow_virtualmin=0
pid_file=/run/httpd/httpd.pid
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
show_names=0
allow_virtualmin=0
pid_file=/run/httpd/httpd.pid
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -13,3 +13,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -21,3 +21,4 @@ apachectl_path=/usr/sbin/apache2ctl
show_names=0
apply_cmd=/usr/sbin/apache2ctl graceful
allow_virtualmin=0
format_config=1

View File

@ -22,3 +22,4 @@ apachectl_path=/usr/sbin/apache2ctl
show_names=0
apply_cmd=/usr/sbin/apache2ctl graceful
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
pid_file=/var/run/httpd.pid
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ auto_mods=1
pid_file=/var/run/httpd.pid
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ show_names=0
defines_file=/etc/conf.d/apache2
defines_name=APACHE2_OPTS
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -13,3 +13,4 @@ defines_mods=/usr/lib/apache /usr/lib/apache2 /usr/lib/apache-extramodules /usr/
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -13,3 +13,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -14,3 +14,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -14,3 +14,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -14,3 +14,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ defines_name=OPTIONS
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -13,3 +13,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ defines_name=OPTIONS
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ defines_name=OPTIONS
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -10,3 +10,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -19,3 +19,4 @@ pid_file=/var/run/httpd/httpd.pid
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -13,3 +13,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ test_always=0
test_apachectl=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ apachectl_path=/usr/sbin/apachectl
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -18,3 +18,4 @@ virt_file=/etc/apache2/vhosts.d
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -20,3 +20,4 @@ defines_name=APACHE_SERVER_FLAGS
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -13,3 +13,4 @@ test_config=1
show_list=0
httpd_path=/opt/sbin/httpd
max_servers=100
format_config=1

View File

@ -15,3 +15,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -13,3 +13,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -16,3 +16,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ test_apachectl=1
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -17,3 +17,4 @@ stop_cmd=net stop apache2
auto_mods=1
show_names=0
allow_virtualmin=0
format_config=1

View File

@ -5,6 +5,7 @@ max_servers=Maximum number of servers to display,0,5
virt_file=File or directory to add virtual servers to,3,httpd.conf
virt_name=Filename pattern for virtual servers,3,Default ($DOM.conf)
link_dir=Directory to create links in for new virtual servers,3,None
format_config=Format indents for config file when editing manually?,1,1-Yes,0-No
test_config=Test config file before applying changes?,1,1-Yes,0-No
test_manual=Test config file after manual changes?,1,1-Yes,0-No
test_always=Test config file after other changes?,1,1-Yes,0-No

View File

@ -89,8 +89,15 @@ if (!defined($start)) {
$end = @$lref - 1;
}
for($i=$start; $i<=$end; $i++) {
$buf .= $lref->[$i]."\n";
push(@buf, $lref->[$i]);
}
# Display nicely too
if ($config{'format_config'}) {
&format_config(\@buf);
}
$buf = join("\n", @buf);
print &ui_textarea("directives", $buf, 15, 80, undef, undef,
"style='width:100%'"),"<br>\n";
print &ui_submit($text{'save'});

View File

@ -64,6 +64,7 @@ if (!defined($start)) {
$end = @$lref - 1;
}
splice(@$lref, $start, $end-$start+1, @dirs);
&format_config($lref) if ($config{'format_config'});
&flush_file_lines();
if ($config{'test_manual'}) {
$err = &test_config();