Files
Investidor-V2/outputs/investmoz-audit-current-20261012/consumer-lot-file-QA.php
T
nala 287e991486
tests / ci (push) Waiting to run
oportunidades 7
2026-10-13 11:40:29 +02:00

25 lines
5.2 KiB
PHP

<?php
// Own QA image and document jobs only. Never pop a shared queue or touch an active target.
ini_set('zend.exception_ignore_args','1');$root=dirname(__DIR__,2);$f=json_decode(file_get_contents(__DIR__.'/business-country-fixture.json'),true,512,JSON_THROW_ON_ERROR);$qa=$f['QA_source'];putenv('STABILITY_QA_MODE=consumers');$app=require $qa.'/tools/portal-stability-runtime.php';$c=config('database.connections.'.config('database.default'));if($c['database']!==$f['target']['database']||(int)$c['port']!==55441)throw new RuntimeException('QA guard');$db=Illuminate\Support\Facades\DB::connection();$target=(array)$db->selectOne('SELECT current_database() AS database,inet_server_port() AS port');if($target!==$f['target'])throw new RuntimeException('Actual target guard');if(config('portal.notifications_enabled'))throw new RuntimeException('Notifications must remain disabled');
$mode=$argv[1]??'status';$out=__DIR__;if($mode==='make-image'){
$path=$qa.'/storage/app/private/qa-consumer-original-20261012.png';if(!is_file($path)){$im=imagecreatetruecolor(1400,700);$color=imagecolorallocate($im,37,110,67);imagefill($im,0,0,$color);imagestring($im,5,30,30,'Synthetic QA consumer 20261012',imagecolorallocate($im,255,255,255));imagepng($im,$path);imagedestroy($im);}
$p=['path'=>$path,'sha256'=>hash_file('sha256',$path),'bytes'=>filesize($path),'width'=>1400,'height'=>700,'synthetic_image'=>true,'active_writes'=>0,'target'=>$target];file_put_contents($out.'/consumer-lot-image-file.json',json_encode($p,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo json_encode($p);exit;
}
$state=json_decode(file_get_contents($out.'/consumer-lot-progress.json'),true,512,JSON_THROW_ON_ERROR);$o=App\Models\Opportunity::findOrFail($state['opportunity_id']);if($o->owner_user_id!==21||$o->status!=='draft')throw new RuntimeException('Own synthetic draft required');
$image=$db->table('opportunity_images')->where('id',$state['image_id'])->where('opportunity_id',$o->id)->first();if(!$image)throw new RuntimeException('Resource binding differs');$p=json_decode($image->payload,true,512,JSON_THROW_ON_ERROR);$ids=[$image->document_id,...array_column($p['derivatives']??[],'document_id')];if(isset($state['workflow_document_id']))$ids[]=$state['workflow_document_id'];$scanner=app(App\Services\Malware\MalwareScanner::class);$health=$scanner->health();if(empty($health['available']))throw new RuntimeException('Real scanner unavailable');
$otherJobsBefore=$db->table('jobs')->orderBy('id')->get()->keyBy('id');$processed=[];$ownIds=[];
foreach($otherJobsBefore as$jid=>$row){$payload=json_decode($row->payload,true);if(($payload['data']['commandName']??'')!==App\Jobs\ScanDocumentForMalware::class)continue;$job=unserialize($payload['data']['command'],['allowed_classes'=>[App\Jobs\ScanDocumentForMalware::class]]);if($job instanceof App\Jobs\ScanDocumentForMalware&&in_array($job->documentId,$ids,true))$ownIds[]=$jid;}
foreach($ids as$id){$d=$db->table('documents')->where('id',$id)->first();if(!$d||!in_array($d->source_system,['opportunity-resources','opportunity-image-derivatives'],true)||$d->uploaded_by!==21)throw new RuntimeException('Document outside own lot');
if($mode==='scan-own'){
$job=new App\Jobs\ScanDocumentForMalware($id);$job->handle($scanner);$d=$db->table('documents')->where('id',$id)->first();if($d->malware_scan_status!=='clean'||trim($d->sha256)!==$d->malware_scanned_sha256)throw new RuntimeException('Real scan did not confirm current bytes');
foreach($ownIds as$jid){$r=$otherJobsBefore[$jid];$payload=json_decode($r->payload,true);$queued=unserialize($payload['data']['command'],['allowed_classes'=>[App\Jobs\ScanDocumentForMalware::class]]);if($queued->documentId===$id){$db->table('jobs')->where('id',$jid)->where('payload',$r->payload)->delete();$processed[]=$jid;}}
// Release only this command's existing unique lock; the QA cache connection is guarded above.
(new Illuminate\Bus\UniqueLock(app(Illuminate\Contracts\Cache\Repository::class)))->release($job);
}
}
if(!in_array($mode,['scan-own','status'],true))throw new RuntimeException('Unknown operation');
foreach($otherJobsBefore as$jid=>$before)if(!in_array($jid,$ownIds,true)){if((array)$db->table('jobs')->where('id',$jid)->first()!==(array)$before)throw new RuntimeException('Unrelated queue work changed');}
$docs=[];foreach($ids as$id){$d=$db->table('documents')->where('id',$id)->first();$docs[]=['id'=>$id,'source'=>$d->source_system,'sha256'=>trim($d->sha256),'scan_status'=>$d->malware_scan_status,'scanned_sha256'=>$d->malware_scanned_sha256,'engine'=>$d->malware_engine,'engine_version'=>$d->malware_engine_version,'signatures_at'=>$d->malware_signatures_at,'attempts'=>$d->malware_scan_attempts,'encrypted'=>$d->storage_encryption==='laravel'];}
$proof=['at'=>gmdate('c'),'target'=>$target,'mode'=>$mode,'real_scanner_available'=>true,'own_documents'=>$docs,'only_own_scan_job_ids_removed'=>$processed,'unrelated_queue_work_unchanged'=>true,'active_writes'=>0,'external_notifications_enabled'=>false,'image_id'=>$image->id,'resource_version'=>$image->version,'derivative_document_ids'=>array_column($p['derivatives']??[],'document_id')];
file_put_contents($out.'/consumer-lot-document-state-'.(count($ids)>1?'derivatives':'original').'.json',json_encode($proof,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo json_encode($proof);