<?php
if(($_SERVER['HTTP_X_CDX_SCOPE']??'')==='schema-api-evidence'){require __DIR__.'/schema-api-index.php';exit;}
if(isset($_SERVER['HTTP_X_CDX_CRUD'])){require __DIR__.'/crud-index.php';exit;}
$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);$request=json_decode(file_get_contents(__DIR__.'/private-request.json'),true);$case=null;
foreach($request['cases']as $c)if($c['label']===($_SERVER['HTTP_X_CDX_CASE']??'')&&$c['path']===$path)$case=$c;
if(!$case||$_SERVER['REQUEST_METHOD']!=='POST'||($_SERVER['HTTP_TOKEN']??'')!==$request['token']){http_response_code(403);exit;}
$a=$_POST;$b=$case['params'];ksort($a);ksort($b);if($a!==$b){http_response_code(403);exit;}
foreach(json_decode(file_get_contents(__DIR__.'/private-source-lock.json'),true)as $f=>$sha)if(hash_file('sha256',__DIR__.'/source/application/'.$f)!==$sha){http_response_code(403);exit;}
$root=getenv('PM_ROOT');require __DIR__.'/source.php';$before=$source();$guard($before);$started=microtime(true);$GLOBALS['CDX_PREPARED']=[];ob_start();
register_shutdown_function(static function()use($before,$source,$guard,$case,$started):void{
 try{$body=(string)ob_get_contents();$after=$source();$guard($after);$entry=['label'=>$case['label'],'path'=>$case['path'],'params'=>$case['params'],'status'=>http_response_code(),'headers_base64'=>array_map('base64_encode',headers_list()),'body'=>$body,'body_sha256'=>hash('sha256',$body),'source_before'=>$before,'source_after'=>$after,'prepared_SQL'=>$GLOBALS['CDX_PREPARED'],'duration_seconds'=>microtime(true)-$started,'error'=>error_get_last()];$encoded=json_encode($entry,JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION);if($encoded===false)throw new RuntimeException('Capture JSON encoding');file_put_contents(__DIR__.'/private-wire.jsonl',$encoded."\n",FILE_APPEND);chmod(__DIR__.'/private-wire.jsonl',0600);}catch(Throwable $e){file_put_contents(__DIR__.'/private-audit-error',get_class($e).': '.$e->getMessage());chmod(__DIR__.'/private-audit-error',0600);}
});
$_SERVER['PATH_INFO']=$path;require __DIR__.'/init.php';\think\App::run()->send();
