-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
143 lines (135 loc) · 4.82 KB
/
index.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
<?php
include 'config.php';
?>
<!doctype html>
<html>
<head>
<title><?php echo $html_title ?></title>
<link rel="stylesheet" type="text/css" href="gallery.css" />
<link rel="stylesheet" href="blueimp-jquery-image-gallery/jquery-ui.css" id="theme">
<link rel="stylesheet" href="blueimp-jquery-image-gallery/bla/blueimp-gallery.min.css">
</head>
<body>
<?php
$pathPara = $_GET['p'];
$saniPath = sanitizePath($image_start_dirs, $pathPara);
if ($saniPath != null) {
printDirContent($saniPath, false, $thumbnail_cache_dir);
} else {
foreach ($image_start_dirs as $path) {
printDirContent($path, true, $thumbnail_cache_dir);
}
}
printBottom();
?>
</body>
</html>
<?php
function sanitizePath($image_start_dirs, $path) {
if (strpos($path, "../")) {
return null;
}
$posOfFirstSlash = strpos($path, "/");
if ($posOfFirstSlash) {
$firstDirPart = substr($path, 0, $posOfFirstSlash);
foreach ($image_start_dirs as $startDir) {
if ($startDir === $firstDirPart) {
return $path;
}
}
}
return null;
}
function printDirContent($path, $isStartDir, $thumbnail_cache_dir) {
$dirObj = dir($path);
#echo "<ul>";
echo "<div id=\"links\">";
if (!$isStartDir) {
$parentDir = dirname($path);
printDir($parentDir, "..");
}
$dirs = array();
$files = array();
while (false !== ($entry = $dirObj->read())) {
// ignore dot files
$isDotFile = (substr($entry, 0, 1) === ".");
if (!$isDotFile) {
$fullPath = $path . "/" . $entry;
if (is_dir($fullPath)) {
array_push($dirs, $entry);
} elseif (is_file($fullPath)) {
array_push($files, $entry);
}
}
}
sort($dirs);
sort($files);
foreach ($dirs as $dir) {
$fullPath = $path . "/" . $dir;
printDir($fullPath, $dir);
}
foreach ($files as $file) {
handleFile($path, $file, $thumbnail_cache_dir);
}
#echo "</ul>";
echo "</div>";
$dirObj->close();
}
function handleFile($path, $fileName, $thumbnail_cache_dir) {
$lastIndexOfDot = strrpos($fileName, ".");
if ($lastIndexOfDot) {
$fileExtension = substr($fileName, $lastIndexOfDot + 1, strlen($fileName));
$fileExtensionLower = strtolower($fileExtension);
if ($fileExtensionLower === "jpg" || $fileExtensionLower === "jpeg" || $fileExtensionLower === "png") {
$thumbDirFullPath = $thumbnail_cache_dir."/".$path;
$thumbFileFullPath = $thumbDirFullPath."/".$fileName;
$originalFullPath = $path."/".$fileName;
if (!file_exists($thumbFileFullPath)) {
if (!file_exists($thumbDirFullPath)) {
mkdir($thumbDirFullPath, 0777, true);
}
createThumb($originalFullPath, $thumbFileFullPath);
}
printImgFile($originalFullPath, $thumbFileFullPath);
printImgPrefetch($originalFullPath);
}
}
}
function createThumb($originalFullPath, $thumbFullPath) {
$originalFullPath = escapePathForConvert($originalFullPath);
$thumbFullPath = escapePathForConvert($thumbFullPath);
exec("convert $originalFullPath -resize 64x64\> $thumbFullPath");
}
function escapePathForConvert($path) {
return str_replace(" ", "\\ ", $path);
}
function printImgFile($originalFullPath, $thumbFullPath) {
#echo "<li class=\"file\"><a href=\"$originalFullPath\"><img src=\"$thumbFullPath\" /></a></li>";
echo "<a class=\"file\" href=\"$originalFullPath\" data-dialog>";
echo "<img src=\"$thumbFullPath\" />";
echo "</a>";
}
function printImgPrefetch($originalFullPath) {
echo "<link rel=\"prefetch\" href=\"$originalFullPath\" />";
}
function printDir($path, $name) {
#echo "<li class=\"dir\"><a href=\"?p=$path\">$name</a></li>";
echo "<div class=\"dir\"><a href=\"?p=$path\">$name</a></div>";
}
function printBottom() {
// <!-- The dialog widget -->
echo "<div id=\"blueimp-gallery-dialog\" data-show=\"fade\" data-hide=\"fade\">";
// <!-- The gallery widget -->
echo "<div class=\"blueimp-gallery blueimp-gallery-carousel blueimp-gallery-controls\">";
echo "<div class=\"slides\"></div>";
echo "<a class=\"prev\">‹</a>";
echo "<a class=\"next\">›</a>";
echo "<a class=\"play-pause\"></a>";
echo "</div>";
echo "</div>";
echo "<script src=\"blueimp-jquery-image-gallery/jquery.min.js\"></script>";
echo "<script src=\"blueimp-jquery-image-gallery/jquery-ui.min.js\"></script>";
echo "<script src=\"blueimp-jquery-image-gallery/jquery.blueimp-gallery.min.js\"></script>";
echo "<script src=\"blueimp-jquery-image-gallery/jquery.image-gallery.min.js\"></script>";
}
?>