forked from unimatrix/web
111 lines
8.6 KiB
PHP
111 lines
8.6 KiB
PHP
<?php
|
|
// Install from a CI-approved release. CLI input is an explicit organisation
|
|
// snapshot from the central service, never a browser's claimed membership.
|
|
if (PHP_SAPI !== 'cli') { http_response_code(404); exit; }
|
|
$input = json_decode(stream_get_contents(STDIN), true, 512, JSON_THROW_ON_ERROR);
|
|
if (!is_array($input) || !isset($input['organisations']) || !is_array($input['organisations'])) throw new RuntimeException('Organisation snapshot required');
|
|
foreach ($input['organisations'] as $org) {
|
|
if (!in_array($org['kind'] ?? '', ['clan','company'], true) || !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']) > 120) throw new RuntimeException('Invalid organisation');
|
|
}
|
|
define('IN_MYBB', 1);
|
|
define('MYBB_ROOT', '/home/dunerealms/apps/integrations/mybb/');
|
|
require MYBB_ROOT.'inc/init.php';
|
|
require __DIR__.'/system-board-policy.php';
|
|
$lock = $db->fetch_array($db->write_query("SELECT GET_LOCK('dune_system_boards_provision',10) acquired"));
|
|
if ((int)($lock['acquired'] ?? 0) !== 1) throw new RuntimeException('System Boards publication is busy');
|
|
try {
|
|
$db->write_query('CREATE TABLE IF NOT EXISTS '.TABLE_PREFIX.'dune_system_boards (kind VARCHAR(12) NOT NULL, organisation_id VARCHAR(36) NOT NULL, fid INT NOT NULL, member_gid INT NOT NULL DEFAULT 0, leader_gid INT NOT NULL DEFAULT 0, PRIMARY KEY(kind,organisation_id), UNIQUE KEY forum_id(fid)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4');
|
|
$db->write_query('START TRANSACTION');
|
|
$groups = []; $query = $db->simple_select('usergroups');
|
|
while ($row = $db->fetch_array($query)) $groups[(int)$row['gid']] = $row;
|
|
if (!isset($groups[2])) throw new RuntimeException('Registered group unavailable');
|
|
$template = $db->fetch_array($db->simple_select('forums', '*', "type='f'", ['limit'=>1]));
|
|
if (!$template) throw new RuntimeException('Native forum template unavailable');
|
|
$permissionColumns = ['canview','canviewthreads','canonlyviewownthreads','candlattachments','canpostthreads','canpostreplys','canonlyreplyownthreads','canpostattachments','canratethreads','caneditposts','candeleteposts','candeletethreads','caneditattachments','canviewdeletionnotice','modposts','modthreads','mod_edit_posts','modattachments','canpostpolls','canvotepolls','cansearch'];
|
|
function board_row($kind, $id) {
|
|
global $db;
|
|
return $db->fetch_array($db->simple_select('dune_system_boards', '*', "kind='".$db->escape_string($kind)."' AND organisation_id='".$db->escape_string($id)."'"));
|
|
}
|
|
function make_board($kind, $id, $name, $parent, $type = 'c') {
|
|
global $db, $template;
|
|
if ($existing = board_row($kind, $id)) return $existing;
|
|
$row = $template; unset($row['fid']);
|
|
foreach (['threads','posts','lastpost','lastposteruid','unapprovedthreads','unapprovedposts','deletedthreads','deletedposts'] as $key) if (array_key_exists($key, $row)) $row[$key] = 0;
|
|
foreach (['lastposter','lastpostsubject','linkto','password','rules','rulestitle'] as $key) if (array_key_exists($key, $row)) $row[$key] = '';
|
|
$row = array_merge($row, ['name'=>htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), 'description'=>'', 'type'=>$type, 'pid'=>$parent, 'parentlist'=>'', 'disporder'=>1000, 'active'=>0, 'open'=>1, 'allowhtml'=>0, 'allowmycode'=>1, 'allowsmilies'=>1, 'allowimgcode'=>1, 'showinjump'=>1]);
|
|
foreach ($row as $key=>$value) if (is_string($value)) $row[$key]=$db->escape_string($value);
|
|
$fid = (int)$db->insert_query('forums', $row);
|
|
$parentRow = $parent ? $db->fetch_array($db->simple_select('forums','parentlist','fid='.$parent)) : null;
|
|
$parentlist = ($parentRow ? $parentRow['parentlist'].',' : '').$fid;
|
|
$db->update_query('forums', ['parentlist'=>$db->escape_string($parentlist)], 'fid='.$fid);
|
|
$db->insert_query('dune_system_boards', ['kind'=>$db->escape_string($kind), 'organisation_id'=>$db->escape_string($id), 'fid'=>$fid]);
|
|
return board_row($kind, $id);
|
|
}
|
|
$root = make_board('system','root','System Boards',0);
|
|
$clans = make_board('system','clans','Clan Boards',(int)$root['fid']);
|
|
$companies = make_board('system','companies','Company Boards',(int)$root['fid']);
|
|
make_board('system','elections','Election Board',(int)$root['fid'],'f');
|
|
foreach ($input['organisations'] as $org) {
|
|
$board = make_board($org['kind'], $org['id'], $org['name'], (int)($org['kind']==='clan' ? $clans['fid'] : $companies['fid']), 'f');
|
|
foreach (['member_gid','leader_gid'] as $field) {
|
|
$role=$field==='leader_gid'?'leaders':'members';
|
|
$title=my_substr(($org['kind']==='clan'?'Clan':'Company').' '.$role.' · '.$org['name'],0,45);
|
|
if ((int)$board[$field] > 0) {
|
|
$db->update_query('usergroups',['title'=>$db->escape_string($title)],'gid='.(int)$board[$field]);
|
|
continue;
|
|
}
|
|
$group = $groups[2]; unset($group['gid']);
|
|
$group['title'] = $title;
|
|
$group['cancp'] = 0; $group['issupermod'] = 0; $group['canmodcp'] = $field==='leader_gid' ? 1 : 0;
|
|
foreach ($group as $key=>$value) if (is_string($value)) $group[$key]=$db->escape_string($value);
|
|
$gid = (int)$db->insert_query('usergroups', $group);
|
|
$board[$field] = $gid; $groups[$gid] = $group;
|
|
$db->update_query('dune_system_boards', [$field=>$gid], 'fid='.(int)$board['fid']);
|
|
}
|
|
// Every known group gets an explicit policy: additional political
|
|
// groups cannot accidentally make an unrelated organisation visible.
|
|
foreach ($groups as $gid=>$group) {
|
|
$member = in_array($gid, [(int)$board['member_gid'],(int)$board['leader_gid'],3,4,6], true);
|
|
$permissions = dune_board_private_permissions($permissionColumns, $member);
|
|
save_board_permissions((int)$board['fid'], (int)$gid, $permissions);
|
|
}
|
|
$moderator = ['fid'=>(int)$board['fid'], 'id'=>(int)$board['leader_gid'], 'isgroup'=>1,
|
|
'caneditposts'=>1,'cansoftdeleteposts'=>1,'canrestoreposts'=>1,'cansoftdeletethreads'=>1,'canrestorethreads'=>1,
|
|
'canopenclosethreads'=>1,'canstickunstickthreads'=>1,'canapproveunapprovethreads'=>1,'canapproveunapproveposts'=>1,
|
|
'canapproveunapproveattachs'=>1,'canmanagethreads'=>1,'canmanagepolls'=>1,'canpostclosedthreads'=>1,'canmanagereportedposts'=>1,'canviewmodlog'=>1];
|
|
$db->delete_query('moderators', 'fid='.(int)$board['fid'].' AND id='.(int)$board['leader_gid'].' AND isgroup=1');
|
|
$db->insert_query('moderators', $moderator);
|
|
// Existing native Moderators retain moderation on every new board;
|
|
// Super Moderators and Administrators already have native global power.
|
|
$moderator['id']=6;
|
|
$db->delete_query('moderators', 'fid='.(int)$board['fid'].' AND id=6 AND isgroup=1');
|
|
$db->insert_query('moderators', $moderator);
|
|
}
|
|
// Refresh earlier boards too, since later provisioning adds managed groups.
|
|
// These groups must also be denied access to every other private board.
|
|
$query = $db->simple_select('dune_system_boards', '*', "kind IN ('clan','company')");
|
|
while ($board = $db->fetch_array($query)) foreach ($groups as $gid=>$group) {
|
|
$member = in_array($gid, [(int)$board['member_gid'],(int)$board['leader_gid'],3,4,6], true);
|
|
save_board_permissions((int)$board['fid'], (int)$gid, dune_board_private_permissions($permissionColumns, $member));
|
|
}
|
|
// No guest feed may publish System Boards topics.
|
|
save_board_permissions((int)$root['fid'], 1, dune_board_private_permissions($permissionColumns, false));
|
|
$query = $db->simple_select('dune_system_boards', 'fid');
|
|
while ($row = $db->fetch_array($query)) $db->update_query('forums', ['active'=>1], 'fid='.(int)$row['fid']);
|
|
$db->write_query('COMMIT');
|
|
foreach (['usergroups','forums','forumpermissions','moderators'] as $key) { $method='update_'.$key; $cache->$method(); }
|
|
echo json_encode(['status'=>'provisioned', 'organisations'=>count($input['organisations']), 'rootForum'=>(int)$root['fid']], JSON_THROW_ON_ERROR)."\n";
|
|
} catch (Throwable $error) {
|
|
$db->write_query('ROLLBACK');
|
|
throw $error;
|
|
} finally {
|
|
$db->write_query("SELECT RELEASE_LOCK('dune_system_boards_provision')");
|
|
}
|
|
|
|
function save_board_permissions(int $fid, int $gid, array $permissions): void {
|
|
global $db;
|
|
$where = 'fid='.$fid.' AND gid='.$gid;
|
|
$existing = $db->fetch_array($db->simple_select('forumpermissions', 'pid', $where));
|
|
if ($existing) $db->update_query('forumpermissions', $permissions, $where);
|
|
else $db->insert_query('forumpermissions', array_merge($permissions, ['fid'=>$fid,'gid'=>$gid]));
|
|
}
|

Staff