SLOT 2 โ€ข SERVER SEES CIPHERTEXT ONLY NO GRAPHICS โ€ข TEXT ONLY
M
New C-Mail โ€” encrypted
0 / 4096 bytes

This is cryptographic c-mail, like Save System Slot 2. Server cannot see contents. You get a crypto armor block + PIN. System treats it just like a save game.

Backend  ยท  Proposed Architecture  ยท  Not connected

C-Mail Backend

Right now this page is local-only: messages are seeded in JavaScript, decrypt happens in this browser with WebCrypto, PIN never leaves the browser. This is the map to make it real โ€” same rules: server sees ciphertext only, text only, 4 KB max.

01 / Files to create
api/cmail/list.php          GET  /api/cmail/list?folder=inbox
api/cmail/send.php          POST /api/cmail/send  (ciphertext only)
api/cmail/move.php          POST /api/cmail/move  (folder / star / trash)
sql/cmail_tables.sql
cron/cmail_cleanup.php      CLI โ€” delete trash > 30 days

Keep the tinyfiles PHP pattern: each endpoint loads /api/bootstrap.php, uses json_in() / json_out(), prepared PDO. No endpoint ever accepts plaintext body or a PIN.

02 / Database tables
-- sql/cmail_tables.sql
cmail_messages
  id INT AUTO_INCREMENT PRIMARY KEY
  folder VARCHAR(20) NOT NULL        -- inbox / sent / drafts / spam / trash
  from_handle VARCHAR(80) NOT NULL
  to_handle VARCHAR(80) NOT NULL
  subject VARCHAR(200) NOT NULL      -- subject is NOT encrypted in list view
  ciphertext TEXT NOT NULL           -- the armor block, base64
  size_bytes INT NOT NULL            -- must be <= 4096
  label VARCHAR(30) NULL
  starred TINYINT NOT NULL DEFAULT 0
  created_at DATETIME NOT NULL

Server stores ciphertext only. It never stores plaintext, PIN, or decrypted body. Subject stays plaintext so the list can render โ€” same as today.

03 / Template: api/cmail/list.php
<?php
// api/cmail/list.php โ€” list only, no decryption server-side
require __DIR__ . '/../bootstrap.php';
$folder = $_GET['folder'] ?? 'inbox';
$stmt = $pdo->prepare(
  "SELECT id, folder, from_handle, subject, size_bytes,
          label, starred, created_at
   FROM cmail_messages
   WHERE folder = ? ORDER BY created_at DESC LIMIT 100"
);
$stmt->execute([$folder]);
json_out(['messages' => $stmt->fetchAll(PDO::FETCH_ASSOC)]);
04 / Template: api/cmail/send.php
<?php
// api/cmail/send.php โ€” ciphertext only, never plaintext
require __DIR__ . '/../bootstrap.php';
$in = json_in(); // { to_handle, subject, ciphertext }
if (!str_starts_with($in['ciphertext'] ?? '', '-----BEGIN ENCRYPTED C-MAIL-----'))
  json_out(['error' => 'armor block required'], 400);
if (strlen($in['ciphertext']) > 8000)
  json_out(['error' => 'over 4 KB'], 400);
if (preg_match('/<img|data:image|<svg|<iframe/i', $in['ciphertext']))
  json_out(['error' => 'graphics rejected โ€” text only'], 400);
$stmt = $pdo->prepare(
  "INSERT INTO cmail_messages
   (folder, from_handle, to_handle, subject, ciphertext, size_bytes, created_at)
   VALUES ('sent', ?, ?, ?, ?, ?, NOW())"
);
$stmt->execute(['me', $in['to_handle'], $in['subject'],
  $in['ciphertext'], strlen($in['ciphertext'])]);
json_out(['ok' => true]);
05 / Template: sql/cmail_tables.sql
CREATE TABLE IF NOT EXISTS cmail_messages (
  id INT AUTO_INCREMENT PRIMARY KEY,
  folder VARCHAR(20) NOT NULL,
  from_handle VARCHAR(80) NOT NULL,
  to_handle VARCHAR(80) NOT NULL,
  subject VARCHAR(200) NOT NULL,
  ciphertext TEXT NOT NULL,
  size_bytes INT NOT NULL,
  label VARCHAR(30) NULL,
  starred TINYINT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL,
  INDEX (folder, created_at)
);
06 / Validation & rules
// Server owns the truth. Browser submits intent only.
- PIN: never sent, never stored. Decrypt is client-side only.
- Body: ciphertext only. Never accept plaintext body.
- Size: ciphertext <= 8000 chars (~4 KB plaintext). Over = reject.
- Graphics: reject <img>, data:image, <svg>, <iframe> anywhere.
- Subject: plain text, max 200 chars, strip tags.
- Move / star / trash: folder must be one of
  inbox / sent / drafts / snoozed / all / spam / trash / armor.
Keep it local-first: if the API is down, this page must still show the seeded demo mail + compose + local decrypt. The backend only adds real storage โ€” it must never make the page blank, and it must never see plaintext.