187 lines
6.2 KiB
PHP
187 lines
6.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* DSC-01 Spool Handler
|
|
* Validates POST submissions, builds the spool envelope,
|
|
* and POSTs to the orchestrator receiver.
|
|
* See DSC-RENDERER-SPEC.md — Record shape / Spool sections.
|
|
*/
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// POST HANDLER
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function dsc01_handle_post($association_slug, $access) {
|
|
$categories = dsc01_load_categories();
|
|
$valid_codes = array_keys($categories);
|
|
|
|
$raw_codes = $_POST['dsc_codes'] ?? [];
|
|
$codes = [];
|
|
if (is_array($raw_codes)) {
|
|
foreach ($raw_codes as $code) {
|
|
$code = strtoupper(trim((string) $code));
|
|
if (in_array($code, $valid_codes, true)) {
|
|
$codes[] = $code;
|
|
}
|
|
}
|
|
}
|
|
$codes = array_values(array_unique($codes));
|
|
|
|
$narrative = isset($_POST['narrative'])
|
|
? substr(strip_tags((string) $_POST['narrative']), 0, 8192)
|
|
: '';
|
|
|
|
// Validation — at least one category required
|
|
if (empty($codes)) {
|
|
$out = '<div class="alert alert-danger"><strong>Please select at least one category.</strong></div>';
|
|
$out .= dsc01_render_landing($association_slug, $access, $codes, $narrative);
|
|
return $out;
|
|
}
|
|
|
|
// Load association config
|
|
$cfg_raw = @file_get_contents('addon/vs01/config.json');
|
|
$cfg = $cfg_raw ? json_decode($cfg_raw, true) : [];
|
|
$assoc = $cfg['associations'][$association_slug] ?? [];
|
|
|
|
// Load dsc01 config for receiver_url and node_token
|
|
$dsc_config = dsc01_load_config();
|
|
|
|
$envelope = dsc01_build_spool_envelope(
|
|
$codes,
|
|
$narrative,
|
|
$association_slug,
|
|
$assoc['channel_id'] ?? '',
|
|
$access,
|
|
$dsc_config['node_token'] ?? ''
|
|
);
|
|
|
|
$result = dsc01_post_to_orchestrator($envelope, $dsc_config);
|
|
|
|
if ($result === true) {
|
|
$assoc_name = dsc01_h($assoc['name'] ?? $association_slug);
|
|
return '<div class="dsc01-content">
|
|
<div class="alert alert-success">
|
|
Your checklist for ' . $assoc_name . ' has been saved.
|
|
<a href="' . z_root() . '/vs01/' . dsc01_h($association_slug) . '">
|
|
Return to ' . $assoc_name . '
|
|
</a>
|
|
</div>
|
|
</div>';
|
|
}
|
|
|
|
$out = '<div class="alert alert-danger">
|
|
Submission failed. Please try again or contact the operator.
|
|
<br><small>' . dsc01_h($result) . '</small>
|
|
</div>';
|
|
$out .= dsc01_render_landing($association_slug, $access, $codes, $narrative);
|
|
return $out;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// SPOOL ENVELOPE
|
|
// Matches contracts/dsc01/record-v1.json — _header / _payload shape.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function dsc01_build_spool_envelope($dsc_codes, $narrative, $association_slug, $channel_id, $standing, $node_token) {
|
|
return [
|
|
'_header' => [
|
|
'addon' => 'dsc01',
|
|
'contract_version' => '1.0',
|
|
'association_slug' => $association_slug,
|
|
'association_channel_id' => (string) $channel_id,
|
|
'participant_id' => dsc01_get_participant_id(),
|
|
'submitted_at' => date('c'),
|
|
'standing' => $standing,
|
|
'node_token_hash' => hash('sha256', $node_token),
|
|
],
|
|
'_payload' => [
|
|
'fields' => [
|
|
'dsc_codes' => $dsc_codes,
|
|
'narrative' => $narrative,
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// PARTICIPANT ID
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function dsc01_get_participant_id() {
|
|
$observer = get_observer_hash();
|
|
if (!$observer) return '';
|
|
|
|
$r = q("SELECT xchan_addr FROM xchan WHERE xchan_hash = '%s' LIMIT 1",
|
|
dbesc($observer)
|
|
);
|
|
if (!$r) return '';
|
|
|
|
$addr = $r[0]['xchan_addr'] ?? '';
|
|
$parts = explode('@', $addr);
|
|
return $parts[0] ?? '';
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// ORCHESTRATOR POST
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function dsc01_post_to_orchestrator($envelope, $config = null) {
|
|
if ($config === null) {
|
|
$config = dsc01_load_config();
|
|
}
|
|
$receiver_url = $config['receiver_url'] ?? '';
|
|
$node_token = $config['node_token'] ?? '';
|
|
|
|
if (!$receiver_url) {
|
|
logger('dsc01_spool: receiver_url not configured');
|
|
return 'Orchestrator receiver URL not configured.';
|
|
}
|
|
|
|
$payload = json_encode($envelope);
|
|
if ($payload === false) {
|
|
logger('dsc01_spool: failed to encode envelope');
|
|
return 'Failed to encode submission.';
|
|
}
|
|
|
|
$ch = curl_init($receiver_url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $payload,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'X-Node-Token: ' . $node_token,
|
|
],
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curl_error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($curl_error) {
|
|
logger('dsc01_spool: curl error: ' . $curl_error);
|
|
return 'Network error: ' . $curl_error;
|
|
}
|
|
|
|
if ($http_code === 200 || $http_code === 201) {
|
|
return true;
|
|
}
|
|
|
|
logger('dsc01_spool: orchestrator returned HTTP ' . $http_code . ': ' . $response);
|
|
return 'Orchestrator error (HTTP ' . $http_code . ').';
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// CONFIG
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function dsc01_load_config() {
|
|
$path = 'addon/dsc01/config.json';
|
|
$raw = @file_get_contents($path);
|
|
if ($raw === false) return [];
|
|
$data = json_decode($raw, true);
|
|
return (json_last_error() === JSON_ERROR_NONE) ? $data : [];
|
|
}
|