'method']); exit; } /* --- Same config that config.php uses. --- */ $conf = []; if (file_exists(__DIR__.'/conf/default.php')) include __DIR__.'/conf/default.php'; if (file_exists(DOKU_INC.'conf/local.php')) include DOKU_INC.'conf/local.php'; $p = $conf['plugin']['xmppsupport'] ?? []; /* --- Secret is mandatory. --- */ $secret = $p['fingerprint_secret'] ?? ''; if (!is_string($secret) || strlen($secret) < 32) { http_response_code(500); echo json_encode(['error' => 'not_configured']); exit; } /* --- Client fingerprint (optional; falls back to header-only). --- */ $jsFp = $_POST['fp'] ?? ''; if (!is_string($jsFp) || !preg_match('/^[0-9a-f]{64}$/', $jsFp)) { $jsFp = ''; } /* --- Existing account name, if the browser already has one. --- */ $requestedNick = $_POST['nick'] ?? ''; $requestedNick = is_string($requestedNick) ? strtolower(trim($requestedNick)) : ''; if ($requestedNick !== '' && !preg_match('/^[a-z][a-z0-9-]{1,63}$/', $requestedNick)) { http_response_code(400); echo json_encode(['error' => 'invalid_nick']); exit; } /* --- Rotation counter for a name that is already registered. --- */ $ext = $_POST['ext'] ?? '0'; if (!is_string($ext) || !preg_match('/^[0-9]{1,4}$/', $ext)) { $ext = 0; } else { $ext = (int)$ext; } /* --- HTTP header signals. Coarse IP (/24 IPv4, /48 IPv6). --- */ $ip = ''; foreach ([ 'HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP', 'REMOTE_ADDR', ] as $k) { if (empty($_SERVER[$k])) continue; foreach (explode(',', $_SERVER[$k]) as $cand) { $cand = trim($cand); if (filter_var( $cand, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE )) { $ip = $cand; break 2; } } } $ipCoarse = ''; if ($ip !== '') { if (strpos($ip, ':') !== false) { $parts = explode(':', $ip); $ipCoarse = implode(':', array_slice($parts, 0, 3)); } else { $parts = explode('.', $ip); $ipCoarse = $parts[0].'.'.$parts[1].'.'.$parts[2].'.0'; } } $country = strtoupper(trim((string)($_SERVER['HTTP_CF_IPCOUNTRY'] ?? ''))); if (!preg_match('/^[A-Z]{2}$/', $country) || $country === 'XX' || $country === 'T1') { $country = ''; } $hdrSignal = hash('sha256', implode('|', [ $ipCoarse, $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '', $_SERVER['HTTP_SEC_CH_UA'] ?? '', $_SERVER['HTTP_SEC_CH_UA_PLATFORM'] ?? '', $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ?? '', ])); $material = $jsFp !== '' ? $jsFp . '|' . $hdrSignal : 'hdr|' . $hdrSignal; $adjs = [ 'Swift','Bright','Clever','Calm','Bold','Sly','Wild','Noble', 'Eager','Vast','Iron','Shadow','Frost','Flaring','Silent', ]; $nns = [ 'Falcon','Wizard','Phoenix','Wolf','Knight','Fox','Raven', 'Gargoyle','Forge','Golem','Warden','Specter','Drake', 'Sentinel','Citadel', ]; $reserved = []; foreach ($adjs as $adj) { foreach ($nns as $nn) { $reserved[strtolower($adj.'-'.$nn)] = true; } } $words = require __DIR__.'/words.php'; $wordCnt = count($words); $nickname = $requestedNick; $usedExt = $ext; if ($nickname === '') { for ($attempt = 0; $attempt < 32; $attempt++) { $candidateExt = $ext + $attempt; $digest = hash_hmac('sha256', $material . '|name:' . $candidateExt, $secret); $pair = unpack('N2', hex2bin(substr($digest, 0, 16))); $w1 = $words[$pair[1] % $wordCnt]; $w2 = $words[$pair[2] % $wordCnt]; if ($w1 === $w2) { $w2 = $words[($pair[2] + 1) % $wordCnt]; } $candidate = $w1 . '-' . $w2; if (!isset($reserved[$candidate])) { $nickname = $candidate; $usedExt = $candidateExt; break; } } } if ($nickname === '') { http_response_code(500); echo json_encode(['error' => 'name_generation_failed']); exit; } $ts = time(); $identity = 'v1|'.$nickname.'|'.$ip.'|'.$country.'|'.$ts; $sig = hash_hmac('sha256', $identity, $secret); echo json_encode([ 'nickname' => $nickname, 'ext' => $usedExt, 'mode' => $jsFp !== '' ? 'full' : 'header-only', 'ip' => $ip, 'country' => $country, 'ts' => $ts, 'sig' => $sig, ]); exit;