<?php
declare(strict_types=1);
require __DIR__.'/contract.php';require __DIR__.'/cases.php';$root=getenv('SM7_ROOT');$runtime=getenv('SM7_RUNTIME');$parent=getenv('SM7_PARENT');$path=parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH);$candidates=smLoad('candidates-private.json');$match=null;
if($path!=='/__poc_boundary'){foreach(catCases($candidates['candidates']) as $case){$input=$case['method']==='GET'?$_GET:$_POST;if($path!==$case['path']||($_SERVER['REQUEST_METHOD']??'')!==$case['method']||count($input)!==count($case['params'])){continue;}$same=true;foreach($case['params'] as $k=>$v){if(!isset($input[$k])||is_array($input[$k])||(string)$input[$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;}
$sha=catSha();$cfg=require $parent.'/application/database.php';$ai=catAiConfig();$cfg['ai_zhct']=$ai;$originalDebug=$cfg['debug']??null;$cfg['debug']=true;$GLOBALS['CDX_CATALOG_DATABASE_CONFIG']=$cfg;
// C never passes the legacy RDS nested configuration into application services.
if(getenv('SM7_LANE')==='C'&&($cfg['hostname']!=='127.0.0.1'||(string)$cfg['hostport']!=='5237'||$cfg['database']!=='ZHCT_APP'||ltrim((string)$cfg['type'],'\\')!=='app\\cores\\database\\Dm'||$ai['hostname']!=='127.0.0.1'||(string)$ai['hostport']!=='5238'||$ai['database']!=='AIZHCT_APP')){http_response_code(503);exit;}
$_SERVER['PATH_INFO']=$path;require $parent.'/env.php';if(!function_exists('json')){function json($data=[],$code=200,array $headers=[],$options=[]){return \think\Response::create($data,'json',$code,$headers,$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\Config::set($cfg,'database');\think\Db::clear();\think\Config::set(['type'=>'File','path'=>$runtime.'/cache/','prefix'=>'CATALOG6_'],'cache');\think\Config::set('redis',[]);\think\Log::init(['type'=>'File','path'=>$runtime.'/safe-log/','level'=>['no_raw_application_log']]);
$classes=['app\\api\\controller\\Consume'=>'application/api/controller/Consume.php','app\\api\\service\\Consume'=>'application/api/service/Consume.php','app\\api\\service\\Card'=>'application/api/service/Card.php','app\\p\\logic\\MealOrder'=>'application/p/logic/MealOrder.php','app\\p\\controller\\Api'=>'application/p/controller/Api.php','app\\p\\logic\\Api'=>'application/p/logic/Api.php'];foreach($classes as $class=>$file){if(realpath((new ReflectionClass($class))->getFileName())!==realpath($root.'/'.$file)||hash_file('sha256',$root.'/'.$file)!==$sha[$file]){http_response_code(503);exit;}}
$effective=\think\Config::get('database');foreach(['hostname','database','username','password','prefix','hostport'] as $key){if(($effective['ai_zhct'][$key]??null)!==($ai[$key]??null)){http_response_code(503);exit;}}
if($path==='/__poc_boundary'){$jsonClass=new ReflectionClass(\think\response\Json::class);echo json_encode(['private_source_reflection'=>true,'effective_ai_private_config_verified'=>true,'legacy_ai_fallback_disabled'=>true,'file_cache'=>true,'redis_disabled'=>\think\Config::get('redis')===[],'external_stubs'=>class_exists(\app\common\Serve::class,false),'real_queue'=>false,'original_debug'=>$originalDebug,'observer_debug'=>true,'json_encode_param'=>$jsonClass->getDefaultProperties()['options']['json_encode_param'],'is_oss'=>\think\Config::get('is_oss'),'domain_name'=>\think\Config::get('domain_name'),'domain_name_oss'=>\think\Config::get('domain_name_oss')]);exit;}
$actor=smLoad('actor-private.json');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;}
$green=getenv('SM7_GREEN')==='1';$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';if($scenario!=='PILOT_CAT_'.($green?'GREEN_':'').$match['name']){http_response_code(403);exit;}
require __DIR__.'/SqlCoverageRecorder.php';$observer=new SqlCoverageRecorder($runtime.'/queries.jsonl',realpath($root),$scenario);\think\Db::listen(static function($sql,$time=null)use($observer){if(strpos($sql,'CONNECT:')!==0){$observer->observe($sql,$time);}});register_shutdown_function([$observer,'finish']);
$capture=$runtime.'/raw-'.($green?'green-':'').$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;});
if($match['kind']==='card'){$connection=get_ai_db();$actual=$connection->getConfig();foreach(['hostname','database','username','password','prefix','hostport'] as $key){if((string)($actual[$key]??'')!==(string)$ai[$key]){throw new RuntimeException('Actual framework AI connection rejected');}}$identity=$connection->query(getenv('SM7_LANE')==='C'?"SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') AS schema_name FROM DUAL":'SELECT DATABASE() AS schema_name');$identityRow=array_change_key_case($identity[0],CASE_LOWER);$schema=$identityRow['schema_name']??null;if($schema!==$ai['database']){throw new RuntimeException('Actual framework AI schema mismatch');}}
\think\App::run()->send();
