<?php
$lane=getenv('GP_LANE');$root=getenv('GP_ROOT');$runtime=__DIR__;
$request=json_decode(file_get_contents($runtime.'/private-request.json'),true);$label=$_SERVER['HTTP_X_GD_LABEL']??'';$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);$case=$request['cases'][$label]??null;
if(!$case||$_SERVER['REQUEST_METHOD']!=='POST'||$path!=='/p/accountSetting/giftFlowList'||($_SERVER['HTTP_TOKEN']??'')!==$request['token']||file_exists($runtime.'/evidence/'.$label.'.wire.json')){http_response_code(403);exit;}
$actual=$_POST;$expected=$case;ksort($actual);ksort($expected);if($actual!==$expected){http_response_code(403);exit;}
foreach(json_decode(file_get_contents($runtime.'/source-lock.json'),true)as $file=>$sha)if(hash_file('sha256',$runtime.'/source/application/'.$file)!==$sha){http_response_code(403);exit;}
$GLOBALS['GD_CASE']=$case;$GLOBALS['GD_PREPARED']=[];$GLOBALS['GD_OWNER']=$request['owner'];
require $root.'/env.php';require $root.'/external_stubs.php';
if(!function_exists('json')){function json($data=[],$code=200,array $header=[],$options=[]){return \think\Response::create($data,'json',$code,$header,$options);}}
class GDStatement extends PDOStatement {
 private $bound=[];protected function __construct(){}
 public function bindValue($key,$value,$type=PDO::PARAM_STR){$this->bound[(string)$key]=['value'=>$value,'type'=>$type];return parent::bindValue($key,$value,$type);}
 public function execute($args=null){$sql=$this->queryString;$n=count($GLOBALS['GD_PREPARED']);$entry=['queryString'=>$sql,'bindings'=>$this->bound,'execute_parameters'=>$args];$GLOBALS['GD_PREPARED'][$n]=$entry;
  try{
   if(!preg_match('/^\s*(SELECT|SHOW|EXPLAIN)\b/i',$sql)&&trim($sql)!=='CALL SP_TABLEDEF(?, ?)')throw new RuntimeException('SELECT only; no business write');
   if(preg_match('/^\s*SELECT\b/i',$sql)&&stripos($sql,'user_gift_log')!==false){$values=array_map(static function($v){return (string)$v['value'];},$this->bound);foreach((array)$args as $value)$values[]=(string)$value;
    if(!in_array('%CDXGW11B6A91C%',$values,true)||!in_array('10001',$values,true))throw new RuntimeException('Only exact marked gift scope');
    $boundaries=[$GLOBALS['GD_CASE']['start_date'].' 00:00:00',$GLOBALS['GD_CASE']['end_date'].' 23:59:59'];
    if($GLOBALS['GD_MYSQL_GREEN'])$boundaries=array_map('strtotime',$boundaries);
    foreach($boundaries as $boundary)if(!in_array((string)$boundary,$values,true))throw new RuntimeException('Exact approved date boundary predicate');
   }
   $ok=parent::execute($args);$GLOBALS['GD_PREPARED'][$n]['ok']=$ok;return $ok;
  }catch(Throwable $e){$GLOBALS['GD_PREPARED'][$n]['error']=['class'=>get_class($e),'message'=>$e->getMessage()];throw $e;}
 }
}
$GLOBALS['GD_MYSQL_GREEN']=$lane==='B'&&strpos($label,'green-')===0;
ob_start();register_shutdown_function(static function()use($runtime,$label,$lane,$case){$body=(string)ob_get_contents();$result=['lane'=>$lane,'label'=>$label,'params'=>$case,'HTTP'=>http_response_code(),'body'=>$body,'body_sha256'=>hash('sha256',$body),'prepared'=>$GLOBALS['GD_PREPARED'],'actual_ai_connection_type'=>function_exists('get_ai_db')?get_ai_db()->getConfig('type'):null,'last_error'=>error_get_last()];file_put_contents($runtime.'/evidence/'.$label.'.wire.json',json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION));chmod($runtime.'/evidence/'.$label.'.wire.json',0600);});
putenv('PHP_APP_DEBUG=false');define('APP_PATH',$runtime.'/source/application/');define('RUNTIME_PATH',$runtime.'/runtime/');define('EXTEND_PATH',$root.'/extend/');define('VENDOR_PATH',$root.'/vendor/');require $root.'/thinkphp/base.php';\think\App::initCommon();
\think\Log::init(['type'=>'File','path'=>$runtime.'/runtime/safe-log/','level'=>['no_raw_application_log']]);\think\Config::set(['type'=>'File','path'=>$runtime.'/cache/','prefix'=>'GDATE_'],'cache');\think\Config::set('redis',[]);
$database=(static function($file){return require $file;})($root.'/application/database.php');$database['debug']=false;$database['params'][PDO::ATTR_STRINGIFY_FETCHES]=false;$database['params'][PDO::ATTR_STATEMENT_CLASS]=['GDStatement'];$database['ai_zhct']['params']=$database['params'];\think\Config::set($database,'database');\think\Db::clear();
$_SERVER['PATH_INFO']=$path;\think\App::run()->send();
