-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-file-list.php
85 lines (74 loc) · 2.03 KB
/
get-file-list.php
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
<?php
// LICENSE: GNU GPL v3 You should have received a copy of the GNU General
// Public License along with this program. If not, see
// https://www.gnu.org/licenses/.
//////////////////////////// get-file-list.php //////////////////////////
// used in conjunction with dialog.js, this populates a menu with the //
// files in a given folder that the user can choose frm //
////////////////////////////////////////////////////////////////////////
session_start();
require_once 'send-as-json.php';
$json = file_get_contents('php://input') ?? false;
if ($json === false) {
rage_quit(new StdClass(), 'JSON not posted.',400);
}
$data = json_decode($json) ?? false;
if ($data === false) {
rage_quit(new StdClass(), 'Could not parse posted JSON.', 400);
}
if (!isset($data->folder)) {
}
$dir = $data->folder;
if ($dir == '') {
$dir = getenv('HOME') ?? '';
}
if ($dir == '') {
$dir = '/home';
}
if (file_exists("../php/libauthentication.php")) {
require_once "../php/libauthentication.php";
if (!has_authentication("$dir")) {
rage_quit(new StdClass(), "no authority to see files", 403);
}
}
chdir($dir);
$files = scandir($dir);
$realdir = getcwd();
$listData = array();
function mycmp($a, $b) {
if ($a == '..') {
return -1;
}
if ($b == '..') {
return 1;
}
if ((substr($a,0,1) == '.') and (substr($b,0,1) != '.')) {
return 1;
}
if ((substr($b,0,1) == '.') and (substr($a,0,1) != '.')) {
return -1;
}
if ((is_dir($a)) and (!(is_dir($b)))) {
return -1;
}
if ((is_dir($b)) and (!(is_dir($a)))) {
return 1;
}
return strcasecmp($a, $b);
}
usort($files, "mycmp");
for ($i=0; $i<count($files); $i++) {
if ($files[$i] != '.') {
$x = new stdClass();
$x->dirName = $dir;
$x->name = $files[$i];
$x->isFolder = (is_dir($files[$i]));
array_push($listData, $x);
}
}
$rv = new StdClass();
$rv->listData = $listData;
$rv->dir = $realdir;
$rv->error = false;
send_as_json($rv, 200);
exit(0);