1 package Sesse::pr0n::Index;
5 use Sesse::pr0n::Common qw(error dberror);
10 my $dbh = Sesse::pr0n::Common::get_dbh();
12 my ($event, $abspath, $datesort);
13 if ($r->path_info =~ /^\/\+all\/?/) {
17 $datesort = 'DESC NULLS LAST';
20 $r->path_info =~ /^\/([a-zA-Z0-9-]+)\/?$/
21 or return error($r, "Could not extract event");
24 $datesort = 'ASC NULLS LAST';
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 . "/");
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));
42 # Read the appropriate settings from the query string into the settings hash
60 if ($event eq '+all') {
63 $where = ' AND event=' . $dbh->quote($event);
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;
71 my %settings = %defsettings;
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+)$/) {
78 if ($s eq "num" && defined($val) && $val == -1) {
81 if (($s eq "xres" || $s eq "yres") && defined($val) && ($val == -1 || $val == -2)) {
84 if (($s eq "model" || $s eq "lens" || $s eq "author") && defined($val)) {
85 $settings{$s} = Sesse::pr0n::Common::pretty_unescape($val);
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'};
102 # Construct SQL for this filter
104 $where .= ' AND selected=\'t\'';
106 if (defined($model) && defined($lens)) {
107 my $mq = $dbh->quote($model);
108 my $lq = $dbh->quote($lens);
112 $where .= " AND model IS NULL";
114 $where .= " AND model=$mq";
119 $where .= " AND lens IS NULL";
121 $where .= " AND lens=$lq";
124 if (defined($author)) {
125 my $aq = $dbh->quote($author);
127 $where .= " AND takenby=$aq";
130 if (defined($num) && $num == -1) {
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");
141 $name = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
142 Sesse::pr0n::Common::set_last_modified($r, $ref->{'last_update'});
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");
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'});
153 # # If the client can use cache, do so
154 # if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
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'};
162 # Find all images related to this event.
163 my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : "";
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");
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");
176 Sesse::pr0n::Templates::print_template($r, $io, "fullscreen-header", { title => "$name [$event]" });
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 ];
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");
191 my %settings_no_fullscreen = %settings;
192 $settings_no_fullscreen{'fullscreen'} = 0;
194 my $returnurl = "http://" . Sesse::pr0n::Common::get_server_name($r) . "/" . $event . "/" .
195 Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
198 $returnurl =~ s/&/&/g;
200 Sesse::pr0n::Templates::print_template($r, $io, "fullscreen-footer", {
201 returnurl => $returnurl,
202 start => $settings{'start'} - 1,
203 sel => $settings{'sel'}
206 Sesse::pr0n::Common::header($r, $io, "$name [$event]");
207 if (defined($date)) {
208 Sesse::pr0n::Templates::print_template($r, $io, "date", { date => $date });
211 if (Sesse::pr0n::Overload::is_in_overload($r)) {
212 Sesse::pr0n::Templates::print_template($r, $io, "overloadmode");
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);
219 if (1 || $event ne '+all') {
220 # Find the equipment used
221 my $eq = $dbh->prepare("
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: $!";
235 my %cameras_seen = ();
236 while (my $ref = $eq->fetchrow_hashref) {
237 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
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.
243 $equipment[$#equipment]->{'num'} += $ref->{'num'};
246 push @equipment, $ref;
247 $cameras_seen{$ref->{'model'}} = 1;
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);
258 my %newsettings = %settings;
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;
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;
273 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
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 });
279 Sesse::pr0n::Templates::print_template($r, $io, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
282 Sesse::pr0n::Templates::print_template($r, $io, "equipment-end");
288 my $img_num = (defined($start) && defined($num)) ? $start : 1;
290 # Print out all thumbnails
292 $io->print(" <form method=\"post\" action=\"/rotate\">\n");
293 $io->print(" <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
296 while (my $ref = $q->fetchrow_hashref()) {
298 my $takenby = $ref->{'takenby'};
300 if (defined($ref->{'day'})) {
301 $day = ", " . $ref->{'day'};
304 my $groupkey = $takenby . $day;
306 if ($groupkey ne $lastupl) {
307 $io->print(" </p>\n\n") if ($lastupl ne "" && $rot != 1);
308 $lastupl = $groupkey;
310 my %newsettings = %settings;
313 if (defined($author)) {
314 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
315 $newsettings{'author'} = undef;
316 $newsettings{'start'} = 1;
318 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
319 $newsettings{'author'} = $ref->{'takenby'};
320 $newsettings{'start'} = 1;
323 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
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");
331 $io->print(" <p class=\"photos\">\n");
335 if (defined($ref->{'width'}) && defined($ref->{'height'})) {
336 my $width = $ref->{'width'};
337 my $height = $ref->{'height'};
339 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
340 $imgsz = " width=\"$width\" height=\"$height\"";
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;
349 my $filename = $ref->{'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";
359 $prefix = "/" . $ref->{'event'} . "/";
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");
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(" " .
377 " Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
384 $io->print(" <input type=\"submit\" value=\"Rotate\" />\n");
385 $io->print(" </form>\n");
387 $io->print(" </p>\n");
390 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
391 Sesse::pr0n::Common::footer($r, $io);
402 return 1 if (!defined($a) && !defined($b));
403 return 0 unless (defined($a) && defined($b));
408 my ($r, $io, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
410 my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
412 $io->print(" <p>$title:\n");
414 for my $a (@$alternatives) {
416 my %newsettings = %$settings;
420 ($text, $v1, $v2) = @$a;
422 $newsettings{$var1} = $v1;
423 $newsettings{$var2} = $v2;
427 # Parse the current alternative
428 my ($v1, $v2) = split /x/, $a;
430 $newsettings{$var1} = $v1;
431 $newsettings{$var2} = $v2;
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})) {
441 Sesse::pr0n::Common::print_link($io, $text, "/$event/", \%newsettings, $defsettings);
445 $io->print(" </p>\n");
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();
454 $num = undef if (defined($num) && $num == -1);
455 return unless (defined($start) && defined($num));
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'};
463 return if ($start == 1 && $start + $num >= $num_images);
465 my $end = $start + $num - 1;
466 if ($end > $num_images) {
470 $io->print(" <p class=\"nextprev\">\n");
474 my $newstart = $start - $num;
478 my $newend = $newstart + $num - 1;
479 if ($newend > $num_images) {
480 $newend = $num_images;
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);
491 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
492 $io->print(" $title ($start-$end)\n");
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;
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);
509 $io->print(" </p>\n");
513 my ($r, $io, $event, $settings, $defsettings) = @_;
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'));
519 $io->print(" <p>$title:\n");
521 my %newsettings = %$settings;
523 if ($settings->{'all'} == 0) {
526 $newsettings{'all'} = 0;
527 Sesse::pr0n::Common::print_link($io, $sel, "/$event/", \%newsettings, $defsettings);
532 if ($settings->{'all'} == 1) {
535 $newsettings{'all'} = 1;
536 Sesse::pr0n::Common::print_link($io, $all, "/$event/", \%newsettings, $defsettings);
542 sub print_fullscreen {
543 my ($r, $io, $event, $settings, $defsettings) = @_;
545 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
547 my %newsettings = %$settings;
548 $newsettings{'fullscreen'} = 1;
551 Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
552 $io->print("</p>\n");
555 sub print_fullscreen_fromhere {
556 my ($r, $io, $event, $settings, $defsettings, $start) = @_;
558 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
560 my %newsettings = %$settings;
561 $newsettings{'fullscreen'} = 1;
562 $newsettings{'start'} = $start;
564 $io->print(" <span class=\"fsfromhere\">");
565 Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
566 $io->print("</span>\n");