s3.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /**
  3. * Minimal S3 client for Hetzner Object Storage (or any S3-compatible store).
  4. * Implements AWS Signature v4 in plain PHP — no SDK, no Composer.
  5. *
  6. * - Presigned GET → visitors load gallery images directly from S3
  7. * - Signed PUT → the webhost streams uploaded originals/thumbs to S3
  8. * - Signed DELETE → server-side cleanup when images/galleries are removed
  9. *
  10. * Addressing style is configurable via s3.path_style:
  11. * path-style (default) → https://<endpoint-host>/<bucket>/<key>
  12. * virtual-hosted-style → https://<bucket>.<endpoint-host>/<key>
  13. * Hetzner Object Storage serves path-style reliably; virtual-hosted-style
  14. * requires the bucket to resolve as a TLS subdomain of the endpoint.
  15. */
  16. declare(strict_types=1);
  17. /** Percent-encode an object key, keeping the "/" separators. */
  18. function s3_encode_key(string $key): string
  19. {
  20. return implode('/', array_map('rawurlencode', explode('/', $key)));
  21. }
  22. /** Bare host of the configured endpoint, e.g. "fsn1.your-objectstorage.com". */
  23. function s3_endpoint_host(): string
  24. {
  25. return (string)parse_url(config('s3.endpoint'), PHP_URL_HOST);
  26. }
  27. /** ":port" suffix when the endpoint pins a non-default port, else "". */
  28. function s3_endpoint_port_suffix(): string
  29. {
  30. $port = parse_url(config('s3.endpoint'), PHP_URL_PORT);
  31. return $port ? ':' . $port : '';
  32. }
  33. /** Whether to address the bucket in the path (true) or as a subdomain (false). */
  34. function s3_use_path_style(): bool
  35. {
  36. return (bool)config('s3.path_style', true);
  37. }
  38. /**
  39. * Request host. Path-style keeps the bare endpoint host; virtual-hosted-style
  40. * prepends the bucket as a DNS label (never percent-encoded).
  41. */
  42. function s3_host(): string
  43. {
  44. $host = s3_endpoint_host();
  45. $host = s3_use_path_style() ? $host : config('s3.bucket') . '.' . $host;
  46. // The signed Host header and the request host must match, port included.
  47. return $host . s3_endpoint_port_suffix();
  48. }
  49. /** Base URL for object requests: scheme + host, no trailing slash. */
  50. function s3_base_url(): string
  51. {
  52. $scheme = parse_url(config('s3.endpoint'), PHP_URL_SCHEME) ?: 'https';
  53. return $scheme . '://' . s3_host();
  54. }
  55. /**
  56. * Canonical (and actual) request path for a key. Path-style prefixes the
  57. * bucket as the first, percent-encoded path segment; virtual-hosted-style
  58. * does not, because the bucket lives in the host instead.
  59. */
  60. function s3_canonical_uri(string $key): string
  61. {
  62. $path = '/' . s3_encode_key($key);
  63. return s3_use_path_style() ? '/' . rawurlencode(config('s3.bucket')) . $path : $path;
  64. }
  65. /**
  66. * Key prefix under which one gallery's objects live: "<prefix>/<slug>".
  67. * The prefix is configurable via s3.prefix (default "galleries"); an empty
  68. * prefix puts galleries at the bucket root.
  69. */
  70. function s3_gallery_prefix(string $slug): string
  71. {
  72. $prefix = trim((string)config('s3.prefix', 'galleries'), '/');
  73. return $prefix !== '' ? "$prefix/$slug" : $slug;
  74. }
  75. /** HMAC-SHA256 chain producing the SigV4 signing key. */
  76. function s3_signing_key(string $date): string
  77. {
  78. $k = hash_hmac('sha256', $date, 'AWS4' . config('s3.secret_key'), true);
  79. $k = hash_hmac('sha256', config('s3.region'), $k, true);
  80. $k = hash_hmac('sha256', 's3', $k, true);
  81. return hash_hmac('sha256', 'aws4_request', $k, true);
  82. }
  83. /**
  84. * SigV4 query-string signing core. Separated from s3_presign() so the
  85. * algorithm can be verified against the official AWS example vectors.
  86. * Returns the full query string including X-Amz-Signature.
  87. */
  88. function s3_presign_query(
  89. string $method,
  90. string $host,
  91. string $canonicalUri,
  92. string $accessKey,
  93. string $secretKey,
  94. string $region,
  95. int $ttl,
  96. string $amzDate
  97. ): string {
  98. $date = substr($amzDate, 0, 8);
  99. $scope = $date . '/' . $region . '/s3/aws4_request';
  100. $query = [
  101. 'X-Amz-Algorithm' => 'AWS4-HMAC-SHA256',
  102. 'X-Amz-Credential' => $accessKey . '/' . $scope,
  103. 'X-Amz-Date' => $amzDate,
  104. 'X-Amz-Expires' => (string)$ttl,
  105. 'X-Amz-SignedHeaders' => 'host',
  106. ];
  107. ksort($query);
  108. $canonicalQuery = implode('&', array_map(
  109. fn($k, $v) => rawurlencode($k) . '=' . rawurlencode($v),
  110. array_keys($query),
  111. $query
  112. ));
  113. $canonicalRequest = implode("\n", [
  114. strtoupper($method),
  115. $canonicalUri,
  116. $canonicalQuery,
  117. 'host:' . $host,
  118. '',
  119. 'host',
  120. 'UNSIGNED-PAYLOAD',
  121. ]);
  122. $stringToSign = implode("\n", [
  123. 'AWS4-HMAC-SHA256',
  124. $amzDate,
  125. $scope,
  126. hash('sha256', $canonicalRequest),
  127. ]);
  128. $k = hash_hmac('sha256', $date, 'AWS4' . $secretKey, true);
  129. $k = hash_hmac('sha256', $region, $k, true);
  130. $k = hash_hmac('sha256', 's3', $k, true);
  131. $k = hash_hmac('sha256', 'aws4_request', $k, true);
  132. $signature = hash_hmac('sha256', $stringToSign, $k);
  133. return $canonicalQuery . '&X-Amz-Signature=' . $signature;
  134. }
  135. /**
  136. * Build a presigned URL for an object key. Only the Host header is signed.
  137. * Used for GET so visitors' browsers can load private images directly; uploads
  138. * go through the webhost (s3_put_file), never a presigned PUT.
  139. */
  140. function s3_presign(string $method, string $key, ?int $ttl = null): string
  141. {
  142. $ttl ??= (int)config('s3.url_ttl', 3600);
  143. $canonicalUri = s3_canonical_uri($key);
  144. $query = s3_presign_query(
  145. $method,
  146. s3_host(),
  147. $canonicalUri,
  148. config('s3.access_key'),
  149. config('s3.secret_key'),
  150. config('s3.region'),
  151. $ttl,
  152. gmdate('Ymd\THis\Z')
  153. );
  154. return s3_base_url() . $canonicalUri . '?' . $query;
  155. }
  156. function s3_presign_get(string $key, ?int $ttl = null): string
  157. {
  158. return s3_presign('GET', $key, $ttl);
  159. }
  160. /**
  161. * One curl handle per PHP process, reused across requests to the same endpoint.
  162. * curl_reset() clears the options but keeps the handle's live connection, DNS
  163. * and TLS-session caches, so the second PUT of a request (the thumbnail) and
  164. * any retry skip a full TCP + TLS handshake.
  165. */
  166. function s3_curl(): CurlHandle
  167. {
  168. static $ch = null;
  169. if ($ch === null) {
  170. $ch = curl_init();
  171. } else {
  172. curl_reset($ch);
  173. }
  174. return $ch;
  175. }
  176. /**
  177. * Whether an S3 attempt failed in a way that is worth repeating: a curl-level
  178. * failure (status 0), throttling, or a server-side error. 4xx is a real
  179. * rejection (bad key, bad signature) and must not be retried.
  180. */
  181. function s3_is_transient(int $status): bool
  182. {
  183. return $status === 0 || $status === 408 || $status === 429 || $status >= 500;
  184. }
  185. /**
  186. * Stream a local file to S3 with a signed PUT (header auth). The payload is sent
  187. * as UNSIGNED-PAYLOAD so the body is never hashed or buffered into memory — curl
  188. * streams it straight from the file handle, letting the webhost proxy originals
  189. * far larger than memory_limit. Content-Type is sent but not signed.
  190. *
  191. * Transient failures are retried up to $attempts times with a short backoff; the
  192. * file handle is rewound and the request re-signed for each try, so a dropped
  193. * connection costs one repeat instead of a failed image.
  194. * Returns [httpStatus, responseBody] of the last attempt.
  195. */
  196. function s3_put_file(string $key, string $filePath, string $contentType = 'application/octet-stream', int $attempts = 3): array
  197. {
  198. // Suppressed: a warning printed here would land in front of the JSON body
  199. // the API endpoints emit, and the failure is reported through the return.
  200. $fh = @fopen($filePath, 'rb');
  201. if ($fh === false) {
  202. return [0, 'Cannot open upload for reading'];
  203. }
  204. $size = (int)filesize($filePath);
  205. $status = 0;
  206. $body = '';
  207. for ($try = 1; $try <= $attempts; $try++) {
  208. rewind($fh);
  209. [$status, $body] = s3_put_stream($key, $fh, $size, $contentType);
  210. if (!s3_is_transient($status) || $try === $attempts) {
  211. break;
  212. }
  213. usleep(250000 * $try); // 0.25s, then 0.5s
  214. }
  215. fclose($fh);
  216. return [$status, $body];
  217. }
  218. /**
  219. * One signed PUT attempt streaming from an open, positioned file handle.
  220. *
  221. * @param resource $fh
  222. */
  223. function s3_put_stream(string $key, $fh, int $size, string $contentType): array
  224. {
  225. $host = s3_host();
  226. $amzDate = gmdate('Ymd\THis\Z');
  227. $date = substr($amzDate, 0, 8);
  228. $scope = $date . '/' . config('s3.region') . '/s3/aws4_request';
  229. $canonicalUri = s3_canonical_uri($key);
  230. $payloadHash = 'UNSIGNED-PAYLOAD';
  231. $canonicalRequest = implode("\n", [
  232. 'PUT',
  233. $canonicalUri,
  234. '',
  235. 'host:' . $host,
  236. 'x-amz-content-sha256:' . $payloadHash,
  237. 'x-amz-date:' . $amzDate,
  238. '',
  239. 'host;x-amz-content-sha256;x-amz-date',
  240. $payloadHash,
  241. ]);
  242. $stringToSign = implode("\n", [
  243. 'AWS4-HMAC-SHA256',
  244. $amzDate,
  245. $scope,
  246. hash('sha256', $canonicalRequest),
  247. ]);
  248. $signature = hash_hmac('sha256', $stringToSign, s3_signing_key($date));
  249. $authorization = 'AWS4-HMAC-SHA256 Credential=' . config('s3.access_key') . '/' . $scope
  250. . ', SignedHeaders=host;x-amz-content-sha256;x-amz-date'
  251. . ', Signature=' . $signature;
  252. $ch = s3_curl();
  253. curl_setopt_array($ch, [
  254. CURLOPT_URL => s3_base_url() . $canonicalUri,
  255. CURLOPT_UPLOAD => true, // sets method to PUT and streams CURLOPT_INFILE
  256. CURLOPT_INFILE => $fh,
  257. CURLOPT_INFILESIZE => $size,
  258. CURLOPT_RETURNTRANSFER => true,
  259. CURLOPT_CONNECTTIMEOUT => 30,
  260. CURLOPT_TIMEOUT => 0, // no cap: originals can be large
  261. // Abort a connection that has stalled below 1 KB/s for two minutes,
  262. // instead of pinning a PHP worker on a dead socket until the web
  263. // server kills it. A retry then gets a fresh connection.
  264. CURLOPT_LOW_SPEED_LIMIT => 1024,
  265. CURLOPT_LOW_SPEED_TIME => 120,
  266. CURLOPT_TCP_NODELAY => true,
  267. CURLOPT_HTTPHEADER => [
  268. 'Authorization: ' . $authorization,
  269. 'x-amz-content-sha256: ' . $payloadHash,
  270. 'x-amz-date: ' . $amzDate,
  271. 'Content-Type: ' . $contentType,
  272. 'Expect:', // skip 100-continue round-trip
  273. ],
  274. ]);
  275. $body = curl_exec($ch);
  276. $status = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
  277. return [$status, (string)$body];
  278. }
  279. /**
  280. * Server-side signed request (header auth). Used for DELETE.
  281. * Returns [httpStatus, responseBody].
  282. */
  283. function s3_request(string $method, string $key): array
  284. {
  285. $host = s3_host();
  286. $amzDate = gmdate('Ymd\THis\Z');
  287. $date = substr($amzDate, 0, 8);
  288. $scope = $date . '/' . config('s3.region') . '/s3/aws4_request';
  289. $canonicalUri = s3_canonical_uri($key);
  290. $payloadHash = hash('sha256', '');
  291. $canonicalRequest = implode("\n", [
  292. strtoupper($method),
  293. $canonicalUri,
  294. '', // no query string
  295. 'host:' . $host,
  296. 'x-amz-content-sha256:' . $payloadHash,
  297. 'x-amz-date:' . $amzDate,
  298. '',
  299. 'host;x-amz-content-sha256;x-amz-date',
  300. $payloadHash,
  301. ]);
  302. $stringToSign = implode("\n", [
  303. 'AWS4-HMAC-SHA256',
  304. $amzDate,
  305. $scope,
  306. hash('sha256', $canonicalRequest),
  307. ]);
  308. $signature = hash_hmac('sha256', $stringToSign, s3_signing_key($date));
  309. $authorization = 'AWS4-HMAC-SHA256 Credential=' . config('s3.access_key') . '/' . $scope
  310. . ', SignedHeaders=host;x-amz-content-sha256;x-amz-date'
  311. . ', Signature=' . $signature;
  312. $ch = s3_curl();
  313. curl_setopt_array($ch, [
  314. CURLOPT_URL => s3_base_url() . $canonicalUri,
  315. CURLOPT_CUSTOMREQUEST => strtoupper($method),
  316. CURLOPT_RETURNTRANSFER => true,
  317. CURLOPT_TIMEOUT => 30,
  318. CURLOPT_HTTPHEADER => [
  319. 'Authorization: ' . $authorization,
  320. 'x-amz-content-sha256: ' . $payloadHash,
  321. 'x-amz-date: ' . $amzDate,
  322. ],
  323. ]);
  324. $body = curl_exec($ch);
  325. $status = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
  326. return [$status, (string)$body];
  327. }
  328. /** Delete one object. S3 returns 204 for success and for already-gone keys. */
  329. function s3_delete(string $key): bool
  330. {
  331. [$status] = s3_request('DELETE', $key);
  332. return $status === 204 || $status === 200 || $status === 404;
  333. }
  334. /** Delete every S3 object referenced by a gallery (originals + thumbs). */
  335. function s3_delete_gallery_objects(array $gallery): void
  336. {
  337. foreach ($gallery['images'] ?? [] as $img) {
  338. if (!empty($img['key'])) {
  339. s3_delete($img['key']);
  340. }
  341. if (!empty($img['thumb'])) {
  342. s3_delete($img['thumb']);
  343. }
  344. }
  345. }
  346. /** Human-readable reason for a PHP upload error code. */
  347. function upload_error_message(int $code): string
  348. {
  349. return match ($code) {
  350. UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE => 'file exceeds the server upload size limit',
  351. UPLOAD_ERR_PARTIAL => 'upload was interrupted',
  352. UPLOAD_ERR_NO_FILE => 'no file received',
  353. UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE => 'server cannot store the upload',
  354. default => 'upload error ' . $code,
  355. };
  356. }
  357. /**
  358. * Ingest one uploaded image into a gallery: stream the original (and optional
  359. * browser-generated thumbnail) to S3, then append it to the gallery's JSON file.
  360. *
  361. * Shared by admin/api.php (trusted admin) and upload-api.php (public guest link).
  362. * The browser uploads several images at once, so the gallery entry is appended
  363. * through gallery_append_image(), which re-reads and rewrites the JSON file
  364. * under an exclusive lock — two uploads finishing together cannot drop one
  365. * another's entry. Object keys are generated server-side under the gallery's
  366. * own prefix — never taken from the client.
  367. *
  368. * $original / $thumb are $_FILES entries (or null). When $imagesOnly is true the
  369. * original must have a recognised image extension and decode via getimagesize(),
  370. * so a public link cannot be used to store arbitrary file types.
  371. *
  372. * Returns [int $httpStatus, array $payload] for the caller to hand to
  373. * json_response(); a thumbnail failure is non-fatal (the grid falls back to the
  374. * original key).
  375. */
  376. function gallery_store_s3_upload(array $gallery, ?array $original, ?array $thumb, bool $imagesOnly = false): array
  377. {
  378. if (!is_array($original) || ($original['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
  379. return [400, ['error' => upload_error_message((int)($original['error'] ?? UPLOAD_ERR_NO_FILE))]];
  380. }
  381. if (!is_uploaded_file((string)$original['tmp_name'])) {
  382. return [400, ['error' => 'Invalid upload']];
  383. }
  384. if ($imagesOnly) {
  385. $ext = strtolower(pathinfo((string)($original['name'] ?? ''), PATHINFO_EXTENSION));
  386. if (!in_array($ext, MEDIA_EXTENSIONS, true) || getimagesize((string)$original['tmp_name']) === false) {
  387. return [400, ['error' => 'Only image files are allowed']];
  388. }
  389. }
  390. $slug = $gallery['slug'];
  391. $name = substr(safe_filename((string)($original['name'] ?? '')), 0, 120);
  392. $token = random_token(6);
  393. $base = s3_gallery_prefix($slug);
  394. $key = "$base/originals/$token-$name";
  395. // Stream the original to S3 byte-for-byte from the PHP upload temp file.
  396. $type = (string)($original['type'] ?? '') ?: 'application/octet-stream';
  397. [$status] = s3_put_file($key, (string)$original['tmp_name'], $type);
  398. if ($status < 200 || $status >= 300) {
  399. return [502, ['error' => "S3 rejected the original (HTTP $status)"]];
  400. }
  401. // Optional browser-generated thumbnail. A thumb failure is non-fatal: the
  402. // original stays, and the grid falls back to the original key.
  403. $thumbKey = null;
  404. if (is_array($thumb)
  405. && ($thumb['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_OK
  406. && is_uploaded_file((string)$thumb['tmp_name'])
  407. ) {
  408. $candidate = "$base/thumbs/$token-$name.jpg";
  409. [$tstatus] = s3_put_file($candidate, (string)$thumb['tmp_name'], 'image/jpeg');
  410. if ($tstatus >= 200 && $tstatus < 300) {
  411. $thumbKey = $candidate;
  412. } else {
  413. s3_delete($candidate);
  414. }
  415. }
  416. // Locked read-modify-write: concurrent uploads append without clobbering.
  417. $count = gallery_append_image($slug, [
  418. 'key' => $key,
  419. 'thumb' => $thumbKey,
  420. 'name' => substr((string)($original['name'] ?? basename($key)), 0, 200),
  421. 'size' => (int)($original['size'] ?? 0),
  422. ]);
  423. // The gallery was deleted while this image was in flight: drop the objects
  424. // we just wrote rather than leaving them unreferenced in the bucket.
  425. if ($count === null) {
  426. s3_delete($key);
  427. if ($thumbKey !== null) {
  428. s3_delete($thumbKey);
  429. }
  430. return [404, ['error' => 'Gallery no longer exists']];
  431. }
  432. return [200, ['ok' => true, 'key' => $key, 'thumb' => $thumbKey, 'count' => $count]];
  433. }