-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsync.php
More file actions
283 lines (231 loc) · 7.92 KB
/
Copy pathsync.php
File metadata and controls
283 lines (231 loc) · 7.92 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env php
<?php
/**
* SPDX-FileCopyrightText: 2016-2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
include_once __DIR__ . '/vendor/autoload.php';
use CraftCms\UrlValidator\UrlValidationException;
use CraftCms\UrlValidator\UrlValidator;
if (php_sapi_name() !== 'cli') {
die('Can only be invoked from CLI');
}
$allVersionsJson = file_get_contents('https://apps.nextcloud.com/api/v1/platforms.json');
if ($allVersionsJson === false) {
die('Unable to read platforms.json');
}
$allVersions = json_decode($allVersionsJson, true);
$supportedVersionObjects = array_filter($allVersions, fn (array $ver): bool => $ver['isSupported'] && str_ends_with($ver['version'], '.0.0'));
$supportedVersions = array_map(fn (array $ver): string => $ver['version'], $supportedVersionObjects);
const MAX_SCREENSHOT_SIZE = 2 * 1024 * 1024; // 2 MiB
const HTTP_TIMEOUT_S = 30;
const RETRY_BACKOFF_S = 6 * 3600; // don't retry a failed fetch more often than this
/**
* @param string $cacheUrl path to screenshot in cache
* @param string $url HTTP URL of screenshot
* @param string $message warning message to display
*/
function generateWarningImage(string $cacheUrl, string $url, string $message): void {
$data = imagecreatetruecolor(640, 360);
if ($data === false) {
// This should never happen, but just in case, replace current cache data with an empty file instead
file_put_contents($cacheUrl, '');
echo(sprintf("Synced url %s (%s, unable to generate warning image)\n", $url, $message));
return;
}
$textColorError = imagecolorallocate($data, 255, 0, 0); // red
$textColorNormal = imagecolorallocate($data, 255, 255, 255); // white
imagestring($data, 5, 8, 150, 'Preview not available', $textColorError);
imagestring($data, 5, 8, 170, $message, $textColorNormal);
imagestring($data, 5, 8, 190, basename($url), $textColorNormal);
imagepng($data, $cacheUrl);
echo(sprintf("Synced url %s (%s)\n", $url, $message));
}
/**
* Marks a screenshot fetch as failed, so it gets retried later instead of being
* treated as permanently done, and renders a placeholder in the meantime.
*
* @param string $cacheUrl path to screenshot in cache
* @param string $failMarker path to the retry marker for this screenshot
* @param string $url HTTP URL of screenshot
* @param string $message warning message to display
*/
function markFailedFetch(string $cacheUrl, string $failMarker, string $url, string $message): void {
touch($failMarker);
generateWarningImage($cacheUrl, $url, $message);
}
/**
* Fetches a URL over HTTP, pinning each connection to a pre-validated list of
* IP addresses for the URL's hostname.
*
* @param UrlValidator $validator used to validate each redirect target
* @param string $url URL to fetch
* @param string[] $ips validated IP addresses the initial host resolves to
* @return string|false the response body, or false on failure
*/
function fetchScreenshot(UrlValidator $validator, string $url, array $ips): string|false {
$maxRedirects = 3;
$redirects = 0;
$currentUrl = $url;
$currentIps = $ips;
while (true) {
$host = parse_url($currentUrl, PHP_URL_HOST);
$port = parse_url($currentUrl, PHP_URL_PORT)
?: (str_starts_with($currentUrl, 'https://') ? 443 : 80);
if (!is_string($host) || $host === '') {
return false;
}
$resolveEntries = array_map(
function (string $ip) use ($host, $port): string {
$ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false
? "[$ip]"
: $ip;
return "$host:$port:$ip";
},
$currentIps,
);
$body = '';
$abortedByLimit = false;
$ch = curl_init($currentUrl);
curl_setopt_array($ch, [
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_TIMEOUT => HTTP_TIMEOUT_S,
CURLOPT_USERAGENT => 'nextcloud-usercontent-sync/1.0',
CURLOPT_RESOLVE => $resolveEntries,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
CURLOPT_WRITEFUNCTION => function ($curl, $chunk) use (&$body, &$abortedByLimit): int {
if (strlen($body) + strlen($chunk) > MAX_SCREENSHOT_SIZE + 1) {
$remaining = (MAX_SCREENSHOT_SIZE + 1) - strlen($body);
if ($remaining > 0) {
$body .= substr($chunk, 0, $remaining);
}
$abortedByLimit = true;
return -1;
}
$body .= $chunk;
return strlen($chunk);
},
]);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
$errno = curl_errno($ch);
curl_close($ch);
if ($status >= 300 && $status < 400 && is_string($redirectUrl) && $redirectUrl !== '') {
$redirects++;
if ($redirects > $maxRedirects) {
return false;
}
// Validate the redirect target the same way as the initial URL before following it
try {
$currentIps = $validator->validate($redirectUrl);
} catch (UrlValidationException $e) {
return false;
}
$currentUrl = $redirectUrl;
continue;
}
if ($abortedByLimit) {
return $body;
}
if ($errno !== 0 || $status < 200 || $status >= 300) {
return false;
}
return $body;
}
}
/**
* @param UrlValidator $validator
* @param array $screenshot decoded JSON of a single screenshot entry
*/
function handleScreenshot(UrlValidator $validator, array $screenshot): void {
$url = $screenshot['url'];
$trimmedUrl = trim($url);
if (!str_starts_with($trimmedUrl, 'https://')) {
return;
}
$cacheUrl = __DIR__ . '/cache/' . base64_encode($url);
$failMarker = __DIR__ . '/cache-failed/' . base64_encode($url);
if (file_exists($cacheUrl)) {
if (!file_exists($failMarker)) {
// Already fetched successfully, nothing to do
return;
}
if ((time() - filemtime($failMarker)) < RETRY_BACKOFF_S) {
// Fetch failed recently, don't hammer the origin - try again later
return;
}
// Backoff has elapsed, fall through and retry the fetch
}
if (!is_dir(dirname($failMarker))) {
mkdir(dirname($failMarker), 0755, true);
}
try {
$ips = $validator->validate($trimmedUrl);
} catch (UrlValidationException $e) {
markFailedFetch($cacheUrl, $failMarker, $url, 'Failed to fetch image');
return;
}
$data = fetchScreenshot($validator, $trimmedUrl, $ips);
if ($data === false) {
markFailedFetch($cacheUrl, $failMarker, $url, 'Failed to fetch image');
return;
}
if (strlen($data) > MAX_SCREENSHOT_SIZE) {
markFailedFetch($cacheUrl, $failMarker, $url, 'Image exceeds file size limit');
return;
}
$tempUrl = tempnam(sys_get_temp_dir(), 'screenshot');
if ($tempUrl === false) {
// This should never happen, but just in case, treat this as an internal error
// Do nothing for now and try again later
return;
}
file_put_contents($tempUrl, $data);
$mimeType = mime_content_type($tempUrl);
if ($mimeType === false || !str_starts_with($mimeType, 'image/')) {
unlink($tempUrl);
markFailedFetch($cacheUrl, $failMarker, $url, 'Image not recognized');
return;
}
rename($tempUrl, $cacheUrl);
if (file_exists($failMarker)) {
unlink($failMarker);
}
echo(sprintf("Synced url %s\n", $url));
}
/**
* @param array $apps decoded JSON from appstore
*/
function handleApps(array $apps): void {
$validator = new UrlValidator();
foreach ($apps as $app) {
foreach ($app['screenshots'] as $screenshot) {
try {
handleScreenshot($validator, $screenshot);
} catch (\Throwable $e) {
// A single broken screenshot must never abort the whole sync run
echo(sprintf("Error while syncing %s: %s\n", $screenshot['url'] ?? '?', $e->getMessage()));
}
}
}
}
foreach($supportedVersions as $version) {
$json = file_get_contents(
sprintf('https://apps.nextcloud.com/api/v1/platform/%s/apps.json', $version)
);
if ($json === false) {
echo(sprintf("Unable to read apps.json for version %s\n", $version));
continue;
}
$apps = json_decode($json, true);
handleApps($apps);
}
$json = file_get_contents('https://apps.nextcloud.com/api/v1/appapi_apps.json');
if ($json === false) {
die('Unable to read appapi_apps.json');
}
$apps = json_decode($json, true);
handleApps($apps);