-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebloc-to-csv.php
More file actions
167 lines (138 loc) · 5.06 KB
/
webloc-to-csv.php
File metadata and controls
167 lines (138 loc) · 5.06 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* WebLoc to CSV Converter
* Scans a directory for .webloc files and exports their information to CSV
*/
// ============================================================================
// CONFIGURATION - Set your path here
// ============================================================================
$basePath = 'xxxx'; // Change this to your target directory
$outputCsv = 'webloc_export_' . date('Y-m-d_H-i-s') . '.csv';
// ============================================================================
// MAIN SCRIPT
// ============================================================================
// Validate base path
if (!is_dir($basePath)) {
die("Error: Base path does not exist: $basePath\n");
}
// Normalize base path (remove trailing slash)
$basePath = rtrim($basePath, '/');
echo "Scanning directory: $basePath\n";
echo "Output file: $outputCsv\n\n";
// Open CSV file for writing
$csvFile = fopen($outputCsv, 'w');
if (!$csvFile) {
die("Error: Could not create CSV file: $outputCsv\n");
}
// Write CSV header
fputcsv($csvFile, ['Filename', 'URL', 'Creation Date', 'Relative Path']);
// Initialize counters
$totalFiles = 0;
$successCount = 0;
$errorCount = 0;
// Recursively scan for .webloc files
try {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($basePath, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
// Only process .webloc files
if ($file->isFile() && strtolower($file->getExtension()) === 'webloc') {
$totalFiles++;
$filePath = $file->getPathname();
echo "Processing: $filePath\n";
// Extract information
$filename = $file->getFilename();
$url = extractUrlFromWebloc($filePath);
$creationDate = getMacCreationDate($filePath);
// Calculate relative path from base path
$relativePath = str_replace($basePath, '', $file->getPath());
$relativePath = ltrim($relativePath, '/');
if ($relativePath === '') {
$relativePath = '/';
}
// Write to CSV
if ($url !== null) {
fputcsv($csvFile, [$filename, $url, $creationDate, $relativePath]);
$successCount++;
} else {
echo " ⚠️ Warning: Could not extract URL from $filename\n";
fputcsv($csvFile, [$filename, '[ERROR: Could not extract URL]', $creationDate, $relativePath]);
$errorCount++;
}
}
}
} catch (Exception $e) {
fclose($csvFile);
die("Error scanning directory: " . $e->getMessage() . "\n");
}
// Close CSV file
fclose($csvFile);
// Print summary
echo "\n" . str_repeat('=', 60) . "\n";
echo "SUMMARY\n";
echo str_repeat('=', 60) . "\n";
echo "Total .webloc files found: $totalFiles\n";
echo "Successfully processed: $successCount\n";
echo "Errors: $errorCount\n";
echo "Output saved to: $outputCsv\n";
/**
* Get the actual creation date (birth time) of a file on macOS
*
* Uses the stat command to retrieve the birth time, which is the actual
* file creation date on macOS (not inode change time)
*
* @param string $filePath Path to the file
* @return string The formatted creation date or 'Unknown' on failure
*/
function getMacCreationDate($filePath) {
// Escape the file path for shell command
$escapedPath = escapeshellarg($filePath);
// Use macOS stat command with -f %B to get birth time (creation time) as Unix timestamp
$command = "stat -f %B $escapedPath 2>/dev/null";
$timestamp = trim(shell_exec($command));
if ($timestamp && is_numeric($timestamp)) {
return date('Y-m-d H:i:s', $timestamp);
}
// Fallback to filectime if stat command fails
return date('Y-m-d H:i:s', filectime($filePath));
}
/**
* Extract URL from a .webloc file
*
* .webloc files are XML plist files that contain a URL
*
* @param string $filePath Path to the .webloc file
* @return string|null The extracted URL or null on failure
*/
function extractUrlFromWebloc($filePath) {
// Read file contents
$contents = @file_get_contents($filePath);
if ($contents === false) {
return null;
}
// Suppress XML errors for malformed files
libxml_use_internal_errors(true);
// Try to parse as XML plist
$xml = @simplexml_load_string($contents);
if ($xml === false) {
libxml_clear_errors();
return null;
}
// Navigate the plist structure to find the URL
// Structure: <plist><dict><key>URL</key><string>URL_HERE</string></dict></plist>
if (isset($xml->dict)) {
$dict = $xml->dict;
$key = null;
foreach ($dict->children() as $child) {
if ($child->getName() === 'key' && (string)$child === 'URL') {
$key = $child;
} elseif ($key !== null && $child->getName() === 'string') {
return (string)$child;
}
}
}
return null;
}
?>