<?php
declare(strict_types=1);
$mode=(string)($argv[1]??'');if(!in_array($mode,['--preflight','--execute','--audit'],true)||($mode==='--execute'&&(string)getenv('CDX_DMT_WRAPPER_APPROVE')!=='ROOT_TARGET_CHAIN_V1')){echo json_encode(['status'=>'default_deny','business_cli'=>0]),PHP_EOL;exit(78);} $pid=112175;$cmd=(string)file_get_contents('/proc/'.$pid.'/cmdline');if(strpos($cmd,'/tmp/zhct-dm-poc-20260928.fcpiiN/store/public')===false||strpos($cmd,'127.0.0.1:18080')===false)throw new RuntimeException('C worker drift');$env=[];foreach(explode("\0",(string)file_get_contents('/proc/'.$pid.'/environ'))as$entry){$at=strpos($entry,'=');if($at!==false)$env[substr($entry,0,$at)]=substr($entry,$at+1);} $svc=(string)($env['DM_SVC_PATH']??'');if(($env['POC_PORT']??'')!=='5237'||($env['POC_SCHEMA']??'')!=='ZHCT_APP'||substr(hash('sha256',$svc),0,12)!=='5a92d26aec07'||!is_file($svc.'/dm_svc.conf')||strpos((string)file_get_contents($svc.'/dm_svc.conf'),'CHAR_CODE=(PG_UTF8)')===false)throw new RuntimeException('C DM client drift');$dir=__DIR__;$runner=$dir.'/verify_target_chain.php';$sha=(string)getenv('CDX_DMT_EXPECTED_SHA');if(!preg_match('/^[a-f0-9]{64}$/D',$sha)||is_link($dir)||is_link($runner)||!is_file($runner)||(fileperms($dir)&0777)!==0700||(fileperms($runner)&0777)!==0600||hash_file('sha256',$runner)!==$sha)throw new RuntimeException('C runner boundary drift');$env['CDX_DMT_LANE']='C';$env['CDX_DMT_SHA']=$sha;if($mode==='--execute')$env['CDX_DMT_APPROVE']='ROOT_TARGET_CHAIN_V1';if($mode==='--audit')$env['CDX_DMT_JOURNAL']=(string)getenv('CDX_DMT_JOURNAL');$command=escapeshellarg('/www/server/php/73/bin/php').' -d display_errors=0 -d log_errors=0 -d extension=/home/dmdba/dmdbms/drivers/php_pdo/php73_pdo_dm.so '.escapeshellarg($runner).' '.escapeshellarg($mode);$proc=proc_open($command,[1=>['pipe','w'],2=>['pipe','w']],$pipes,null,$env);if(!is_resource($proc))throw new RuntimeException('C PHP unavailable');$stdout=stream_get_contents($pipes[1]);$stderr=stream_get_contents($pipes[2]);fclose($pipes[1]);fclose($pipes[2]);$exit=proc_close($proc);$result=json_decode((string)$stdout,true);if(!is_array($result)){echo json_encode(['status'=>'redacted_child_error','exit'=>$exit,'stderr'=>$stderr===''?'empty':'nonempty']),PHP_EOL;exit($exit===0?2:$exit);}echo json_encode(['exit'=>$exit,'result'=>$result,'stderr'=>$stderr===''?'empty':'nonempty'],JSON_UNESCAPED_SLASHES),PHP_EOL;exit($exit);
