web/website/integrations/grant-current-alpha-seals.php

8 lines
2.4 KiB
PHP

<?php
if(PHP_SAPI!=='cli'){http_response_code(404);exit;}
$c=json_decode(file_get_contents('/home/dunerealms/.config/integrations.json'),true);$p=new PDO('mysql:host='.$c['host'].';dbname='.$c['database'].';charset=utf8mb4',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$backup='/root/ticket365-before-20261104';if(!is_dir($backup))mkdir($backup,0700,true);
$eligible="SELECT a.id,a.username FROM site_accounts a WHERE a.password_hash NOT LIKE '!%' AND a.role<>'memorial' AND NOT EXISTS(SELECT 1 FROM cp_account_ranks r WHERE r.user_id=a.id AND r.rank_key='memorial') AND NOT EXISTS(SELECT 1 FROM site_memorials m WHERE m.user_id=a.id)";
$rows=$p->query($eligible)->fetchAll(PDO::FETCH_ASSOC);if(!$rows)throw new Exception('No eligible accounts');$before=$p->query("SELECT * FROM website_badges WHERE badge_key='alpha-tester'")->fetchAll(PDO::FETCH_ASSOC);$native=$p->query("SELECT * FROM dune_community_badges WHERE badge_key='alpha-tester'")->fetchAll(PDO::FETCH_ASSOC);$snap=$backup.'/alpha-badges.json';if(!file_exists($snap)){file_put_contents($snap,json_encode(['central'=>$before,'native'=>$native,'eligible'=>$rows]));chmod($snap,0600);}
$rankHash=hash('sha256',json_encode($p->query('SELECT id,role FROM site_accounts ORDER BY id')->fetchAll(PDO::FETCH_ASSOC)));$p->beginTransaction();$insert=$p->prepare("INSERT INTO website_badges(user_id,badge_key,earned_at) VALUES(?,'alpha-tester',?) ON DUPLICATE KEY UPDATE user_id=VALUES(user_id)");$project=$p->prepare("INSERT INTO dune_community_badges(website_id,badge_key,username) VALUES(?,'alpha-tester',?) ON DUPLICATE KEY UPDATE username=VALUES(username)");foreach($rows as$r){$insert->execute([$r['id'],(int)round(microtime(true)*1000)]);$project->execute([$r['id'],$r['username']]);}$p->commit();
$missing=$p->query("SELECT COUNT(*) FROM ($eligible) e LEFT JOIN website_badges b ON b.user_id=e.id AND b.badge_key='alpha-tester' WHERE b.user_id IS NULL")->fetchColumn();$excluded=$p->query("SELECT COUNT(*) FROM site_memorials m JOIN website_badges b ON b.user_id=m.user_id AND b.badge_key='alpha-tester'")->fetchColumn();if($missing||$excluded)throw new Exception('Coverage/exclusion failed');if($rankHash!==hash('sha256',json_encode($p->query('SELECT id,role FROM site_accounts ORDER BY id')->fetchAll(PDO::FETCH_ASSOC))))throw new Exception('Roles changed');echo json_encode(['eligible'=>count($rows),'previousAlpha'=>count($before),'missing'=>(int)$missing,'memorialAlpha'=>(int)$excluded,'rolesUnchanged'=>true]);