Add an “equipment used” header on top of all image listings.
[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 $q;
104         my $where = ($all == 0) ? ' AND selected=\'t\'' : '';
105
106         if (defined($start) && defined($num) && !$settings{'fullscreen'}) {
107                 $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))
108                         or dberror($r, "prepare()");
109         } else {
110                 $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")
111                         or dberror($r, "prepare()");
112         }
113         $q->execute($event)
114                 or dberror($r, "image enumeration");
115
116         # Print the page itself
117         if ($settings{'fullscreen'}) {
118                 $r->content_type("text/html; charset=utf-8");
119                 Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => "$name [$event]" });
120                 while (my $ref = $q->fetchrow_hashref()) {
121                         $r->print("        \"" . $infobox . $ref->{'filename'} . "\",\n");
122                 }
123
124                 my %settings_no_fullscreen = %settings;
125                 $settings_no_fullscreen{'fullscreen'} = 0;
126
127                 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
128                         Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
129
130                 # *whistle*
131                 $returnurl =~ s/&/&/g;
132
133                 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
134                         vhost => $r->get_server_name,
135                         event => $event,
136                         start => $settings{'start'} - 1,
137                         returnurl => $returnurl,
138                         sel => $settings{'sel'},
139                 });
140         } else {
141                 Sesse::pr0n::Common::header($r, "$name [$event]");
142                 Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
143
144                 if (Sesse::pr0n::Overload::is_in_overload($r)) {
145                         Sesse::pr0n::Templates::print_template($r, "overloadmode");
146                 }
147
148                 print_thumbsize($r, $event, \%settings, \%defsettings);
149                 print_viewres($r, $event, \%settings, \%defsettings);
150                 print_pagelimit($r, $event, \%settings, \%defsettings);
151                 print_infobox($r, $event, \%settings, \%defsettings);
152                 print_nextprev($r, $event, \%settings, \%defsettings);
153                 print_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
154                 print_fullscreen($r, $event, \%settings, \%defsettings);
155                 
156                 # Find the equipment used
157                 my $eq = $dbh->prepare('
158                         SELECT DISTINCT
159                                 model.value AS model,
160                                 coalesce(lens_spec.value, lens.value) AS lens
161                         FROM images i
162                                 LEFT JOIN exif_info model ON i.id=model.image
163                                 LEFT JOIN ( SELECT * FROM exif_info WHERE tag=\'Lens\' ) lens ON i.id=lens.image
164                                 LEFT JOIN ( SELECT * FROM exif_info WHERE tag=\'LensSpec\') lens_spec ON i.id=lens_spec.image
165                         WHERE event=? AND model.tag=\'Model\'
166                         ORDER BY 1,2')
167                         or die "Couldn't prepare to find equipment: $!";
168                 $eq->execute($event)
169                         or die "Couldn't find equipment: $!";
170
171                 my @equipment = ();
172                 my %cameras_seen = ();
173                 while (my $ref = $eq->fetchrow_hashref) {
174                         if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
175                                 #
176                                 # Some compact cameras seem to add lens info sometimes and not at other
177                                 # times; if we have seen a camera with at least one specific lens earlier,
178                                 # just ignore entries without a lens.
179                                 #
180                                 next;
181                         }
182                         push @equipment, $ref;
183                         $cameras_seen{$ref->{'model'}} = 1;
184                 }
185                 $eq->finish;
186
187                 if (scalar @equipment > 0) {
188                         Sesse::pr0n::Templates::print_template($r, "equipment-start");
189                         for my $e (@equipment) {
190                                 $r->print("  <li>" . $e->{'model'});
191                                 if (defined($e->{'lens'})) {
192                                         $r->print(", " . $e->{'lens'} . "</li>\n");
193                                 } else {
194                                         $r->print("</li>\n");
195                                 }
196                         }
197                         Sesse::pr0n::Templates::print_template($r, "equipment-end");
198                 }
199
200
201                 my $toclose = 0;
202                 my $lastupl = "";
203                 
204                 # Print out all thumbnails
205                 if ($rot == 1) {
206                         $r->print("    <form method=\"post\" action=\"/rotate\">\n");
207                         $r->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
208                 
209                         while (my $ref = $q->fetchrow_hashref()) {
210                                 my $imgsz = "";
211                                 my $takenby = $ref->{'takenby'};
212                                 if (defined($ref->{'day'})) {
213                                          $takenby .= ", " . $ref->{'day'};
214                                 }
215
216                                 if ($takenby ne $lastupl) {
217                                         $lastupl = $takenby;
218                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
219                                 }
220                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
221                                         my $width = $ref->{'width'};
222                                         my $height = $ref->{'height'};
223                                                 
224                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
225                                         $imgsz = " width=\"$width\" height=\"$height\"";
226                                 }
227
228                                 my $filename = $ref->{'filename'};
229                                 my $uri = $infobox . $filename;
230                                 if (defined($xres) && defined($yres) && $xres != -1) {
231                                         $uri = "${xres}x$yres/$infobox$filename";
232                                 } elsif (defined($xres) && $xres == -1) {
233                                         $uri = "original/$infobox$filename";
234                                 }
235
236                                 $r->print("    <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
237                                 $r->print("      90 <input type=\"checkbox\" name=\"rot-" .
238                                         $ref->{'id'} . "-90\" />\n");
239                                 $r->print("      180 <input type=\"checkbox\" name=\"rot-" .
240                                         $ref->{'id'} . "-180\" />\n");
241                                 $r->print("      270 <input type=\"checkbox\" name=\"rot-" .
242                                         $ref->{'id'} . "-270\" />\n");
243                                 $r->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
244                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
245                         }
246                         $r->print("      <input type=\"submit\" value=\"Rotate\" />\n");
247                         $r->print("    </form>\n");
248                 } elsif ($sel == 1) {
249                         $r->print("    <form method=\"post\" action=\"/select\">\n");
250                         $r->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
251                 
252                         while (my $ref = $q->fetchrow_hashref()) {
253                                 my $imgsz = "";
254                                 my $takenby = $ref->{'takenby'};
255                                 if (defined($ref->{'day'})) {
256                                          $takenby .= ", " . $ref->{'day'};
257                                 }
258
259                                 if ($takenby ne $lastupl) {
260                                         $lastupl = $takenby;
261                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
262                                 }
263                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
264                                         my $width = $ref->{'width'};
265                                         my $height = $ref->{'height'};
266                                                 
267                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
268                                         $imgsz = " width=\"$width\" height=\"$height\"";
269                                 }
270
271                                 my $filename = $ref->{'filename'};
272                                 my $uri = $infobox . $filename;
273                                 if (defined($xres) && defined($yres) && $xres != -1) {
274                                         $uri = "${xres}x$yres/$infobox$filename";
275                                 } elsif (defined($xres) && $xres == -1) {
276                                         $uri = "original/$infobox$filename";
277                                 }
278
279                                 my $selected = $ref->{'selected'} ? ' checked="checked"' : '';
280
281                                 $r->print("    <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
282                                 $r->print("      <input type=\"checkbox\" name=\"sel-" .
283                                         $ref->{'id'} . "\"$selected /></p>\n");
284                         }
285                         $r->print("      <input type=\"submit\" value=\"Select\" />\n");
286                         $r->print("    </form>\n");
287                 } else {
288                         while (my $ref = $q->fetchrow_hashref()) {
289                                 my $imgsz = "";
290                                 my $takenby = $ref->{'takenby'};
291                                 if (defined($ref->{'day'})) {
292                                          $takenby .= ", " . $ref->{'day'};
293                                 }
294
295                                 if ($takenby ne $lastupl) {
296                                         $r->print("    </p>\n\n") if ($lastupl ne "");
297                                         $lastupl = $takenby;
298                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
299                                         $r->print("    <p>\n");
300                                 }
301                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
302                                         my $width = $ref->{'width'};
303                                         my $height = $ref->{'height'};
304                                                 
305                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
306                                         $imgsz = " width=\"$width\" height=\"$height\"";
307                                 }
308
309                                 my $filename = $ref->{'filename'};
310                                 my $uri = $infobox . $filename;
311                                 if (defined($xres) && defined($yres) && $xres != -1) {
312                                         $uri = "${xres}x$yres/$infobox$filename";
313                                 } elsif (defined($xres) && $xres == -1) {
314                                         $uri = "original/$infobox$filename";
315                                 }
316                                 
317                                 $r->print("      <a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
318                         }
319                         $r->print("    </p>\n");
320                 }
321
322                 print_nextprev($r, $event, \%settings, \%defsettings);
323                 Sesse::pr0n::Common::footer($r);
324         }
325
326         return Apache2::Const::OK;
327 }
328
329 sub eq_with_undef {
330         my ($a, $b) = @_;
331         
332         return 1 if (!defined($a) && !defined($b));
333         return 0 unless (defined($a) && defined($b));
334         return ($a eq $b);
335 }
336
337 sub print_changes {
338         my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
339
340         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
341         chomp $title;
342         $r->print("    <p>$title:\n");
343
344         for my $a (@$alternatives) {
345                 my $text;
346                 my %newsettings = %$settings;
347
348                 if (ref $a) {
349                         my ($v1, $v2);
350                         ($text, $v1, $v2) = @$a;
351                         
352                         $newsettings{$var1} = $v1;
353                         $newsettings{$var2} = $v2;
354                 } else {
355                         $text = $a;
356
357                         # Parse the current alternative
358                         my ($v1, $v2) = split /x/, $a;
359
360                         $newsettings{$var1} = $v1;
361                         $newsettings{$var2} = $v2;
362                 }
363
364                 $r->print("      ");
365
366                 # Check if these settings are current (print only label)
367                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
368                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
369                         $r->print($text);
370                 } else {
371                         Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
372                 }
373                 $r->print("\n");
374         }
375         $r->print("    </p>\n");
376 }
377
378 sub print_thumbsize {
379         my ($r, $event, $settings, $defsettings) = @_;
380         my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
381
382         print_changes($r, $event, 'thumbsize', $settings, $defsettings,
383                       'thumbxres', 'thumbyres', \@alternatives);
384 }
385 sub print_viewres {
386         my ($r, $event, $settings, $defsettings) = @_;
387         my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1280x960);
388         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
389         chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
390         push @alternatives, [ $unlimited, undef, undef ];
391         push @alternatives, [ $original, -1, -1 ];
392
393         print_changes($r, $event, 'viewres', $settings, $defsettings,
394                       'xres', 'yres', \@alternatives);
395 }
396
397 sub print_pagelimit {
398         my ($r, $event, $settings, $defsettings) = @_;
399         
400         my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
401         chomp $title;
402         $r->print("    <p>$title:\n");
403         
404         # Get choices
405         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
406         my @alternatives = qw(10 50 100 500);
407         push @alternatives, $unlimited;
408         
409         for my $num (@alternatives) {
410                 my %newsettings = %$settings;
411
412                 if ($num !~ /^\d+$/) { # unlimited
413                         $newsettings{'num'} = -1;
414                 } else {
415                         $newsettings{'num'} = $num;
416                 }
417
418                 $r->print("      ");
419                 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
420                         $r->print($num);
421                 } else {
422                         Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
423                 }
424                 $r->print("\n");
425         }
426         $r->print("    </p>\n");
427 }
428
429 sub print_infobox {
430         my ($r, $event, $settings, $defsettings) = @_;
431
432         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
433         chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
434         chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
435
436         $r->print("    <p>$title:\n");
437
438         my %newsettings = %$settings;
439
440         if ($settings->{'infobox'} == 1) {
441                 $r->print($on);
442         } else {
443                 $newsettings{'infobox'} = 1;
444                 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
445         }
446
447         $r->print(' ');
448
449         if ($settings->{'infobox'} == 0) {
450                 $r->print($off);
451         } else {
452                 $newsettings{'infobox'} = 0;
453                 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
454         }
455         
456         $r->print('</p>');
457 }
458
459 sub print_nextprev {
460         my ($r, $event, $settings, $defsettings) = @_;
461         my $start = $settings->{'start'};
462         my $num = $settings->{'num'};
463         my $dbh = Sesse::pr0n::Common::get_dbh();
464
465         $num = undef if (defined($num) && $num == -1);
466         return unless (defined($start) && defined($num));
467
468         # determine total number
469         my $ref = $dbh->selectrow_hashref('SELECT count(*) AS num_images FROM images WHERE event=?',
470                 undef, $event)
471                 or dberror($r, "image enumeration");
472         my $num_images = $ref->{'num_images'};
473
474         return if ($start == 1 && $start + $num >= $num_images);
475
476         my $end = $start + $num - 1;
477         if ($end > $num_images) {
478                 $end = $num_images;
479         }
480
481         $r->print("    <p>\n");
482
483         # Previous
484         if ($start > 1) {
485                 my $newstart = $start - $num;
486                 if ($newstart < 1) {
487                         $newstart = 1;
488                 }
489                 my $newend = $newstart + $num - 1;
490                 if ($newend > $num_images) {
491                         $newend = $num_images;
492                 }
493
494                 my %newsettings = %$settings;
495                 $newsettings{'start'} = $newstart;
496                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
497                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
498                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
499         }
500
501         # This
502         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
503         $r->print("    $title ($start-$end)\n");
504
505         # Next
506         if ($end < $num_images) {
507                 my $newstart = $start + $num;
508                 my $newend = $newstart + $num - 1;
509                 if ($newend > $num_images) {
510                         $newend = $num_images;
511                 }
512                 
513                 my %newsettings = %$settings;
514                 $newsettings{'start'} = $newstart;
515                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
516                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
517                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
518         }
519
520         $r->print("    </p>\n");
521 }
522
523 sub print_selected {
524         my ($r, $event, $settings, $defsettings) = @_;
525
526         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
527         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
528         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
529
530         $r->print("    <p>$title:\n");
531
532         my %newsettings = %$settings;
533
534         if ($settings->{'all'} == 0) {
535                 $r->print($sel);
536         } else {
537                 $newsettings{'all'} = 0;
538                 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
539         }
540
541         $r->print(' ');
542
543         if ($settings->{'all'} == 1) {
544                 $r->print($all);
545         } else {
546                 $newsettings{'all'} = 1;
547                 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
548         }
549         
550         $r->print('</p>');
551 }
552
553 sub print_fullscreen {
554         my ($r, $event, $settings, $defsettings) = @_;
555
556         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
557
558         my %newsettings = %$settings;
559         $newsettings{'fullscreen'} = 1;
560
561         $r->print("    <p>");
562         Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
563         $r->print("</p>\n");
564 }
565         
566 1;
567
568