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