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') {
61 $where = ' AND (event,vhost) IN ( SELECT event,vhost FROM events WHERE NOT hidden )';
63 $where = ' AND event=' . $dbh->quote($event);
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;
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 @authors = split /,/, $author;
126 $where .= " AND takenby IN (" . join(', ', map { $dbh->quote($_) } @authors) . ")";
129 if (defined($num) && $num == -1) {
135 if ($event eq '+all') {
136 $ref = $dbh->selectrow_hashref("SELECT EXTRACT(EPOCH FROM MAX(last_update)) AS last_update FROM last_picture_cache WHERE vhost=?",
137 undef, Sesse::pr0n::Common::get_server_name($r))
138 or return error($r, "Could not list events", 404, "File not found");
140 $name = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
141 Sesse::pr0n::Common::set_last_modified($r, $ref->{'last_update'});
143 $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=?",
144 undef, Sesse::pr0n::Common::get_server_name($r), $event)
145 or return error($r, "Could not find event $event", 404, "File not found");
147 $date = HTML::Entities::encode_entities($ref->{'date'});
148 $name = HTML::Entities::encode_entities($ref->{'name'});
149 Sesse::pr0n::Common::set_last_modified($r, $ref->{'last_update'});
152 # # If the client can use cache, do so
153 # if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
157 # Count the number of selected images.
158 $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));
159 my $num_selected = $ref->{'num_selected'};
161 # Find all images related to this event.
162 my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : "";
164 my $extra_joins = "";
165 my $extra_fields = "";
166 if ($settings{'fullscreen'}) {
167 $extra_joins = <<"EOF";
168 LEFT JOIN exif_info exif_prog ON images.id=exif_prog.image AND exif_prog.key = 'ExposureProgram'
169 LEFT JOIN exif_info exif_focal ON images.id=exif_focal.image AND exif_focal.key = 'FocalLength'
170 LEFT JOIN exif_info exif_shutter ON images.id=exif_shutter.image AND exif_shutter.key = 'ExposureTime'
171 LEFT JOIN exif_info exif_fnum ON images.id=exif_fnum.image AND exif_fnum.key = 'FNumber'
172 LEFT JOIN exif_info exif_iso1 ON images.id=exif_iso1.image AND exif_iso1.key = 'ISO'
173 LEFT JOIN exif_info exif_iso2 ON images.id=exif_iso2.image AND exif_iso2.key = 'ISOSetting'
174 LEFT JOIN exif_info exif_ev1 ON images.id=exif_ev1.image AND exif_ev1.key = 'ExposureBiasValue'
175 LEFT JOIN exif_info exif_ev2 ON images.id=exif_ev2.image AND exif_ev2.key = 'ExposureCompensation'
176 LEFT JOIN exif_info exif_date ON images.id=exif_date.image AND exif_date.key = 'DateTimeOriginal'
177 LEFT JOIN exif_info exif_model ON images.id=exif_model.image AND exif_model.key = 'Model'
178 LEFT JOIN exif_info exif_flash ON images.id=exif_flash.image AND exif_flash.key = 'Flash'
180 $extra_fields = <<"EOF";
181 exif_prog.value AS "ExposureProgram",
182 exif_focal.value AS "FocalLength",
183 exif_shutter.value AS "ExposureTime",
184 exif_fnum.value AS "FNumber",
185 exif_iso1.value AS "ISO",
186 exif_iso2.value AS "ISOSetting",
187 exif_ev1.value AS "ExposureBiasValue",
188 exif_ev2.value AS "ExposureCompensation",
189 exif_date.value AS "DateTimeOriginal",
190 exif_model.value AS "Model",
191 exif_flash.value AS "Flash",
195 my $q = $dbh->prepare(<<"EOF")
198 (date - INTERVAL '6 hours')::date AS day
206 ORDER BY (date - INTERVAL '6 hours')::date $datesort,takenby,date,filename
209 or return dberror($r, "prepare()");
210 $q->execute(Sesse::pr0n::Common::get_server_name($r))
211 or return dberror($r, "image enumeration");
213 # Print the page itself
214 my $res = Plack::Response->new(200);
215 my $io = IO::String->new;
216 if ($settings{'fullscreen'}) {
217 $res->content_type("text/html; charset=utf-8");
219 Sesse::pr0n::Templates::print_template($r, $io, "fullscreen-header", { title => "$name [$event]" });
222 while (my $ref = $q->fetchrow_hashref()) {
223 my $width = defined($ref->{'width'}) ? $ref->{'width'} : -1;
224 my $height = defined($ref->{'height'}) ? $ref->{'height'} : -1;
225 my @parts = Sesse::pr0n::Common::make_infobox_parts($ref);
226 @parts = map { $_->[1] ? "<strong>" . HTML::Entities::encode_entities($_->[0]) . "</strong>" : HTML::Entities::encode_entities($_->[0]) } @parts;
227 push @files, [ $ref->{'event'}, $ref->{'filename'}, $width, $height, join('', @parts) ];
230 for my $i (0..$#files) {
231 my $line = sprintf " [ \"%s\", \"%s\", %d, %d, \"%s\" ]", @{$files[$i]};
232 $line .= "," unless ($i == $#files);
233 $io->print($line . "\n");
236 my %settings_no_fullscreen = %settings;
237 $settings_no_fullscreen{'fullscreen'} = 0;
239 my $returnurl = "http://" . Sesse::pr0n::Common::get_server_name($r) . "/" . $event . "/" .
240 Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
243 $returnurl =~ s/&/&/g;
245 Sesse::pr0n::Templates::print_template($r, $io, "fullscreen-footer", {
246 returnurl => $returnurl,
247 start => $settings{'start'} - 1,
248 sel => $settings{'sel'}
251 Sesse::pr0n::Common::header($r, $io, "$name [$event]");
252 if (defined($date)) {
253 Sesse::pr0n::Templates::print_template($r, $io, "date", { date => $date });
256 if (Sesse::pr0n::Overload::is_in_overload($r)) {
257 Sesse::pr0n::Templates::print_template($r, $io, "overloadmode");
260 print_selected($r, $io, $event, \%settings, \%defsettings) if ($num_selected > 0);
261 print_fullscreen($r, $io, $event, \%settings, \%defsettings);
262 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
264 if (1 || $event ne '+all') {
265 # Find the equipment used
266 my $eq = $dbh->prepare("
276 or die "Couldn't prepare to find equipment: $!";
277 $eq->execute(Sesse::pr0n::Common::get_server_name($r))
278 or die "Couldn't find equipment: $!";
281 my %cameras_seen = ();
282 while (my $ref = $eq->fetchrow_hashref) {
283 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
285 # Some compact cameras seem to add lens info sometimes and not at other
286 # times; if we have seen a camera with at least one specific lens earlier,
287 # just combine entries without a lens with the previous one.
289 $equipment[$#equipment]->{'num'} += $ref->{'num'};
292 push @equipment, $ref;
293 $cameras_seen{$ref->{'model'}} = 1;
297 if (scalar @equipment > 0) {
298 Sesse::pr0n::Templates::print_template($r, $io, "equipment-start");
299 for my $e (@equipment) {
300 my $eqspec = $e->{'model'};
301 $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
302 $eqspec = HTML::Entities::encode_entities($eqspec);
304 my %newsettings = %settings;
307 if (defined($model) && defined($lens)) {
308 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
309 $newsettings{'model'} = undef;
310 $newsettings{'lens'} = undef;
311 $newsettings{'start'} = 1;
313 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
314 $newsettings{'model'} = $e->{'model'};
315 $newsettings{'lens'} = defined($e->{'lens'}) ? $e->{'lens'} : '';
316 $newsettings{'start'} = 1;
319 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
321 # This isn't correct for all languages. Fix if we ever need to care. :-)
322 if ($e->{'num'} == 1) {
323 Sesse::pr0n::Templates::print_template($r, $io, "equipment-item-singular", { eqspec => $eqspec, filterurl => $url, action => $action });
325 Sesse::pr0n::Templates::print_template($r, $io, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
328 Sesse::pr0n::Templates::print_template($r, $io, "equipment-end");
334 my $img_num = (defined($start) && defined($num)) ? $start : 1;
336 # Print out all thumbnails
338 $io->print(" <form method=\"post\" action=\"/rotate\">\n");
339 $io->print(" <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
342 while (my $ref = $q->fetchrow_hashref()) {
344 my $takenby = $ref->{'takenby'};
346 if (defined($ref->{'day'})) {
347 $day = ", " . $ref->{'day'};
350 my $groupkey = $takenby . $day;
352 if ($groupkey ne $lastupl) {
353 $io->print(" </p>\n\n") if ($lastupl ne "" && $rot != 1);
354 $lastupl = $groupkey;
356 my %newsettings = %settings;
359 if (defined($author)) {
360 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
361 $newsettings{'author'} = undef;
362 $newsettings{'start'} = 1;
364 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
365 $newsettings{'author'} = $ref->{'takenby'};
366 $newsettings{'start'} = 1;
369 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
372 Sesse::pr0n::Templates::print_template($r, $io, "submittedby", { author => $takenby, action => $action, filterurl => $url, date => $day });
373 print_fullscreen_fromhere($r, $io, $event, \%settings, \%defsettings, $img_num);
374 $io->print("</h2>\n");
377 $io->print(" <p class=\"photos\">\n");
381 if (defined($ref->{'width'}) && defined($ref->{'height'})) {
382 my $width = $ref->{'width'};
383 my $height = $ref->{'height'};
385 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
386 $imgsz = " width=\"$width\" height=\"$height\"";
389 # Add fullscreen link.
390 my %fssettings = %settings;
391 $fssettings{'fullscreen'} = 1;
392 my $fsquery = Sesse::pr0n::Common::get_query_string(\%fssettings, \%defsettings);
393 $fsquery .= '#' . $img_num;
395 my $filename = $ref->{'filename'};
397 if (defined($xres) && defined($yres) && $xres != -1 && $xres != -2) {
398 $uri = "${xres}x$yres/$filename";
399 } elsif (defined($xres) && $xres == -1) {
400 $uri = "original/$filename";
405 $prefix = "/" . $ref->{'event'} . "/";
413 $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");
416 $io->print(" 90 <input type=\"checkbox\" name=\"rot-" .
417 $ref->{'id'} . "-90\" />\n");
418 $io->print(" 180 <input type=\"checkbox\" name=\"rot-" .
419 $ref->{'id'} . "-180\" />\n");
420 $io->print(" 270 <input type=\"checkbox\" name=\"rot-" .
421 $ref->{'id'} . "-270\" />\n");
422 $io->print(" " .
423 " Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
430 $io->print(" <input type=\"submit\" value=\"Rotate\" />\n");
431 $io->print(" </form>\n");
433 $io->print(" </p>\n");
436 print_nextprev($r, $io, $event, $where, \%settings, \%defsettings);
437 Sesse::pr0n::Common::footer($r, $io);
448 return 1 if (!defined($a) && !defined($b));
449 return 0 unless (defined($a) && defined($b));
454 my ($r, $io, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
456 my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
458 $io->print(" <p>$title:\n");
460 for my $a (@$alternatives) {
462 my %newsettings = %$settings;
466 ($text, $v1, $v2) = @$a;
468 $newsettings{$var1} = $v1;
469 $newsettings{$var2} = $v2;
473 # Parse the current alternative
474 my ($v1, $v2) = split /x/, $a;
476 $newsettings{$var1} = $v1;
477 $newsettings{$var2} = $v2;
482 # Check if these settings are current (print only label)
483 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
484 eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
487 Sesse::pr0n::Common::print_link($io, $text, "/$event/", \%newsettings, $defsettings);
491 $io->print(" </p>\n");
495 my ($r, $io, $event, $where, $settings, $defsettings) = @_;
496 my $start = $settings->{'start'};
497 my $num = $settings->{'num'};
498 my $dbh = Sesse::pr0n::Common::get_dbh();
500 $num = undef if (defined($num) && $num == -1);
501 return unless (defined($start) && defined($num));
503 # determine total number
504 my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where AND NOT is_render",
505 undef, Sesse::pr0n::Common::get_server_name($r))
506 or return dberror($r, "image enumeration");
507 my $num_images = $ref->{'num_images'};
509 return if ($start == 1 && $start + $num >= $num_images);
511 my $end = $start + $num - 1;
512 if ($end > $num_images) {
516 $io->print(" <p class=\"nextprev\">\n");
520 my $newstart = $start - $num;
524 my $newend = $newstart + $num - 1;
525 if ($newend > $num_images) {
526 $newend = $num_images;
529 my %newsettings = %$settings;
530 $newsettings{'start'} = $newstart;
531 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
532 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
533 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
537 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
538 $io->print(" $title ($start-$end)\n");
541 if ($end < $num_images) {
542 my $newstart = $start + $num;
543 my $newend = $newstart + $num - 1;
544 if ($newend > $num_images) {
545 $newend = $num_images;
548 my %newsettings = %$settings;
549 $newsettings{'start'} = $newstart;
550 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
551 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
552 Sesse::pr0n::Common::print_link($io, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
555 $io->print(" </p>\n");
559 my ($r, $io, $event, $settings, $defsettings) = @_;
561 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
562 chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
563 chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
565 $io->print(" <p>$title:\n");
567 my %newsettings = %$settings;
569 if ($settings->{'all'} == 0) {
572 $newsettings{'all'} = 0;
573 Sesse::pr0n::Common::print_link($io, $sel, "/$event/", \%newsettings, $defsettings);
578 if ($settings->{'all'} == 1) {
581 $newsettings{'all'} = 1;
582 Sesse::pr0n::Common::print_link($io, $all, "/$event/", \%newsettings, $defsettings);
588 sub print_fullscreen {
589 my ($r, $io, $event, $settings, $defsettings) = @_;
591 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
593 my %newsettings = %$settings;
594 $newsettings{'fullscreen'} = 1;
597 Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
598 $io->print("</p>\n");
601 sub print_fullscreen_fromhere {
602 my ($r, $io, $event, $settings, $defsettings, $start) = @_;
604 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
606 my %newsettings = %$settings;
607 $newsettings{'fullscreen'} = 1;
608 $newsettings{'start'} = $start;
610 $io->print(" <span class=\"fsfromhere\">");
611 Sesse::pr0n::Common::print_link($io, $title, "/$event/", \%newsettings, $defsettings);
612 $io->print("</span>\n");