Handle hostnames with upper-case letters
[webmin.git] / squid / clear.cgi
1 #!/usr/local/bin/perl
2 # clear.cgi
3 # Delete the cache, chown the directory to the correct user and re-run squid -z
4
5 require './squid-lib.pl';
6 $access{'rebuild'} || &error($text{'clear_ecannot'});
7 &ReadParse();
8 $config{'cache_dir'} =~ /^\/\S+/ || &error("Cache directory not set");
9 $conf = &get_config();
10
11 if (!$in{'confirm'}) {
12         # Ask the user if he is sure
13         &ui_print_header(undef, $text{'clear_header'}, "");
14         print $text{'clear_msgclear'},"<br>\n";
15         print $text{'clear_msgclear2'},"<p>\n";
16         print "<form action=clear.cgi>\n";
17         print "<center><input type=submit name=confirm ",
18               "value=\"$text{'clear_buttclear'}\"></center></form>\n";
19
20         if (&has_command($config{'squidclient'})) {
21                 # Show form to clear just one URL
22                 print &ui_hr();
23                 print &ui_form_start("purge.cgi");
24                 print "<b>$text{'clear_url'}</b>\n";
25                 print &ui_textbox("url", undef, 50),"\n";
26                 print &ui_submit($text{'clear_ok'}),"\n";
27                 print &ui_form_end();
28                 }
29
30         &ui_print_footer("", $text{'clear_return'});
31         exit;
32         }
33
34 &ui_print_unbuffered_header(undef, $text{'clear_header'}, "");
35
36 # Stop squid (if running)
37 if ($pidstruct = &find_config("pid_filename", $conf)) {
38         $pidfile = $pidstruct->{'values'}->[0];
39         }
40 else { $pidfile = $config{'pid_file'}; }
41 if (open(PID, $pidfile)) {
42         <PID> =~ /(\d+)/; $pid = $1;
43         close(PID);
44         }
45 if ($pid && kill(0, $pid)) {
46         print "$text{'clear_stop'}<br>\n";
47         &system_logged("$config{'squid_path'} -f $config{'squid_conf'} ".
48                        "-k shutdown >/dev/null 2>&1");
49         for($i=0; $i<40; $i++) {
50                 if (!kill(0, $pid)) { last; }
51                 sleep(1);
52                 }
53         print "$text{'clear_done'}<p>\n";
54         $stopped++;
55         }
56
57 # Get list of cache dirs
58 if (@cachestruct = &find_config("cache_dir", $conf)) {
59         if ($squid_version >= 2.3) {
60                 @caches = map { $_->{'values'}->[1] } @cachestruct;
61                 }
62         else {
63                 @caches = map { $_->{'values'}->[0] } @cachestruct;
64                 }
65         }
66 else { @caches = ( $config{'cache_dir'} ); }
67
68 # Delete old cache files and re-create with same permissions!
69 print "$text{'clear_del'}<br>\n";
70 foreach $c (@caches) {
71         @st = stat($c);
72         if (@st) {
73                 &system_logged("rm -rf $c/* >/dev/null 2>&1");
74                 #mkdir($c, 0755);       # only remove contents
75                 #chown($st[4], $st[5], $c);
76                 #chmod($st[2], $c);
77                 }
78         }
79 print "$text{'clear_done'}<p>\n";
80
81 $cmd = "$config{'squid_path'} -f $config{'squid_conf'} -z";
82 print &text('clear_init',$cmd)."<br>\n";
83 print "<pre>\n";
84 &additional_log('exec', undef, $cmd);
85 open(INIT, "$cmd 2>&1 |");
86 while(<INIT>) {
87         print &html_escape($_);
88         }
89 close(INIT);
90 print "</pre>\n";
91 print "$text{'clear_done'}<p>\n";
92
93 # Try to re-start squid
94 if ($stopped) {
95         $temp = &transname();
96         &system_logged("$config{'squid_path'} -sY -f $config{'squid_conf'} >$temp 2>&1 </dev/null &");
97         sleep(3);
98         $errs = `cat $temp`;
99         unlink($temp);
100         if ($errs) {
101                 &system_logged("$config{'squid_path'} -k shutdown -f $config{'squid_conf'} >/dev/null 2>&1");
102                 print "$text{'clear_failrestart'}<br>\n";
103                 print "<pre>$errs</pre>\n";
104                 }
105         }
106
107 &webmin_log("clear");
108 &ui_print_footer("", $text{'clear_return'});
109