<?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);$match=null;
if($path!=='/__poc_boundary'){foreach(n7Cases(smLoad('candidates-private.json')['candidates']) as $case){if($path!==$case['path']||($_SERVER['REQUEST_METHOD']??'')!=='POST'||count($_POST)!==count($case['params'])){continue;}$same=true;foreach($case['params'] as $key=>$value){if(!isset($_POST[$key])||is_array($_POST[$key])||(string)$_POST[$key]!== (string)$value){$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';$cfg['ai_zhct']=catAiConfig();$originalDebug=$cfg['debug']??null;$cfg['debug']=true;$GLOBALS['CDX_CATALOG_DATABASE_CONFIG']=$cfg;
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')){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'=>'CATALOG7_'],'cache');\think\Config::set('redis',[]);\think\Log::init(['type'=>'File','path'=>$runtime.'/safe-log/','level'=>['no_raw_application_log']]);
$classes=['app\\api\\controller\\Card'=>'application/api/controller/Card.php','app\\api\\service\\Card'=>'application/api/service/Card.php','app\\p\\controller\\Cater'=>'application/p/controller/Cater.php','app\\p\\logic\\Cater'=>'application/p/logic/Cater.php','app\\p\\controller\\Bill'=>'application/p/controller/Bill.php','app\\p\\logic\\Bill'=>'application/p/logic/Bill.php','app\\p\\controller\\Restaurant'=>'application/p/controller/Restaurant.php','app\\p\\logic\\Restaurant'=>'application/p/logic/Restaurant.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;}}
if($path==='/__poc_boundary'){$json=new ReflectionClass(\think\response\Json::class);echo json_encode(['private_source_reflection'=>true,'effective_local_or_dm_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'=>$json->getDefaultProperties()['options']['json_encode_param'],'business_models_instantiated'=>false]);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;}
$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';$cardGreen=getenv('N7_CARD_GREEN')==='1'&&in_array($match['name'],['CARDS_1','LOGS_1'],true)&&$scenario==='PILOT_CAT7_CARD_GREEN_'.$match['name'];$restaurantGreen=getenv('N7_RESTAURANT_GREEN')==='1'&&in_array($match['name'],['RESTAURANT_LIST','RESTAURANT_READ'],true)&&$scenario==='PILOT_CAT7_RESTAURANT_GREEN_'.$match['name'];if(!$cardGreen&&!$restaurantGreen&&$scenario!=='PILOT_CAT7_'.$match['name']){http_response_code(403);exit;}
$star=strpos($match['kind'],'restaurant_')===0;if($star){require_once __DIR__.'/oracle.php';$guardDb=smPdo();$owned=n7OwnedRestaurant($guardDb)[0];$check=n7Rows($guardDb,'restaurants',['*'],'id=?',[$owned['id']]);n7SafeOwnedStar($guardDb,$check);if($match['kind']==='restaurant_list'&&$match['params']['name']!==$owned['name']){http_response_code(503);exit;}if($match['kind']==='restaurant_read'&&(int)$match['params']['id']!==0&&(int)$match['params']['id']!==$owned['id']){http_response_code(503);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-'.($cardGreen?'card-green-':($restaurantGreen?'restaurant-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,$star):string{if($star){$decoded=json_decode($bytes);if(json_last_error()!==JSON_ERROR_NONE||!n7NoCredentials($decoded)){http_response_code(503);$safe='{"harness_error":"owned_credential_or_json_guard"}';file_put_contents($capture,$safe,FILE_APPEND);return $safe;}}file_put_contents($capture,$bytes,FILE_APPEND);return $bytes;});\think\App::run()->send();
