-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextcloudtest.php
More file actions
103 lines (83 loc) · 3.08 KB
/
nextcloudtest.php
File metadata and controls
103 lines (83 loc) · 3.08 KB
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
<?php $sqlpath = $_SERVER['DOCUMENT_ROOT'];
$sqlpath .= "/sql-connect.php";
include_once($sqlpath);
$nextcloud_url = "https://cloud.bkconnor.com/remote.php/dav/files/brian/";
$username = "brian";
$password = "p!CVp4;f7-!Rqya";
// Configuration
$excluded_folders = ['Storage', 'NVME'];
$file_limit = 100; // Maximum number of files to return
function fetchNextcloudFiles($url, $username, $password, $excluded_folders, &$file_count, $file_limit, $current_path = '') {
// Stop if we've reached the limit
if ($file_count >= $file_limit) {
return [];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $current_path);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Depth: 1",
"Content-Type: application/xml"
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PROPFIND");
$response = curl_exec($ch);
curl_close($ch);
if (!$response) {
return [];
}
$xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
$xml->registerXPathNamespace('d', 'DAV:');
$files = [];
foreach ($xml->xpath('//d:response') as $item) {
// Stop if we've reached the limit
if ($file_count >= $file_limit) {
break;
}
$href = (string)$item->xpath('d:href')[0];
$name = basename(urldecode($href));
// Skip the current directory itself
if ($name === '') continue;
// Check if this is a directory (collection)
$is_dir = false;
$resource_type = $item->xpath('d:propstat/d:prop/d:resourcetype');
if ($resource_type && $resource_type[0]->children('d', true)->collection) {
$is_dir = true;
}
$full_path = $current_path . $name;
// Skip excluded folders
if ($is_dir && in_array($name, $excluded_folders)) {
continue;
}
if ($is_dir) {
// Recursively fetch files from this directory
$subdir_files = fetchNextcloudFiles($url, $username, $password, $excluded_folders, $file_count, $file_limit, $full_path . '/');
$files = array_merge($files, $subdir_files);
} else {
// Add file to the list
$files[] = [
'name' => $name,
'path' => $href,
'url' => "https://notes.bkconnor.com" . $href
];
$file_count++;
// Stop if we've reached the limit
if ($file_count >= $file_limit) {
break;
}
}
}
return $files;
}
// Initialize file counter
$file_count = 0;
// Fetch files with limit
$files = fetchNextcloudFiles($nextcloud_url, $username, $password, $excluded_folders, $file_count, $file_limit);
// Return a JSON response for JavaScript to use
header('Content-Type: application/json');
echo json_encode([
'files' => $files,
'total' => count($files),
'limit_reached' => ($file_count >= $file_limit)
], JSON_PRETTY_PRINT);
?>