<?php
declare(strict_types=1);
require_once __DIR__.'/../lib/guidelines.php';
$user=require_auth();$scope=guideline_scope($user);$q=db()->prepare('SELECT * FROM guideline_assets WHERE id=?');$q->execute([(int)($_GET['id']??0)]);$asset=$q->fetch();
if(!$asset)reply(404,['status'=>'error','message'=>'Resource not found.']);
guideline_course($scope,(int)$asset['course_id']);
if(!$scope['can_edit']&&!(int)$asset['is_published'])reply(404,['status'=>'error','message'=>'This resource is not published.']);
if(!preg_match('/^[a-f0-9]{48}\.[a-z0-9]+$/',(string)$asset['storage_key']))reply(404,['status'=>'error','message'=>'Resource unavailable.']);
$path=private_storage_root().'/guidelines/'.$asset['storage_key'];if(!is_file($path))reply(404,['status'=>'error','message'=>'Resource unavailable.']);
$name=preg_replace('/[^a-zA-Z0-9._-]/','_',basename((string)$asset['file_name']));
$inline=$asset['asset_kind']==='image'||($asset['media_type']==='application/pdf'&&!isset($_GET['download']));
header('Content-Type: '.$asset['media_type']);header('Content-Length: '.filesize($path));header('X-Content-Type-Options: nosniff');header("Content-Security-Policy: default-src 'none'; sandbox");header('Cache-Control: private, no-store');header('Content-Disposition: '.($inline?'inline':'attachment').'; filename="'.$name.'"');session_write_close();readfile($path);
