<?php
declare(strict_types=1);
try {
$started=microtime(true);$path=parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH);$method=$_SERVER['REQUEST_METHOD']??'';
if($path!=='/__boundary'&&!preg_match('#^/(store|terminal)/market\.sign/(list|detail|add|edit|delete)$#iD',(string)$path)){http_response_code(403);echo '{"denied":true}';return;}
$root=getenv('SIGN_ROOT');$runtime=getenv('SIGN_RUNTIME');ini_set('log_errors','1');ini_set('error_log',$runtime.'/private-errors.log');$_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';define('TP_ENV','poc');define('BUSINESS','aizhct');define('ENTERPRISE_NO','10001');require $root.'/vendor/autoload.php';spl_autoload_register(static function($class)use($root){foreach(['app\\'=>'app/','cores\\'=>'cores/'] as $prefix=>$dir)if(strpos($class,$prefix)===0){$f=$root.'/'.$dir.str_replace('\\','/',substr($class,strlen($prefix))).'.php';if(is_file($f))require $f;}},true,true);require __DIR__.'/external_boundary.php';require __DIR__.'/dependencies.php';
if($path==='/__boundary'){$hashes=[];foreach(['app/store/controller/market/Sign.php','app/terminal/controller/market/Sign.php','app/store/service/Sign.php','app/terminal/service/Sign.php','app/store/model/SignAction.php','app/terminal/model/SignAction.php','app/common/model/SignAction.php','app/common/model/SignAddress.php','app/common/service/Qrcode.php','app/store/service/Upload.php','app/common/model/store/Setting.php','app/common/model/wxapp/Setting.php','app/store/model/UploadFile.php','app/terminal/model/UploadFile.php','app/common/model/UploadFile.php','app/store/model/Setting.php','app/common/library/storage/Driver.php','app/common/library/storage/engine/Local.php','app/common/library/storage/engine/Basics.php','app/common/library/wechat/Qrcode.php','app/common/library/wechat/WxBase.php','cores/BaseModel.php','app/common/database/DmBuilder.php'] as $f){$class=str_replace('/','\\',substr($f,0,-4));$actual=(new ReflectionClass($class))->getFileName();if(realpath($actual)!==realpath($root.'/'.$f))throw new RuntimeException('Source path drift');$hashes[$f]=hash_file('sha256',$actual);}$a=new \think\App($root);$a->setEnvName('poc');$a->setRuntimePath($runtime.'/boundary-runtime/');$a->initialize();chdir($root);$paths=['app_root'=>$a->getRootPath(),'public_path'=>public_path(),'filesystem_public'=>config('filesystem.disks.public.root'),'cwd'=>getcwd()];foreach($paths as $v)if(strpos(realpath($v),realpath($root))!==0)throw new RuntimeException('Private file root drift');$resolved=[];foreach(['curl_init','curl_setopt','curl_exec','curl_close'] as $n)$resolved[$n]=(new ReflectionFunction('app\\common\\library\\wechat\\'.$n))->getFileName()===__DIR__.'/external_boundary.php';header('Content-Type: application/json');echo json_encode(['private_cache'=>true,'only_network_and_png_leaves_synthetic'=>true,'hashes'=>$hashes,'paths'=>$paths,'namespace_resolution'=>$resolved]);return;}
$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';if(!preg_match('/^PILOT_SG_[A-Z0-9_]{1,60}$/D',$scenario)){http_response_code(403);echo '{"denied":true}';return;}
$app=new \think\App($root);$app->setEnvName('poc');$app->setRuntimePath($runtime.'/runtime/');$app->initialize();$hosts=['store.sign-config.invalid'=>2147001571,'terminal.sign-config.invalid'=>2147001572];$app->env->set('store.host',$hosts);$tenant=$hosts[$app->request->host()]??null;if($tenant===null||getStoreId()!==$tenant){http_response_code(403);echo '{"tenant_denied":true}';return;}
$cfg=$app->config->get('database');foreach($cfg['connections'] as &$c){$c['trigger_sql']=true;$c['debug']=false;}unset($c);$app->config->set($cfg,'database');$app->db->setConfig($app->config);$app->config->set(['default'=>'file','stores'=>['file'=>['type'=>'File','path'=>$runtime.'/cache/','prefix'=>'','expire'=>0]]],'cache');$app->instance('cache',new \think\Cache($app));$log=$app->config->get('log');$log['close']=true;foreach($log['channels'] as &$c)$c['close']=true;unset($c);$app->config->set($log,'log');$app->instance('log',new \think\Log($app));$app->log->channel()->close();
spl_autoload_register(static function($class){if(in_array($class,['app\\common\\library\\storage\\engine\\Aliyun','app\\common\\library\\storage\\engine\\Qcloud','app\\common\\library\\storage\\engine\\Qiniu'],true))throw new RuntimeException('Cloud storage forbidden');},true,true);
require __DIR__.'/SqlCoverageRecorder.php';$rec=new SqlCoverageRecorder($runtime.'/queries.jsonl',$root,$scenario);$app->db->listen(static function($sql,$elapsed=null)use($rec,$runtime,$scenario){if(strpos((string)$sql,'CONNECT:')===0)return;$rec->observe($sql,$elapsed);file_put_contents($runtime.'/private-sql.jsonl',json_encode(['scenario'=>$scenario,'sql'=>$sql],JSON_UNESCAPED_UNICODE)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-sql.jsonl',0600);});register_shutdown_function([$rec,'finish']);
$http=$app->http;$response=$http->run();file_put_contents($runtime.'/private-wire.jsonl',json_encode(['scenario'=>$scenario,'method'=>$method,'path'=>$path,'http_status'=>$response->getCode(),'elapsed_ms'=>(microtime(true)-$started)*1000,'raw'=>$response->getContent()],JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-wire.jsonl',0600);$response->send();$http->end($response);
} catch (Throwable $e) { if(isset($a))$a->config->set(['close'=>true,'channels'=>[]],'log');if(isset($app))$app->config->set(['close'=>true,'channels'=>[]],'log');file_put_contents($runtime.'/harness-trace.json',json_encode(['file'=>$e->getFile(),'line'=>$e->getLine(),'frames'=>array_map(static function($f){return array_intersect_key($f,array_flip(['file','line','class','function']));},$e->getTrace())]));http_response_code(500);header('Content-Type: application/json');echo json_encode(['harness_error'=>get_class($e),'message'=>$e->getMessage()]); }
