-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview-document.php
More file actions
167 lines (150 loc) · 6.67 KB
/
Copy pathview-document.php
File metadata and controls
167 lines (150 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
include 'includes/db.php';
if (!isset($_GET['file'])) {
die('File not found');
}
$fileParam = $_GET['file'];
// Security: Validate file path
$file = realpath($fileParam);
$allowed_dir = realpath(__DIR__);
if ($file === false || strpos($file, $allowed_dir) !== 0 || !file_exists($file)) {
http_response_code(404);
die('Access denied or file not found');
}
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$filename = basename($file);
// If caller requested the raw file bytes, serve inline (prevents direct-download behavior)
if (isset($_GET['raw'])) {
$mimeTypes = [
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'odt' => 'application/vnd.oasis.opendocument.text'
];
$mime = $mimeTypes[$ext] ?? 'application/octet-stream';
// Serve inline so fetch() returns bytes instead of triggering browser download
header('Content-Type: ' . $mime);
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
// For direct PDF serving (viewer page will directly stream PDFs)
if ($ext === 'pdf') {
$scheme = (!empty($_SERVER['REQUEST_SCHEME'])) ? $_SERVER['REQUEST_SCHEME'] : (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
$baseUrl = $scheme . '://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['REQUEST_URI']), '/');
$viewerFileUrl = $baseUrl . '/view-document.php?file=' . urlencode($fileParam) . '&raw=1';
$downloadUrl = $baseUrl . '/view-document.php?file=' . urlencode($fileParam) . '&raw=1&download=1';
$viewerFileUrlEsc = htmlspecialchars($viewerFileUrl, ENT_QUOTES);
$title = htmlspecialchars($filename, ENT_QUOTES | ENT_SUBSTITUTE);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?php echo $title; ?></title>
<style>
/* remove any page header and let the PDF fill the viewport */
html, body { height: 100%; margin: 0; }
/* full-bleed PDF */
embed#pdfEmbed { width: 100%; height: 100vh; border: 0; display: block; }
/* ensure tab title stays */
</style>
</head>
<body>
<!-- header removed: only embed shown -->
<embed id="pdfEmbed" src="<?php echo $viewerFileUrlEsc; ?>" type="application/pdf">
<script>
(function(){
var forcedTitle = <?php echo json_encode($title); ?>;
document.title = forcedTitle;
var keep = setInterval(function(){
if (document.title !== forcedTitle) document.title = forcedTitle;
}, 200);
setTimeout(function(){ clearInterval(keep); }, 5000);
try { history.replaceState(history.state, forcedTitle, window.location.href); } catch(e) {}
})();
</script>
</body>
</html>
<?php
exit;
}
// For Word documents, show HTML viewer
if (in_array($ext, ['doc', 'docx'])) {
// Build a URL that fetches the raw bytes via this same script (raw=1)
$scheme = (!empty($_SERVER['REQUEST_SCHEME'])) ? $_SERVER['REQUEST_SCHEME'] : (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
$baseUrl = $scheme . '://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['REQUEST_URI']), '/');
$viewerFileUrl = $baseUrl . '/view-document.php?file=' . urlencode($fileParam) . '&raw=1';
// Safely encode for JS
$viewerFileUrlJs = json_encode($viewerFileUrl);
$title = htmlspecialchars($filename, ENT_QUOTES | ENT_SUBSTITUTE);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?php echo $title; ?></title>
<style>
/* ... minimal styles (kept short) ... */
body{font-family:Arial,Helvetica,sans-serif;background:#f5f7fa;margin:0;padding:20px}
.container{max-width:1000px;margin:0 auto;background:#fff;border-radius:6px;box-shadow:0 2px 8px rgba(0,0,0,.06);overflow:hidden}
.header{background:#185a9d;color:#fff;padding:12px 16px;display:flex;justify-content:space-between;align-items:center}
.content{padding:24px;min-height:320px}
.error{background:#fff0f0;color:#c33;padding:12px;border-radius:4px}
</style>
</head>
<body>
<div class="container">
<div class="header">
<div><?php echo $title; ?></div>
<div><button onclick="window.close()">Close</button></div>
</div>
<div class="content" id="content">
<div>Loading document…</div>
</div>
</div>
<script>
(function() {
const fileUrl = <?php echo $viewerFileUrlJs; ?>;
function showError(msg) {
document.getElementById('content').innerHTML =
'<div class="error"><strong>Error loading document:</strong> ' + msg +
'<br><br>Please try downloading the file instead.</div>';
}
function loadDocument() {
fetch(fileUrl).then(resp => {
if (!resp.ok) throw new Error('Failed to fetch document (' + resp.status + ')');
return resp.arrayBuffer();
}).then(arrayBuffer => {
// mammoth should be defined by the CDN script loaded below
if (typeof mammoth === 'undefined') {
throw new Error('mammoth library not available');
}
return mammoth.convertToHtml({ arrayBuffer: arrayBuffer });
}).then(result => {
document.getElementById('content').innerHTML =
'<div>' + result.value + '</div>';
}).catch(err => {
showError(err.message);
});
}
// Dynamically load Mammoth and call loadDocument on success, show error on failure
var s = document.createElement('script');
s.src = 'assets/js/mammoth.min.js'; // relative to current viewer page
s.onload = loadDocument;
s.onerror = function(){ showError('Failed to load local mammoth.js library. Check that assets/js/mammoth.min.js exists and is reachable.'); };
document.head.appendChild(s);
})();
</script>
</body>
</html>
<?php
exit;
}
// Unsupported file type
http_response_code(400);
die('Unsupported file type: ' . htmlspecialchars($ext));
?>