Skip to content

Sync v2: write pgacc's payments into Admidio (accsync.php) - #2

Merged
PeterTheOne merged 8 commits into
mainfrom
feature/sync-v2-accsync
Nov 1, 2026
Merged

PeterTheOne merged 8 commits into
mainfrom
feature/sync-v2-accsync

Conversation

@PeterTheOne

@PeterTheOne PeterTheOne commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Sync v2, reader side in Admidio: pgacc/accsync.php writes pgacc's payments into the members' profiles. It replaces and removes the old mcrypt/mysql_* accsync.php and lgfmail.sh; the old cron's registration.php and lgfmail.php are not ported. Counterpart: PPOE/pgacc#83.

  • A CLI script for a systemd timer (unit examples in the README): php pgacc/accsync.php [--dry-run] [--secrets=<file>], or the ACCSYNC_SECRETS env var.
  • Deployment check first: fields 1/2/26/27 with the expected types, roles 2 and 37–45, and the unique key on adm_user_data (usd_usr_id, usd_usf_id). If any is missing: exit 1.
  • Fetch of pgacc's internal mbapi.php: https only, verify_peer/verify_peer_name on, pgacc's certificate as cafile, no redirects, a fixed 20 s timeout, a status line HTTP/<version> 200 with or without reason phrase, 1 MiB limit, bearer token. The secrets file is accsync-secrets.php; pgacc's certificate is /etc/admidio-addons/pgacc-tls.crt (convention: own /etc/<app>/sync-tls.{crt,key}, peer's /etc/<app>/<peer>-tls.crt, self-signed by the callee, handed to the caller through the inventory).
  • The whole document is validated before anything is written; [] is a no-op.
  • One transaction for eligible members (valid user, active role-2 membership; others are skipped and logged):
    • "paid until" (field 26) only moves forward (GREATEST upsert), so a later date entered by hand stays;
    • the amount (field 27) is set to exact euros from cents and may go down;
    • any error rolls back with exit 1; --dry-run always rolls back.
  • Two deviations from the design, both commented:
    • The transaction runs at READ COMMITTED. Under MariaDB's default isolation, a hand edit during a run aborted the whole run.
    • The writer has UPDATE on (usd_usr_id, usd_usf_id, usd_value), because MariaDB needs that for INSERT … ON DUPLICATE KEY UPDATE. The grants are column-level only (pgacc/pgacc_accsync.sql).
  • Output: one log line per change, with ids and values only, printed after the commit; no names or secrets.

Tests (red first): Docker with MariaDB 12.3, PHP 8.5 and a local HTTPS server with self-signed certificates, also in GitHub Actions. They cover:

  • 36 invalid documents and the refusals (secrets file, token, 500/404, a 4-digit status, redirect, wrong CA, name mismatch, body over 1 MiB, timeout), and a status line without reason phrase accepted;
  • 9 deployment-check failures;
  • date only forward, including concurrent hand edits, and amount going down;
  • rollback on an SQL error, dry run, a second run changing nothing;
  • what the writer user is denied, and that no secrets appear in the output.

Merging with #1: the test harness (tests/schema.sql with every Admidio table both add-ons use, tests/users.sql, tests/grants.sh, compose.yaml, Dockerfile, run.sh running each tests/*-test.php, .github/workflows/tests.yml) is identical in both branches; each branch only adds its own test files and pgacc/*.sql. Whichever merges second merges cleanly except the README sections and the LICENSE list of imported files (keep only mail-alias-sync/adm_mail.php).

Astra review: not run yet (Codex usage limit; pending).

🤖 Generated with Claude Code

PeterTheOne and others added 3 commits November 1, 2026 02:52
Replaces the old accsync.php and its cron wrapper lgfmail.sh (the other
two scripts of that cron, registration.php and lgfmail.php, are not
ported). A CLI script for a systemd timer: a deployment check, a fetch
over verified TLS with a bearer token, validation of the whole JSON
document, then one transaction that moves "paid until" (field 26) only
forward and sets the amount (field 27) to the exact euros, for eligible
members only. --dry-run rolls back. The writer's column grants are in
pgacc/pgacc_accsync.sql; tests with MariaDB 12.3, PHP 8.5 and a local
HTTPS server run in Docker and on GitHub Actions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On an SQL error the run rolls back; it no longer prints change lines
for writes that were undone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s timeout; shared test harness

Aligned with pgacc's client: the status line check is #^HTTP/\S+ 200( |$)#
(accepts "HTTP/1.0 200" without a reason phrase), the timeout is a fixed
20 s (no timeout key in the secrets file), no method or max_redirects
options (follow_location 0 stays); the 1 MiB cap and the timed_out check
stay. Tests for these, the cap and a 4-digit status.

The test harness is the same in both sync v2 branches: one schema with
every Admidio table the add-ons use, one users file, every pgacc/*.sql
applied, and run.sh runs each tests/*-test.php, each resetting its own data.
README: the sync v2 section in the shared structure, with the certificate
path convention. LICENSE: the imported files as a list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PeterTheOne and others added 5 commits November 1, 2026 12:51
…unts-only log

- pgacc_accsync.sql: the writer reads and writes profile values only
  through the definer view pgacc_accsync_user_data (fields 26 and 27,
  WITH CHECK OPTION; the upserts work through it). No data privilege on
  adm_user_data any more; REFERENCES on its two key columns only keeps the
  unique key visible for the deployment check. The file first revokes all
  of the writer's privileges, so re-applying it leaves exactly these.
- Validation rejects the whole document if any date is later than
  31 December of next year (GREATEST would keep it for good).
- A normal run prints only the counts (no ids or values in the journal);
  --dry-run still prints each change and skip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es the pinned cert

A stored field 26 value that is not an ISO date (e.g. "unknown",
"31.12.2027", "2027-1-5") sorted above every new date in the string
GREATEST and was never replaced, while the pgacc_members view (ISO only)
kept reporting the member as unpaid. The upsert now keeps the old value
only if it matches the view's rule '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' and is
later; otherwise it takes the new value. Tests with non-ISO fixtures,
also for the dry-run change lines.

The "certificate for another name" refusal now pins the served
certificate as the cafile, so it fails only through verify_peer_name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PHP's stream timeout applies to each read, so a peer trickling the
response could keep the oneshot run alive far longer. The fetch now
reads the response itself under one deadline and fails closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README: one pgacc section with both sync v2 flows; adm_names.php,
the old accsync.php and lgfmail.sh removed and replaced; the
certificate convention and the tests written once. LICENSE: only
mail-alias-sync/adm_mail.php remains imported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If the response has a Content-Length header, it must be digits only and
equal the body length; otherwise the fetch fails and nothing is written.
Tests: a 200 body cut short by an early close, a longer body and a
malformed value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PeterTheOne
PeterTheOne merged commit bf5dc93 into main Nov 1, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant