forked from assembler-institute/filesystem-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open.php
198 lines (191 loc) · 6.4 KB
/
open.php
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
session_start();
define("ROOT", "./root");
require_once('utils/utils.php');
if (isset($_GET['search'])) {
$absolutePath = $_GET['path'];
$fileName = $_GET['name'];
} else {
$fileName = $_REQUEST['name'];
$relativePath = $_SESSION['relativePath'];
$absolutePath = $_SESSION['absolutePath'];
}
$GLOBALS['fileExtensionsAllowed'] = ['jpg', 'png', 'txt', 'docx', 'csv', 'ppt', 'odt', 'pdf', 'zip', 'rar', 'exe', 'svg', 'mp3', 'mp4'];
include('templates/header.php');
?>
<body>
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-dark">
<h4 class="text-white m-0 ms-4">File Explorer</h4>
<form class="navbar-brand m-0 ms-3 me-3 text-white" action="index.php" method="POST">
<input type="hidden" name="path" value=<?php echo ROOT ?>>
<button type="submit" class="home-btn folder-btn"><i class="bi bi-house"></i></button>
</form>
</nav>
</header>
<article class="container-fluid mt-3">
<?php
if (isset($_GET['search'])) {
openFile(ROOT . '/' . $absolutePath, $fileName);
} else {
openFile($absolutePath . '/' . $fileName, $fileName);
}
?>
</article>
<?php include('templates/footer.php') ?>
<?php
function openFile($path, $fileName)
{
$fileExtension = Utils::getFileExtension($fileName);
?>
<div></div>
<p class="p"><strong>File "<?php echo $fileName ?>"</strong></p>
<p class="p"><strong>Path: </strong>
<?php echo Utils::getAbsolutePath($path) ?>
</p>
<p class="p"><strong>Size: </strong> <?php echo Utils::formatSize(filesize($path)) ?></p>
<p class="p"><strong>MIME-Type: </strong>
<?php echo mime_content_type($path) ?>
</p>
<p class="p"><strong>Charset: </strong> <?php echo strtolower(mb_detect_encoding($path, ['UTF-8'], false)); ?></p>
<div class="mb-3">
<a class="link text-primary fw-bold ms-5 me-3" href="<?php echo $path ?>" download="<?php echo $fileName ?>">
<i class="bi bi-cloud-arrow-down"></i>
Download
</a>
<?php
switch ($fileExtension) {
case 'txt':
case 'jpg':
case 'svg':
case 'png':
case 'mp3':
case 'pdf':
case 'mp4':
?>
<a class="link text-primary fw-bold me-3" target="_blank" href="<?php echo $path ?>">
<i class="bi bi-door-open"></i>
Open
</a>
<?php
break;
}
?>
<a class="link text-primary fw-bold" href="index.php?p=<?php echo $_SESSION['relativePath'] ?>">
<i class="bi bi-arrow-90deg-left"></i>
Back
</a>
</div>
<?php
switch ($fileExtension) {
case 'docx':
?>
<pre class="pre">
<code class="code">
<?php
echo Utils::formatDocx($path);
?>
</code>
</pre>
<?php
break;
case "rar":
?>
<pre class="pre">
<code class="code">
<?php
$archive = RarArchive::open($path);
$entries = $archive->getEntries();
foreach ($entries as $entry) {
$entry = str_replace('RarEntry for file "', "File: ", $entry);
echo "<p>" . $entry . "</p><br>";
}
$archive->close();
?>
</code>
</pre>
<?php
break;
case "zip":
?>
<pre class="pre">
<code class="code">
<?php
$zip = new ZipArchive;
if ($zip->open($path) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
echo "<p>" . $filename . "</p><br>";
}
$zip->close();
}
?>
</code>
</pre>
<?php
break;
case "jpg":
case "png":
case "svg":
?>
<img src="<?php echo $path ?>" alt="<?php echo $fileName ?>">
<?php
break;
case "mp3":
?>
<audio controls autoplay>
<source src="<?php echo $path ?>" type="audio/mpeg">
</audio>
<?php
break;
case "mp4":
?>
<video controls autoplay>
<source src="<?php echo $path ?>" type="video/mp4">
</video>
<?php
break;
case "txt":
case "html":
?>
<pre class="pre">
<code class="code">
<?php
$fileStream = fopen($path, 'r') or die("Unable to open file.");
echo "<p>" . Utils::formatHtml(fread($fileStream, filesize($path))) . "</p>";
?>
</code>
</pre>
<?php
break;
case "csv":
?>
<pre class="pre">
<code class="code">
<?php
Utils::formatCsv($path);
?>
</code>
</pre>
<?php
break;
case "ppt":
case "odt":
case "exe":
break;
case "pdf":
?>
<pre class="pre">
<code class="code">
<?php
Utils::formatPdf($path);
?>
</code>
</pre>
<?php
}
?>
</code>
</pre>
<?php
}