sirius-press/plugins/sirius-press-sia-export/includes/class-spe-admin.php
Silent Mode 5465b65756 feat(sirius-press): a WordPress where the account is a key, not a mailbox
WordPress makes two assumptions this project cannot accept: that identity
comes from an email address, and that a site lives at one server. Both are
things somebody else can take away — a mailbox is rented from a provider who
can close it or be compelled to open it, and a server is one seizure from
being gone.

Sirius Press replaces the first and hedges the second.

Signing in means signing a challenge with the key that controls a CashAddress.
The address is recovered from the signature, so nothing is typed but the
signature itself, and the result is an ordinary WordPress session cookie —
roles, capabilities, nonces and the REST API never learn the login was
different. Three ways to produce one: a wallet the browser already exposes, a
phrase used once in the page and wiped, or a signature pasted in from any
BIP-137 wallet, which needs no JavaScript and lets the key stay on a machine
that never touches the web.

There is no password reset, and the recovery page says so plainly rather than
offering a form that cannot work. A reset mechanism is by construction a way
to take an account from its owner, and it is always easier to attack than the
cryptography it bypasses.

Publishing a post also exports it as static HTML to the name's storage on Sia,
signed by the key that owns the name, so the site keeps answering when the
server does not.

Email as a feature is untouched. wp_mail() still works, SMTP still sends, and
contact forms still deliver to addresses real people typed. Only mail to the
site's own unroutable placeholder addresses is diverted to an in-app inbox.
The objection was to email as identity, not to email.

Core is pinned and patched rather than vendored. WordPress 7.1.1 is 149 MB and
5,008 files; the fork's entire core diff is 75 lines in wp-admin/install.php.
Carrying the former to express the latter would bury the patch where nobody
reviews it and make every clone of the monorepo pay for it. Upstream releases
still merge through tools/update-wordpress.sh, which reapplies the series and
says exactly which hunk needs a human.

The cryptography is implemented twice — PHP on the server, JavaScript in the
page — because the server must verify and the browser must sign. Both are
pinned against libauth, the library the Sirius portal wallet and the BNS
gateway already use, so a disagreement of one byte fails the test suite rather
than presenting as a rejected login at three in the morning.

132 checks, no framework, about a second.
2026-09-21 01:39:38 +02:00

311 lines
10 KiB
PHP

<?php
/**
* The export screen.
*
* Shows what is waiting, what failed and why, and offers the two buttons that
* matter: export everything, and try the failures again.
*
* It also hosts manual signing. In manual mode the server has no key, so the
* upload cannot happen without a person: this page builds each queued file
* server-side, hands the bytes to the browser, and the browser signs and PUTs
* them straight to the gateway. The file never round-trips through anyone
* else's server, and the key never touches this one.
*
* @package SiriusPress
*/
defined( 'ABSPATH' ) || exit;
final class SPE_Admin {
const PAGE = 'sirius-press-export';
public static function hooks() {
add_action( 'admin_menu', array( __CLASS__, 'menu' ), 30 );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) );
add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
}
public static function menu() {
add_submenu_page(
'sirius-press',
__( 'Publishing', 'sirius-press' ),
__( 'Publishing', 'sirius-press' ),
'manage_options',
self::PAGE,
array( __CLASS__, 'render' )
);
}
public static function enqueue( $hook ) {
if ( false === strpos( (string) $hook, self::PAGE ) ) {
return;
}
if ( SP_Settings::MODE_SERVER === SP_Settings::mode() ) {
return; // Nothing to sign here; cron has the key.
}
if ( ! class_exists( 'SPA_Login' ) ) {
return; // Auth plugin inactive — no wallet library to lean on.
}
SPA_Login::enqueue_wallet();
wp_enqueue_script(
'sirius-press-export',
SIRIUS_PRESS_EXPORT_URL . 'assets/export.js',
array( 'sirius-press-wallet', 'wp-api-fetch' ),
SIRIUS_PRESS_EXPORT_VERSION,
true
);
wp_localize_script(
'sirius-press-export',
'SIRIUS_EXPORT',
array(
'restUrl' => esc_url_raw( rest_url( 'sirius-press/v1/export' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'gateway' => SP_Settings::gateway(),
'name' => SP_Settings::name(),
'prefix' => SP_Settings::prefix(),
'path' => SP_Settings::derivation_path(),
)
);
add_action( 'admin_footer', array( 'SPA_Login', 'print_config' ) );
}
// -------------------------------------------------- manual-signing routes
public static function register_routes() {
$can_manage = function () {
return current_user_can( 'manage_options' );
};
register_rest_route(
'sirius-press/v1',
'/export/next',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => $can_manage,
'callback' => array( __CLASS__, 'rest_next' ),
)
);
register_rest_route(
'sirius-press/v1',
'/export/ack',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => $can_manage,
'callback' => array( __CLASS__, 'rest_ack' ),
)
);
}
/**
* Build the next few queued files and hand them to the browser.
*
* Bodies come back base64-encoded because a queued file may be a PNG, and
* JSON has no way to carry arbitrary bytes otherwise.
*/
public static function rest_next( WP_REST_Request $request ) {
$limit = max( 1, min( 5, (int) $request->get_param( 'limit' ) ?: 3 ) );
$items = array();
foreach ( SPE_Queue::claim( $limit ) as $row ) {
$body = SPE_Runner::build( $row );
if ( is_wp_error( $body ) ) {
SPE_Queue::mark_failed( $row->id, $body->get_error_message() );
continue;
}
$hash = hash( 'sha256', $body );
if ( '' !== $row->content_hash && hash_equals( $row->content_hash, $hash ) ) {
SPE_Queue::mark_done( $row->id, $hash );
continue;
}
$items[] = array(
'id' => (int) $row->id,
'path' => $row->path,
'mime' => SP_Gateway::mime_for( $row->path ),
'sha256' => $hash,
'body_b64' => base64_encode( $body ),
);
}
return rest_ensure_response(
array(
'items' => $items,
'left' => SPE_Queue::counts()[ SPE_Queue::STATUS_PENDING ],
)
);
}
/** The browser reports how an upload went. */
public static function rest_ack( WP_REST_Request $request ) {
$id = (int) $request->get_param( 'id' );
$hash = (string) $request->get_param( 'sha256' );
$error = (string) $request->get_param( 'error' );
if ( '' !== $error ) {
SPE_Queue::mark_failed( $id, $error );
return rest_ensure_response( array( 'ok' => false ) );
}
if ( ! preg_match( '/^[a-f0-9]{64}$/', $hash ) ) {
return new WP_Error( 'sirius_bad_hash', __( 'Bad hash.', 'sirius-press' ), array( 'status' => 400 ) );
}
SPE_Queue::mark_done( $id, $hash );
return rest_ensure_response( array( 'ok' => true ) );
}
// ------------------------------------------------------------------ page
public static function render() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to publish this site.', 'sirius-press' ) );
}
$notes = array();
if ( isset( $_POST['sirius_export_action'] ) && check_admin_referer( 'sirius_export' ) ) {
$action = sanitize_key( wp_unslash( $_POST['sirius_export_action'] ) );
if ( 'full' === $action ) {
$n = SPE_Runner::queue_full_site();
$notes[] = sprintf(
/* translators: %d: number of pages queued. */
_n( '%d page queued for export.', '%d pages queued for export.', $n, 'sirius-press' ),
$n
);
} elseif ( 'retry' === $action ) {
$n = SPE_Queue::retry_failed();
$notes[] = sprintf(
/* translators: %d: number of failed exports re-queued. */
_n( '%d failed export queued again.', '%d failed exports queued again.', $n, 'sirius-press' ),
$n
);
} elseif ( 'run' === $action ) {
$r = SPE_Runner::run_batch( 20 );
$notes[] = sprintf(
/* translators: 1: uploaded, 2: unchanged, 3: failed, 4: remaining. */
__( 'Uploaded %1$d, %2$d unchanged, %3$d failed, %4$d still queued.', 'sirius-press' ),
$r['done'],
$r['skipped'],
$r['failed'],
$r['left']
);
} elseif ( 'clear' === $action ) {
SPE_Queue::clear_done();
$notes[] = __( 'Completed rows cleared.', 'sirius-press' );
}
}
$counts = SPE_Queue::counts();
$manual = SP_Settings::MODE_SERVER !== SP_Settings::mode();
echo '<div class="wrap"><h1>' . esc_html__( 'Publishing', 'sirius-press' ) . '</h1>';
foreach ( $notes as $n ) {
printf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( $n ) );
}
if ( ! SP_Settings::is_configured() ) {
printf(
'<div class="notice notice-error"><p>%s <a href="%s">%s</a></p></div>',
esc_html__( 'No BCNR name is configured, so there is nowhere to publish to.', 'sirius-press' ),
esc_url( admin_url( 'admin.php?page=sirius-press' ) ),
esc_html__( 'Settings', 'sirius-press' )
);
echo '</div>';
return;
}
printf(
'<p>%s <code>%s</code>%s</p>',
esc_html__( 'Publishing to', 'sirius-press' ),
esc_html( SP_Settings::name() ),
$manual
? ' — ' . esc_html__( 'signing happens in your browser, on this page.', 'sirius-press' )
: ' — ' . esc_html__( 'this server signs uploads by itself.', 'sirius-press' )
);
printf(
'<p><strong>%1$d</strong> %2$s &nbsp; <strong>%3$d</strong> %4$s &nbsp; <strong>%5$d</strong> %6$s</p>',
(int) $counts[ SPE_Queue::STATUS_PENDING ],
esc_html__( 'queued', 'sirius-press' ),
(int) $counts[ SPE_Queue::STATUS_DONE ],
esc_html__( 'published', 'sirius-press' ),
(int) $counts[ SPE_Queue::STATUS_FAILED ],
esc_html__( 'failed', 'sirius-press' )
);
echo '<form method="post" style="margin:16px 0">';
wp_nonce_field( 'sirius_export' );
echo '<button class="button button-primary" name="sirius_export_action" value="full">'
. esc_html__( 'Export everything', 'sirius-press' ) . '</button> ';
if ( ! $manual ) {
echo '<button class="button" name="sirius_export_action" value="run">'
. esc_html__( 'Run a batch now', 'sirius-press' ) . '</button> ';
}
echo '<button class="button" name="sirius_export_action" value="retry">'
. esc_html__( 'Try failures again', 'sirius-press' ) . '</button> ';
echo '<button class="button" name="sirius_export_action" value="clear">'
. esc_html__( 'Clear finished rows', 'sirius-press' ) . '</button>';
echo '</form>';
if ( $manual ) {
self::render_manual_panel();
}
self::render_queue_table();
echo '</div>';
}
private static function render_manual_panel() {
?>
<div id="sirius-export-panel" class="sirius-wallet" style="max-width:820px">
<h2 style="margin-top:0"><?php esc_html_e( 'Sign and upload', 'sirius-press' ); ?></h2>
<p class="description">
<?php esc_html_e( 'Your phrase is used in this page to sign each upload and is wiped when the run finishes. Files go straight from this browser to the gateway.', 'sirius-press' ); ?>
</p>
<p>
<label for="sirius_export_phrase"><strong><?php esc_html_e( 'Recovery phrase', 'sirius-press' ); ?></strong></label><br>
<textarea id="sirius_export_phrase" rows="2" style="width:100%;font-family:monospace"
autocomplete="off" spellcheck="false"></textarea>
</p>
<p>
<button type="button" class="button button-primary" id="sirius_export_run">
<?php esc_html_e( 'Publish queued pages', 'sirius-press' ); ?>
</button>
<button type="button" class="button" id="sirius_export_stop" hidden>
<?php esc_html_e( 'Stop', 'sirius-press' ); ?>
</button>
</p>
<p id="sirius_export_status" role="status" aria-live="polite"></p>
<ul id="sirius_export_log" style="font-family:monospace;font-size:12px;max-height:240px;overflow:auto;margin:0"></ul>
</div>
<?php
}
private static function render_queue_table() {
$rows = SPE_Queue::recent( 100 );
echo '<h2>' . esc_html__( 'Queue', 'sirius-press' ) . '</h2>';
if ( ! $rows ) {
echo '<p>' . esc_html__( 'Nothing queued. Publish a post, or use “Export everything”.', 'sirius-press' ) . '</p>';
return;
}
echo '<table class="widefat striped"><thead><tr>';
echo '<th>' . esc_html__( 'Path', 'sirius-press' ) . '</th>';
echo '<th style="width:100px">' . esc_html__( 'Status', 'sirius-press' ) . '</th>';
echo '<th style="width:60px">' . esc_html__( 'Tries', 'sirius-press' ) . '</th>';
echo '<th>' . esc_html__( 'Last error', 'sirius-press' ) . '</th>';
echo '</tr></thead><tbody>';
foreach ( $rows as $row ) {
$colour = SPE_Queue::STATUS_FAILED === $row->status
? '#b32d2e'
: ( SPE_Queue::STATUS_DONE === $row->status ? '#007017' : '#646970' );
printf(
'<tr><td><code>%s</code></td><td style="color:%s;font-weight:600">%s</td><td>%d</td><td><small>%s</small></td></tr>',
esc_html( $row->path ),
esc_attr( $colour ),
esc_html( $row->status ),
(int) $row->attempts,
esc_html( (string) $row->last_error )
);
}
echo '</tbody></table>';
}
}