Hide the viewing resolution choice.
[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_selected($r, $io, $event, \%settings, \%defsettings) if ($num_selected > 0);
216                 print_fullscreen($r, $io, $event, \%settings, \%defsettings);
217                 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
218         
219                 if (1 || $event ne '+all') {
220                         # Find the equipment used
221                         my $eq = $dbh->prepare("
222                                 SELECT
223                                         model,
224                                         lens,
225                                         COUNT(*) AS num
226                                 FROM images
227                                 WHERE vhost=? $where
228                                 GROUP BY 1,2
229                                 ORDER BY 1,2")
230                                 or die "Couldn't prepare to find equipment: $!";
231                         $eq->execute(Sesse::pr0n::Common::get_server_name($r))
232                                 or die "Couldn't find equipment: $!";
233
234                         my @equipment = ();
235                         my %cameras_seen = ();
236                         while (my $ref = $eq->fetchrow_hashref) {
237                                 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
238                                         #
239                                         # Some compact cameras seem to add lens info sometimes and not at other
240                                         # times; if we have seen a camera with at least one specific lens earlier,
241                                         # just combine entries without a lens with the previous one.
242                                         #
243                                         $equipment[$#equipment]->{'num'} += $ref->{'num'};
244                                         next;
245                                 }
246                                 push @equipment, $ref;
247                                 $cameras_seen{$ref->{'model'}} = 1;
248                         }
249                         $eq->finish;
250
251                         if (scalar @equipment > 0) {
252                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-start");
253                                 for my $e (@equipment) {
254                                         my $eqspec = $e->{'model'};
255                                         $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
256                                         $eqspec = HTML::Entities::encode_entities($eqspec);
257
258                                         my %newsettings = %settings;
259
260                                         my $action;
261                                         if (defined($model) && defined($lens)) {
262                                                 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
263                                                 $newsettings{'model'} = undef;
264                                                 $newsettings{'lens'} = undef;
265                                                 $newsettings{'start'} = 1;
266                                         } else {
267                                                 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
268                                                 $newsettings{'model'} = $e->{'model'};
269                                                 $newsettings{'lens'} = defined($e->{'lens'}) ? $e->{'lens'} : '';
270                                                 $newsettings{'start'} = 1;
271                                         }
272                                         
273                                         my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
274
275                                         # This isn't correct for all languages. Fix if we ever need to care. :-)
276                                         if ($e->{'num'} == 1) {
277                                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-item-singular", { eqspec => $eqspec, filterurl => $url, action => $action });
278                                         } else {
279                                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
280                                         }
281                                 }
282                                 Sesse::pr0n::Templates::print_template($r, $io, "equipment-end");
283                         }
284                 }
285
286                 my $toclose = 0;
287                 my $lastupl = "";
288                 my $img_num = (defined($start) && defined($num)) ? $start : 1;
289                 
290                 # Print out all thumbnails
291                 if ($rot == 1) {
292                         $io->print("    <form method=\"post\" action=\"/rotate\">\n");
293                         $io->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
294                 }
295
296                 while (my $ref = $q->fetchrow_hashref()) {
297                         my $imgsz = "";
298                         my $takenby = $ref->{'takenby'};
299                         my $day = '';
300                         if (defined($ref->{'day'})) {
301                                 $day = ", " . $ref->{'day'};
302                         }
303
304                         my $groupkey = $takenby . $day;
305
306                         if ($groupkey ne $lastupl) {
307                                 $io->print("    </p>\n\n") if ($lastupl ne "" && $rot != 1);
308                                 $lastupl = $groupkey;
309
310                                 my %newsettings = %settings;
311
312                                 my $action;
313                                 if (defined($author)) {
314                                         chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
315                                         $newsettings{'author'} = undef;
316                                         $newsettings{'start'} = 1;
317                                 } else {
318                                         chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
319                                         $newsettings{'author'} = $ref->{'takenby'};
320                                         $newsettings{'start'} = 1;
321                                 }
322
323                                 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
324                                 
325                                 $io->print("    <h2>");
326                                 Sesse::pr0n::Templates::print_template($r, $io, "submittedby", { author => $takenby, action => $action, filterurl => $url, date => $day });
327                                 print_fullscreen_fromhere($r, $io, $event, \%settings, \%defsettings, $img_num);
328                                 $io->print("</h2>\n");
329
330                                 if ($rot != 1) {
331                                         $io->print("    <p class=\"photos\">\n");
332                                 }
333                         }
334
335                         if (defined($ref->{'width'}) && defined($ref->{'height'})) {
336                                 my $width = $ref->{'width'};
337                                 my $height = $ref->{'height'};
338                                         
339                                 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
340                                 $imgsz = " width=\"$width\" height=\"$height\"";
341                         }
342
343                         my $filename = $ref->{'filename'};
344                         my $uri = $filename;
345                         if (defined($xres) && defined($yres) && $xres != -1 && $xres != -2) {
346                                 $uri = "${xres}x$yres/$filename";
347                         } elsif (defined($xres) && $xres == -1) {
348                                 $uri = "original/$filename";
349                         }
350                         
351                         my $prefix = "";
352                         if ($abspath) {
353                                 $prefix = "/" . $ref->{'event'} . "/";
354                         }
355                 
356                         if ($rot == 1) {        
357                                 $io->print("    <p>");
358                         } else {
359                                 $io->print("     ");
360                         }
361                         $io->print("<a href=\"$prefix$uri\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
362                 
363                         if ($rot == 1) {
364                                 $io->print("      90 <input type=\"checkbox\" name=\"rot-" .
365                                         $ref->{'id'} . "-90\" />\n");
366                                 $io->print("      180 <input type=\"checkbox\" name=\"rot-" .
367                                         $ref->{'id'} . "-180\" />\n");
368                                 $io->print("      270 <input type=\"checkbox\" name=\"rot-" .
369                                         $ref->{'id'} . "-270\" />\n");
370                                 $io->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
371                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
372                         }
373                         
374                         ++$img_num;
375                 }
376
377                 if ($rot == 1) {
378                         $io->print("      <input type=\"submit\" value=\"Rotate\" />\n");
379                         $io->print("    </form>\n");
380                 } else {
381                         $io->print("    </p>\n");
382                 }
383
384                 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
385                 Sesse::pr0n::Common::footer($r, $io);
386         }
387
388         $io->setpos(0);
389         $res->body($io);
390         return $res;
391 }
392
393 sub eq_with_undef {
394         my ($a, $b) = @_;
395         
396         return 1 if (!defined($a) && !defined($b));
397         return 0 unless (defined($a) && defined($b));
398         return ($a eq $b);
399 }
400
401 sub print_changes {
402         my ($r, $io, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
403
404         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
405         chomp $title;
406         $io->print("    <p>$title:\n");
407
408         for my $a (@$alternatives) {
409                 my $text;
410                 my %newsettings = %$settings;
411
412                 if (ref $a) {
413                         my ($v1, $v2);
414                         ($text, $v1, $v2) = @$a;
415                         
416                         $newsettings{$var1} = $v1;
417                         $newsettings{$var2} = $v2;
418                 } else {
419                         $text = $a;
420
421                         # Parse the current alternative
422                         my ($v1, $v2) = split /x/, $a;
423
424                         $newsettings{$var1} = $v1;
425                         $newsettings{$var2} = $v2;
426                 }
427
428                 $io->print("      ");
429
430                 # Check if these settings are current (print only label)
431                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
432                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
433                         $io->print($text);
434                 } else {
435                         Sesse::pr0n::Common::print_link($io, $text, "/$event/", \%newsettings, $defsettings);
436                 }
437                 $io->print("\n");
438         }
439         $io->print("    </p>\n");
440 }
441
442 sub print_nextprev {
443         my ($r, $io, $event, $where, $settings, $defsettings) = @_;
444         my $start = $settings->{'start'};
445         my $num = $settings->{'num'};
446         my $dbh = Sesse::pr0n::Common::get_dbh();
447
448         $num = undef if (defined($num) && $num == -1);
449         return unless (defined($start) && defined($num));
450
451         # determine total number
452         my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where",
453                 undef, Sesse::pr0n::Common::get_server_name($r))
454                 or return dberror($r, "image enumeration");
455         my $num_images = $ref->{'num_images'};
456
457         return if ($start == 1 && $start + $num >= $num_images);
458
459         my $end = $start + $num - 1;
460         if ($end > $num_images) {
461                 $end = $num_images;
462         }
463
464         $io->print("    <p class=\"nextprev\">\n");
465
466         # Previous
467         if ($start > 1) {
468                 my $newstart = $start - $num;
469                 if ($newstart < 1) {
470                         $newstart = 1;
471                 }
472                 my $newend = $newstart + $num - 1;
473                 if ($newend > $num_images) {
474                         $newend = $num_images;
475                 }
476
477                 my %newsettings = %$settings;
478                 $newsettings{'start'} = $newstart;
479                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
480                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
481                 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
482         }
483
484         # This
485         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
486         $io->print("    $title ($start-$end)\n");
487
488         # Next
489         if ($end < $num_images) {
490                 my $newstart = $start + $num;
491                 my $newend = $newstart + $num - 1;
492                 if ($newend > $num_images) {
493                         $newend = $num_images;
494                 }
495                 
496                 my %newsettings = %$settings;
497                 $newsettings{'start'} = $newstart;
498                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
499                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
500                 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
501         }
502
503         $io->print("    </p>\n");
504 }
505
506 sub print_selected {
507         my ($r, $io, $event, $settings, $defsettings) = @_;
508
509         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
510         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
511         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
512
513         $io->print("    <p>$title:\n");
514
515         my %newsettings = %$settings;
516
517         if ($settings->{'all'} == 0) {
518                 $io->print($sel);
519         } else {
520                 $newsettings{'all'} = 0;
521                 Sesse::pr0n::Common::print_link($io, $sel, "/$event/", \%newsettings, $defsettings);
522         }
523
524         $io->print(' ');
525
526         if ($settings->{'all'} == 1) {
527                 $io->print($all);
528         } else {
529                 $newsettings{'all'} = 1;
530                 Sesse::pr0n::Common::print_link($io, $all, "/$event/", \%newsettings, $defsettings);
531         }
532         
533         $io->print('</p>');
534 }
535
536 sub print_fullscreen {
537         my ($r, $io, $event, $settings, $defsettings) = @_;
538
539         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
540
541         my %newsettings = %$settings;
542         $newsettings{'fullscreen'} = 1;
543
544         $io->print("    <p>");
545         Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
546         $io->print("</p>\n");
547 }
548
549 sub print_fullscreen_fromhere {
550         my ($r, $io, $event, $settings, $defsettings, $start) = @_;
551
552         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
553
554         my %newsettings = %$settings;
555         $newsettings{'fullscreen'} = 1;
556         $newsettings{'start'} = $start;
557
558         $io->print("    <span class=\"fsfromhere\">");
559         Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
560         $io->print("</span>\n");
561 }
562         
563 1;
564
565