<?php
$root=getenv('PM_ROOT');$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);$spec=json_decode(file_get_contents(__DIR__.'/private-spec.json'),true);if($_SERVER['REQUEST_METHOD']!=='POST'||$path!=='/p/weight/'.$spec['method']||($_SERVER['HTTP_X_CDX_CASE']??'')!==$spec['label']){http_response_code(403);exit;}$post=$_POST;$actual=$post;$expect=$spec['params'];ksort($actual);ksort($expect);if($actual!==$expect){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;}
require __DIR__.'/oracle.php';$before=$source();$guard($before);$started=microtime(true);ob_start();register_shutdown_function(static function()use($before,$source,$guard,$spec,$post,$path,$started):void{try{$after=$source();$guard($after);$entry=['label'=>$spec['label'],'path'=>$path,'params'=>$post,'status'=>http_response_code(),'response'=>(string)ob_get_contents(),'source_before'=>$before,'source_after'=>$after,'expected'=>$spec['expected'],'sql'=>$GLOBALS['CDX_SQL']??[],'duration'=>microtime(true)-$started];file_put_contents(__DIR__.'/private-wire.jsonl',json_encode($entry,JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION)."\n",FILE_APPEND);chmod(__DIR__.'/private-wire.jsonl',0600);}catch(Throwable $e){file_put_contents(__DIR__.'/private-audit-error',get_class($e).': '.$e->getMessage());}});$_SERVER['PATH_INFO']=$path;require __DIR__.'/init.php';\think\Config::set('database.debug',true);\think\Db::clear();$GLOBALS['CDX_SQL']=[];\think\Db::listen(static function($sql){$GLOBALS['CDX_SQL'][]=$sql;});\think\App::run()->send();
