<?php
declare(strict_types=1);
$root=getenv('SM7_ROOT');$parent=getenv('SM7_PARENT');$runtime=getenv('SM7_RUNTIME');$path=parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH);
require __DIR__.'/cases.php';require __DIR__.'/hardware_cases.php';$pre=json_decode(file_get_contents($runtime.'/preflight-private.json'),true);$allowed=array_merge(n6Cases($pre),n6HardwareCases(json_decode(file_get_contents($runtime.'/h7-fixture-private.json'),true)));$match=null;
if($path!=='/__poc_boundary'){foreach($allowed as $case){if($case['path']===$path&&$_SERVER['REQUEST_METHOD']==='POST'&&count($_POST)===count($case['params'])){$same=true;foreach($case['params'] as $k=>$v){if(!isset($_POST[$k])||is_array($_POST[$k])||(string)$_POST[$k]!== (string)$v){$same=false;}}if($same){$match=$case;break;}}}if(!$match){http_response_code(403);exit;}}
elseif($_SERVER['REQUEST_METHOD']!=='GET'){http_response_code(403);exit;}
$_SERVER['PATH_INFO']=$path;require $parent.'/env.php';if(!function_exists('json')){function json($data=[],$code=200,array $header=[],$options=[]){return \think\Response::create($data,'json',$code,$header,$options);}}
if(getenv('POC_EXTERNAL_STUBS')!=='1'||getenv('POC_REAL_QUEUE')!=='0'){http_response_code(503);exit;}require $parent.'/external_stubs.php';define('APP_PATH',$root.'/application/');define('RUNTIME_PATH',$runtime.'/runtime/');require $parent.'/thinkphp/base.php';
\think\App::initCommon();\think\Request::instance()->pathinfo();\think\Log::init(['type'=>'File','path'=>$runtime.'/safe-log/','level'=>['no_raw_application_log']]);\think\Config::set(['type'=>'File','path'=>$runtime.'/cache/','prefix'=>'N6READ_'],'cache');\think\Config::set('redis',[]);
$cfg=require $parent.'/application/database.php';$originalDebug=$cfg['debug']??null;$cfg['debug']=true;\think\Config::set($cfg,'database');\think\Db::clear();
$classes=['app\\p\\controller\\Equipment'=>'application/p/controller/Equipment.php','app\\p\\logic\\Equipment'=>'application/p/logic/Equipment.php','app\\api\\controller\\Weigh'=>'application/api/controller/Weigh.php','app\\api\\service\\Weigh'=>'application/api/service/Weigh.php','app\\m\\controller\\Dishes'=>'application/m/controller/Dishes.php','app\\m\\logic\\Dishes'=>'application/m/logic/Dishes.php','app\\p\\controller\\DateMenu'=>'application/p/controller/DateMenu.php','app\\p\\logic\\DateMenu'=>'application/p/logic/DateMenu.php','app\\cores\\BaseModel'=>'application/cores/BaseModel.php'];
foreach($classes as $class=>$file){if(realpath((new ReflectionClass($class))->getFileName())!==realpath($root.'/'.$file)||hash_file('sha256',$root.'/'.$file)!==$pre['source_sha256'][$file]){http_response_code(503);exit;}}
if($path==='/__poc_boundary'){$jsonReflection=new ReflectionClass(\think\response\Json::class);$jsonOptions=$jsonReflection->getDefaultProperties()['options'];echo json_encode(['private_source_reflection'=>true,'file_cache'=>true,'redis_disabled'=>\think\Config::get('redis')===[],'external_stubs'=>class_exists(\app\common\Serve::class,false),'real_queue'=>false,'mqtt_path_enabled'=>false,'original_debug'=>$originalDebug,'observer_debug'=>true,'page_size'=>\think\Config::get('pagesize'),'status_is_show'=>(bool)\think\Config::get('status_is_show'),'json_encode_param'=>$jsonOptions['json_encode_param'],'json_response_sha256'=>hash_file('sha256',$jsonReflection->getFileName())]);exit;}
$actor=json_decode(file_get_contents($runtime.'/actor-private.json'),true);require_once $root.'/application/base/Security.php';$parts=explode("\t",(string)\app\base\Security::decrypt((string)($_SERVER['HTTP_TOKEN']??'')));if((int)($parts[0]??0)!==$actor['id']||($parts[1]??'')!==$actor['uuid']||($parts[2]??'')!==$actor['marker']||($parts[3]??'')!=='0'){http_response_code(403);exit;}
$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';if($scenario!=='PILOT_N6_'.$match['name']){http_response_code(403);exit;}
require __DIR__.'/SqlCoverageRecorder.php';$recorder=new SqlCoverageRecorder($runtime.'/queries.jsonl',realpath($root),$scenario);\think\Db::listen(static function($sql,$time=null)use($recorder){if(strpos($sql,'CONNECT:')!==0){$recorder->observe($sql,$time);}});register_shutdown_function([$recorder,'finish']);
if(strpos($match['name'],'H7_NATIVE_')===0){$capture=$runtime.'/raw-'.$match['name'].'-private.json';$handle=fopen($capture,'x');if(!$handle){http_response_code(409);exit;}fclose($handle);chmod($capture,0600);ob_start(static function(string $bytes)use($capture):string{file_put_contents($capture,$bytes,FILE_APPEND);return $bytes;});}
\think\App::run()->send();
