<?php
$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;}
require __DIR__.'/common.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);$headers=[];foreach(headers_list() as $h)if(preg_match('/^(Content-Type|X-Powered-By):/i',$h))$headers[]=$h;
 $reflection=[];foreach(['app\\p\\logic\\Nutrition','app\\p\\logic\\NutritionTime','app\\p\\controller\\NutritionSet','app\\p\\controller\\NutritionTime'] as $class)if(class_exists($class,false)){$file=(new ReflectionClass($class))->getFileName();$reflection[$class]=['file'=>$file,'sha256'=>hash_file('sha256',$file)];}
 $entry=['label'=>$case['label'],'path'=>$case['path'],'params'=>$case['params'],'status'=>http_response_code(),'headers'=>$headers,'body'=>$body,'body_sha256'=>hash('sha256',$body),'source_before'=>$before,'source_after'=>$after,'prepared_SQL'=>$GLOBALS['CDX_PREPARED'],'reflection'=>$reflection,'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('JSON encoding');file_put_contents(__DIR__.'/private-retest-wire.jsonl',$encoded."\n",FILE_APPEND);chmod(__DIR__.'/private-retest-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();
