3 # Save, create or delete a cache_host directive
5 require './squid-lib.pl';
6 $access{'othercaches'} || &error($text{'eicp_ecannot'});
8 &lock_file($config{'squid_conf'});
10 $cache_host = $squid_version >= 2 ? "cache_peer" : "cache_host";
11 @ch = &find_config($cache_host, $conf);
12 @chd = &find_config($cache_host."_domain", $conf);
14 $dom = $ch[$in{'num'}]->{'values'}->[0];
16 # delete some directive
17 $dir = $ch[$in{'num'}];
18 splice(@ch, $in{'num'}, 1);
20 # delete any cache_host directives as well
21 for($i=0; $i<@chd; $i++) {
22 if ($chd[$i]->{'values'}->[0] eq $dom) {
23 splice(@chd, $i--, 1);
29 $whatfailed = $text{'schost_ftsc'};
30 &to_ipaddress($in{'host'}) || &to_ip6address($in{'host'}) ||
31 &error(&text('schost_emsg1',$in{'host'}));
32 $in{'proxy'} =~ /^\d+$/ ||
33 &error(&text('schost_emsg2',$in{'proxy'}));
34 $in{'icp'} =~ /^\d+$/ ||
35 &error(&text('schost_emsg3',$in{'icp'}));
36 $in{'ttl'} =~ /^\d*$/ ||
37 &error(&text('schost_emsg4',$in{'ttl'}));
38 $in{'weight'} =~ /^\d*$/ ||
39 &error(&text('schost_emsg5',$in{'weight'}));
41 @vals = ($in{'host'}, $in{'type'}, $in{'proxy'}, $in{'icp'});
42 @optlist = ("proxy-only", "weight", "ttl", "no-query", "default",
43 "round-robin", "multicast-responder",
44 "closest-only", "no-digest", # squid 2+
45 "no-netdb-exchange", "no-delay",
46 "connect-timeout", "digest-url", # squid 3+
47 "allow-miss", "max-conn", "htcp", "forceddomain",
48 "originserver", "ssl");
49 foreach $o (@optlist) {
50 $def = $in{$o."_def"};
52 if (!$def) { push(@vals, "$o=$in{$o}"); }
54 elsif ($in{$o}) { push(@vals, $o); }
56 if ($in{'login'} == 1) {
57 push(@vals, "login=".$in{'login_user'}.":".$in{'login_pass'});
59 elsif ($in{'login'} == 2) {
60 push(@vals, "login=PASS");
62 elsif ($in{'login'} == 3) {
63 push(@vals, "login=*:".$in{'login_pass2'});
66 # Add any old options that are not supported
68 %supported = map { $_, 1 } @optlist;
69 $supported{'login'} = 1;
70 @ech = @{$ch[$in{'num'}]->{'values'}};
71 for($i=4; $i<@ech; $i++) {
72 if ($ech[$i] =~ /^(\S+)=(\S+)$/ ||
73 $ech[$i] =~ /^(\S+)$/) {
74 if (!$supported{$1}) {
75 push(@vals, $ech[$i]);
81 $dir = { 'name' => $cache_host, 'values' => \@vals };
82 @chdl = split(/\s+/, $in{'doq'});
83 foreach $dontq (split(/\s+/, $in{'dontq'})) { push(@chdl, "!$dontq"); }
85 # adding a new cache_host and domains
88 push(@chd, { 'name' => $cache_host."_domain",
89 'values' => [ $vals[0], @chdl ] });
93 # updating existing cache_host and domains
94 $ch[$in{'num'}] = $dir;
96 # replace the first cache_host_domain directive
97 # for this host and remove the rest
98 $newv = { 'name' => $cache_host."_domain",
99 'values' => [ $vals[0], @chdl ] };
100 for($i=0; $i<@chd; $i++) {
101 if ($chd[$i]->{'values'}->[0] eq $dom) {
103 splice(@chd, $i--, 1);
111 if (!$already) { push(@chd, $newv); }
114 # remove all the old cache_host_domain directives
116 @chd = grep { $_->{'values'}->[0] ne $dom } @chd;
120 &save_directive($conf, $cache_host, \@ch);
121 &save_directive($conf, $cache_host."_domain", \@chd);
123 &unlock_file($config{'squid_conf'});
124 &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
125 "host", $dir->{'values'}->[0], \%in);
126 &redirect("edit_icp.cgi");