| 1 | <?php |
| 2 | ini_set('display_errors', '1'); |
| 3 | ini_set('display_startup_errors', '1'); |
| 4 | error_reporting(E_ALL); |
| 5 | |
| 6 | header('Content-Type: application/json; charset=utf-8'); |
| 7 | header('Access-Control-Allow-Origin: *'); |
| 8 | header('Access-Control-Allow-Methods: GET, HEAD, OPTIONS, POST'); |
| 9 | header('Access-Control-Allow-Headers: *'); |
| 10 | header('X-Robots-Tag: all'); |
| 11 | |
| 12 | header('X-Content-Type-Options: nosniff'); |
| 13 | header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); |
| 14 | header('Pragma: no-cache'); |
| 15 | |
| 16 | set_error_handler(function ($severity, $message, $file, $line) { |
| 17 | http_response_code(500); |
| 18 | echo json_encode([ |
| 19 | 'error' => 'PHP Error', |
| 20 | 'severity' => $severity, |
| 21 | 'message' => $message, |
| 22 | 'file' => $file, |
| 23 | 'line' => $line, |
| 24 | ], JSON_PRETTY_PRINT); |
| 25 | exit; |
| 26 | }); |
| 27 | |
| 28 | set_exception_handler(function ($e) { |
| 29 | http_response_code(500); |
| 30 | echo json_encode([ |
| 31 | 'error' => 'Uncaught Exception', |
| 32 | 'type' => get_class($e), |
| 33 | 'message' => $e->getMessage(), |
| 34 | 'file' => $e->getFile(), |
| 35 | 'line' => $e->getLine(), |
| 36 | 'trace' => explode("\n", $e->getTraceAsString()), |
| 37 | ], JSON_PRETTY_PRINT); |
| 38 | exit; |
| 39 | }); |
| 40 | |
| 41 | |
| 42 | require_once __DIR__ . '/folderNode.php'; |
| 43 | |
| 44 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') { |
| 45 | http_response_code(204); |
| 46 | exit; |
| 47 | } |
| 48 | |
| 49 | $dataRoot = __DIR__ . DIRECTORY_SEPARATOR . 'data'; |
| 50 | $chaptersRoot = $dataRoot . DIRECTORY_SEPARATOR . 'chapters'; |
| 51 | |
| 52 | $allJson = loadAllJson($dataRoot); |
| 53 | $visited = []; |
| 54 | $baseUrl = getBaseUrl(); |
| 55 | |
| 56 | $chaptersTree = loadFolderTree($chaptersRoot); |
| 57 | $chapters = $chaptersTree->jsonRecursive(); |
| 58 | |
| 59 | usort($chapters, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0)); |
| 60 | |
| 61 | foreach ($chapters as $chapterObj) { |
| 62 | $chapter = json_decode(json_encode($chapterObj), true); |
| 63 | $chapterLink = normalizeWikiPath($chapter['wikiPath'] ?? ''); |
| 64 | $relPath = normalizeDataRelPath($chapterLink); |
| 65 | if ($relPath !== '') |
| 66 | $visited[$relPath] = true; |
| 67 | } |
| 68 | |
| 69 | $index = []; |
| 70 | foreach ($chapters as $chapterObj) { |
| 71 | $chapter = json_decode(json_encode($chapterObj), true); |
| 72 | $chapterLink = normalizeWikiPath($chapter['wikiPath'] ?? ''); |
| 73 | |
| 74 | $index[] = buildChapterNode($chapter, $chapterLink, $allJson, $visited, $baseUrl); |
| 75 | } |
| 76 | |
| 77 | $indexSha256 = computeIndexSha256($index, $baseUrl); |
| 78 | header('X-Index-Sha256: ' . $indexSha256); |
| 79 | |
| 80 | if (isset($_GET['meta'])) { |
| 81 | // Opt-in wrapper; the default output stays the plain node list. |
| 82 | echo json_encode([ |
| 83 | 'indexSha256' => $indexSha256, |
| 84 | 'nodes' => $index, |
| 85 | ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 86 | } else { |
| 87 | echo json_encode($index, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * sha256 (lowercase hex) of the exact bytes of a data file, given its path |
| 92 | * relative to the web root (e.g. "/data/powers/feint.json"). |
| 93 | * Returns null if the file does not exist or cannot be read. |
| 94 | * Results are memoised per request and keyed by path + mtime + size. |
| 95 | */ |
| 96 | function fileSha256(string $webRelPath): ?string |
| 97 | { |
| 98 | static $cache = []; |
| 99 | |
| 100 | $webRelPath = '/' . ltrim(str_replace('\\', '/', $webRelPath), '/'); |
| 101 | $path = __DIR__ . $webRelPath; |
| 102 | if (!is_file($path)) |
| 103 | return null; |
| 104 | |
| 105 | $key = $webRelPath . '|' . (int) @filemtime($path) . '|' . (int) @filesize($path); |
| 106 | if (!array_key_exists($key, $cache)) { |
| 107 | $hash = hash_file('sha256', $path); |
| 108 | $cache[$key] = $hash === false ? null : $hash; |
| 109 | } |
| 110 | |
| 111 | return $cache[$key]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * indexSha256 definition: |
| 116 | * Collect every node in the index (recursively) that has a non-null sha256. |
| 117 | * Take the unique data files they point to, as paths relative to the web |
| 118 | * root without a leading slash (e.g. "data/powers/feint.json"). |
| 119 | * Sort them by path (byte-wise, strcmp / LC_ALL=C order) and build one line |
| 120 | * per file in sha256sum format: "<sha256 hex> <path>\n" (two spaces). |
| 121 | * indexSha256 = lowercase hex sha256 of the concatenation of those lines. |
| 122 | * Reproducible from the web/ directory with: |
| 123 | * sha256sum <those files, LC_ALL=C sorted> | sha256sum |
| 124 | */ |
| 125 | function computeIndexSha256(array $nodes, string $baseUrl): string |
| 126 | { |
| 127 | $files = []; |
| 128 | collectNodeHashes($nodes, $baseUrl, $files); |
| 129 | ksort($files, SORT_STRING); |
| 130 | |
| 131 | $lines = ''; |
| 132 | foreach ($files as $path => $hash) |
| 133 | $lines .= $hash . ' ' . $path . "\n"; |
| 134 | |
| 135 | return hash('sha256', $lines); |
| 136 | } |
| 137 | |
| 138 | function collectNodeHashes(array $nodes, string $baseUrl, array &$files): void |
| 139 | { |
| 140 | $prefix = $baseUrl . '/'; |
| 141 | foreach ($nodes as $node) { |
| 142 | if (!is_array($node)) |
| 143 | continue; |
| 144 | |
| 145 | $link = (string) ($node['link'] ?? ''); |
| 146 | if (isset($node['sha256']) && str_starts_with($link, $prefix)) |
| 147 | $files[substr($link, strlen($prefix))] = $node['sha256']; |
| 148 | |
| 149 | collectNodeHashes($node['children'] ?? [], $baseUrl, $files); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | function loadAllJson(string $dataRoot): array |
| 154 | { |
| 155 | $map = []; |
| 156 | |
| 157 | $it = new RecursiveIteratorIterator( |
| 158 | new RecursiveDirectoryIterator($dataRoot, FilesystemIterator::SKIP_DOTS) |
| 159 | ); |
| 160 | |
| 161 | foreach ($it as $file) { |
| 162 | /** @var SplFileInfo $file */ |
| 163 | if (strtolower($file->getExtension()) !== 'json') |
| 164 | continue; |
| 165 | |
| 166 | $path = $file->getPathname(); |
| 167 | $rel = str_replace('\\', '/', substr($path, strlen($dataRoot))); |
| 168 | if ($rel === '') |
| 169 | $rel = '/' . $file->getFilename(); |
| 170 | if ($rel[0] !== '/') |
| 171 | $rel = '/' . $rel; |
| 172 | |
| 173 | try { |
| 174 | $raw = file_get_contents($path); |
| 175 | if ($raw === false) |
| 176 | continue; |
| 177 | $json = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); |
| 178 | if (!is_array($json)) |
| 179 | continue; |
| 180 | } catch (Throwable $e) { |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | $json['_relPath'] = $rel; |
| 185 | $map[$rel] = $json; |
| 186 | } |
| 187 | |
| 188 | return $map; |
| 189 | } |
| 190 | |
| 191 | function buildChapterNode(array $chapter, string $chapterLink, array $allJson, array &$visited, string $baseUrl): array |
| 192 | { |
| 193 | $name = $chapterLink !== '' ? basename($chapterLink, '.json') : ($chapter['name'] ?? 'chapter'); |
| 194 | $fullChapterLink = $chapterLink !== '' ? $baseUrl . $chapterLink : ''; |
| 195 | |
| 196 | $node = [ |
| 197 | 'name' => $name, |
| 198 | 'type' => $chapter['type'] ?? 'chapter', |
| 199 | 'pageType' => 'page', |
| 200 | 'link' => $fullChapterLink, |
| 201 | 'sha256' => $chapterLink !== '' ? fileSha256($chapterLink) : null, |
| 202 | 'children' => [], |
| 203 | ]; |
| 204 | |
| 205 | $folderIncludes = extractFolderIncludes($chapter, ['subheaders']); |
| 206 | |
| 207 | $children = []; |
| 208 | |
| 209 | foreach (($chapter['subheaders'] ?? []) as $subheader) { |
| 210 | if (!is_array($subheader)) |
| 211 | continue; |
| 212 | |
| 213 | $subNode = [ |
| 214 | 'name' => $subheader['name'] ?? 'subheader', |
| 215 | 'type' => 'subheader', |
| 216 | 'pageType' => 'page child element', |
| 217 | 'link' => $fullChapterLink, |
| 218 | 'sha256' => $node['sha256'], |
| 219 | 'children' => [], |
| 220 | ]; |
| 221 | |
| 222 | $subIncludes = extractFolderIncludes($subheader); |
| 223 | $subNode['children'] = mergeChildren( |
| 224 | buildChildrenFromFolderIncludes($subIncludes, $allJson, $visited, $baseUrl) |
| 225 | ); |
| 226 | |
| 227 | $children[] = $subNode; |
| 228 | } |
| 229 | |
| 230 | $children = mergeChildren( |
| 231 | $children, |
| 232 | buildChildrenFromFolderIncludes($folderIncludes, $allJson, $visited, $baseUrl) |
| 233 | ); |
| 234 | |
| 235 | $node['children'] = $children; |
| 236 | |
| 237 | return $node; |
| 238 | } |
| 239 | |
| 240 | function buildChildrenFromFolderIncludes(array $folders, array $allJson, array &$visited, string $baseUrl): array |
| 241 | { |
| 242 | $children = []; |
| 243 | $rels = array_keys($allJson); |
| 244 | sort($rels, SORT_NATURAL | SORT_FLAG_CASE); |
| 245 | foreach ($folders as $folder) { |
| 246 | $folderRel = '/' . trim(str_replace('\\', '/', $folder), '/'); |
| 247 | foreach ($rels as $rel) { |
| 248 | if (str_starts_with($rel, $folderRel . '/')) { |
| 249 | $children[] = buildDataLinkNode($rel, '', $allJson, $visited, $baseUrl); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return $children; |
| 255 | } |
| 256 | |
| 257 | function buildDataLinkNode(string $relPath, string $label, array $allJson, array &$visited, string $baseUrl): array |
| 258 | { |
| 259 | $link = $baseUrl . '/data' . $relPath; |
| 260 | $json = $allJson[$relPath] ?? null; |
| 261 | |
| 262 | $name = $label; |
| 263 | $type = 'linkedData'; |
| 264 | |
| 265 | if (is_array($json)) { |
| 266 | $type = $json['type'] ?? 'data'; |
| 267 | if ($name === '') { |
| 268 | $name = $json['name'] ?? ($json['title'] ?? basename($relPath, '.json')); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | $node = [ |
| 273 | 'name' => $name !== '' ? $name : basename($relPath, '.json'), |
| 274 | 'type' => $type, |
| 275 | 'pageType' => 'data link', |
| 276 | 'link' => $link, |
| 277 | 'sha256' => fileSha256('/data' . $relPath), |
| 278 | 'children' => [], |
| 279 | ]; |
| 280 | |
| 281 | if (!is_array($json)) |
| 282 | return $node; |
| 283 | |
| 284 | if (isset($visited[$relPath])) |
| 285 | return $node; |
| 286 | |
| 287 | $visited[$relPath] = true; |
| 288 | |
| 289 | $folderChildren = buildChildrenFromFolderIncludes(extractFolderIncludes($json), $allJson, $visited, $baseUrl); |
| 290 | $node['children'] = mergeChildren($folderChildren); |
| 291 | |
| 292 | return $node; |
| 293 | } |
| 294 | |
| 295 | function normalizeWikiPath(string $path): string |
| 296 | { |
| 297 | $path = str_replace('\\', '/', $path); |
| 298 | if ($path === '') |
| 299 | return ''; |
| 300 | |
| 301 | if (!str_starts_with($path, '/')) |
| 302 | $path = '/' . $path; |
| 303 | |
| 304 | return $path; |
| 305 | } |
| 306 | |
| 307 | function getBaseUrl(): string |
| 308 | { |
| 309 | $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
| 310 | $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; |
| 311 | $scriptName = $_SERVER['SCRIPT_NAME'] ?? '/dataIndex.php'; |
| 312 | $dir = rtrim(str_replace('\\', '/', dirname($scriptName)), '/'); |
| 313 | return $scheme . '://' . $host . ($dir === '' ? '' : $dir); |
| 314 | } |
| 315 | |
| 316 | function normalizeDataRelPath(string $path): string |
| 317 | { |
| 318 | $path = trim($path); |
| 319 | if ($path === '') |
| 320 | return ''; |
| 321 | |
| 322 | $path = str_replace('\\', '/', $path); |
| 323 | if (str_starts_with($path, '/data/')) |
| 324 | $path = substr($path, strlen('/data')); |
| 325 | if (str_starts_with($path, 'data/')) |
| 326 | $path = substr($path, strlen('data')); |
| 327 | |
| 328 | if (!str_starts_with($path, '/')) |
| 329 | $path = '/' . $path; |
| 330 | |
| 331 | return $path; |
| 332 | } |
| 333 | |
| 334 | function extractFolderIncludes(mixed $value, array $skipKeys = []): array |
| 335 | { |
| 336 | $folders = []; |
| 337 | |
| 338 | if (is_array($value)) { |
| 339 | foreach ($value as $k => $v) { |
| 340 | if (in_array($k, $skipKeys, true)) |
| 341 | continue; |
| 342 | $folders = array_merge($folders, extractFolderIncludes($v, $skipKeys)); |
| 343 | } |
| 344 | return $folders; |
| 345 | } |
| 346 | |
| 347 | if (is_string($value)) { |
| 348 | return extractFolderIncludesFromString($value); |
| 349 | } |
| 350 | |
| 351 | return $folders; |
| 352 | } |
| 353 | |
| 354 | function extractFolderIncludesFromString(string $text): array |
| 355 | { |
| 356 | $folders = []; |
| 357 | $pattern = '/\[\[\[\s*([a-zA-Z0-9_.-]+)\s*\]\]\]/'; |
| 358 | |
| 359 | if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) { |
| 360 | foreach ($matches as $m) { |
| 361 | $folders[] = $m[1]; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return $folders; |
| 366 | } |
| 367 | |
| 368 | function mergeChildren(array ...$groups): array |
| 369 | { |
| 370 | $merged = []; |
| 371 | $seen = []; |
| 372 | |
| 373 | foreach ($groups as $children) { |
| 374 | foreach ($children as $child) { |
| 375 | if (!is_array($child)) |
| 376 | continue; |
| 377 | |
| 378 | $key = ($child['link'] ?? '') . '|' . ($child['name'] ?? '') . '|' . ($child['pageType'] ?? ''); |
| 379 | if (isset($seen[$key])) |
| 380 | continue; |
| 381 | |
| 382 | $seen[$key] = true; |
| 383 | $merged[] = $child; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return $merged; |
| 388 | } |