Make the next/prev right when all=0.
[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 => -1,
38                 yres => -1,
39                 start => 1,
40                 num => -1,
41                 all => 1,
42                 infobox => 1,
43                 rot => 0,
44                 sel => 0,
45                 fullscreen => 0,
46         );
47         
48         # Any NEF files => default to processing
49         my $ref = $dbh->selectrow_hashref('SELECT * FROM images WHERE event=? AND LOWER(filename) LIKE \'%.nef\' LIMIT 1',
50                 undef, $event)
51                 and $defsettings{'xres'} = $defsettings{'yres'} = undef;
52         
53         # Reduce the front page load when in overload mode.
54         if (Sesse::pr0n::Overload::is_in_overload($r)) {
55                 $defsettings{'num'} = 100;
56         }
57                 
58         my %settings = %defsettings;
59
60         for my $s qw(thumbxres thumbyres xres yres start num all infobox rot sel fullscreen) {
61                 my $val = $apr->param($s);
62                 if (defined($val) && $val =~ /^(\d+)$/) {
63                         $settings{$s} = $val;
64                 }
65                 if (($s eq "num" || $s eq "xres" || $s eq "yres") && defined($val) && $val == -1) {
66                         $settings{$s} = $val;
67                 }
68         }
69
70         my $thumbxres = $settings{'thumbxres'};
71         my $thumbyres = $settings{'thumbyres'};
72         my $xres = $settings{'xres'};
73         my $yres = $settings{'yres'};
74         my $start = $settings{'start'};
75         my $num = $settings{'num'};
76         my $all = $settings{'all'};
77         my $infobox = $settings{'infobox'} ? '' : 'nobox/';
78         my $rot = $settings{'rot'};
79         my $sel = $settings{'sel'};
80
81         if (defined($num) && $num == -1) {
82                 $num = undef;
83         }
84
85         $ref = $dbh->selectrow_hashref('SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE id=? AND vhost=?',
86                 undef, $event, $r->get_server_name)
87                 or error($r, "Could not find event $event", 404, "File not found");
88
89         my $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
90         my $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
91         $r->set_last_modified($ref->{'last_update'});
92                                 
93         # If the client can use cache, do so
94         if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
95                 return $rc;
96         }
97         
98         # Count the number of selected images.
99         $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE event=? AND selected=\'t\'", undef, $event);
100         my $num_selected = $ref->{'num_selected'};
101
102         # Find all images related to this event.
103         my $where = ($all == 0) ? ' AND selected=\'t\'' : '';
104         my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : "";
105
106         my $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")
107                 or dberror($r, "prepare()");
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_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
148                 print_fullscreen($r, $event, \%settings, \%defsettings);
149                 print_nextprev($r, $event, \%settings, \%defsettings);
150                 
151                 # Find the equipment used
152                 my $eq = $dbh->prepare("
153                         SELECT 
154                                 TRIM(model.value) AS model,
155                                 coalesce(TRIM(lens_spec.value), TRIM(lens.value)) AS lens,
156                                 COUNT(*) AS num
157                         FROM ( SELECT * FROM images WHERE event=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename $limit ) i
158                                 LEFT JOIN exif_info model ON i.id=model.image
159                                 LEFT JOIN ( SELECT * FROM exif_info WHERE tag='Lens' ) lens ON i.id=lens.image
160                                 LEFT JOIN ( SELECT * FROM exif_info WHERE tag='LensSpec') lens_spec ON i.id=lens_spec.image
161                         WHERE model.tag='Model'
162                         GROUP BY 1,2
163                         ORDER BY 1,2")
164                         or die "Couldn't prepare to find equipment: $!";
165                 $eq->execute($event)
166                         or die "Couldn't find equipment: $!";
167
168                 my @equipment = ();
169                 my %cameras_seen = ();
170                 while (my $ref = $eq->fetchrow_hashref) {
171                         if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
172                                 #
173                                 # Some compact cameras seem to add lens info sometimes and not at other
174                                 # times; if we have seen a camera with at least one specific lens earlier,
175                                 # just combine entries without a lens with the previous one.
176                                 #
177                                 $equipment[$#equipment]->{'num'} += $ref->{'num'};
178                                 next;
179                         }
180                         push @equipment, $ref;
181                         $cameras_seen{$ref->{'model'}} = 1;
182                 }
183                 $eq->finish;
184
185                 if (scalar @equipment > 0) {
186                         Sesse::pr0n::Templates::print_template($r, "equipment-start");
187                         for my $e (@equipment) {
188                                 my $eqspec = $e->{'model'};
189                                 $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
190
191                                 # This isn't correct for all languages. Fix if we ever need to care. :-)
192                                 if ($e->{'num'} == 1) {
193                                         Sesse::pr0n::Templates::print_template($r, "equipment-item-singular", { eqspec => $eqspec });
194                                 } else {
195                                         Sesse::pr0n::Templates::print_template($r, "equipment-item", { eqspec => $eqspec, num => $e->{'num'} });
196                                 }
197                         }
198                         Sesse::pr0n::Templates::print_template($r, "equipment-end");
199                 }
200
201
202                 my $toclose = 0;
203                 my $lastupl = "";
204                 my $img_num = $start;
205                 
206                 # Print out all thumbnails
207                 if ($rot == 1) {
208                         $r->print("    <form method=\"post\" action=\"/rotate\">\n");
209                         $r->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
210         
211                         while (my $ref = $q->fetchrow_hashref()) {
212                                 my $imgsz = "";
213                                 my $takenby = $ref->{'takenby'};
214                                 if (defined($ref->{'day'})) {
215                                          $takenby .= ", " . $ref->{'day'};
216                                 }
217
218                                 if ($takenby ne $lastupl) {
219                                         $lastupl = $takenby;
220                                         $r->print("    <h2>");
221                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
222                                         print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
223                                         $r->print("</h2>\n");
224                                 }
225                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
226                                         my $width = $ref->{'width'};
227                                         my $height = $ref->{'height'};
228                                                 
229                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
230                                         $imgsz = " width=\"$width\" height=\"$height\"";
231                                 }
232
233                                 my $filename = $ref->{'filename'};
234                                 my $uri = $infobox . $filename;
235                                 if (defined($xres) && defined($yres) && $xres != -1) {
236                                         $uri = "${xres}x$yres/$infobox$filename";
237                                 } elsif (defined($xres) && $xres == -1) {
238                                         $uri = "original/$infobox$filename";
239                                 }
240
241                                 $r->print("    <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
242                                 $r->print("      90 <input type=\"checkbox\" name=\"rot-" .
243                                         $ref->{'id'} . "-90\" />\n");
244                                 $r->print("      180 <input type=\"checkbox\" name=\"rot-" .
245                                         $ref->{'id'} . "-180\" />\n");
246                                 $r->print("      270 <input type=\"checkbox\" name=\"rot-" .
247                                         $ref->{'id'} . "-270\" />\n");
248                                 $r->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
249                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
250                         
251                                 ++$img_num;
252                         }
253                         $r->print("      <input type=\"submit\" value=\"Rotate\" />\n");
254                         $r->print("    </form>\n");
255                 } else {
256                         while (my $ref = $q->fetchrow_hashref()) {
257                                 my $imgsz = "";
258                                 my $takenby = $ref->{'takenby'};
259                                 if (defined($ref->{'day'})) {
260                                          $takenby .= ", " . $ref->{'day'};
261                                 }
262
263                                 if ($takenby ne $lastupl) {
264                                         $r->print("    </p>\n\n") if ($lastupl ne "");
265                                         $lastupl = $takenby;
266                                         $r->print("    <h2>");
267                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
268                                         print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
269                                         $r->print("</h2>\n");
270                                         $r->print("    <p class=\"photos\">\n");
271                                 }
272                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
273                                         my $width = $ref->{'width'};
274                                         my $height = $ref->{'height'};
275                                                 
276                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
277                                         $imgsz = " width=\"$width\" height=\"$height\"";
278                                 }
279
280                                 my $filename = $ref->{'filename'};
281                                 my $uri = $infobox . $filename;
282                                 if (defined($xres) && defined($yres) && $xres != -1) {
283                                         $uri = "${xres}x$yres/$infobox$filename";
284                                 } elsif (defined($xres) && $xres == -1) {
285                                         $uri = "original/$infobox$filename";
286                                 }
287                                 
288                                 $r->print("      <a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
289                                 
290                                 ++$img_num;
291                         }
292                         $r->print("    </p>\n");
293                 }
294
295                 print_nextprev($r, $event, \%settings, \%defsettings);
296                 Sesse::pr0n::Common::footer($r);
297         }
298
299         return Apache2::Const::OK;
300 }
301
302 sub eq_with_undef {
303         my ($a, $b) = @_;
304         
305         return 1 if (!defined($a) && !defined($b));
306         return 0 unless (defined($a) && defined($b));
307         return ($a eq $b);
308 }
309
310 sub print_changes {
311         my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
312
313         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
314         chomp $title;
315         $r->print("    <p>$title:\n");
316
317         for my $a (@$alternatives) {
318                 my $text;
319                 my %newsettings = %$settings;
320
321                 if (ref $a) {
322                         my ($v1, $v2);
323                         ($text, $v1, $v2) = @$a;
324                         
325                         $newsettings{$var1} = $v1;
326                         $newsettings{$var2} = $v2;
327                 } else {
328                         $text = $a;
329
330                         # Parse the current alternative
331                         my ($v1, $v2) = split /x/, $a;
332
333                         $newsettings{$var1} = $v1;
334                         $newsettings{$var2} = $v2;
335                 }
336
337                 $r->print("      ");
338
339                 # Check if these settings are current (print only label)
340                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
341                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
342                         $r->print($text);
343                 } else {
344                         Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
345                 }
346                 $r->print("\n");
347         }
348         $r->print("    </p>\n");
349 }
350
351 sub print_thumbsize {
352         my ($r, $event, $settings, $defsettings) = @_;
353         my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
354
355         print_changes($r, $event, 'thumbsize', $settings, $defsettings,
356                       'thumbxres', 'thumbyres', \@alternatives);
357 }
358 sub print_viewres {
359         my ($r, $event, $settings, $defsettings) = @_;
360         my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1280x960);
361         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
362         chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
363         push @alternatives, [ $unlimited, undef, undef ];
364         push @alternatives, [ $original, -1, -1 ];
365
366         print_changes($r, $event, 'viewres', $settings, $defsettings,
367                       'xres', 'yres', \@alternatives);
368 }
369
370 sub print_pagelimit {
371         my ($r, $event, $settings, $defsettings) = @_;
372         
373         my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
374         chomp $title;
375         $r->print("    <p>$title:\n");
376         
377         # Get choices
378         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
379         my @alternatives = qw(10 50 100 500);
380         push @alternatives, $unlimited;
381         
382         for my $num (@alternatives) {
383                 my %newsettings = %$settings;
384
385                 if ($num !~ /^\d+$/) { # unlimited
386                         $newsettings{'num'} = -1;
387                 } else {
388                         $newsettings{'num'} = $num;
389                 }
390
391                 $r->print("      ");
392                 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
393                         $r->print($num);
394                 } else {
395                         Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
396                 }
397                 $r->print("\n");
398         }
399         $r->print("    </p>\n");
400 }
401
402 sub print_infobox {
403         my ($r, $event, $settings, $defsettings) = @_;
404
405         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
406         chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
407         chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
408
409         $r->print("    <p>$title:\n");
410
411         my %newsettings = %$settings;
412
413         if ($settings->{'infobox'} == 1) {
414                 $r->print($on);
415         } else {
416                 $newsettings{'infobox'} = 1;
417                 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
418         }
419
420         $r->print(' ');
421
422         if ($settings->{'infobox'} == 0) {
423                 $r->print($off);
424         } else {
425                 $newsettings{'infobox'} = 0;
426                 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
427         }
428         
429         $r->print('</p>');
430 }
431
432 sub print_nextprev {
433         my ($r, $event, $settings, $defsettings) = @_;
434         my $start = $settings->{'start'};
435         my $num = $settings->{'num'};
436         my $all = $settings->{'all'};
437         my $dbh = Sesse::pr0n::Common::get_dbh();
438         my $where = ($all == 0) ? ' AND selected=\'t\'' : '';
439
440         $num = undef if (defined($num) && $num == -1);
441         return unless (defined($start) && defined($num));
442
443         # determine total number
444         my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE event=? $where",
445                 undef, $event)
446                 or dberror($r, "image enumeration");
447         my $num_images = $ref->{'num_images'};
448
449         return if ($start == 1 && $start + $num >= $num_images);
450
451         my $end = $start + $num - 1;
452         if ($end > $num_images) {
453                 $end = $num_images;
454         }
455
456         $r->print("    <p class=\"nextprev\">\n");
457
458         # Previous
459         if ($start > 1) {
460                 my $newstart = $start - $num;
461                 if ($newstart < 1) {
462                         $newstart = 1;
463                 }
464                 my $newend = $newstart + $num - 1;
465                 if ($newend > $num_images) {
466                         $newend = $num_images;
467                 }
468
469                 my %newsettings = %$settings;
470                 $newsettings{'start'} = $newstart;
471                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
472                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
473                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
474         }
475
476         # This
477         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
478         $r->print("    $title ($start-$end)\n");
479
480         # Next
481         if ($end < $num_images) {
482                 my $newstart = $start + $num;
483                 my $newend = $newstart + $num - 1;
484                 if ($newend > $num_images) {
485                         $newend = $num_images;
486                 }
487                 
488                 my %newsettings = %$settings;
489                 $newsettings{'start'} = $newstart;
490                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
491                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
492                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
493         }
494
495         $r->print("    </p>\n");
496 }
497
498 sub print_selected {
499         my ($r, $event, $settings, $defsettings) = @_;
500
501         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
502         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
503         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
504
505         $r->print("    <p>$title:\n");
506
507         my %newsettings = %$settings;
508
509         if ($settings->{'all'} == 0) {
510                 $r->print($sel);
511         } else {
512                 $newsettings{'all'} = 0;
513                 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
514         }
515
516         $r->print(' ');
517
518         if ($settings->{'all'} == 1) {
519                 $r->print($all);
520         } else {
521                 $newsettings{'all'} = 1;
522                 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
523         }
524         
525         $r->print('</p>');
526 }
527
528 sub print_fullscreen {
529         my ($r, $event, $settings, $defsettings) = @_;
530
531         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
532
533         my %newsettings = %$settings;
534         $newsettings{'fullscreen'} = 1;
535
536         $r->print("    <p>");
537         Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
538         $r->print("</p>\n");
539 }
540
541 sub print_fullscreen_fromhere {
542         my ($r, $event, $settings, $defsettings, $start) = @_;
543
544         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
545
546         my %newsettings = %$settings;
547         $newsettings{'fullscreen'} = 1;
548         $newsettings{'start'} = $start;
549
550         $r->print("    <p class=\"fsfromhere\">");
551         Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
552         $r->print("</p>\n");
553 }
554         
555 1;
556
557