Files
hyprcrd/relay/index.php
T
thandClaude Opus 4.6 670f125627 shopping list: phone-synced card via relay
Pick one #einkauf-tagged card as "active"; its body is pushed to the
relay on every sync.tick and rendered on a phone-friendly session-
auth'd page at /shopping. Phone shows only the open "- [ ]" items as
tappable rows — tapping flips the line to "- [x]" on the relay in
place. The next client tick pulls those toggles back and PATCHes the
local card, so hyprcrd stays the source of truth for item text.

  - Relay: shopping_state table (one row per tenant), POST/GET
    /api/shopping for content push/pull, POST /api/shopping/check
    for phone toggles, GET /shopping for the phone UI.
  - Client: hamburger menu in the header with an "Einkaufsliste"
    entry that opens a picker listing all #einkauf cards with a
    radio for the active one. Timestamp cursors in localStorage
    keep push/pull idempotent.
  - Phone can only toggle, not edit text — edit stays in hyprcrd,
    which makes the merge trivial (last-writer wins by timestamp).

Multi-card support is there (any card with #einkauf is a candidate)
but only one is active for the phone at a time, per the spec.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 00:32:42 +02:00

80 lines
3.7 KiB
PHP

<?php
// ── hyprcrd Relay — Multi-tenant ──
//
// Entry point. Loads bootstrap + helpers, dispatches API routes,
// renders the UI layout. The actual logic lives in inc/ and ui/.
//
// Architecture (encryption flow for cross-tenant messages):
// 1. Client A encrypts a message with A's tenant secret
// 2. POST /api/messages, server authenticates A, decrypts with A's secret
// 3. Server re-encrypts with the inter-tenant key (relay-wide shared
// key, generated at install, stored encrypted with the master key)
// 4. Stored in messages table with recipient_id = B's tenant id
// 5. B fetches /api/messages, server decrypts with inter-tenant key,
// re-encrypts with B's tenant secret, returns to client B
// 6. Client B decrypts with its own secret
//
// The relay sees plaintext briefly during the cross-tenant hop. This
// is not strict end-to-end — that requires per-pair key exchange and
// is on the roadmap for the planned encrypted chat feature. See
// README.md.
define('RELAY_BOOTSTRAPPED', true);
require_once __DIR__ . '/inc/bootstrap.php';
require_once __DIR__ . '/inc/crypto.php';
require_once __DIR__ . '/inc/tenants.php';
require_once __DIR__ . '/inc/auth.php';
// ── Routing ────────────────────────────────────────────────────────
$route = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$base = dirname($_SERVER['SCRIPT_NAME']);
if ($base !== '/') $route = substr($route, strlen($base));
$route = '/' . ltrim($route ?: '/', '/');
$method = $_SERVER['REQUEST_METHOD'];
$isSetup = tenantCount($db) === 0;
// ── API ────────────────────────────────────────────────────────────
if (str_starts_with($route, '/api/')) {
header('Content-Type: application/json');
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if ($origin) {
header('Access-Control-Allow-Origin: ' . $origin);
header('Access-Control-Allow-Credentials: true');
header('Vary: Origin');
}
header('Access-Control-Allow-Headers: Content-Type, X-Relay-User, X-Relay-Key');
header('Access-Control-Allow-Methods: GET, POST, PATCH, DELETE, OPTIONS');
if ($method === 'OPTIONS') exit;
$contentLength = (int)($_SERVER['CONTENT_LENGTH'] ?? 0);
$sizeLimit = ($route === '/api/attachments') ? 15728640 : 262144;
if ($contentLength > $sizeLimit) jsonResponse(['error' => 'payload too large'], 413);
// Each route file contains its handlers as if-blocks that exit on
// match. Order matters: public.php sets up $sessionTenant which
// admin.php and messages.php use later in the chain.
require __DIR__ . '/inc/routes/public.php';
require __DIR__ . '/inc/routes/account.php';
require __DIR__ . '/inc/routes/admin.php';
require __DIR__ . '/inc/routes/messages.php';
require __DIR__ . '/inc/routes/chat.php';
require __DIR__ . '/inc/routes/collab-buckets.php';
require __DIR__ . '/inc/routes/collab-cards.php';
require __DIR__ . '/inc/routes/collab-locks.php';
require __DIR__ . '/inc/routes/shopping.php';
jsonResponse(['error' => 'not found'], 404);
}
// ── UI ─────────────────────────────────────────────────────────────
// Shopping UI lives outside /api/ so phones can bookmark a clean URL.
// File has both /api/shopping* and /shopping handlers; they short-
// circuit on match, so loading it here for the non-API path is safe.
require __DIR__ . '/inc/routes/shopping.php';
require __DIR__ . '/ui/layout.php';