Connector Open-Source Code
Browse connector files locally. Exchange API keys stay on your device.
ui/nuxvision.php
<?php
// /opt/nuxvision_connector/ui/nuxvision.php
declare(strict_types=1);
require_once __DIR__ . '/common.php';
/* =========================================================
CONFIG
========================================================= */
const NV_BASE_DEFAULT = 'https://nuxvision.com/api/v1';
/* -------------------- local session helpers -------------------- */
function nv_ui_get(string $k, $default = null) {
return $_SESSION['nv_ui'][$k] ?? $default;
}
function nv_ui_set(string $k, $v): void {
if (!isset($_SESSION['nv_ui']) || !is_array($_SESSION['nv_ui'])) $_SESSION['nv_ui'] = [];
$_SESSION['nv_ui'][$k] = $v;
}
function nv_ui_clear(string $k): void {
if (isset($_SESSION['nv_ui']) && is_array($_SESSION['nv_ui'])) unset($_SESSION['nv_ui'][$k]);
}
function nv_ui_clear_all(): void {
if (isset($_SESSION['nv_ui']) && is_array($_SESSION['nv_ui'])) $_SESSION['nv_ui'] = [];
}
/* -------------------- local config helpers -------------------- */
function cfg_defaults(): array {
return [
'nuxvision' => [
'base_url' => NV_BASE_DEFAULT,
'api_key' => '',
],
'exchange' => [
'name' => '',
'base_url' => '',
'api_key' => '',
'api_secret' => '',
],
];
}
function load_instance_config(int $instanceId): array {
$d = cfg_defaults();
if ($instanceId <= 0) return $d;
$path = nv_instance_config_path($instanceId);
if (!is_file($path)) return $d;
$cfg = @require $path;
if (!is_array($cfg)) return $d;
foreach (['nuxvision','exchange'] as $k) {
if (isset($cfg[$k]) && is_array($cfg[$k])) {
$d[$k] = array_replace($d[$k], $cfg[$k]);
}
}
// force prod default base_url (locked)
$d['nuxvision']['base_url'] = NV_BASE_DEFAULT;
return $d;
}
function save_instance_config(int $instanceId, array $cfg): bool {
if ($instanceId <= 0) return false;
$dir = nv_instance_dir($instanceId);
if (!is_dir($dir)) @mkdir($dir, 0775, true);
$path = nv_instance_config_path($instanceId);
$out = [
'nuxvision' => [
'base_url' => NV_BASE_DEFAULT,
'api_key' => (string)($cfg['nuxvision']['api_key'] ?? ''),
],
'exchange' => [
'name' => (string)($cfg['exchange']['name'] ?? ''),
'api_key' => (string)($cfg['exchange']['api_key'] ?? ''),
'api_secret' => (string)($cfg['exchange']['api_secret'] ?? ''),
],
];
$export = var_export($out, true);
$php = "<?php\n// generated by connector UI\nreturn {$export};\n";
return (bool)@file_put_contents($path, $php, LOCK_EX);
}
/* -------------------- NV API -------------------- */
function nv_instances_fetch(string $apiKey): array {
$apiKey = trim($apiKey);
if ($apiKey === '') {
return ['ok' => false, 'http' => 0, 'err' => 'missing_key', 'instances' => []];
}
$base = rtrim(NV_BASE_DEFAULT, '/');
$url = $base . '/instance_list.php';
[$code, $curlErr, $body, $json] = curl_json($url, [
'Accept' => 'application/json',
'X-API-KEY' => $apiKey,
], 8);
if ($curlErr !== '') {
return ['ok'=>false,'http'=>(int)$code,'err'=>$curlErr,'raw'=>is_string($body)?substr($body,0,250):null,'instances'=>[]];
}
if ($code < 200 || $code >= 400 || !is_array($json)) {
return ['ok'=>false,'http'=>(int)$code,'err'=>'http_error','raw'=>is_string($body)?substr($body,0,250):null,'instances'=>[]];
}
if (empty($json['ok']) || !isset($json['instances']) || !is_array($json['instances'])) {
return ['ok'=>false,'http'=>(int)$code,'err'=>'bad_response','raw'=>is_string($body)?substr($body,0,250):null,'instances'=>[]];
}
$out = [];
foreach ($json['instances'] as $row) {
if (!is_array($row)) continue;
$id = isset($row['id']) ? (int)$row['id'] : 0;
if ($id <= 0) continue;
$name = trim((string)($row['name'] ?? ''));
$ex = strtoupper(trim((string)($row['exchange'] ?? '')));
$label = trim((string)($row['connector']['label'] ?? ''));
$parts = [];
if ($name !== '') $parts[] = $name;
if ($ex !== '') $parts[] = $ex;
$text = $parts ? implode(' • ', $parts) : 'Instance';
$hintParts = [];
if ($label !== '') $hintParts[] = $label;
$hint = $hintParts ? implode(' • ', $hintParts) : '';
$out[] = [
'id' => $id,
'text' => $text,
'hint' => $hint,
];
}
usort($out, fn($a,$b) => strcmp((string)$a['text'], (string)$b['text']));
return ['ok'=>true,'http'=>(int)$code,'instances'=>$out];
}
/* =========================================================
PAGE STATE
========================================================= */
// Edit mode only when instance_id is provided.
// Without instance_id, the page starts blank (no fallback).
$editingInstanceId = 0;
if (isset($_GET['instance_id'])) {
$editingInstanceId = to_int($_GET['instance_id'], 0);
if ($editingInstanceId < 0) $editingInstanceId = 0;
}
// Clear UI state on first GET without instance_id
if ($editingInstanceId <= 0 && (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST')) {
nv_ui_clear_all();
}
$cfg = ($editingInstanceId > 0) ? load_instance_config($editingInstanceId) : cfg_defaults();
$instances = nv_ui_get('instances', []);
if (!is_array($instances)) $instances = [];
$selectedNvInstanceId = to_int(nv_ui_get('selected_nv_instance_id', 0), 0);
if ($selectedNvInstanceId < 0) $selectedNvInstanceId = 0;
$sessionKey = (string)nv_ui_get('api_key', '');
if ($sessionKey !== '' && (string)($cfg['nuxvision']['api_key'] ?? '') === '') {
$cfg['nuxvision']['api_key'] = $sessionKey;
}
/* -------------------- POST -------------------- */
if (($_SERVER['REQUEST_METHOD'] ?? '') === 'POST') {
$do = (string)($_POST['do'] ?? '');
$apiKey = trim((string)($_POST['api_key'] ?? ''));
$cfg['nuxvision']['api_key'] = $apiKey;
nv_ui_set('api_key', $apiKey);
if (isset($_POST['nv_instance_id'])) {
$tmp = to_int($_POST['nv_instance_id'], 0);
$selectedNvInstanceId = $tmp > 0 ? $tmp : 0;
nv_ui_set('selected_nv_instance_id', $selectedNvInstanceId);
}
if ($do === 'connect') {
if ($apiKey === '') {
nv_flash_set('err', 'Please enter your NuxVision API key.');
} else {
$r = nv_instances_fetch($apiKey);
if (!empty($r['ok'])) {
$instances = (array)$r['instances'];
nv_ui_set('instances', $instances);
if ($editingInstanceId > 0) {
foreach ($instances as $it) {
if ((int)($it['id'] ?? 0) === $editingInstanceId) {
$selectedNvInstanceId = $editingInstanceId;
nv_ui_set('selected_nv_instance_id', $selectedNvInstanceId);
break;
}
}
}
if (!$instances) {
nv_flash_set('warn', 'Connection successful, but no instances were found on your account.');
} else {
nv_flash_set('ok', 'Connection successful. Please select your instance.');
}
} else {
$msg = 'Connection failed';
if (!empty($r['http'])) $msg .= ' (HTTP '.$r['http'].')';
if (!empty($r['err'])) $msg .= ' • '.$r['err'];
nv_flash_set('err', $msg);
nv_ui_clear_all();
}
}
}
if ($do === 'save') {
if ($apiKey === '') {
nv_flash_set('err', 'Please enter your NuxVision API key.');
} elseif ($selectedNvInstanceId <= 0) {
nv_flash_set('err', 'Please select an instance.');
} else {
$targetId = $selectedNvInstanceId;
$exists = is_file(nv_instance_config_path($targetId));
$cfgToSave = load_instance_config($targetId);
$cfgToSave['nuxvision']['api_key'] = $apiKey;
$cfgToSave['nuxvision']['base_url'] = NV_BASE_DEFAULT;
if (save_instance_config($targetId, $cfgToSave)) {
if (function_exists('nv_set_instance_in_session')) {
nv_set_instance_in_session($targetId);
}
nv_flash_set('ok', $exists ? 'Configuration updated.' : 'Configuration saved.');
header('Location: ./exchange.php?instance_id=' . urlencode((string)$targetId));
exit;
} else {
nv_flash_set('err', 'Unable to write configuration file (permissions issue).');
}
}
}
}
/* =========================================================
RENDER
========================================================= */
render_header('NuxVision', 'Step 1 — NuxVision');
render_flash();
$hasInstances = !empty($instances);
?>
<div class="nv-titlebar">
<div class="d-flex align-items-center gap-2">
<strong>NuxVision</strong>
<span class="nv-muted">Connection</span>
</div>
<a class="btn btn-soft btn-sm" href="./index.php" title="Home">
<i class="bi bi-house me-1"></i>Home
</a>
</div>
<div class="nv-pad">
<form method="post" action="./nuxvision.php<?= $editingInstanceId > 0 ? '?instance_id=' . h((string)$editingInstanceId) : '' ?>">
<input type="hidden" name="do" value="">
<div class="row g-3">
<div class="col-12">
<label class="form-label">Server address</label>
<input class="form-control" value="<?=h(NV_BASE_DEFAULT)?>" readonly>
<div class="nv-muted small mt-1">Default production endpoint.</div>
</div>
<div class="col-12">
<label class="form-label">API key</label>
<input class="form-control" name="api_key" value="<?=h((string)($cfg['nuxvision']['api_key'] ?? ''))?>" placeholder="Your NuxVision API key">
<div class="nv-muted small mt-1">Stored locally on this connector.</div>
</div>
<?php if ($hasInstances): ?>
<div class="col-12">
<hr class="nv-soft">
<div class="fw-semibold mb-1">Your instance</div>
<div class="nv-muted small mb-2">Select the instance this connector should use.</div>
<select class="form-select" name="nv_instance_id">
<option value="0" <?=($selectedNvInstanceId<=0?'selected':'')?>>— Select an instance —</option>
<?php foreach ($instances as $it):
$id = (int)($it['id'] ?? 0);
if ($id <= 0) continue;
?>
<option value="<?=h((string)$id)?>" <?=($selectedNvInstanceId===$id?'selected':'')?>>
<?=h((string)$it['text'])?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
</div>
<div class="d-flex justify-content-end gap-2 mt-4">
<button class="btn btn-soft" type="submit" onclick="this.form.do.value='connect'">
<i class="bi bi-plug me-1"></i>Connect
</button>
<?php if ($hasInstances && $selectedNvInstanceId > 0): ?>
<button class="btn btn-accent" type="submit" onclick="this.form.do.value='save'">
<i class="bi bi-check2-circle me-1"></i>Save & Continue
</button>
<?php endif; ?>
</div>
<?php if ($hasInstances && $selectedNvInstanceId <= 0): ?>
<div class="nv-muted small mt-2 text-end">
Please select an instance to continue.
</div>
<?php endif; ?>
</form>
</div>
</div>
<?php render_footer(); ?>