Files
compras/scripts/sgf-read-candidate.php
T
2026-11-02 20:27:11 +02:00

32 lines
2.1 KiB
PHP

<?php
// Freeze only the authorised adapter correction over the previously proved artifact.
$root=dirname(__DIR__);
$prior=json_decode(file_get_contents($root.'/docs/proofs/sgf-corrected-delivery.json'),true,64,JSON_THROW_ON_ERROR);
$old=$prior['artifact']['directory'];
$manifest=json_decode(file_get_contents($old.'/manifest.json'),true,64,JSON_THROW_ON_ERROR);
$file='integrations/sgf/internal-requisitions/Port.php';
$payload=[];
foreach($manifest['support_files'] as $path=>$hash){
$text=file_get_contents($old.'/'.$path);
if(!hash_equals($hash,hash('sha256',$text)))throw new RuntimeException('Prior artifact changed');
$payload[$path]=$path===$file?file_get_contents($root.'/'.$file):$text;
}
$manifest['support_files'][$file]=hash('sha256',$payload[$file]);
$manifestText=json_encode($manifest,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR);
$id='OPERATIONAL-CANDIDATE-'.substr(hash('sha256',$manifestText),0,16);
$dir=$root.'/storage/app/private/candidates/'.$id;
if(file_exists($dir))throw new RuntimeException('Candidate already exists; verify rather than overwrite');
mkdir($dir,0700,true);
foreach($payload+['manifest.json'=>$manifestText] as $path=>$text){
if(!is_dir(dirname($dir.'/'.$path)))mkdir(dirname($dir.'/'.$path),0700,true);
file_put_contents($dir.'/'.$path,$text);
}
$archive=$root.'/storage/app/private/candidate-archives/'.$id.'.zip';
$zip=new ZipArchive;
if($zip->open($archive,ZipArchive::CREATE|ZipArchive::EXCL)!==true)throw new RuntimeException('Archive collision');
foreach([...array_keys($payload),'manifest.json'] as $path)$zip->addFile($dir.'/'.$path,$path);
$zip->close();
$result=['directory'=>$dir,'archive'=>['file'=>$archive,'sha256'=>hash_file('sha256',$archive),'manifest_sha256'=>hash('sha256',$manifestText)],'prior_manifest_sha256'=>$prior['artifact']['archive']['manifest_sha256'],'changed_files'=>[$file],'schema_changed'=>false,'rights_changed'=>false,'installed_real'=>false];
file_put_contents($root.'/docs/proofs/sgf-read-candidate.json',json_encode($result,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));
echo json_encode($result,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR);