web/website/integrations/mybb/system-board-embed.php
2026-11-21 20:22:37 +01:00

38 lines
4.1 KiB
PHP

<?php
function dune_system_board_embed(&$page): void {
global $db;
if (($_GET['dune_embed'] ?? '') !== '1') return;
$fid=(int)($_GET['fid'] ?? 0); $identity=$GLOBALS['dune_system_board_identity'] ?? [];
$boards=dune_system_board_registry(); $parents=[];
$query=$db->simple_select('forums','fid,pid'); while($row=$db->fetch_array($query))$parents[(int)$row['fid']]=(int)$row['pid'];
$manage='';
foreach($identity['systemBoards']['organisations'] ?? [] as $grant){
$board=$boards[($grant['kind'] ?? '').':'.($grant['id'] ?? '')] ?? null;
if(!$board||($grant['manage'] ?? false)!==true||!dune_board_descendant($fid,(int)$board['fid'],$parents))continue;
$href='/system-board-manage.php?kind='.rawurlencode($grant['kind']).'&amp;organisation='.rawurlencode($grant['id']).'&amp;dune_embed=1';
$manage='<a href="'.$href.'">Manage board</a>';
}
$page=preg_replace_callback('/<body\b([^>]*)>/i',function($match){$attributes=$match[1];if(str_contains($attributes,'class="'))$attributes=str_replace('class="','class="dune-board-embedded ',$attributes);else $attributes.=' class="dune-board-embedded"';return '<body'.$attributes.'>';},$page,1);
$path=parse_url($_SERVER['REQUEST_URI'] ?? '/index.php',PHP_URL_PATH);if(!in_array($path,['/forumdisplay.php','/showthread.php'],true))$path='/index.php';
$params=$_GET;unset($params['dune_embed']);$outside='https://forums.dunerealms.com'.$path.($params?'?'.http_build_query($params):'');
$toolbar='<section class="system-board-toolbar"><div><strong>Internal Boards</strong><small>Private conversations · Your organisation</small></div><nav>'.$manage.'<a href="'.htmlspecialchars($outside,ENT_QUOTES|ENT_SUBSTITUTE,'UTF-8').'" target="_blank" rel="noopener" data-system-board-external>Open in Boards ↗</a></nav></section>';
if(!str_contains($page,'data-system-board-toolbar'))$page=preg_replace('/(<body\b[^>]*>)/i','$1'.$toolbar,$page,1);
$style = '<link rel="stylesheet" href="/system-board.css?v=dev2">';
$script = <<<'JS'
<script>(()=>{const parents=new Set(['https://cp.dunerealms.com','https://clans.dunerealms.com','https://corp.dunerealms.com','https://pol.dunerealms.com']);const send=()=>{const height=Math.ceil(Math.max(document.body.scrollHeight,document.body.getBoundingClientRect().height))+16;if(parent!==window&&height>=100&&height<=200000)for(const origin of parents)parent.postMessage({type:'dune-embed-size',height},origin);};for(const a of document.querySelectorAll('a[href]')){try{const u=new URL(a.href);if(u.origin===location.origin&&!u.pathname.startsWith('/admin/')&&!u.pathname.endsWith('dune-sso.php')&&!a.hasAttribute('data-system-board-external')){u.searchParams.set('dune_embed','1');a.href=u.href;}}catch{}}for(const f of document.forms){const input=document.createElement('input');input.type='hidden';input.name='dune_embed';input.value='1';f.append(input);try{const u=new URL(f.action);if(u.origin===location.origin){u.searchParams.set('dune_embed','1');f.action=u.href;}}catch{}}window.addEventListener('message',e=>{if(e.source===parent&&parents.has(e.origin)&&e.data?.type==='dune-embed-measure')send();});new ResizeObserver(send).observe(document.body);send();})();</script>
JS;
$page = str_replace('</head>', $style.'</head>', $page);
$page = str_replace('</body>', $script.'</body>', $page);
}
// Initialize once, then let MyBB's existing category expander and cookie keep
// the user's preference. No second collapse implementation or local storage.
function dune_system_board_default_collapse(): void {
global $mybb,$db,$collapse,$collapsed,$collapsedimg,$collapsedthead;
if(!empty($mybb->cookies['dune_system_board_default'])||!$db->table_exists('dune_system_boards'))return;
$root=$db->fetch_array($db->simple_select('dune_system_boards','fid',"kind='system' AND organisation_id='root'"));
if(empty($root['fid']))return;$key='cat_'.(int)$root['fid'];
$collapse=array_values(array_unique(array_merge($collapse??[],[$key])));
$collapsed[$key.'_e']='display: none;';$collapsedimg[$key]='_collapsed';$collapsedthead[$key]=' thead_collapsed';
my_setcookie('collapsed',implode('|',$collapse));my_setcookie('dune_system_board_default','1');
}