| Current Path : /var/www/cesa.co.za/cceconvenors/ |
| Current File : /var/www/cesa.co.za/cceconvenors/document-view.php |
<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
include_once("ewcfg7.php");
include_once("ewmysql7.php");
include_once("phpfn7.php");
include_once("userfn7.php");
if (!isset($_SESSION[EW_SESSION_STATUS]) || ($_SESSION[EW_SESSION_STATUS] != "login")) {
header("Location: /cceconvenors/login.php");
exit;
}
$file = isset($_GET['file']) ? trim((string) $_GET['file']) : '';
$source = isset($_GET['source']) ? trim((string) $_GET['source']) : 'assignmentsupload';
if ($file === '') {
include_once('header.php');
echo '<h1>View Document</h1>';
echo '<div class="alert alert-warning">No document specified.</div>';
include_once('footer.php');
exit;
}
if (strpos($file, '..') !== false || strpos($file, '/') !== false || strpos($file, '\\') !== false) {
include_once('header.php');
echo '<h1>View Document</h1>';
echo '<div class="alert alert-danger">Invalid file path.</div>';
include_once('footer.php');
exit;
}
$allowedSources = array('assignmentsupload', 'documents');
if (!in_array($source, $allowedSources, true)) {
$source = 'assignmentsupload';
}
$baseDir = ($source === 'documents') ? '/cceadmin/documents/' : '/cceadmin/assignmentsupload/';
$filePath = $_SERVER['DOCUMENT_ROOT'] . $baseDir . $file;
if (!file_exists($filePath)) {
include_once('header.php');
echo '<h1>View Document</h1>';
echo '<div class="alert alert-warning">File not found.</div>';
include_once('footer.php');
exit;
}
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($ext !== 'pdf') {
header("Location: " . $baseDir . rawurlencode($file));
exit;
}
$_SESSION['convenor_document_token'] = $file;
$_SESSION['convenor_document_source'] = $source;
$pdfPath = '/cceconvenors/document-pdf-proxy.php?token=' . rawurlencode($file) . '&source=' . rawurlencode($source);
include_once('header.php');
?>
<h1>View Document</h1>
<div id="pdf-viewer" style="position: relative; width: 100%; max-width: 1000px; margin: 0 auto;">
<div style="text-align: center; margin-bottom: 10px; padding: 10px; background: #f8f9fa; border-radius: 5px;">
<button id="prev-page" class="btn btn-secondary" style="margin-right: 10px;">Previous</button>
<span id="page-info" style="font-weight: bold;">Page 1</span>
<button id="next-page" class="btn btn-secondary" style="margin-left: 10px;">Next</button>
<button id="zoom-in" class="btn btn-outline-secondary" style="margin-left: 10px;">Zoom In</button>
<button id="zoom-out" class="btn btn-outline-secondary" style="margin-left: 5px;">Zoom Out</button>
</div>
<div id="canvas-container" style="position: relative; border: 1px solid #ddd; background: white; text-align: center; overflow: auto; max-height: 700px;">
<canvas id="pdf-canvas" style="border: none; user-select: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none;"></canvas>
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) rotate(-45deg); color: rgba(0,0,0,0.1); font-size: 24px; font-weight: bold; pointer-events: none; user-select: none; white-space: nowrap; z-index: 10;">
<?php echo htmlspecialchars((string) $_SESSION[EW_SESSION_USER_NAME]); ?> - <?php echo date("Y-m-d H:i:s"); ?>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
let pdfDoc = null, pageNum = 1, pageRendering = false, pageNumPending = null, scale = 1.5;
let canvas = document.getElementById('pdf-canvas');
let ctx = canvas.getContext('2d');
pdfjsLib.getDocument('<?php echo $pdfPath; ?>').promise.then(function(pdf) {
pdfDoc = pdf;
document.getElementById('page-info').textContent = `Page ${pageNum} of ${pdf.numPages}`;
renderPage(pageNum);
}).catch(function() {
document.getElementById('canvas-container').innerHTML = '<div class="alert alert-danger">Error loading PDF.</div>';
});
function renderPage(num) {
pageRendering = true;
pdfDoc.getPage(num).then(function(page) {
let viewport = page.getViewport({ scale: scale });
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({ canvasContext: ctx, viewport: viewport }).promise.then(function() {
pageRendering = false;
if (pageNumPending !== null) {
renderPage(pageNumPending);
pageNumPending = null;
}
});
});
document.getElementById('page-info').textContent = `Page ${num} of ${pdfDoc.numPages}`;
}
function queueRenderPage(num) { if (pageRendering) pageNumPending = num; else renderPage(num); }
function onPrevPage() { if (pageNum <= 1) return; pageNum--; queueRenderPage(pageNum); }
function onNextPage() { if (pageNum >= pdfDoc.numPages) return; pageNum++; queueRenderPage(pageNum); }
function zoomIn() { scale *= 1.2; queueRenderPage(pageNum); }
function zoomOut() { scale /= 1.2; queueRenderPage(pageNum); }
document.getElementById('prev-page').addEventListener('click', onPrevPage);
document.getElementById('next-page').addEventListener('click', onNextPage);
document.getElementById('zoom-in').addEventListener('click', zoomIn);
document.getElementById('zoom-out').addEventListener('click', zoomOut);
</script>
<?php include_once('footer.php'); ?>