Files
2026-06-12 15:33:51 -04:00

278 lines
10 KiB
PHP

<?php
/**
* Name: CRY01 Value Layer
* Description: Ğ1 balance display, capacity signal registration, and Ğ1 web of trust bridge.
* Version: 0.1.0
* MinVersion: 11.0
* MaxVersion: 12.0
*/
use Zotlabs\Extend\Widget;
require_once 'addon/cry01/cry01_substrate.php';
require_once 'addon/cry01/cry01_chain.php';
require_once 'addon/cry01/cry01_attestation.php';
require_once 'addon/cry01/cry01_renderer.php';
require_once 'addon/cry01/cry01_spool.php';
function cry01_module() {}
function cry01_load() {
register_hook('load_pdl', 'addon/cry01/cry01.php', 'cry01_load_pdl');
Widget::register('addon/cry01/Widget/Cry01.php', 'cry01');
}
function cry01_unload() {
unregister_hook('load_pdl', 'addon/cry01/cry01.php', 'cry01_load_pdl');
Widget::unregister('addon/cry01/Widget/Cry01.php', 'cry01');
}
function cry01_load_pdl(&$b) {
// Loads the cry01 PDL layout for cry01 module pages.
if (!is_array($b) || empty($b['module']) || $b['module'] !== 'cry01') {
return;
}
$layout = @file_get_contents('addon/cry01/mod_cry01.pdl');
if ($layout !== false) {
$b['layout'] = $layout;
}
}
// ----------------------------------------------------------------------------
// HELPERS
// ----------------------------------------------------------------------------
function cry01_h($value) {
// HTML-escapes a value for safe output.
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}
// ----------------------------------------------------------------------------
// ACCESS
// ----------------------------------------------------------------------------
function cry01_access_state($association_slug = '') {
// Returns operator, participant, or public. Does not call local_channel() for group checks.
if (local_channel()) {
$channel = App::get_channel();
if (local_channel() === intval($channel['channel_id'])) {
return 'operator';
}
}
if (!$association_slug) return 'public';
$raw = @file_get_contents('addon/vs01/config.json');
if ($raw === false) return 'public';
$cfg = json_decode($raw, true);
if (json_last_error() !== JSON_ERROR_NONE) return 'public';
$assoc = $cfg['associations'][$association_slug] ?? null;
if (!$assoc) return 'public';
$observer = get_observer_hash();
if (!$observer) return 'public';
$groups = $assoc['groups'] ?? [];
// Direct pgrp_member query — works for guest tokens.
foreach (['corpus_builder', 'sase_participant', 'civic_professional'] as $group_key) {
$gid = intval($groups[$group_key] ?? 0);
if ($gid) {
$r = q("SELECT xchan FROM pgrp_member WHERE gid = %d AND xchan = '%s' LIMIT 1",
intval($gid),
dbesc($observer)
);
if ($r) return 'participant';
}
}
return 'public';
}
function cry01_access_wall($association_slug = '') {
// Renders the access wall for non-participants attempting to submit a signal.
$raw = @file_get_contents('addon/vs01/config.json');
$cfg = $raw ? json_decode($raw, true) : [];
$assoc = $association_slug ? ($cfg['associations'][$association_slug] ?? null) : null;
$name = $assoc ? cry01_h($assoc['name']) : 'this association';
return '
<div class="cry01-content">
<div class="alert alert-info" role="alert">
<strong>HOA_MEMBER standing required to register a signal for ' . $name . '.</strong>
The signal board is readable by verified participants only.
To participate, you must complete the SASE process.
Visit <a href="https://directory.diagnostics.kane-il.us/channel/theron">
directory.diagnostics.kane-il.us
</a> to begin.
</div>
</div>
';
}
// ----------------------------------------------------------------------------
// CONTENT ROUTER
// ----------------------------------------------------------------------------
function cry01_content() {
if (function_exists('head_add_css')) {
head_add_css('/addon/cry01/view/css/cry01.css');
}
if (function_exists('head_add_js')) {
head_add_js('/addon/cry01/view/js/cry01.js');
}
$association_slug = argv(1) ?? '';
$sub_route = strtolower(argv(2) ?? '');
if (!$association_slug) {
return cry01_render_index();
}
$raw = @file_get_contents('addon/vs01/config.json');
if ($raw === false) return cry01_render_error('Configuration unavailable. Contact the operator.');
$cfg = json_decode($raw, true);
if (!isset($cfg['associations'][$association_slug])) {
return cry01_render_not_found();
}
$access = cry01_access_state($association_slug);
switch ($sub_route) {
case 'signal':
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($access === 'public') return cry01_access_wall($association_slug);
if (!cry01_verify_csrf()) {
return cry01_render_error('Invalid form token. Please reload and try again.');
}
return cry01_handle_signal_post($association_slug, $access);
}
if ($access === 'public') return cry01_access_wall($association_slug);
return cry01_render_signal_form($association_slug, $access);
case 'g1':
if ($access !== 'operator') return cry01_access_wall($association_slug);
return cry01_render_g1_candidates($association_slug);
case 'manage':
if ($access !== 'operator') return cry01_access_wall($association_slug);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
return cry01_handle_manage_post($association_slug);
}
return cry01_render_manage($association_slug);
case 'lookup':
// Public Ğ1 account lookup. No SASE gate, no wallet session, no
// storage of any kind. Address in, full AccountInfo out.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!cry01_verify_csrf()) {
return cry01_render_error('Invalid form token. Please reload and try again.');
}
return cry01_handle_lookup_post($association_slug, $access);
}
return cry01_render_landing($association_slug, $access);
default:
return cry01_render_landing($association_slug, $access);
}
}
// ----------------------------------------------------------------------------
// LOOKUP HANDLER
// ----------------------------------------------------------------------------
function cry01_handle_lookup_post($association_slug, $access) {
// Public account lookup: decode the pasted Ğ1 address, query the chain
// for the full AccountInfo struct, re-render the landing page with the
// result (or an error) inline. No data is stored anywhere — this is a
// pure read.
$address = trim($_POST['g1_lookup_address'] ?? '');
if (!$address) {
return cry01_render_landing($association_slug, $access, [
'lookup_error' => 'Please enter a Ğ1 address.',
]);
}
$account_id = cry01_ss58_decode($address);
if ($account_id === null) {
return cry01_render_landing($association_slug, $access, [
'lookup_error' => 'That doesn\'t look like a valid Ğ1 address. Check for typos and try again.',
'lookup_address' => $address,
]);
}
$info = cry01_get_account_info($account_id);
if ($info === null) {
return cry01_render_landing($association_slug, $access, [
'lookup_error' => 'Could not reach the Ğ1 network right now. Please try again shortly.',
'lookup_address' => $address,
]);
}
return cry01_render_landing($association_slug, $access, [
'lookup_address' => $address,
'lookup_balance' => cry01_format_g1_amount($info['free']),
'lookup_account_info' => $info,
]);
}
// ----------------------------------------------------------------------------
// RENDER — INDEX
// ----------------------------------------------------------------------------
function cry01_render_index() {
// Lists all registered associations with links to their value layer pages.
$raw = @file_get_contents('addon/vs01/config.json');
$cfg = $raw ? json_decode($raw, true) : [];
$list = $cfg['associations'] ?? [];
if (empty($list)) {
return '<div class="cry01-content"><p class="text-muted">No associations registered.</p></div>';
}
$out = '<div class="cry01-content">';
$out .= '<h2>Value Layer</h2>';
$out .= '<ul class="list-group">';
foreach ($list as $slug => $assoc) {
$name = cry01_h($assoc['name'] ?? $slug);
$url = z_root() . '/cry01/' . cry01_h($slug);
$out .= '<li class="list-group-item"><a href="' . $url . '">' . $name . '</a></li>';
}
$out .= '</ul></div>';
return $out;
}
// ----------------------------------------------------------------------------
// RENDER — NOT FOUND / ERROR
// ----------------------------------------------------------------------------
function cry01_render_not_found() {
return '<div class="cry01-content"><div class="alert alert-warning">Association not found.</div></div>';
}
function cry01_render_error($message) {
// Shows a plain-language error message. Never shows a blank page or stack trace.
return '<div class="cry01-content"><div class="alert alert-danger">' . cry01_h($message) . '</div></div>';
}
// ----------------------------------------------------------------------------
// CSRF
// ----------------------------------------------------------------------------
function cry01_csrf_token() {
// Generates and stores a CSRF token for the current session.
if (empty($_SESSION['cry01_csrf'])) {
$_SESSION['cry01_csrf'] = bin2hex(random_bytes(16));
}
return '<input type="hidden" name="cry01_csrf" value="'
. cry01_h($_SESSION['cry01_csrf']) . '">';
}
function cry01_verify_csrf() {
// Returns true if the CSRF token in POST matches the session token.
return isset($_POST['cry01_csrf'], $_SESSION['cry01_csrf'])
&& hash_equals($_SESSION['cry01_csrf'], $_POST['cry01_csrf']);
}