This repository has been archived by the owner on Jul 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
323 lines (266 loc) · 10.7 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
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
<?php // UberGallery by, Chris Kankiewicz (http://www.ubergallery.net)
// Customize your gallery by changing the following variables. If a variable
// is contained within quotes make sure you don't delete the quotes.
$galleryTitle = "Gallery Title"; // Gallery title
$galleryDir = "ubergallery/images"; // Original images directory (No trailing slash!)
$thumbsDir = "ubergallery/cache"; // Thumbnails directory (No trailing slash!)
$logFile = "ubergallery/log.txt"; // Directory/Name of log file
$thumbSize = 100; // Thumbnail width/height in pixels
$imgPerPage = 0; // Images per page (0 disables pagination)
$cacheExpire = 0; // Frequency (in minutes) of cache refresh
$formatted = true; // Set to false to output bare (no styles) page
$verCheck = false; // Set to true to enable update notifications
// *** DO NOT EDIT ANYTHING BELOW HERE UNLESS YOU ARE A PHP NINJA ***
$version = "1.6.2"; // Gallery version
if ($_GET['page']) {
// Sanitize input and set current page
$currentPage = (integer) $_GET['page'];
} else {
$currentPage = 1;
}
// Create log file if it does not exist, otherwise open log for writing
if (!file_exists($logFile)) {
$log = fopen($logFile, "a");
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $logFile\r\n\r\n");
} else {
$log = fopen($logFile, "a");
}
// Create image directory if it doesn't exist
if (!file_exists($galleryDir)) {
mkdir($galleryDir);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $galleryDir\r\n");
}
// Create thumbnail directory if it doesn't exist
if (!file_exists($thumbsDir)) {
mkdir($thumbsDir);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsDir\r\n");
}
// Clean up thumbnail directory
if ($dirHandle = opendir($thumbsDir)) {
while (($file = readdir($dirHandle)) !== false) {
if (isImage("$thumbsDir/$file")) {
$size = getimagesize("$thumbsDir/$file");
if (!file_exists("$galleryDir/$file") || $size[0] !== $thumbSize) {
unlink("$thumbsDir/$file");
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." REMOVED: $thumbsDir/$file\r\n");
}
}
}
closedir($dirHandle);
}
// Alcohol! The cause of, and solution to, all of life's problems!
// Create array from gallery directory
if ($dirHandle = opendir($galleryDir)) {
while (($file = readdir($dirHandle)) !== false) {
if (isImage("$galleryDir/$file")) {
$images[] = $file;
}
}
closedir($dirHandle);
}
// Page varriables
$totalImages = count($images);
if ($imgPerPage <= 0 || $imgPerPage >= $totalImages) {
$imgStart = 0;
$imgEnd = $totalImages;
$totalPages = 1;
} elseif ($imgPerPage > 0 && $imgPerPage < $totalImages) {
$totalPages = ceil($totalImages / $imgPerPage);
if ($_GET['page'] < 1) {
$currentPage = 1;
} elseif ($_GET['page'] > $totalPages) {
$currentPage = $totalPages;
} else {
$currentPage = (integer) $_GET['page'];
}
$imgStart = ($currentPage - 1) * $imgPerPage;
$currentPage * $imgPerPage > $totalImages ? $imgEnd = $totalImages : $imgEnd = $currentPage * $imgPerPage;
}
?>
<?php if($formatted): ?>
<?php substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') ? ob_start("ob_gzhandler") && $gzip = 1 : ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $galleryTitle; ?> | Powered by, UberGallery</title>
<link rel="shortcut icon" href="ubergallery/resources/images/images.png" />
<link rel="stylesheet" type="text/css" href="ubergallery/resources/css/ubergallery.css" />
<link rel="stylesheet" type="text/css" href="ubergallery/resources/css/colorbox.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="ubergallery/resources/js/jquery.colorbox.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a[rel='colorbox']").colorbox({maxWidth: "90%", maxHeight: "90%", opacity: ".5"});
<?php if($_GET['img']): ?>
$('#img-<?php echo (integer) $_GET['img']; ?>').click();
<?php endif; ?>
});
</script>
</head>
<?php flush() ?>
<body>
<?php endif; ?>
<?php
// *** START PAGE CACHING ***
// Create cache directory if it doesn't exist
$cacheDir = "ubergallery/cache";
if (!file_exists($cacheDir) && $cacheExpire > 0) {
mkdir($cacheDir);
}
$cacheFile = "$cacheDir/page$currentPage-cached.html";
$cacheTime = $cacheExpire * 60;
// Serve from the cache if it is younger than $cacheTime
if (file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile) && $cacheExpire > 0) {
include($cacheFile);
echo "<!-- Cached page: created ".date('H:i:s', filemtime($cacheFile))." / expires ".date('H:i:s', (filemtime($cacheFile)) + $cacheTime)." -->\n";
} else {
ob_start();
// Opening markup
echo("<!-- Start UberGallery v$version - Created by, Chris Kankiewicz <http://www.ChrisKankiewicz.com> -->\r\n");
echo("<div id=\"gallery-wrapper\">\r\n");
echo(" <div id=\"gallery-constraint\">\r\n");
echo(" <div id=\"gallery-header\">$galleryTitle</div>\r\n");
echo(" <div id=\"gallery-images\" class=\"clearfix\">\r\n");
for ($x = $imgStart; $x < $imgEnd; $x++) {
$filePath = "$galleryDir/$images[$x]";
// Convert file name and extension for processing
if (ctype_upper(pathinfo($filePath,PATHINFO_EXTENSION))
|| strpos(basename($filePath),' ') !== false
|| strpos($filePath,'.jpeg') !== false) {
$source = "$filePath";
$fileParts = pathinfo($filePath); // Create array of file parts
$ext = $fileParts['extension']; // Original extension
$name = basename($filePath, ".$ext"); // Original file name without extension
$dir = $fileParts['dirname']; // Directory path
// Change extension to all lowercase
if (ctype_upper($ext)) {
$ext = strtolower($ext);
}
// Convert .jpeg to .jpg
if ($ext == 'jpeg') {
$ext = 'jpg';
}
// Replace spaces with underscores
if (strpos($name,' ') !== false) {
$extOld = $fileParts['extension'];
$name = str_replace(' ','_',basename($filePath, ".$extOld"));
}
$destination = "$dir/$name.$ext";
// Rename file and array element
if (rename($source,"$dir/$name.tmp")) {
if (rename("$dir/$name.tmp",$destination)) {
$images[$x] = "$name.$ext";
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." RENAMED: $source to $destination\r\n");
}
}
}
$filePath = "$galleryDir/$images[$x]";
$thumbPath = "$thumbsDir/$images[$x]";
// Create thumbnail if it doesn't already exist
if (!file_exists("$thumbPath")) {
createThumb("$filePath","$thumbPath",$thumbSize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsDir/$images[$x]\r\n");
}
// Create XHTML compliant markup
$noExt = substr($images[$x],0,strrpos($images[$x],'.'));
$altText = str_replace("_"," ",$noExt);
echo " <a href=\"$filePath\" title=\"$altText\" id=\"img-$x\" rel=\"colorbox\"><img src=\"$thumbPath\" alt=\"$altText\"/></a>\r\n";
}
echo(" </div>\r\n");
// Version check and notification
if ($verCheck) {
$verInfo = @file("http://www.ubergallery.net/version-check.php?ver=$version");
$verInfo = @implode($verInfo);
if ($verInfo == "upgrade") {
echo(" <div id=\"uber-notice\" class=\"clearfix\">A new version of UberGallery is availabe. <a href=\"http://www.ubergallery.net\" target=\"_blank\">Get the latest version here</a>.</div>");
} elseif ($verInfo == "development") {
echo(" <div id=\"uber-notice\" class=\"clearfix\">This is a development version of UberGallery.</div>\r\n");
}
}
echo(" <div id=\"uber-footer\" class=\"clearfix\">\r\n");
// If pagination enabled, create page navigation
if ($imgPerPage > 0 && $imgPerPage < $totalImages) {
$pageName = basename($_SERVER["PHP_SELF"]); // Get current page file name
echo(" <ul id=\"uber-pagination\" style=\"margin: 0 !important; padding: 0 !important;\">\r\n");
// Pageination title
echo(" <li class=\"title\">Page $currentPage of $totalPages</li>\r\n");
// Previous arrow
$previousPage = $currentPage - 1;
echo(" <li".($currentPage > 1 ? "><a href=\"$pageName?page=$previousPage\" title=\"Previous Page\"><</a>" : " class=\"inactive\"><")."</li>\r\n");
// Page links
for ($x = 1; $x <= $totalPages; $x++) {
echo(" <li".($x == $currentPage ? " class=\"current-page\">$x" : "><a href=\"$pageName?page=$x\" title=\"Page $x\">$x</a>")."</li>\r\n");
}
// Next arrow
$nextPage = $currentPage + 1;
echo(" <li".($currentPage < $totalPages ? "><a href=\"$pageName?page=$nextPage\" title=\"Next Page\">></a>" : " class=\"inactive\">>")."</li>\r\n");
echo(" </ul>\r\n");
}
// Closing markup
echo(" <div id=\"credit\">Powered by, <a href=\"http://www.ubergallery.net\">UberGallery</a></div>\r\n");
echo(" </div>\r\n");
echo(" </div>\r\n");
echo("</div>\r\n");
echo("<!-- Page $currentPage of $totalPages -->\r\n");
echo("<!-- End UberGallery - Licensed under the MIT License <http://creativecommons.org/licenses/MIT/> -->\r\n");
fclose($log); // Close log
if ($cacheExpire > 0) {
// Cache the output to a file
$fp = fopen($cacheFile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
}
}
?>
<?php if($formatted): ?>
<?php if ($gzip == "1") echo("<!-- Page served with gzip compression -->\r\n"); ?>
</body>
</html>
<?php endif; ?>
<?php
// *** START FUNCTIONS ***
function createThumb($source,$dest,$thumb_size) {
// Create thumbnail, modified from function found on http://www.findmotive.com/tag/php/
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
if ($width > $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height > $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
@$imgInfo = getimagesize($source);
if ($imgInfo[2] == IMAGETYPE_JPEG) {
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagejpeg($new_im,$dest,75); // Thumbnail quality (Value from 1 to 100)
} elseif ($imgInfo[2] == IMAGETYPE_GIF) {
$im = imagecreatefromgif($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagegif($new_im,$dest);
} elseif ($imgInfo[2] == IMAGETYPE_PNG) {
$im = imagecreatefrompng($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagepng($new_im,$dest);
}
}
function isImage($fileName) {
// Verifies that a file is an image
if ($fileName !== '.' && $fileName !== '..') {
@$imgInfo = getimagesize($fileName);
$imgType = array(
IMAGETYPE_JPEG,
IMAGETYPE_GIF,
IMAGETYPE_PNG,
);
if (in_array($imgInfo[2],$imgType))
return true;
return false;
}
}
// EOF
?>