<?php
declare(strict_types=1);
require_once __DIR__ . '/../../authentication/api/connect.php';
$u=require_auth();$fileId=(int)($_GET['id']??0);$q=db()->prepare('SELECT e.storage_key,e.relative_path,ps.submitted_by,p.department_id,p.campus_id FROM extracted_submission_files e JOIN project_submissions ps ON ps.id=e.submission_id JOIN project_assignments a ON a.id=ps.assignment_id JOIN practical_projects p ON p.id=a.project_id WHERE e.id=?');$q->execute([$fileId]);$f=$q->fetch();if(!$f)reply(404,['status'=>'error','message'=>'Code file not found.']);$allowed=in_array($u['role'],['admin','staff'],true);
if($u['role']==='student'){$s=db()->prepare('SELECT id FROM students WHERE user_id=?');$s->execute([(int)$u['id']]);$student=$s->fetch();$allowed=$student&&(int)$f['submitted_by']===(int)$student['id'];}elseif($u['role']==='hod'){$s=db()->prepare('SELECT 1 FROM hod_assignments WHERE user_id=? AND department_id=? AND campus_id=?');$s->execute([(int)$u['id'],(int)$f['department_id'],(int)$f['campus_id']]);$allowed=(bool)$s->fetch();}if(!$allowed)reply(403,['status'=>'error','message'=>'You do not have access to this code file.']);
$key=(string)$f['storage_key'];if(!preg_match('~^[0-9]+/[A-Za-z0-9._/-]+$~',$key)||str_contains($key,'..'))reply(404,['status'=>'error','message'=>'File unavailable.']);$path=private_storage_root().'/submissions/'.$key;if(!is_file($path)||filesize($path)>2*1024*1024)reply(404,['status'=>'error','message'=>'File unavailable or too large to preview.']);$content=file_get_contents($path);$name=htmlspecialchars((string)$f['relative_path'],ENT_QUOTES|ENT_SUBSTITUTE,'UTF-8');$body=htmlspecialchars($content===false?'':$content,ENT_QUOTES|ENT_SUBSTITUTE,'UTF-8');header('Content-Type: text/html; charset=utf-8');header("Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self'; connect-src 'self'; base-uri 'none'; frame-ancestors 'self'");header('X-Content-Type-Options: nosniff');header('Cache-Control: private, no-store');$assets=htmlspecialchars(app_path().'/shared/',ENT_QUOTES,'UTF-8');
echo '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>'.$name.' | SLPS code viewer</title><link rel="stylesheet" href="'.$assets.'vendor/ace/ace.css"><link rel="stylesheet" href="'.$assets.'vendor/ace/tomorrow-night.css"><link rel="stylesheet" href="'.$assets.'css/file-code-viewer.css?v=1"><script src="'.$assets.'vendor/ace/ace.js?v=2" defer></script><script src="'.$assets.'js/code-languages.js?v=1" defer></script><script src="'.$assets.'js/file-code-viewer.js?v=1" defer></script></head><body data-ace-base="'.$assets.'vendor/ace"><header><div><small>ROCKVIEW UNIVERSITY · SOURCE CODE</small><h1 id="codeViewerFilename">'.$name.'</h1></div><span id="codeViewerLanguage">Loading syntax highlighting…</span></header><main><div id="codeViewerEditor" aria-label="Read-only source code editor"></div><textarea id="codeViewerSource" aria-label="Source code fallback" readonly>'.$body.'</textarea></main><footer id="codeViewerStatus">Read-only preview · Source code is never executed</footer></body></html>';
