-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
462 lines (428 loc) · 14.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
# We have four modes:
# - gallery list
# - gallery page
# - photo
# - comment
# sub modes - login, logout
# There's also stuff to do with a login cookie.
# We'll set that on the first opportunity and
# then keep it forever... (until signout, anyway)
# We also need some javascript for showing and
# hiding the comment box, etc.
# URL scheme:
# ?a=<album> - album view
# &pg=<page> - page view
# &p=<photo> - photo view
# Directory structure:
# a_<alb>/<photo>.jpg = full-sized
# a_<alb>/NAME = metadata
# a_<alb>/COVER = name of cover photo (thumbnail)
# a_<alb>/m/m_<photo>.jpg = mid-sized
# a_<alb>/t/t_<photo>.jpg = thumbnail
# c/<alb>_<photo>.comment = comments
# c/<alb>_<photo>.desc = photo description (same fmt as comments, uses 1st)
ini_set("magic_quotes_gpc", "off");
ini_set("magic_quotes_sybase", "off");
$GLOBALS['PERPAGE'] = 50;
$GLOBALS['PERROW'] = 5;
$GLOBALS['FACEBOOK_ENABLED'] = false;
# Change this to true to enable OWNER_EMAIL to write descriptions
$GLOBALS['WRITE_DESCRIPTION'] = false;
# These should be set in config.php
$GLOBALS['OWNER_EMAIL'] = ''; # note: replace @ with %40
$GLOBALS['FB_API_KEY'] = '';
$GLOBALS['FB_SECRET'] = '';
if (file_exists('config.php')) {
require_once 'config.php';
}
if ($GLOBALS['FACEBOOK_ENABLED'] && file_exists('facebook_api')) {
require_once 'facebook_api/facebook_php5_photoslib.php';
require_once 'facebook_inc.php'; # sequester all the facebook-specific code
} else {
$GLOBALS['FACEBOOK_ENABLED'] = false;
}
function printHeader($title, $class) {
print "<html><head><title>$title</title>\n"
. "<link rel='stylesheet' type='text/css' href='photo.css'/>\n"
. "<script type='text/javascript' src='photo.js'></script>\n"
. "</head><body class='$class'><div class='page'>\n";
}
function listAlbums() {
printHeader("Albums", "gallery");
print "<h1>Albums</h1><div class='main'>\n"
. "<div class='links top'>"
. "<a href='?activity=1'>Recent Activity</a></div>\n";
foreach (getAlbumList() as $album => $info) {
$cover = $info['cover'];
$desc = $info['desc'];
print "<div class='row'><a href='?a=$album'><div "
. "class='album-cover'><img src='$cover'/></div><div "
. "class='album-title'>$desc</div></a></div>";
}
print "<div style='clear:both'></div>\n";
print "</div><div class='copyright'>Photo gallery software © "
. "2010 Stephen Hicks. All rights reserved.</div></div></body></html>";
}
function getAlbumList() {
$dir = opendir(".");
$gal = array();
while ($f = readdir($dir)) {
if (strpos($f, "a_") === 0 and is_dir($f)) {
$album = substr($f, 2);
$description = getAlbumDescription($album);
$cover = file_get_contents("$f/COVER");
$thumb = "$f/" . thumbnail($cover);
$gal[$album] = array("desc" => $description, "cover" => $thumb);
}
}
krsort($gal);
return $gal;
}
function getAlbumDescription($album) {
return file_get_contents("a_$album/NAME");
}
function getPhotoDescription($album, $photo) {
$descFile = "c/${album}_$photo.desc";
if (!file_exists($descFile)) {
return "";
}
$lines = file($descFile);
$line = $lines[0];
$parts = explode("&", trim($line));
if ($parts[0] != "comment") {
return "";
}
return str_replace("\n", "<br/>", urldecode($parts[5]));
}
function getPhotoList($album) {
$dir = opendir("a_$album");
$photos = array();
while ($f = readdir($dir)) {
if (strpos($f, ".jpg") !== false || strpos($f, ".mp4") !== false) {
array_push($photos, $f);
}
}
sort($photos);
return $photos;
}
function getCommentsList() {
$gal = getAlbumList();
$dir = opendir("c");
$activity = array();
while ($f = readdir($dir)) {
$ext = strpos($f, ".comment");
if ($ext !== false) {
$lines = file("c/$f");
$f = substr($f, 0, $ext);
$underscore = strpos($f, "_");
if ($underscore === false) {
$type = "an <a href='?a=$f'>album</a>";
} else {
$alb = substr($f, 0, $underscore);
$desc = $gal[$alb]['desc'];
$photo = substr($f, $underscore + 1);
if (strpos($photo, ".mp4") !== false) {
$type = "a <a href='?a=$alb&p=$photo'>video</a> in \"$desc\"";
} else {
$type = "a <a href='?a=$alb&p=$photo'>photo</a> in \"$desc\"";
}
}
foreach ($lines as $line) {
$parts = explode("&", trim($line));
$name = urldecode($parts[1]);
$time = $parts[4];
$year = $time < time() - 320 * 86400 ? ", Y" : "";
$date = date("F j$year \\a\\t g:ia \\P\\T", $time); # use JS too...?
$datetext = "<span class='date'>$date</span>";
if ($parts[0] == 'like') {
$activity[$time] = "$name likes $type $datetext";
} else {
$activity[$time] = "$name commented on $type $datetext";
}
}
}
}
krsort($activity);
return $activity;
}
function listPhotos() {
global $PERPAGE, $PERROW;
$alb = $_REQUEST['a'];
$page = $_REQUEST['pg'];
$description = getAlbumDescription($alb);
$photos = getPhotoList($alb);
$total = count($photos);
$pages = floor(($total - 1) / $PERPAGE) + 1;
printHeader("Album: $description", "album");
print "<input type='hidden' id='info' value='${alb}'>\n";
print "<h1>$description</h1><div class='main'><div class='links top'>\n";
paginate("a=$alb", $page, $pages);
print "<a href='?'>All Albums</a>";
if (file_exists("zip/$alb.zip")) {
print " - <a href='zip/$alb.zip'>Download Album</a>";
}
if ($GLOBALS['FACEBOOK_ENABLED']) {
print " - <a href='?fb=$alb'>Upload to Facebook</a>";
}
print "</div><div style='clear:both'></div>\n";
for ($i = $page * $PERPAGE;
$i < ($page + 1) * $PERPAGE and $i < $total;
$i++) {
if ($i % $PERROW == 0) {
print "<div class='row'>\n";
}
$photo = $photos[$i];
$thumbnail = thumbnail($photo);
print "<div class='item'>"
. "<a href='?a=$alb&p=$photo'><img class='thumbnail' "
. "src='a_$alb/$thumbnail'/></a></div>\n";
if ($i % $PERROW == $PERROW - 1) {
print "</div>\n";
}
}
print "<div class='links bottom'>";
paginate("a=$alb", $page, $pages);
print "</div>\n";
showComments($alb);
print "</div>\n";
print "</div></div><div class='copyright'>Photo gallery software © "
. "2010 Stephen Hicks. All rights reserved.</div></body></html>";
}
function paginate($base, $page, $pages) {
if ($pages > 1) {
print "<div class='right'>";
if ($page != 0) {
$p = $page - 1;
print "<a href='?$base&pg=$p'>Previous</a> ";
}
for ($p = 0; $p < $pages; $p++) {
$pp = $p + 1;
if ($p == $page) {
print "<b>$pp</b> ";
} else {
print "<a href='?$base&pg=$p'>$pp</a> ";
}
}
if ($page < $pages - 1) {
$p = $page + 1;
print "<a href='?$base&pg=$p'>Next</a>";
}
print "</div>\n";
}
}
function thumbnail($photo) {
return "t/t_" . str_replace(".mp4", ".jpg", $photo);
}
function embed($album, $photo, $next) {
if (strpos($photo, ".jpg") !== false) { # picture
print "<div class='img'><a href='?a=$album&p=$next'>"
. "<img src='a_$album/m/m_$photo'></a></div>\n";
} else if (strpos($photo, ".mp4") !== false) { # video
$flv = str_replace(".mp4", ".flv", $photo);
print "<div class='flv'>";
embedVideo("a_$album/m/m_$flv");
print "</div>\n";
}
}
function showPhoto() {
$alb = $_REQUEST['a'];
$photo = $_REQUEST['p'];
$description = getAlbumDescription($alb);
$photos = getPhotoList($alb);
$ind = array_search($photo, $photos);
if ($ind === false) {
printError("not found.");
return;
}
printHeader("${alb}_$photo", "photo");
print "<input type='hidden' id='info' value='${alb}_$photo'>\n";
print "<h1><a href='?a=$alb'>$description</a></h1><div class='main'>"
. "<div class='links'>\n";
$total = count($photos);
$prev = $photos[($ind + $total - 1) % $total];
$next = $photos[($ind + 1) % $total];
print "<div class='right'><a href='?a=$alb&p=$prev'>Previous</a>"
. " <a href='?a=$alb&p=$next'>Next</a></div>";
$oneind = $ind + 1;
$page = floor($ind / $GLOBALS['PERPAGE']);
print "Photo $oneind of $total  "
. "<a href='a_$alb/$photo'>Full Size</a> - "
. "<a href='?a=$alb&pg=$page'>Back to Album</a> - "
. "<a href='?'>All Albums</a></div>\n"; # </photo-links>
embed($alb, $photo, $next);
$desc = getPhotoDescription($alb, $photo);
if ($desc) {
print "<div class='description'>$desc</div>\n";
}
# load comments
showComments("${alb}_$photo");
print "</div></div><div class='copyright'>Photo gallery software © "
. "2010 Stephen Hicks. All rights reserved.</div></body></html>";
}
function showComments($id) {
$commentsFile = "c/$id.comment";
$lines = file_exists($commentsFile) ? file($commentsFile) : array();
$likes = array();
$comments = array();
foreach ($lines as $line) {
$parts = explode("&", trim($line));
$name = urldecode($parts[1]);
# $email = $parts[2]; # also escape \n as &lb;, then replace w/ <br/>
$href = urldecode($parts[3]);
if ($href) {
if (strpos($href, "http://") !== 0) {
$href = "http://$href";
}
$name = "<a href='$href'>$name</a>";
}
if ($parts[0] == 'like') {
array_push($likes, $name);
} else if ($parts[0] == 'comment') {
$time = $parts[4];
$year = $time < time() - 320 * 86400 ? ", Y" : "";
$date = date("F j$year \\a\\t g:ia", $time); # use JS too...?
$text = str_replace("\n", "<br/>", urldecode($parts[5]));
$comment = "<div class='wrapper'><div class='comment'>\n"
. "<div class='person'>$name</div> "
. "<div class='text'>$text</div><div class='date'>$date"
. "</div></div></div>\n";
array_push($comments, $comment);
} else {
# weird...?
}
}
print "<div class='comments'><div class='comments-links'>"
. "<a id='a-comment' href='javascript:;'>Comment</a> - "
. "<a id='a-like' href='javascript:;'>Like</a><span id='signout' "
. "style='display:none'> - <a id='a-signout' href='javascript:;'"
. ">Signout</a></span></div>\n";
$likers = count($likes);
if ($likers > 0) {
print "<div class='wrapper'><div class='likes'>";
if ($likers == 1) {
print "<div class='person'>$likes[0]</div> likes this.";
} else if ($likers == 2) {
print "<div class='person'>$likes[0]</div> and "
. "<div class='person'>$likes[1]</div> like this.";
} else {
print "<div class='person'>$likes[0]</div>";
for ($i = 1; $i < $likers - 1; $i++) {
print ", <div class='person'>$likes[$i]</div>";
}
$last = $likes[$likers - 1];
print " and <div class='person'>$last</div> like this.";
}
print "</div></div>\n";
}
print "<div class='comments-comments'>\n";
foreach ($comments as $comment) {
print "$comment\n";
}
?><div class='wrapper'><div class='comments-write'>
<div id='login' style='display:none' class='login'>
Name: <input id='name'/>
Email: <input id='email'/>
Website (optional): <input id='website'/>
<input type='submit' id='submit-like' value='Like' style='display:none'></div>
<textarea
id='comment-text'>Write a comment...</textarea><input id='submit-comment'
style='display:none' type='submit' value='Comment'/>
<div style='clear:both'></div>
</div></div></div><?php
}
# We hardcode the size here, too...
function embedVideo($video) {
$base = "http://" . $_SERVER['HTTP_HOST']
. preg_replace("|/[^/]*$|", "", $_SERVER['PHP_SELF']);
$file = "$base/$video";
$still = str_replace(".flv", ".jpg", $file);
$player = "$base/mediaplayer.swf";
print "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' "
. "codebase='http://download.macromedia.com/pub/shockwave/cabs"
. "/flash/swflash.cab#version=6,0,40,0' "
. "width='640' height='380'><param name='flashvars' value='file=$file"
. "&image=$still'/>"
. "<param name='movie' value='$player'/><embed src='$player' width='640' "
. "height='380' type='application/x-shockwave-flash' "
. "pluginspage='http://www.macromedia.com/go/getflashplayer' "
. "flashvars='file=$file&image=$still'/></object>";
}
function printError($message) {
print "<html><head><title>Error</title></head>"
. "<body>Error: $message</body></html>";
}
function sanitize($s) {
return urlencode(htmlspecialchars(urldecode($s), ENT_QUOTES));
}
function sanitizeUrl($s) {
return urlencode(str_replace(array("'", "<", ">"), array("", "", ""),
urldecode($s)));
}
function unslash($s) {
return str_replace("\\'", "'", $s);
}
# Sanitize inputs BEFORE saving to file; trust file not monkeyed with...
function appendComment($type, $text) {
$cookie = unslash($_COOKIE['logged']); # cf. magic_quotes_gpc (_sybase)
$parts = explode("&", $cookie);
$name = sanitize($parts[0]);
$email = sanitize($parts[1]);
$website = sanitizeUrl($parts[2]);
$date = time();
$text = sanitize(unslash($text));
$fname = $_REQUEST[$type];
if ($name and $email and ($type=='like' or $text)) {
if ($GLOBALS['WRITE_DESCRIPTION'] && $email==$GLOBALS['OWNER_EMAIL']) {
$f = fopen("c/$_REQUEST[$type].desc", 'w');
} else {
$f = fopen("c/$_REQUEST[$type].comment", 'a');
}
fwrite($f, "$type&$name&$email&$website&$date&$text\n");
fclose($f);
print "OK"; # : <$cookie> <$text>";
} else {
print "error: <$cookie> <$text>";
}
}
function showActivity() {
global $PERPAGE;
$page = $_REQUEST['pg'];
$activity = getCommentsList();
$keys = array_keys($activity);
$total = count($keys);
$pages = floor(($total - 1) / $PERPAGE) + 1;
printHeader("Recent Activity", "activity");
print "<h1>Recent Activity</h1><div class='main'>"
. "<div class='links top'>\n";
paginate("activity=1", $page, $pages);
//var_dump($activity);
print "<a href='?'>All Albums</a></div><div style='clear:both'></div>\n";
for ($i = $page * $PERPAGE;
$i < ($page + 1) * $PERPAGE and $i < $total;
$i++) {
$key = $keys[$i];
$item = $activity[$key];
print "<div class='item'>$item</div>\n";
}
print "<div class='links bottom'>";
paginate("activity=1", $page, $pages);
print "</div>\n";
print "</div></div><div class='copyright'>Photo gallery software © "
. "2010 Stephen Hicks. All rights reserved.</div></body></html>";
}
if ($GLOBALS['FACEBOOK_ENABLED'] && $_REQUEST['fb']) {
doFacebook();
} else if ($_REQUEST['like']) {
appendComment("like", "");
} else if ($_REQUEST['comment']) {
appendComment("comment", $_REQUEST['text']);
} else if ($_REQUEST['activity']) {
showActivity();
} else if ($_REQUEST['p']) {
showPhoto();
} else if ($_REQUEST['a']) {
listPhotos();
} else {
listAlbums();
}