Handle hostnames with upper-case letters
[webmin.git] / file / search.cgi
1 #!/usr/local/bin/perl
2 # search.cgi
3 # Find files under some directory
4
5 require './file-lib.pl';
6 $disallowed_buttons{'search'} && &error($text{'ebutton'});
7 &ReadParse();
8 &switch_acl_uid();
9 print "Content-type: text/plain\n\n";
10 if (!&can_access($in{'dir'})) {
11         print $text{'search_eaccess'},"\n";
12         }
13
14 $in{'dir'} =~ s/^\/+/\//g;
15 if ($in{'dir'} ne '/') {
16         $in{'dir'} =~ s/\/$//;
17         }
18 $cmd = "find ".quotemeta(&unmake_chroot($in{'dir'}))." -name ".quotemeta($in{'match'});
19 if ($in{'type'}) {
20         $cmd .= " -type $in{'type'}";
21         }
22 if ($in{'user'}) {
23         $cmd .= " -user $in{'user'}";
24         }
25 if ($in{'group'}) {
26         $cmd .= " -group $in{'group'}";
27         }
28 if ($in{'size'}) {
29         $cmd .= " -size $in{'size'}";
30         }
31 if ($in{'xdev'}) {
32         $cmd .= " -mount";
33         }
34
35 print "\n";
36 open(CMD, "$cmd 2>/dev/null |");
37 while($f = <CMD>) {
38         chop($f);
39         if (defined($in{'cont'})) {
40                 # Check the file contents for the given pattern
41                 $found = 0;
42                 if ($f =~ /\.pdf$/i && &has_command("pdftotext")) {
43                         # Convert PDF to text
44                         open(FILE, "pdftotext -raw ".quotemeta($f)." - |");
45                         }
46                 else {
47                         open(FILE, $f);
48                         }
49                 while(<FILE>) {
50                         if (/\Q$in{'cont'}\E/i) {
51                                 $found = 1;
52                                 last;
53                                 }
54                         }
55                 close(FILE);
56                 next if (!$found);
57                 }
58         local $rf = &make_chroot($f);
59         local $fil = &file_info_line($f, $rf);
60         print $fil,"\n" if (defined($fil));
61         }
62 close(CMD);
63