Remove burned-in infoboxes. Turn infobox always on in fullscreen mode (for now).
[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 POSIX;
7
8 sub handler {
9         my $r = shift;
10         my $dbh = Sesse::pr0n::Common::get_dbh();
11
12         my ($event, $abspath, $datesort);
13         if ($r->path_info =~ /^\/\+all\/?/) {
14                 $event = '+all';
15                 $abspath = 1;
16
17                 $datesort = 'DESC NULLS LAST';
18         } else {
19                 # Find the event
20                 $r->path_info =~ /^\/([a-zA-Z0-9-]+)\/?$/
21                         or return error($r, "Could not extract event");
22                 $event = $1;
23                 $abspath = 0;
24                 $datesort = 'ASC NULLS LAST';
25         }
26
27         # Fix common error: pr0n.sesse.net/event -> pr0n.sesse.net/event/
28         if ($r->path_info !~ /\/$/) {
29                 my $res = Plack::Response->new(301);
30                 $res->header('Location' => $r->path_info . "/");
31                 return $res;
32         }
33
34         # Internal? (Ugly?) 
35         if (Sesse::pr0n::Common::get_server_name($r) =~ /internal/ ||
36             Sesse::pr0n::Common::get_server_name($r) =~ /skoyen\.bilder\.knatten\.com/ ||
37             Sesse::pr0n::Common::get_server_name($r) =~ /lia\.heimdal\.org/) {
38                 my $user = Sesse::pr0n::Common::check_access($r);
39                 return Sesse::pr0n::Common::generate_401($r) if (!defined($user));
40         }
41
42         # Read the appropriate settings from the query string into the settings hash
43         my %defsettings = (
44                 thumbxres => 320,
45                 thumbyres => 256,
46                 xres => -1,
47                 yres => -1,
48                 start => 1,
49                 num => 250,
50                 all => 1,
51                 rot => 0,
52                 sel => 0,
53                 fullscreen => 0,
54                 model => undef,
55                 lens => undef,
56                 author => undef
57         );
58         
59         my $where;
60         if ($event eq '+all') {
61                 $where = '';
62         } else {
63                 $where = ' AND event=' . $dbh->quote($event);
64         }
65         
66         # Any NEF files => default to processing
67         my $ref = $dbh->selectrow_hashref("SELECT * FROM images WHERE vhost=? $where AND ( LOWER(filename) LIKE '%.nef' OR LOWER(filename) LIKE '%.cr2' ) LIMIT 1",
68                 undef, Sesse::pr0n::Common::get_server_name($r))
69                 and $defsettings{'xres'} = $defsettings{'yres'} = undef;
70         
71         my %settings = %defsettings;
72
73         for my $s (qw(thumbxres thumbyres xres yres start num all rot sel fullscreen model lens author)) {
74                 my $val = $r->param($s);
75                 if (defined($val) && $val =~ /^(\d+)$/) {
76                         $settings{$s} = $val;
77                 }
78                 if ($s eq "num" && defined($val) && $val == -1) {
79                         $settings{$s} = $val;
80                 }
81                 if (($s eq "xres" || $s eq "yres") && defined($val) && ($val == -1 || $val == -2)) {
82                         $settings{$s} = $val;
83                 }
84                 if (($s eq "model" || $s eq "lens" || $s eq "author") && defined($val)) {
85                         $settings{$s} = Sesse::pr0n::Common::pretty_unescape($val);
86                 }
87         }
88
89         my $thumbxres = $settings{'thumbxres'};
90         my $thumbyres = $settings{'thumbyres'};
91         my $xres = $settings{'xres'};
92         my $yres = $settings{'yres'};
93         my $start = $settings{'start'};
94         my $num = $settings{'num'};
95         my $all = $settings{'all'};
96         my $rot = $settings{'rot'};
97         my $sel = $settings{'sel'};
98         my $model = $settings{'model'};
99         my $lens = $settings{'lens'};
100         my $author = $settings{'author'};
101
102         # Construct SQL for this filter
103         if ($all == 0) {
104                 $where .= ' AND selected=\'t\'';        
105         }
106         if (defined($model) && defined($lens)) {
107                 my $mq = $dbh->quote($model);
108                 my $lq = $dbh->quote($lens);
109
110                 if ($model eq '') {
111                         # no defined model
112                         $where .= " AND model IS NULL";
113                 } else {
114                         $where .= " AND model=$mq";
115                 }
116         
117                 if ($lens eq '') {
118                         # no defined lens
119                         $where .= " AND lens IS NULL";
120                 } else {
121                         $where .= " AND lens=$lq";
122                 }
123         }
124         if (defined($author)) {
125                 my $aq = $dbh->quote($author);
126
127                 $where .= " AND takenby=$aq";
128         }
129
130         if (defined($num) && $num == -1) {
131                 $num = undef;
132         }
133
134         my ($date, $name);
135
136         if ($event eq '+all') {
137                 $ref = $dbh->selectrow_hashref("SELECT EXTRACT(EPOCH FROM MAX(last_update)) AS last_update FROM last_picture_cache WHERE vhost=?",
138                         undef, Sesse::pr0n::Common::get_server_name($r))
139                         or return error($r, "Could not list events", 404, "File not found");
140                 $date = undef;
141                 $name = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
142                 Sesse::pr0n::Common::set_last_modified($r, $ref->{'last_update'});
143         } else {
144                 $ref = $dbh->selectrow_hashref("SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events NATURAL JOIN last_picture_cache WHERE vhost=? AND event=?",
145                         undef, Sesse::pr0n::Common::get_server_name($r), $event)
146                         or return error($r, "Could not find event $event", 404, "File not found");
147
148                 $date = HTML::Entities::encode_entities($ref->{'date'});
149                 $name = HTML::Entities::encode_entities($ref->{'name'});
150                 Sesse::pr0n::Common::set_last_modified($r, $ref->{'last_update'});
151         }
152                                 
153         # # If the client can use cache, do so
154         # if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
155         #       return $rc;
156         # }
157         
158         # Count the number of selected images.
159         $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE vhost=? $where AND selected=\'t\'", undef, Sesse::pr0n::Common::get_server_name($r));
160         my $num_selected = $ref->{'num_selected'};
161
162         # Find all images related to this event.
163         my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : "";
164
165         my $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE vhost=? $where ORDER BY (date - INTERVAL '6 hours')::date $datesort,takenby,date,filename $limit")
166                 or return dberror($r, "prepare()");
167         $q->execute(Sesse::pr0n::Common::get_server_name($r))
168                 or return dberror($r, "image enumeration");
169
170         # Print the page itself
171         my $res = Plack::Response->new(200);
172         my $io = IO::String->new;
173         if ($settings{'fullscreen'}) {
174                 $res->content_type("text/html; charset=utf-8");
175
176                 Sesse::pr0n::Templates::print_template($r, $io, "fullscreen-header", { title => "$name [$event]" });
177
178                 my @files = ();
179                 while (my $ref = $q->fetchrow_hashref()) {
180                         my $width = defined($ref->{'width'}) ? $ref->{'width'} : -1;
181                         my $height = defined($ref->{'height'}) ? $ref->{'height'} : -1;
182                         push @files, [ $ref->{'event'}, $ref->{'filename'}, $width, $height ];
183                 }
184                 
185                 for my $i (0..$#files) {
186                         my $line = sprintf "        [ \"%s\", \"%s\", %d, %d ]", @{$files[$i]};
187                         $line .= "," unless ($i == $#files);
188                         $io->print($line . "\n");
189                 }
190
191                 my %settings_no_fullscreen = %settings;
192                 $settings_no_fullscreen{'fullscreen'} = 0;
193
194                 my $returnurl = "http://" . Sesse::pr0n::Common::get_server_name($r) . "/" . $event . "/" .
195                         Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
196                 
197                 # *whistle*
198                 $returnurl =~ s/&/&/g;
199
200                 Sesse::pr0n::Templates::print_template($r, $io, "fullscreen-footer", {
201                         returnurl => $returnurl,
202                         start => $settings{'start'} - 1,
203                         sel => $settings{'sel'}
204                 });
205         } else {
206                 Sesse::pr0n::Common::header($r, $io, "$name [$event]");
207                 if (defined($date)) {
208                         Sesse::pr0n::Templates::print_template($r, $io, "date", { date => $date });
209                 }
210
211                 if (Sesse::pr0n::Overload::is_in_overload($r)) {
212                         Sesse::pr0n::Templates::print_template($r, $io, "overloadmode");
213                 }
214
215                 print_viewres($r, $io, $event, \%settings, \%defsettings);
216                 print_selected($r, $io, $event, \%settings, \%defsettings) if ($num_selected > 0);
217                 print_fullscreen($r, $io, $event, \%settings, \%defsettings);
218                 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
219         
220                 if (1 || $event ne '+all') {
221                         # Find the equipment used
222                         my $eq = $dbh->prepare("
223                                 SELECT
224                                         model,
225                                         lens,
226                                         COUNT(*) AS num
227                                 FROM images
228                                 WHERE vhost=? $where
229                                 GROUP BY 1,2
230                                 ORDER BY 1,2")
231                                 or die "Couldn't prepare to find equipment: $!";
232                         $eq->execute(Sesse::pr0n::Common::get_server_name($r))
233                                 or die "Couldn't find equipment: $!";
234
235                         my @equipment = ();
236                         my %cameras_seen = ();
237                         while (my $ref = $eq->fetchrow_hashref) {
238                                 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
239                                         #
240                                         # Some compact cameras seem to add lens info sometimes and not at other
241                                         # times; if we have seen a camera with at least one specific lens earlier,
242                                         # just combine entries without a lens with the previous one.
243                                         #
244                                         $equipment[$#equipment]->{'num'} += $ref->{'num'};
245                                         next;
246                                 }
247                                 push @equipment, $ref;
248                                 $cameras_seen{$ref->{'model'}} = 1;
249                         }
250                         $eq->finish;
251
252                         if (scalar @equipment > 0) {
253                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-start");
254                                 for my $e (@equipment) {
255                                         my $eqspec = $e->{'model'};
256                                         $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
257                                         $eqspec = HTML::Entities::encode_entities($eqspec);
258
259                                         my %newsettings = %settings;
260
261                                         my $action;
262                                         if (defined($model) && defined($lens)) {
263                                                 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
264                                                 $newsettings{'model'} = undef;
265                                                 $newsettings{'lens'} = undef;
266                                                 $newsettings{'start'} = 1;
267                                         } else {
268                                                 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
269                                                 $newsettings{'model'} = $e->{'model'};
270                                                 $newsettings{'lens'} = defined($e->{'lens'}) ? $e->{'lens'} : '';
271                                                 $newsettings{'start'} = 1;
272                                         }
273                                         
274                                         my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
275
276                                         # This isn't correct for all languages. Fix if we ever need to care. :-)
277                                         if ($e->{'num'} == 1) {
278                                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-item-singular", { eqspec => $eqspec, filterurl => $url, action => $action });
279                                         } else {
280                                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
281                                         }
282                                 }
283                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-end");
284                         }
285                 }
286
287                 my $toclose = 0;
288                 my $lastupl = "";
289                 my $img_num = (defined($start) && defined($num)) ? $start : 1;
290                 
291                 # Print out all thumbnails
292                 if ($rot == 1) {
293                         $io->print("    <form method=\"post\" action=\"/rotate\">\n");
294                         $io->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
295                 }
296
297                 while (my $ref = $q->fetchrow_hashref()) {
298                         my $imgsz = "";
299                         my $takenby = $ref->{'takenby'};
300                         my $day = '';
301                         if (defined($ref->{'day'})) {
302                                 $day = ", " . $ref->{'day'};
303                         }
304
305                         my $groupkey = $takenby . $day;
306
307                         if ($groupkey ne $lastupl) {
308                                 $io->print("    </p>\n\n") if ($lastupl ne "" && $rot != 1);
309                                 $lastupl = $groupkey;
310
311                                 my %newsettings = %settings;
312
313                                 my $action;
314                                 if (defined($author)) {
315                                         chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
316                                         $newsettings{'author'} = undef;
317                                         $newsettings{'start'} = 1;
318                                 } else {
319                                         chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
320                                         $newsettings{'author'} = $ref->{'takenby'};
321                                         $newsettings{'start'} = 1;
322                                 }
323
324                                 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
325                                 
326                                 $io->print("    <h2>");
327                                 Sesse::pr0n::Templates::print_template($r, $io, "submittedby", { author => $takenby, action => $action, filterurl => $url, date => $day });
328                                 print_fullscreen_fromhere($r, $io, $event, \%settings, \%defsettings, $img_num);
329                                 $io->print("</h2>\n");
330
331                                 if ($rot != 1) {
332                                         $io->print("    <p class=\"photos\">\n");
333                                 }
334                         }
335
336                         if (defined($ref->{'width'}) && defined($ref->{'height'})) {
337                                 my $width = $ref->{'width'};
338                                 my $height = $ref->{'height'};
339                                         
340                                 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
341                                 $imgsz = " width=\"$width\" height=\"$height\"";
342                         }
343
344                         my $filename = $ref->{'filename'};
345                         my $uri = $filename;
346                         if (defined($xres) && defined($yres) && $xres != -1 && $xres != -2) {
347                                 $uri = "${xres}x$yres/$filename";
348                         } elsif (defined($xres) && $xres == -1) {
349                                 $uri = "original/$filename";
350                         }
351                         
352                         my $prefix = "";
353                         if ($abspath) {
354                                 $prefix = "/" . $ref->{'event'} . "/";
355                         }
356                 
357                         if ($rot == 1) {        
358                                 $io->print("    <p>");
359                         } else {
360                                 $io->print("     ");
361                         }
362                         $io->print("<a href=\"$prefix$uri\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
363                 
364                         if ($rot == 1) {
365                                 $io->print("      90 <input type=\"checkbox\" name=\"rot-" .
366                                         $ref->{'id'} . "-90\" />\n");
367                                 $io->print("      180 <input type=\"checkbox\" name=\"rot-" .
368                                         $ref->{'id'} . "-180\" />\n");
369                                 $io->print("      270 <input type=\"checkbox\" name=\"rot-" .
370                                         $ref->{'id'} . "-270\" />\n");
371                                 $io->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
372                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
373                         }
374                         
375                         ++$img_num;
376                 }
377
378                 if ($rot == 1) {
379                         $io->print("      <input type=\"submit\" value=\"Rotate\" />\n");
380                         $io->print("    </form>\n");
381                 } else {
382                         $io->print("    </p>\n");
383                 }
384
385                 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
386                 Sesse::pr0n::Common::footer($r, $io);
387         }
388
389         $io->setpos(0);
390         $res->body($io);
391         return $res;
392 }
393
394 sub eq_with_undef {
395         my ($a, $b) = @_;
396         
397         return 1 if (!defined($a) && !defined($b));
398         return 0 unless (defined($a) && defined($b));
399         return ($a eq $b);
400 }
401
402 sub print_changes {
403         my ($r, $io, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
404
405         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
406         chomp $title;
407         $io->print("    <p>$title:\n");
408
409         for my $a (@$alternatives) {
410                 my $text;
411                 my %newsettings = %$settings;
412
413                 if (ref $a) {
414                         my ($v1, $v2);
415                         ($text, $v1, $v2) = @$a;
416                         
417                         $newsettings{$var1} = $v1;
418                         $newsettings{$var2} = $v2;
419                 } else {
420                         $text = $a;
421
422                         # Parse the current alternative
423                         my ($v1, $v2) = split /x/, $a;
424
425                         $newsettings{$var1} = $v1;
426                         $newsettings{$var2} = $v2;
427                 }
428
429                 $io->print("      ");
430
431                 # Check if these settings are current (print only label)
432                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
433                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
434                         $io->print($text);
435                 } else {
436                         Sesse::pr0n::Common::print_link($io, $text, "/$event/", \%newsettings, $defsettings);
437                 }
438                 $io->print("\n");
439         }
440         $io->print("    </p>\n");
441 }
442
443 sub print_viewres {
444         my ($r, $io, $event, $settings, $defsettings) = @_;
445         my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1152x864 1280x960 1400x1050 1600x1200 1920x1440 2048x1536 2304x1728);
446         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
447         chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
448         push @alternatives, [ $unlimited, -2, -2 ];
449         push @alternatives, [ $original, -1, -1 ];
450
451         print_changes($r, $io, $event, 'viewres', $settings, $defsettings,
452                       'xres', 'yres', \@alternatives);
453 }
454
455 sub print_nextprev {
456         my ($r, $io, $event, $where, $settings, $defsettings) = @_;
457         my $start = $settings->{'start'};
458         my $num = $settings->{'num'};
459         my $dbh = Sesse::pr0n::Common::get_dbh();
460
461         $num = undef if (defined($num) && $num == -1);
462         return unless (defined($start) && defined($num));
463
464         # determine total number
465         my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where",
466                 undef, Sesse::pr0n::Common::get_server_name($r))
467                 or return dberror($r, "image enumeration");
468         my $num_images = $ref->{'num_images'};
469
470         return if ($start == 1 && $start + $num >= $num_images);
471
472         my $end = $start + $num - 1;
473         if ($end > $num_images) {
474                 $end = $num_images;
475         }
476
477         $io->print("    <p class=\"nextprev\">\n");
478
479         # Previous
480         if ($start > 1) {
481                 my $newstart = $start - $num;
482                 if ($newstart < 1) {
483                         $newstart = 1;
484                 }
485                 my $newend = $newstart + $num - 1;
486                 if ($newend > $num_images) {
487                         $newend = $num_images;
488                 }
489
490                 my %newsettings = %$settings;
491                 $newsettings{'start'} = $newstart;
492                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
493                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
494                 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
495         }
496
497         # This
498         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
499         $io->print("    $title ($start-$end)\n");
500
501         # Next
502         if ($end < $num_images) {
503                 my $newstart = $start + $num;
504                 my $newend = $newstart + $num - 1;
505                 if ($newend > $num_images) {
506                         $newend = $num_images;
507                 }
508                 
509                 my %newsettings = %$settings;
510                 $newsettings{'start'} = $newstart;
511                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
512                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
513                 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
514         }
515
516         $io->print("    </p>\n");
517 }
518
519 sub print_selected {
520         my ($r, $io, $event, $settings, $defsettings) = @_;
521
522         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
523         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
524         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
525
526         $io->print("    <p>$title:\n");
527
528         my %newsettings = %$settings;
529
530         if ($settings->{'all'} == 0) {
531                 $io->print($sel);
532         } else {
533                 $newsettings{'all'} = 0;
534                 Sesse::pr0n::Common::print_link($io, $sel, "/$event/", \%newsettings, $defsettings);
535         }
536
537         $io->print(' ');
538
539         if ($settings->{'all'} == 1) {
540                 $io->print($all);
541         } else {
542                 $newsettings{'all'} = 1;
543                 Sesse::pr0n::Common::print_link($io, $all, "/$event/", \%newsettings, $defsettings);
544         }
545         
546         $io->print('</p>');
547 }
548
549 sub print_fullscreen {
550         my ($r, $io, $event, $settings, $defsettings) = @_;
551
552         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
553
554         my %newsettings = %$settings;
555         $newsettings{'fullscreen'} = 1;
556
557         $io->print("    <p>");
558         Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
559         $io->print("</p>\n");
560 }
561
562 sub print_fullscreen_fromhere {
563         my ($r, $io, $event, $settings, $defsettings, $start) = @_;
564
565         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
566
567         my %newsettings = %$settings;
568         $newsettings{'fullscreen'} = 1;
569         $newsettings{'start'} = $start;
570
571         $io->print("    <span class=\"fsfromhere\">");
572         Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
573         $io->print("</span>\n");
574 }
575         
576 1;
577
578