]> git.sesse.net Git - pr0n/blob - perl/Sesse/pr0n/Index.pm
When closing fullscreen mode, go back to where we clicked from.
[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                         # Add fullscreen link.
344                         my %fssettings = %settings;
345                         $fssettings{'fullscreen'} = 1;
346                         my $fsquery = Sesse::pr0n::Common::get_query_string(\%fssettings, \%defsettings);
347                         $fsquery .= '#' . $img_num;
348
349                         my $filename = $ref->{'filename'};
350                         my $uri = $filename;
351                         if (defined($xres) && defined($yres) && $xres != -1 && $xres != -2) {
352                                 $uri = "${xres}x$yres/$filename";
353                         } elsif (defined($xres) && $xres == -1) {
354                                 $uri = "original/$filename";
355                         }
356                         
357                         my $prefix = "";
358                         if ($abspath) {
359                                 $prefix = "/" . $ref->{'event'} . "/";
360                         }
361                 
362                         if ($rot == 1) {        
363                                 $io->print("    <p>");
364                         } else {
365                                 $io->print("     ");
366                         }
367                         $io->print("<a href=\"$prefix$uri\" id=\"$img_num\" onclick=\"location.href='$prefix$fsquery';return false;\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
368                 
369                         if ($rot == 1) {
370                                 $io->print("      90 <input type=\"checkbox\" name=\"rot-" .
371                                         $ref->{'id'} . "-90\" />\n");
372                                 $io->print("      180 <input type=\"checkbox\" name=\"rot-" .
373                                         $ref->{'id'} . "-180\" />\n");
374                                 $io->print("      270 <input type=\"checkbox\" name=\"rot-" .
375                                         $ref->{'id'} . "-270\" />\n");
376                                 $io->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
377                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
378                         }
379                         
380                         ++$img_num;
381                 }
382
383                 if ($rot == 1) {
384                         $io->print("      <input type=\"submit\" value=\"Rotate\" />\n");
385                         $io->print("    </form>\n");
386                 } else {
387                         $io->print("    </p>\n");
388                 }
389
390                 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
391                 Sesse::pr0n::Common::footer($r, $io);
392         }
393
394         $io->setpos(0);
395         $res->body($io);
396         return $res;
397 }
398
399 sub eq_with_undef {
400         my ($a, $b) = @_;
401         
402         return 1 if (!defined($a) && !defined($b));
403         return 0 unless (defined($a) && defined($b));
404         return ($a eq $b);
405 }
406
407 sub print_changes {
408         my ($r, $io, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
409
410         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
411         chomp $title;
412         $io->print("    <p>$title:\n");
413
414         for my $a (@$alternatives) {
415                 my $text;
416                 my %newsettings = %$settings;
417
418                 if (ref $a) {
419                         my ($v1, $v2);
420                         ($text, $v1, $v2) = @$a;
421                         
422                         $newsettings{$var1} = $v1;
423                         $newsettings{$var2} = $v2;
424                 } else {
425                         $text = $a;
426
427                         # Parse the current alternative
428                         my ($v1, $v2) = split /x/, $a;
429
430                         $newsettings{$var1} = $v1;
431                         $newsettings{$var2} = $v2;
432                 }
433
434                 $io->print("      ");
435
436                 # Check if these settings are current (print only label)
437                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
438                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
439                         $io->print($text);
440                 } else {
441                         Sesse::pr0n::Common::print_link($io, $text, "/$event/", \%newsettings, $defsettings);
442                 }
443                 $io->print("\n");
444         }
445         $io->print("    </p>\n");
446 }
447
448 sub print_nextprev {
449         my ($r, $io, $event, $where, $settings, $defsettings) = @_;
450         my $start = $settings->{'start'};
451         my $num = $settings->{'num'};
452         my $dbh = Sesse::pr0n::Common::get_dbh();
453
454         $num = undef if (defined($num) && $num == -1);
455         return unless (defined($start) && defined($num));
456
457         # determine total number
458         my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where",
459                 undef, Sesse::pr0n::Common::get_server_name($r))
460                 or return dberror($r, "image enumeration");
461         my $num_images = $ref->{'num_images'};
462
463         return if ($start == 1 && $start + $num >= $num_images);
464
465         my $end = $start + $num - 1;
466         if ($end > $num_images) {
467                 $end = $num_images;
468         }
469
470         $io->print("    <p class=\"nextprev\">\n");
471
472         # Previous
473         if ($start > 1) {
474                 my $newstart = $start - $num;
475                 if ($newstart < 1) {
476                         $newstart = 1;
477                 }
478                 my $newend = $newstart + $num - 1;
479                 if ($newend > $num_images) {
480                         $newend = $num_images;
481                 }
482
483                 my %newsettings = %$settings;
484                 $newsettings{'start'} = $newstart;
485                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
486                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
487                 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
488         }
489
490         # This
491         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
492         $io->print("    $title ($start-$end)\n");
493
494         # Next
495         if ($end < $num_images) {
496                 my $newstart = $start + $num;
497                 my $newend = $newstart + $num - 1;
498                 if ($newend > $num_images) {
499                         $newend = $num_images;
500                 }
501                 
502                 my %newsettings = %$settings;
503                 $newsettings{'start'} = $newstart;
504                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
505                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
506                 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
507         }
508
509         $io->print("    </p>\n");
510 }
511
512 sub print_selected {
513         my ($r, $io, $event, $settings, $defsettings) = @_;
514
515         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
516         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
517         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
518
519         $io->print("    <p>$title:\n");
520
521         my %newsettings = %$settings;
522
523         if ($settings->{'all'} == 0) {
524                 $io->print($sel);
525         } else {
526                 $newsettings{'all'} = 0;
527                 Sesse::pr0n::Common::print_link($io, $sel, "/$event/", \%newsettings, $defsettings);
528         }
529
530         $io->print(' ');
531
532         if ($settings->{'all'} == 1) {
533                 $io->print($all);
534         } else {
535                 $newsettings{'all'} = 1;
536                 Sesse::pr0n::Common::print_link($io, $all, "/$event/", \%newsettings, $defsettings);
537         }
538         
539         $io->print('</p>');
540 }
541
542 sub print_fullscreen {
543         my ($r, $io, $event, $settings, $defsettings) = @_;
544
545         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
546
547         my %newsettings = %$settings;
548         $newsettings{'fullscreen'} = 1;
549
550         $io->print("    <p>");
551         Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
552         $io->print("</p>\n");
553 }
554
555 sub print_fullscreen_fromhere {
556         my ($r, $io, $event, $settings, $defsettings, $start) = @_;
557
558         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
559
560         my %newsettings = %$settings;
561         $newsettings{'fullscreen'} = 1;
562         $newsettings{'start'} = $start;
563
564         $io->print("    <span class=\"fsfromhere\">");
565         Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
566         $io->print("</span>\n");
567 }
568         
569 1;
570
571