<?php
$root=getenv('PM_ROOT');$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);$parts=explode('/',trim($path,'/'));$module=$parts[1]??'';$action=$parts[2]??'';
if(($parts[0]??'')!=='p'||!in_array($module,['treatise','matchRecord'],true)||!in_array($action,['add','update','delete','info','lists'],true)||$_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(403);exit;}
foreach(json_decode(file_get_contents(__DIR__.'/private-source-lock.json'),true)as $file=>$sha)if(hash_file('sha256',__DIR__.'/source/application/'.$file)!==$sha){http_response_code(403);exit;}
$post=$_POST;$lease=json_decode(file_get_contents(__DIR__.'/private-lease.json'),true);$own=is_file(__DIR__.'/private-own.json')?json_decode(file_get_contents(__DIR__.'/private-own.json'),true):[];$row=$own[$module]??null;
if(in_array($action,['add','lists'],true)&&isset($post['staff_uuid'])&&$post['staff_uuid']!==$lease['staff']['uuid']){http_response_code(403);exit;}
if($action==='add'&&strpos((string)($post[$module==='treatise'?'name':'demo']??''),$lease['marker'])!==0){http_response_code(403);exit;}
if($action==='update'&&(!$row||($post['uuid']??'')!==$row['uuid']||($module==='treatise'&&(string)($post['id']??'')!==(string)$row['id'])||(isset($post['staff_uuid'])&&$post['staff_uuid']!==$lease['staff']['uuid']))){http_response_code(403);exit;}
if($action==='delete'&&isset($post['uuid'])&&(!$row||$post['uuid']!==$row['uuid'])){http_response_code(403);exit;}
if($action==='info'&&isset($post['uuid'])&&(!$row||$post['uuid']!==$row['uuid'])){http_response_code(403);exit;}
ob_start();register_shutdown_function(static function()use($module,$action,$post):void{try{require __DIR__.'/db.php';$lease=json_decode(file_get_contents(__DIR__.'/private-lease.json'),true);$rows=[];$own=is_file(__DIR__.'/private-own.json')?json_decode(file_get_contents(__DIR__.'/private-own.json'),true):[];foreach($modules as $m=>$scope){$r=$q('SELECT * FROM '.$p.$scope['table'].' WHERE STAFF_UUID=? AND '.$scope['marker'].' LIKE ? ORDER BY ID',[$lease['staff']['uuid'],$lease['marker'].'%']);$check(count($r)<=1,'Own unique source');$rows[$m]=$r;if($r)$own[$m]=$r[0];}$save('private-own.json',$own);$ref=['staff'=>$q('SELECT UUID,NAME,AI_USER_ID,IS_SHOW,JOB_STATUS FROM '.$p.'staff WHERE UUID=?',[$lease['staff']['uuid']])[0],'user'=>$q('SELECT ID,UUID,STAFF_UUID,ACCOUNT,TYPE FROM '.$p.'user WHERE ID=?',[$lease['user']['id']])[0],'actor'=>$q('SELECT ID,UUID,STAFF_UUID,ACCOUNT,TYPE,IS_DELETE FROM '.$p.'user WHERE ID=?',[$lease['actor']['id']])[0]];$entry=['label'=>$_SERVER['HTTP_X_CDX_CASE']??'', 'phase'=>$_SERVER['HTTP_X_CDX_PHASE']??'legacy','module'=>$module,'action'=>$action,'method'=>'POST','path'=>'/p/'.$module.'/'.$action,'params'=>$post,'status'=>http_response_code(),'response'=>(string)ob_get_contents(),'source'=>$rows,'reference'=>$ref];file_put_contents(__DIR__.'/private-wire.jsonl',json_encode($entry,JSON_UNESCAPED_UNICODE)."\n",FILE_APPEND);chmod(__DIR__.'/private-wire.jsonl',0600);}catch(Throwable $e){file_put_contents(__DIR__.'/private-audit-error',json_encode(['class'=>get_class($e),'message'=>$e->getMessage()]));chmod(__DIR__.'/private-audit-error',0600);}});
$_SERVER['PATH_INFO']=$path;require $root.'/env.php';if(!function_exists('json')){function json($data=[],$code=200,array $header=[],$options=[]){return \think\Response::create($data,'json',$code,$header,$options);}}
require $root.'/external_stubs.php';define('APP_PATH',__DIR__.'/source/application/');define('RUNTIME_PATH',__DIR__.'/runtime/');require $root.'/thinkphp/base.php';\think\App::initCommon();\think\Log::init(['type'=>'File','path'=>__DIR__.'/runtime/safe-log/','level'=>['no_raw_application_log']]);\think\Config::set(['type'=>'File','path'=>__DIR__.'/cache/','prefix'=>'PAPER_MATCH10_'],'cache');\think\Config::set('redis',[]);$database=require $root.'/application/database.php';\think\Config::set($database,'database');\think\Db::clear();\think\App::run()->send();
