$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);