prepare('SELECT * FROM doc_storage WHERE id = ? LIMIT 1'); $stmt->execute([$id]); $file = $stmt->fetch(); if (!$file) { json_error(404, 'File not found'); } $allowed = false; if (!empty($file['idhome'])) { $allowed = user_owns_home($pdo, $user, (int) $file['idhome']); } elseif (!empty($file['owner_id'])) { $allowed = user_owns_owner($pdo, $user, (int) $file['owner_id']); } if (!$allowed) { json_error(403, 'No access to this file'); } $data = []; if (array_key_exists('expiry_date', $in)) { $expiry = $in['expiry_date'] !== '' ? $in['expiry_date'] : null; $data['expirydate'] = $expiry; $data['expirystatus'] = $expiry ? 1 : 0; } if (array_key_exists('note', $in)) { $data['note'] = $in['note'] !== '' ? $in['note'] : null; } // null or "" resets the display name back to the requirement's name. if (array_key_exists('title', $in)) { $data['title'] = ($in['title'] !== null && $in['title'] !== '') ? $in['title'] : null; } if (array_key_exists('document_id', $in)) { $documentId = (int) $in['document_id']; if ($documentId <= 0) { json_error(422, 'Validation failed', ['document_id' => ['Must be a positive integer']]); } $exists = $pdo->prepare('SELECT 1 FROM documents WHERE document_id = ? LIMIT 1'); $exists->execute([$documentId]); if (!$exists->fetchColumn()) { json_error(422, 'Validation failed', ['document_id' => ['Unknown document']]); } $data['document_id'] = $documentId; } if (!$data) { json_error(422, 'Nothing to update'); } $set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data))); $pdo->prepare("UPDATE doc_storage SET $set WHERE id = ?") ->execute([...array_values($data), $id]); $row = $pdo->prepare('SELECT * FROM doc_storage WHERE id = ? LIMIT 1'); $row->execute([$id]); json_data(present_file($row->fetch()));