-
Notifications
You must be signed in to change notification settings - Fork 16
/
list.cgi
150 lines (127 loc) · 4.12 KB
/
list.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
#!/usr/bin/perl
# File manager written in perl
require './filemin-lib.pl';
use lib './lib';
use File::MimeInfo;
use Mojo::JSON;
&ReadParse();
get_paths();
print_ajax_header();
my @errors;
# SELinux context
my $selinux = &is_selinux_enabled();
my %context = ();
# To search
if($in{'query'}) {
if($in{'caseins'}) {
$criteria = '-iname';
} else {
$criteria = '-name'
}
@list = split('\n', &backquote_logged(
"find ".quotemeta($cwd)." $criteria ".quotemeta("*$in{'query'}*")));
# Get SELinux context
if(selinux) {
$command = "ls -dZ ".join(' ', map { qq /"$_"/ } @list);
$result = `$command`;
@searray = map { [split(/ /, $_, 5)] } split(/\n/, $result);
%context = map { $_->[4] => $_->[3] } @searray;
}
@list = map { [ $_, lstat($_), mimetype($_), -d, -l $_, $context{$_} ] } @list;
# Or not to search
} else {
unless (opendir ( DIR, $cwd )) {
$path="";
push @errors, "$text{'error_opendir'} <b>$cwd</b> $!";
} else {
# Push file names with full paths to array, filtering out "." and ".."
@list = map { &simplify_path("$cwd/$_") } grep { $_ ne '.' && $_ ne '..' } readdir(DIR);
closedir(DIR);
# Filter out not allowed entries
if($remote_user_info[0] ne 'root' && $allowed_paths[0] ne '$ROOT') {
# Leave only allowed
for $path (@allowed_paths) {
my $slashed = $path;
$slashed .= "/" if ($slashed !~ /\/$/);
push @tmp_list, grep { $slashed =~ /^\Q$_\/\E/ ||
$_ =~ /\Q$slashed\E/ } @list;
}
# Remove duplicates
my %hash = map { $_, 1 } @tmp_list;
@list = keys %hash;
}
# Get SELinux context
if(selinux) {
$command = "ls -dZ ".join(' ', map { qq /"$_"/ } @list);
$result = `$command`;
@searray = map { [split(/ /, $_, 5)] } split(/\n/, $result);
%context = map { $_->[4] => $_->[3] } @searray;
}
# Get info about directory entries
@info = map { [ $_, lstat($_), mimetype($_), -d, -l $_, $context{$_} ] } @list;
# Filter out folders
@folders = map {$_} grep {$_->[15] == 1 } @info;
# Filter out files
@files = map {$_} grep {$_->[15] != 1 } @info;
# Sort stuff by name
@folders = sort { $a->[0] cmp $b->[0] } @folders;
@files = sort { $a->[0] cmp $b->[0] } @files;
# Recreate list
undef(@list);
push @list, @folders, @files;
}
}
# Push everything to JSON
@result = ();
# That is the answer
foreach(@list) {
my $user = getpwuid($_->[5]) ? getpwuid($_->[5]) : $_->[5];
my $group = getgrgid($_->[6]) ? getgrgid($_->[6]) : $_->[6];
my $permissions = sprintf("%04o", $_->[3] & 07777);
my $type = $_->[14];
my $link_target = "";
my $link_target_mime = "";
my $size;
my $link = $_->[0];
if($in{'sizes'} & $_->[15]) {
$size = &recursive_disk_usage($link);
} else {
$size = $_->[8];
}
if($_->[16]) {
$link_target = readlink($link);
$link_target_mime = mimetype($link_target);
unless(-e &resolve_links($link)) {
$_->[16] = 'broken'
}
}
$link =~ s/\Q$cwd\E\///;
$link =~ s/^\///g;
$link = decode('UTF-8', $link, Encode::FB_CROAK);
my %entry = (
'name' => $link,
'type' => $type,
'size' => $size,
'user' => $user,
'group' => $group,
'permissions' => $permissions,
'directory' => $_->[15],
'symlink' => $_->[16],
'atime' => $_->[9],
'mtime' => $_->[10],
'archive' => (index($type, "zip") != -1 or index($type, "compressed") != -1),
'link_target' => $link_target,
'link_target_mime' => $link_target_mime,
'selinux_context' => $_->[17]
);
push @result, \%entry;
}
if (scalar(@errors) > 0) {
$result = '';
foreach $error(@errors) {
$result.= "$error\\n";
}
print '{"error": "'.$result.'"}';
} else {
print Mojo::JSON::to_json(\@result);
}