Support caching of the index pages. (Cache invalidation coming soon!)
[pr0n] / perl / Sesse / pr0n / Index.pm
1 package Sesse::pr0n::Index;
2 use strict;
3 use warnings;
4
5 use Sesse::pr0n::Common qw(error dberror);
6 use Apache2::Request;
7 use POSIX;
8
9 sub handler {
10         my $r = shift;
11         my $apr = Apache2::Request->new($r);
12         my $dbh = Sesse::pr0n::Common::get_dbh();
13
14         # Find the event
15         $r->uri =~ m#^/([a-zA-Z0-9-]+)/?$#
16                 or error($r, "Could not extract event");
17         my $event = $1;
18
19         # Fix common error: pr0n.sesse.net/event -> pr0n.sesse.net/event/
20         if ($r->uri !~ m#/$#) {
21                 $r->headers_out->{'location'} = "/$event/";
22                 return Apache2::Const::REDIRECT;
23         }
24
25         # Internal? (Ugly?) 
26         if ($r->get_server_name =~ /internal/ || $r->get_server_name =~ /skoyen\.bilder\.knatten\.com/) {
27                 my $user = Sesse::pr0n::Common::check_access($r);
28                 if (!defined($user)) {
29                         return Apache2::Const::OK;
30                 }
31         }
32
33         # Read the appropriate settings from the query string into the settings hash
34         my %defsettings = (
35                 thumbxres => 80,
36                 thumbyres => 64,
37                 xres => undef,
38                 yres => undef,
39                 start => 1,
40                 num => -1,
41                 all => 1,
42                 infobox => 1,
43                 rot => 0,
44                 sel => 0,
45                 fullscreen => 0,
46         );
47         
48         # Reduce the front page load when in overload mode.
49         if (Sesse::pr0n::Overload::is_in_overload($r)) {
50                 $defsettings{'num'} = 100;
51         }
52                 
53         my %settings = %defsettings;
54
55         for my $s qw(thumbxres thumbyres xres yres start num all infobox rot sel fullscreen) {
56                 my $val = $apr->param($s);
57                 if (defined($val) && $val =~ /^(\d+)$/) {
58                         $settings{$s} = $val;
59                 }
60                 if (($s eq "num" || $s eq "xres" || $s eq "yres") && defined($val) && $val == -1) {
61                         $settings{$s} = $val;
62                 }
63         }
64
65         my $thumbxres = $settings{'thumbxres'};
66         my $thumbyres = $settings{'thumbyres'};
67         my $xres = $settings{'xres'};
68         my $yres = $settings{'yres'};
69         my $start = $settings{'start'};
70         my $num = $settings{'num'};
71         my $all = $settings{'all'};
72         my $infobox = $settings{'infobox'} ? '' : 'nobox/';
73         my $rot = $settings{'rot'};
74         my $sel = $settings{'sel'};
75
76         if (defined($num) && $num == -1) {
77                 $num = undef;
78         }
79
80         my $ref = $dbh->selectrow_hashref('SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE id=? AND vhost=?',
81                 undef, $event, $r->get_server_name)
82                 or error($r, "Could not find event $event", 404, "File not found");
83
84         my $name = $ref->{'name'};
85         my $date = $ref->{'date'};
86         $r->set_last_modified($ref->{'last_update'});
87                                 
88         # If the client can use cache, do so
89         if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
90                 return $rc;
91         }
92         
93         # Count the number of selected images.
94         $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE event=? AND selected=\'t\'", undef, $event);
95         my $num_selected = $ref->{'num_selected'};
96
97         # Find all images related to this event.
98         my $q;
99         my $where = ($all == 0) ? ' AND selected=\'t\'' : '';
100
101         if (defined($start) && defined($num) && !$settings{'fullscreen'}) {
102                 $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE event=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename LIMIT $num OFFSET " . ($start-1))
103                         or dberror($r, "prepare()");
104         } else {
105                 $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE event=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename")
106                         or dberror($r, "prepare()");
107         }
108         $q->execute($event)
109                 or dberror($r, "image enumeration");
110
111         # Print the page itself
112         if ($settings{'fullscreen'}) {
113                 $r->content_type("text/html; charset=utf-8");
114                 Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => "$name [$event]" });
115                 while (my $ref = $q->fetchrow_hashref()) {
116                         $r->print("        \"" . $infobox . $ref->{'filename'} . "\",\n");
117                 }
118
119                 my %settings_no_fullscreen = %settings;
120                 $settings_no_fullscreen{'fullscreen'} = 0;
121
122                 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
123                         Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
124
125                 # *whistle*
126                 $returnurl =~ s/&/&/g;
127
128                 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
129                         vhost => $r->get_server_name,
130                         event => $event,
131                         start => $settings{'start'} - 1,
132                         returnurl => $returnurl,
133                         sel => $settings{'sel'},
134                 });
135         } else {
136                 Sesse::pr0n::Common::header($r, "$name [$event]");
137                 Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
138
139                 if (Sesse::pr0n::Overload::is_in_overload($r)) {
140                         Sesse::pr0n::Templates::print_template($r, "overloadmode");
141                 }
142
143                 print_thumbsize($r, $event, \%settings, \%defsettings);
144                 print_viewres($r, $event, \%settings, \%defsettings);
145                 print_pagelimit($r, $event, \%settings, \%defsettings);
146                 print_infobox($r, $event, \%settings, \%defsettings);
147                 print_nextprev($r, $event, \%settings, \%defsettings);
148                 print_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
149                 print_fullscreen($r, $event, \%settings, \%defsettings);
150
151                 my $toclose = 0;
152                 my $lastupl = "";
153                 
154                 # Print out all thumbnails
155                 if ($rot == 1) {
156                         $r->print("    <form method=\"post\" action=\"/rotate\">\n");
157                 
158                         while (my $ref = $q->fetchrow_hashref()) {
159                                 my $imgsz = "";
160                                 my $takenby = $ref->{'takenby'};
161                                 if (defined($ref->{'day'})) {
162                                          $takenby .= ", " . $ref->{'day'};
163                                 }
164
165                                 if ($takenby ne $lastupl) {
166                                         $lastupl = $takenby;
167                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
168                                 }
169                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
170                                         my $width = $ref->{'width'};
171                                         my $height = $ref->{'height'};
172                                                 
173                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
174                                         $imgsz = " width=\"$width\" height=\"$height\"";
175                                 }
176
177                                 my $filename = $ref->{'filename'};
178                                 my $uri = $infobox . $filename;
179                                 if (defined($xres) && defined($yres) && $xres != -1) {
180                                         $uri = "${xres}x$yres/$infobox$filename";
181                                 } elsif (defined($xres) && $xres == -1) {
182                                         $uri = "original/$infobox$filename";
183                                 }
184
185                                 $r->print("    <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
186                                 $r->print("      90 <input type=\"checkbox\" name=\"rot-" .
187                                         $ref->{'id'} . "-90\" />\n");
188                                 $r->print("      180 <input type=\"checkbox\" name=\"rot-" .
189                                         $ref->{'id'} . "-180\" />\n");
190                                 $r->print("      270 <input type=\"checkbox\" name=\"rot-" .
191                                         $ref->{'id'} . "-270\" />\n");
192                                 $r->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
193                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
194                         }
195                         $r->print("      <input type=\"submit\" value=\"Rotate\" />\n");
196                         $r->print("    </form>\n");
197                 } elsif ($sel == 1) {
198                         $r->print("    <form method=\"post\" action=\"/select\">\n");
199                         $r->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
200                 
201                         while (my $ref = $q->fetchrow_hashref()) {
202                                 my $imgsz = "";
203                                 my $takenby = $ref->{'takenby'};
204                                 if (defined($ref->{'day'})) {
205                                          $takenby .= ", " . $ref->{'day'};
206                                 }
207
208                                 if ($takenby ne $lastupl) {
209                                         $lastupl = $takenby;
210                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
211                                 }
212                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
213                                         my $width = $ref->{'width'};
214                                         my $height = $ref->{'height'};
215                                                 
216                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
217                                         $imgsz = " width=\"$width\" height=\"$height\"";
218                                 }
219
220                                 my $filename = $ref->{'filename'};
221                                 my $uri = $infobox . $filename;
222                                 if (defined($xres) && defined($yres) && $xres != -1) {
223                                         $uri = "${xres}x$yres/$infobox$filename";
224                                 } elsif (defined($xres) && $xres == -1) {
225                                         $uri = "original/$infobox$filename";
226                                 }
227
228                                 my $selected = $ref->{'selected'} ? ' checked="checked"' : '';
229
230                                 $r->print("    <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
231                                 $r->print("      <input type=\"checkbox\" name=\"sel-" .
232                                         $ref->{'id'} . "\"$selected /></p>\n");
233                         }
234                         $r->print("      <input type=\"submit\" value=\"Select\" />\n");
235                         $r->print("    </form>\n");
236                 } else {
237                         while (my $ref = $q->fetchrow_hashref()) {
238                                 my $imgsz = "";
239                                 my $takenby = $ref->{'takenby'};
240                                 if (defined($ref->{'day'})) {
241                                          $takenby .= ", " . $ref->{'day'};
242                                 }
243
244                                 if ($takenby ne $lastupl) {
245                                         $r->print("    </p>\n\n") if ($lastupl ne "");
246                                         $lastupl = $takenby;
247                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
248                                         $r->print("    <p>\n");
249                                 }
250                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
251                                         my $width = $ref->{'width'};
252                                         my $height = $ref->{'height'};
253                                                 
254                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
255                                         $imgsz = " width=\"$width\" height=\"$height\"";
256                                 }
257
258                                 my $filename = $ref->{'filename'};
259                                 my $uri = $infobox . $filename;
260                                 if (defined($xres) && defined($yres) && $xres != -1) {
261                                         $uri = "${xres}x$yres/$infobox$filename";
262                                 } elsif (defined($xres) && $xres == -1) {
263                                         $uri = "original/$infobox$filename";
264                                 }
265                                 
266                                 $r->print("      <a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
267                         }
268                         $r->print("    </p>\n");
269                 }
270
271                 print_nextprev($r, $event, \%settings, \%defsettings);
272                 Sesse::pr0n::Common::footer($r);
273         }
274
275         return Apache2::Const::OK;
276 }
277
278 sub eq_with_undef {
279         my ($a, $b) = @_;
280         
281         return 1 if (!defined($a) && !defined($b));
282         return 0 unless (defined($a) && defined($b));
283         return ($a eq $b);
284 }
285
286 sub print_changes {
287         my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
288
289         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
290         chomp $title;
291         $r->print("    <p>$title:\n");
292
293         for my $a (@$alternatives) {
294                 my $text;
295                 my %newsettings = %$settings;
296
297                 if (ref $a) {
298                         my ($v1, $v2);
299                         ($text, $v1, $v2) = @$a;
300                         
301                         $newsettings{$var1} = $v1;
302                         $newsettings{$var2} = $v2;
303                 } else {
304                         $text = $a;
305
306                         # Parse the current alternative
307                         my ($v1, $v2) = split /x/, $a;
308
309                         $newsettings{$var1} = $v1;
310                         $newsettings{$var2} = $v2;
311                 }
312
313                 $r->print("      ");
314
315                 # Check if these settings are current (print only label)
316                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
317                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
318                         $r->print($text);
319                 } else {
320                         Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
321                 }
322                 $r->print("\n");
323         }
324         $r->print("    </p>\n");
325 }
326
327 sub print_thumbsize {
328         my ($r, $event, $settings, $defsettings) = @_;
329         my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
330
331         print_changes($r, $event, 'thumbsize', $settings, $defsettings,
332                       'thumbxres', 'thumbyres', \@alternatives);
333 }
334 sub print_viewres {
335         my ($r, $event, $settings, $defsettings) = @_;
336         my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1280x960);
337         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
338         chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
339         push @alternatives, [ $unlimited, undef, undef ];
340         push @alternatives, [ $original, -1, -1 ];
341
342         print_changes($r, $event, 'viewres', $settings, $defsettings,
343                       'xres', 'yres', \@alternatives);
344 }
345
346 sub print_pagelimit {
347         my ($r, $event, $settings, $defsettings) = @_;
348         
349         my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
350         chomp $title;
351         $r->print("    <p>$title:\n");
352         
353         # Get choices
354         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
355         my @alternatives = qw(10 50 100 500);
356         push @alternatives, $unlimited;
357         
358         for my $num (@alternatives) {
359                 my %newsettings = %$settings;
360
361                 if ($num !~ /^\d+$/) { # unlimited
362                         $newsettings{'num'} = -1;
363                 } else {
364                         $newsettings{'num'} = $num;
365                 }
366
367                 $r->print("      ");
368                 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
369                         $r->print($num);
370                 } else {
371                         Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
372                 }
373                 $r->print("\n");
374         }
375         $r->print("    </p>\n");
376 }
377
378 sub print_infobox {
379         my ($r, $event, $settings, $defsettings) = @_;
380
381         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
382         chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
383         chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
384
385         $r->print("    <p>$title:\n");
386
387         my %newsettings = %$settings;
388
389         if ($settings->{'infobox'} == 1) {
390                 $r->print($on);
391         } else {
392                 $newsettings{'infobox'} = 1;
393                 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
394         }
395
396         $r->print(' ');
397
398         if ($settings->{'infobox'} == 0) {
399                 $r->print($off);
400         } else {
401                 $newsettings{'infobox'} = 0;
402                 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
403         }
404         
405         $r->print('</p>');
406 }
407
408 sub print_nextprev {
409         my ($r, $event, $settings, $defsettings) = @_;
410         my $start = $settings->{'start'};
411         my $num = $settings->{'num'};
412         my $dbh = Sesse::pr0n::Common::get_dbh();
413
414         $num = undef if (defined($num) && $num == -1);
415         return unless (defined($start) && defined($num));
416
417         # determine total number
418         my $ref = $dbh->selectrow_hashref('SELECT count(*) AS num_images FROM images WHERE event=?',
419                 undef, $event)
420                 or dberror($r, "image enumeration");
421         my $num_images = $ref->{'num_images'};
422
423         return if ($start == 1 && $start + $num >= $num_images);
424
425         my $end = $start + $num - 1;
426         if ($end > $num_images) {
427                 $end = $num_images;
428         }
429
430         $r->print("    <p>\n");
431
432         # Previous
433         if ($start > 1) {
434                 my $newstart = $start - $num;
435                 if ($newstart < 1) {
436                         $newstart = 1;
437                 }
438                 my $newend = $newstart + $num - 1;
439                 if ($newend > $num_images) {
440                         $newend = $num_images;
441                 }
442
443                 my %newsettings = %$settings;
444                 $newsettings{'start'} = $newstart;
445                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
446                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
447                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
448         }
449
450         # This
451         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
452         $r->print("    $title ($start-$end)\n");
453
454         # Next
455         if ($end < $num_images) {
456                 my $newstart = $start + $num;
457                 my $newend = $newstart + $num - 1;
458                 if ($newend > $num_images) {
459                         $newend = $num_images;
460                 }
461                 
462                 my %newsettings = %$settings;
463                 $newsettings{'start'} = $newstart;
464                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
465                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
466                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
467         }
468
469         $r->print("    </p>\n");
470 }
471
472 sub print_selected {
473         my ($r, $event, $settings, $defsettings) = @_;
474
475         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
476         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
477         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
478
479         $r->print("    <p>$title:\n");
480
481         my %newsettings = %$settings;
482
483         if ($settings->{'all'} == 0) {
484                 $r->print($sel);
485         } else {
486                 $newsettings{'all'} = 0;
487                 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
488         }
489
490         $r->print(' ');
491
492         if ($settings->{'all'} == 1) {
493                 $r->print($all);
494         } else {
495                 $newsettings{'all'} = 1;
496                 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
497         }
498         
499         $r->print('</p>');
500 }
501
502 sub print_fullscreen {
503         my ($r, $event, $settings, $defsettings) = @_;
504
505         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
506
507         my %newsettings = %$settings;
508         $newsettings{'fullscreen'} = 1;
509
510         $r->print("    <p>");
511         Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
512         $r->print("</p>\n");
513 }
514         
515 1;
516
517