-
Notifications
You must be signed in to change notification settings - Fork 1
/
gallery.php
341 lines (270 loc) · 14.3 KB
/
gallery.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
error_reporting(E_ERROR | E_PARSE);
// version control and notification 1/2
$version = 1.10;
$version_url_check = "https://raw.githubusercontent.com/pascalbrax/psg/master/latest_version";
$version_update = "https://github.com/pascalbrax/psg";
// this is where we catch variables sent to this page.
$dir = filter_var($_REQUEST['dir'],FILTER_SANITIZE_STRING);
$thumb = filter_var($_REQUEST['thumb'],FILTER_SANITIZE_STRING);
// fix stuff & block directory traversal
if (strstr($dir,"..") OR ($dir == "/")) {
$dir = "";
}
if (strstr($thumb,"..") OR ($thumb == "/")) {
unset ($thumb);
}
// autodiscover some stuff...
$thisfilelocation = $_SERVER['PHP_SELF']; // result: /dir/thisfile.php
$thisfilename = pathinfo($thisfilelocation)['basename']; // result: thisfile.php
$thisfilepath = str_replace($thisfilename, "",$thisfilelocation); // result: /dir/
$workdir = getcwd(); // get current working dir with no trailing '/'
$webdir = $thisfilepath;
// video support
$video = false;
// check if cache folder is available
$cachefolder = "cache"; // folder name where cache images thumbnails
$cache = false;
if (file_exists($cachefolder)) {
// cache folder is available
if (is_writable($cachefolder)) {
// cache folder is writable
$cache = true;
}
}
// enable psimplebox (lightbox clone)
if (file_exists("psimplebox.js")) {
// add jquery script src and simplebox to html
$htmlscripts = "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js\"></script>\n
<script src=\"".$thisfilepath."psimplebox.js\"></script>\n";
}
if ($thumb) {
// if we need a thumbnail
// generate headers
$expire=60*60*24*1; // seconds, minutes, hours, days
header('Pragma: public');
header('Cache-Control: maxage='.$expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' CET');
header("Content-Type: image/jpeg");
// generate image file path
$imagefilename = $workdir.$dir."/".$thumb;
// check if there is a cache folder
if ($cache) {
// check if the image thumb is available in cache
$cachedfilename = $cachefolder."/thumb".str_replace("/","+",$dir)."+".$thumb;
if (file_exists($cachedfilename)) {
// read thumbnail from cache
readfile($cachedfilename);
} else {
// cache available, but file not exist yet so we create it
imagejpeg(generate_thumb($imagefilename),$cachedfilename);
readfile($cachedfilename);
}
} else {
// cache not available, generate_thumb on the fly
imagejpeg(generate_thumb($imagefilename));
}
exit();
}
// start webpage
$htmlstart = "
<!DOCTYPE html>
<html>
<head>
<!--
If you like this gallery script, you can download it here: $version_update
-->
<title>pSimpleGallery $dir</title>
<style>
body {
background-color: #666699;
}
div.nav {
color:black;font-family:Trebuchet MS,Tahoma,Helvetica,Verdana,Arial;font-size:medium;background-color:#a3a3c2;margin-bottom:10px;
}
div.nav:hover {
color:black;font-family:Trebuchet MS,Tahoma,Helvetica,Verdana,Arial;font-size:medium;background-color:#c2c2d6;margin-bottom:10px;
}
div.dir {
color:black;font-family:Trebuchet MS,Tahoma,Helvetica,Verdana,Arial;font-size:medium;background-color:#a3a3c2;float:left;width:200px;height:180px;text-align:center;margin-bottom:15px; margin-right:10px;
}
div.dir:hover {
color:black;font-family:Trebuchet MS,Tahoma,Helvetica,Verdana,Arial;font-size:medium;background-color:#c2c2d6;float:left;width:200px;height:180px;text-align:center;margin-bottom:15px; margin-right:10px;
}
div.file {
color:black;font-family:Trebuchet MS,Tahoma,Helvetica,Verdana,Arial;font-size:small;background-color:#a3a3c2;float:left;width:200px;height:180px;text-align:center;margin-bottom:15px; margin-right:10px;
}
div.file:hover {
color:black;font-family:Trebuchet MS,Tahoma,Helvetica,Verdana,Arial;font-size:small;background-color:#c2c2d6;float:left;width:200px;height:180px;text-align:center;margin-bottom:15px; margin-right:10px;
}
a:link {color:black;text-decoration:none;}
a:visited {color:black;text-decoration:none;}
a:hover {color:blue;text-decoration:none;}
a:active {color:black;text-decoration:none;}
</style>
$htmlscripts
</head>
<body>";
print $htmlstart;
// version control and notification 2/2
if ($version_online = file_get_contents($version_url_check)) {
if ($version_online > $version) {
echo "<div style='font-family:Trebuchet MS,Tahoma,Helvetica,Verdana,Arial'>
<a href='$version_update'>New version available.</a>
</div>
<br />";
}
}
// echo "<pre> version online: $version_online local version: $version</pre>"; // debug version
// This merge together the base directory and the user's requested folder.
$fulldir = $workdir.$dir;
// path tree
if ($handle = opendir($fulldir)) {
if ($dir) {
$i = 1;
$dirarray = explode("/",$dir);
print "<div class='nav'>";
foreach ($dirarray as &$folder) {
if (!$folder) { $folder = "root"; } // human name to root
print '<a href="?dir=';
for($s = 0; $s < $i; $s++) {
if ($s) { print $dirarray[$s]; }
if ($dirarray[$s] != $folder) { print "/"; } // add '/' in the right places for the GET variables
}
print '">';
print '<img width="12" height="12" alt="'.$folder.'" src="'; // start img tag
print add_icon("nav"); // insert image data
print '" /> '; // close img tag (and add a space)
print "$folder";
print "</a>";
$i++;
}
print "</div>";
}
else {
print '<div class="nav"><img alt="root" width="12" height="12" src="'.add_icon("nav").'" /> root</div>';
}
// directory list with scandir
$dir_path = $fulldir."/";
$exclude_list = array(".", "..",$cachefolder);
$directories = array_diff(scandir($dir_path), $exclude_list);
foreach($directories as $entry) {
if(is_dir($dir_path.$entry)) {
print '<div class="dir">';
print '<a href="'.$thisfilename.'?dir='.urlencode($dir."/".$entry).'">';
print "<img alt='$entry' src='".add_icon("dir")."'><br>";
print substr($entry,0,18);
print '</a>';
print "</div>\n";
}
}
// image list
print "<div id=\"imageSet\">";
foreach($directories as $entry) {
if(is_file($dir_path.$entry) AND ((strpos(strtolower($entry),".jpg") OR strpos(strtolower($entry),".jpeg") OR strpos(strtolower($entry),".png")) AND !strpos(strtolower($entry),".filepart"))) {
print '<div class="file">';
print "<a href='".get_file($dir_path.$entry)['link']."' class='simplebox'>";
print "<img alt='$entry' src='".$thisfilename."?dir=$dir&thumb=$entry"."'><br>";
print substr($entry,0,18); // filename up to 18 chars
print " (".human_filesize(get_file($dir_path.$entry)['size']).")</a>";
print "</div>\n";
}
if(is_file($dir_path.$entry) AND (strpos(strtolower($entry),".mp4") AND !strpos(strtolower($entry),".filepart")) AND $video) {
print '<div class="file">';
print " <video width='200' height='160'>
<source src='".get_file($dir_path.$entry)['link']."' type='video/mp4'>
No video support
</video>";
print "<a href='".get_file($dir_path.$entry)['link']."' class='simpleboxvideo'>";
print substr($entry,0,15); // filename up to 15 chars
print " (".human_filesize(get_file($dir_path.$entry)['size']).")</a>";
print "</div>\n";
}
}
print "</div>";
}
$bodyend = "</body></html>";
print $bodyend;
function get_file($entry) {
global $workdir, $webdir, $dir;
if (file_exists($entry)) {
$name = pathinfo($entry)['basename'];
$folder = pathinfo($entry)['dirname'];
$fixed_dir = substr($dir,1)."/"; // move '/' from start to end
$link = $webdir.$fixed_dir.$name;
$size = filesize($entry);
$updated = date ("d/m/Y", filemtime($dir_path.$entry));
return compact('name','folder','link','size','updated');
}
else {
return false;
}
}
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
function generate_thumb($filename) {
// Set a maximum height and width
$width = 200;
$height = 160;
// read EXIF data
$exif = exif_read_data($filename);
// crea $image dal file
if (strpos(strtolower($filename),".png")) {
$image = imagecreatefrompng($filename); // PNG
} else {
$image = imagecreatefromjpeg($filename); // JPG
}
// rotate image if needed
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
}
// Get new dimensions
$width_orig = imagesx($image);
$height_orig = imagesy($image);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
}
else {
$height = $width/$ratio_orig;
}
// create empty image
$image_p = imagecreatetruecolor($width, $height);
// fill empty image with cropped original
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// destroy old image
imagedestroy($image);
// Output
return $image_p;
}
function add_icon($type) {
if ($type == "nav") {
$icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAHBJREFUeNqU0TEKwCAUA9D2Sp5AXUQQ3cSbewgHN8Ep8tdqaRr44+NDcgO4fkXA87TWcM6BBtZajDHO6A1IBIUQQANJaw0xRtBgQwyQ1FqhlAIFeu/w3nMftra+WkopcS3NOZFz5nYwxqCUclx6CTAAwWgxaW7qSDsAAAAASUVORK5CYII%3D";
}
if ($type == "dir") {
$icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAACWBA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Ag8QZOAA93CBNWrpcAYmWhpYLUKRJwQGooQYpSAGEQMHeXa+9MxRGCDsUvGak2f1mxgBM5kvlj5wf7rK1mZu1h/FsoY+vXkVVYyu6fK91PFvoZL7Ul7fQG5YBhBAZ2YwkigGMQrnyTA62Ob15onTND7IAPgQEpU7SfY6J1Yfj4blkr4Djyzv6wNXYjZy6EcAQRQgucHG7AANmRB9UlcO3qbvPGLLCcHayTytCm5Tp9f0QeFwUbAaKIc8MOyMLQGiEqvJDEAiNkGVd0wD6XauYKKvQB845XGyx+bpwdUtVugHwNT4Jhf6e3cU3Ce+GG3x3UrIQG6H08dsoRW4Jvu6D8tNxNL3iP/kC9mepySQNpKwAAAAASUVORK5CYII%3D";
}
if ($type == "empty") {
$icon = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
}
return $icon;
}
/* pascal brax 2014-2021 */
?>