forked from unimatrix/web
63 lines
5.9 KiB
PHP
63 lines
5.9 KiB
PHP
<?php
|
|
if(!defined('IN_MYBB'))die('Direct access denied');
|
|
require_once __DIR__.'/system-board-provision.php';
|
|
|
|
function dune_board_key_records(array $records): array {
|
|
if(count($records)>25)throw new RuntimeException('Board batch exceeds its bound');
|
|
$result=[];$seen=[];$keys=[];
|
|
foreach($records as $record){
|
|
if(!is_array($record)||!is_string($record['keyId']??null)||!preg_match('/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i',$record['keyId'])||!is_int($record['revision']??null)||$record['revision']<1||$record['revision']>2147483647||!is_string($record['fingerprint']??null)||!preg_match('/^[a-f0-9]{64}$/',$record['fingerprint'])||!is_string($record['desiredJson']??null)||strlen($record['desiredJson'])>131072||!hash_equals($record['fingerprint'],hash('sha256',$record['desiredJson'])))throw new RuntimeException('Verified board key payload required');
|
|
$desired=json_decode($record['desiredJson'],true,512,JSON_THROW_ON_ERROR);$org=$desired['organisation']??null;
|
|
if(($desired['version']??null)!==1||($desired['template']['id']??null)!=='private-organisation'||($desired['template']['version']??null)!==1||!is_array($org)||!in_array($org['kind']??null,['clan','company','politics'],true)||!is_string($org['id']??null)||!preg_match('/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i',$org['id'])||!is_string($org['name']??null)||trim($org['name'])===''||strlen($org['name'])>480||preg_match('/[\x00-\x1f]/',$org['name'])||!in_array($org['lifecycle']??null,['active','archived'],true))throw new RuntimeException('Supported board template and organisation required');
|
|
$identity=$org['kind'].':'.$org['id'];if(isset($seen[$identity])||isset($keys[$record['keyId']]))throw new RuntimeException('Duplicate board batch identity');$seen[$identity]=true;$keys[$record['keyId']]=true;
|
|
$result[]=['keyId'=>$record['keyId'],'revision'=>$record['revision'],'fingerprint'=>$record['fingerprint'],'organisation'=>$org];
|
|
}
|
|
return $result;
|
|
}
|
|
function dune_board_key_order(?array $existing,array $record): string {
|
|
if(!$existing||empty($existing['board_key_id']))return 'apply';
|
|
if($existing['board_key_id']!==$record['keyId'])return 'review';
|
|
if((int)$existing['board_revision']>$record['revision'])return 'superseded';
|
|
if((int)$existing['board_revision']===$record['revision'])return hash_equals((string)$existing['board_fingerprint'],$record['fingerprint'])?'replay':'review';
|
|
return 'apply';
|
|
}
|
|
function dune_prepare_board_key_registry(): void {
|
|
global $db;
|
|
$found=[];$query=$db->write_query('SHOW COLUMNS FROM '.TABLE_PREFIX.'dune_system_boards');while($row=$db->fetch_array($query))$found[$row['Field']]=true;
|
|
foreach(['board_key_id'=>'CHAR(36) NULL','board_revision'=>'INT NOT NULL DEFAULT 0','board_fingerprint'=>'CHAR(64) NULL','board_lifecycle'=>"VARCHAR(16) NOT NULL DEFAULT 'active'"]as $name=>$definition)if(!isset($found[$name]))$db->write_query('ALTER TABLE '.TABLE_PREFIX.'dune_system_boards ADD COLUMN '.$name.' '.$definition);
|
|
}
|
|
function dune_apply_board_keys(array $raw): array {
|
|
global $db;
|
|
if(PHP_SAPI!=='cli')throw new RuntimeException('Native board key publication requires CLI');
|
|
$records=dune_board_key_records($raw);$receipts=[];$apply=[];
|
|
if(!$records)return ['protocol'=>1,'groupsRevision'=>dune_board_group_fingerprint(),'records'=>[]];
|
|
$input=['organisations'=>array_map(fn($r)=>array_intersect_key($r['organisation'],array_flip(['kind','id','name'])),$records)];
|
|
$review=function(array &$input)use($records,&$receipts,&$apply){
|
|
global $db;$input['organisations']=[];
|
|
foreach($records as $record){
|
|
$org=$record['organisation'];$board=dune_provision_board_row($org['kind'],$org['id']);$order=dune_board_key_order($board?:null,$record);
|
|
$receipt=['keyId'=>$record['keyId'],'revision'=>$record['revision'],'fingerprint'=>$record['fingerprint'],'state'=>$order];
|
|
if($board){$forum=$db->fetch_array($db->simple_select('forums','fid','fid='.(int)$board['fid']));if(!$forum)$receipt['state']='review';}
|
|
$bound=$db->fetch_array($db->simple_select('dune_system_boards','kind,organisation_id',"board_key_id='".$db->escape_string($record['keyId'])."'",['limit'=>1]));
|
|
if($bound&&($bound['kind']!==$org['kind']||$bound['organisation_id']!==$org['id']))$receipt['state']='review';
|
|
if($receipt['state']==='apply'){$input['organisations'][]=array_intersect_key($org,array_flip(['kind','id','name']));$apply[]=$record;}
|
|
elseif($receipt['state']==='replay'){$input['organisations'][]=array_intersect_key($org,array_flip(['kind','id','name']));$apply[]=$record;}
|
|
$receipts[$record['keyId']]=$receipt;
|
|
}
|
|
};
|
|
$commit=function(array $groups)use(&$apply,&$receipts){
|
|
global $db;
|
|
foreach($apply as $record){
|
|
$org=$record['organisation'];$board=dune_provision_board_row($org['kind'],$org['id']);
|
|
if(!$board||empty($groups[(int)$board['member_gid']])||empty($groups[(int)$board['leader_gid']]))throw new RuntimeException('Native board mapping requires review');
|
|
$db->update_query('dune_system_boards',['board_key_id'=>$db->escape_string($record['keyId']),'board_revision'=>$record['revision'],'board_fingerprint'=>$db->escape_string($record['fingerprint']),'board_lifecycle'=>$org['lifecycle']],'fid='.(int)$board['fid']);
|
|
$db->update_query('forums',['active'=>$org['lifecycle']==='active'?1:0],'fid='.(int)$board['fid']);
|
|
$receipts[$record['keyId']]=['keyId'=>$record['keyId'],'revision'=>$record['revision'],'fingerprint'=>$record['fingerprint'],'state'=>'applied','native'=>['forumId'=>(int)$board['fid'],'memberGroupId'=>(int)$board['member_gid'],'leaderGroupId'=>(int)$board['leader_gid']]];
|
|
}
|
|
};
|
|
dune_provision_system_boards($input,'dune_prepare_board_key_registry',$review,$commit);
|
|
return ['protocol'=>1,'groupsRevision'=>dune_board_group_fingerprint(),'records'=>array_values($receipts)];
|
|
}
|
|
function dune_board_group_fingerprint(): string {
|
|
global $db;$ids=[];$query=$db->simple_select('usergroups','gid');while($row=$db->fetch_array($query))$ids[]=(int)$row['gid'];sort($ids,SORT_NUMERIC);return hash('sha256',implode(',',$ids));
|
|
}
|

Staff