<?php
// Read-only customer/rehearsal UTF8 acceptance; never prints passwords or business values.
if($argc!==2){fwrite(STDERR,"Usage: php verify_client.php /absolute/private/import.private.json\n");exit(2);}
$cfg=json_decode(file_get_contents($argv[1]),true);
$profiles=[
 'ZHCT_APP'=>['port'=>5236,'instance'=>'DMSERVER','build'=>'--03134284488-20260902-347071-20200 Pack30','tablespace'=>'ZHCT_TBS'],
 'ZHCT2_APP'=>['port'=>5236,'instance'=>'DMSERVER','build'=>'--03134284488-20260902-347071-20200 Pack30','tablespace'=>'ZHCT2_TBS'],
 'FRONT24_APP3'=>['port'=>15236,'instance'=>'FS1R20261021','build'=>'--03134284488-20260212-314192-20200','tablespace'=>'MAIN'],
 'FRONT24_APP4'=>['port'=>15236,'instance'=>'FS1R20261021','build'=>'--03134284488-20260212-314192-20200','tablespace'=>'MAIN']
];
$profile=$profiles[$cfg['user']??'']??null;
if(!is_array($cfg)||!$profile||($cfg['host']??'')!=='127.0.0.1'||($cfg['port']??0)!==$profile['port']||($cfg['expectedInstance']??'')!==$profile['instance']||($cfg['expectedBuild']??'')!==$profile['build']||($cfg['expectedTablespace']??'')!==$profile['tablespace']){fwrite(STDERR,"Target outside approved front profile\n");exit(2);}
try{
 $schema=$cfg['user'];$p=new PDO('dm:host=127.0.0.1;port='.(int)$cfg['port'].';schema='.$schema,$cfg['user'],$cfg['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_CASE=>PDO::CASE_LOWER]);
 $fp=$p->query('SELECT USER AS username,ID_CODE AS build,SF_GET_UNICODE_FLAG() AS unicode_flag,SF_GET_CASE_SENSITIVE_FLAG() AS case_flag,PAGE() AS page_bytes,(SELECT DEFAULT_TABLESPACE FROM USER_USERS) AS tablespace FROM DUAL')->fetch(PDO::FETCH_ASSOC);
 if($fp['username']!==$schema||$fp['build']!==$cfg['expectedBuild']||(int)$fp['unicode_flag']!==1||(int)$fp['case_flag']!==0||(int)$fp['page_bytes']!==32768||$fp['tablespace']!==$profile['tablespace'])throw new RuntimeException('Target fingerprint mismatch');
 $ini=[];foreach($p->query("SELECT PARA_NAME,PARA_VALUE FROM SYS.V\$DM_INI WHERE PARA_NAME IN ('INSTANCE_NAME','PORT_NUM','COMPATIBLE_MODE')")->fetchAll(PDO::FETCH_ASSOC)as$x)$ini[strtoupper($x['para_name'])]=trim((string)$x['para_value']);
 if($ini['INSTANCE_NAME']!==$cfg['expectedInstance']||(int)$ini['PORT_NUM']!==(int)$cfg['port']||$ini['COMPATIBLE_MODE']!=='4')throw new RuntimeException('Instance fingerprint mismatch');
 $text='中文菜牌𠮷😀';$stmt=$p->prepare('SELECT CAST(? AS VARCHAR(128 CHAR)) AS value, HEX(CAST(? AS VARCHAR(128 CHAR))) AS server_hex FROM DUAL');$stmt->execute([$text,$text]);$row=$stmt->fetch(PDO::FETCH_ASSOC);
 if($row['value']!==$text||strtoupper($row['server_hex'])!==strtoupper(bin2hex($text)))throw new RuntimeException('Client/server UTF8 mismatch');
 $package=dirname(dirname(realpath($argv[1])));$baseline=json_decode(file_get_contents($package.'/verification/source/baseline.json'),true);$columns=array_column($baseline['metadata']['ydy_dishes'],'name');$i=array_search('id',$columns,true);$j=array_search('name',$columns,true);
 if($i===false||$j===false)throw new RuntimeException('Source baseline fields missing');
 $f=gzopen($package.'/verification/source/ydy_dishes.hex.jsonl.gz','r');$sample=json_decode(gzgets($f),true);gzclose($f);
 $s=$p->prepare('SELECT NAME FROM YDY_DISHES WHERE ID=?');$s->execute([(int)hex2bin($sample[$i])]);$name=$s->fetchColumn();
 if(!is_string($name)||preg_match('//u',$name)!==1||strtoupper(bin2hex($name))!==strtoupper($sample[$j])||json_encode(['name'=>$name])===false)throw new RuntimeException('Source name bytes differ from PHP result');
 echo json_encode(['passed'=>true,'schema'=>$schema,'build'=>$fp['build'],'unicode_server_bytes_equal'=>true,'source_name_bytes_equal'=>true,'readonly'=>true],JSON_UNESCAPED_UNICODE),"\n";
}catch(Throwable$e){fwrite(STDERR,json_encode(['passed'=>false,'error_class'=>get_class($e),'error_code'=>(string)$e->getCode(),'message'=>'Client check failed; retain local log and do not start application'],JSON_UNESCAPED_UNICODE)."\n");exit(1);}
