1 package Sesse::pr0n::Index;
5 use Sesse::pr0n::Common qw(error dberror);
11 my $apr = Apache2::Request->new($r);
12 my $dbh = Sesse::pr0n::Common::get_dbh();
14 my ($event, $abspath, $datesort, $tag);
15 if ($r->uri =~ /^\/\+all\/?/) {
20 # augh, this needs 8.3, so we'll have to fiddle around a bit instead
21 # $datesort = 'DESC NULLS LAST';
23 } elsif ($r->uri =~ /^\/\+tags\/([a-zA-Z0-9-]+)\/?$/) {
25 $event = "+tags/$tag";
31 $r->uri =~ /^\/([a-zA-Z0-9-]+)\/?$/
32 or error($r, "Could not extract event");
39 # Fix common error: pr0n.sesse.net/event -> pr0n.sesse.net/event/
40 if ($r->uri !~ /\/$/) {
41 $r->headers_out->{'location'} = $r->uri . "/";
42 return Apache2::Const::REDIRECT;
46 if ($r->get_server_name =~ /internal/ || $r->get_server_name =~ /skoyen\.bilder\.knatten\.com/ || $r->get_server_name =~ /lia\.heimdal\.org/) {
47 my $user = Sesse::pr0n::Common::check_access($r);
48 if (!defined($user)) {
49 return Apache2::Const::OK;
53 # Read the appropriate settings from the query string into the settings hash
73 my $tq = $dbh->quote($tag);
74 $where = " AND id IN ( SELECT image FROM tags WHERE tag=$tq )";
75 } elsif ($event eq '+all') {
78 $where = ' AND event=' . $dbh->quote($event);
81 # Any NEF files => default to processing
82 my $ref = $dbh->selectrow_hashref("SELECT * FROM images WHERE vhost=? $where AND LOWER(filename) LIKE '%.nef' LIMIT 1",
83 undef, $r->get_server_name)
84 and $defsettings{'xres'} = $defsettings{'yres'} = undef;
86 # Reduce the front page load when in overload mode.
87 if (Sesse::pr0n::Overload::is_in_overload($r)) {
88 $defsettings{'num'} = 100;
91 my %settings = %defsettings;
93 for my $s qw(thumbxres thumbyres xres yres start num all infobox rot sel fullscreen model lens author) {
94 my $val = $apr->param($s);
95 if (defined($val) && $val =~ /^(\d+)$/) {
98 if (($s eq "num" || $s eq "xres" || $s eq "yres") && defined($val) && $val == -1) {
101 if ($s eq "model" || $s eq "lens" || $s eq "author") {
102 $settings{$s} = Sesse::pr0n::Common::pretty_unescape($val);
106 my $thumbxres = $settings{'thumbxres'};
107 my $thumbyres = $settings{'thumbyres'};
108 my $xres = $settings{'xres'};
109 my $yres = $settings{'yres'};
110 my $start = $settings{'start'};
111 my $num = $settings{'num'};
112 my $all = $settings{'all'};
113 my $infobox = $settings{'infobox'} ? '' : 'nobox/';
114 my $rot = $settings{'rot'};
115 my $sel = $settings{'sel'};
116 my $model = $settings{'model'};
117 my $lens = $settings{'lens'};
118 my $author = $settings{'author'};
120 # Construct SQL for this filter
122 $where .= ' AND selected=\'t\'';
124 if (defined($model) && defined($lens)) {
125 my $mq = $dbh->quote($model);
126 my $lq = $dbh->quote($lens);
130 $where .= " AND id NOT IN ( SELECT image FROM exif_info WHERE key='Model' AND TRIM(value)<>'' )";
132 $where .= " AND id IN ( SELECT image FROM exif_info WHERE key='Model' AND TRIM(value)=$mq )";
135 # This doesn't match 1:1 if there's both lens and lensspec, but it should be OK in practice
138 $where .= " AND id NOT IN ( SELECT image FROM exif_info WHERE (key='Lens' OR key='LensSpec') AND TRIM(value)<>'' )";
140 $where .= " AND id IN ( SELECT image FROM exif_info WHERE (key='Lens' OR key='LensSpec') AND TRIM(value)=$lq )";
143 if (defined($author)) {
144 my $aq = $dbh->quote($author);
146 $where .= " AND takenby=$aq";
149 if (defined($num) && $num == -1) {
155 if ($event eq '+all' || defined($tag)) {
156 $ref = $dbh->selectrow_hashref("SELECT EXTRACT(EPOCH FROM MAX(last_update)) AS last_update FROM events WHERE vhost=?",
157 undef, $r->get_server_name)
158 or error($r, "Could not list events", 404, "File not found");
160 $name = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
161 $r->set_last_modified($ref->{'last_update'});
163 $ref = $dbh->selectrow_hashref("SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE vhost=? AND event=?",
164 undef, $r->get_server_name, $event)
165 or error($r, "Could not find event $event", 404, "File not found");
167 $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
168 $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
169 $r->set_last_modified($ref->{'last_update'});
172 # If the client can use cache, do so
173 if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
177 # Count the number of selected images.
178 $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE vhost=? $where AND selected=\'t\'", undef, $r->get_server_name);
179 my $num_selected = $ref->{'num_selected'};
181 # Find all images related to this event.
182 my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : "";
184 my $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE vhost=? $where ORDER BY COALESCE((date - INTERVAL '6 hours')::date, '1970-01-01') $datesort,takenby,date,filename $limit")
185 or dberror($r, "prepare()");
186 $q->execute($r->get_server_name)
187 or dberror($r, "image enumeration");
189 # Print the page itself
190 if ($settings{'fullscreen'}) {
191 $r->content_type("text/html; charset=utf-8");
194 my $title = Sesse::pr0n::Templates::process_template($r, "tag-title", { tag => $tag });
195 Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => $title });
197 Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => "$name [$event]" });
201 while (my $ref = $q->fetchrow_hashref()) {
202 push @files, [ $ref->{'event'}, $ref->{'filename'} ];
205 for my $i (0..$#files) {
206 $r->print(" [ \"" . $files[$i]->[0] . "\", \"" . $files[$i]->[1] . "\" ]");
207 $r->print(",") unless ($i == $#files);
211 my %settings_no_fullscreen = %settings;
212 $settings_no_fullscreen{'fullscreen'} = 0;
214 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
215 Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
218 $returnurl =~ s/&/&/g;
220 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
221 vhost => $r->get_server_name,
222 start => $settings{'start'} - 1,
223 returnurl => $returnurl,
224 sel => $settings{'sel'},
229 my $title = Sesse::pr0n::Templates::process_template($r, "tag-title", { tag => $tag });
230 Sesse::pr0n::Common::header($r, $title);
232 Sesse::pr0n::Common::header($r, "$name [$event]");
234 if (defined($date)) {
235 Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
238 if (Sesse::pr0n::Overload::is_in_overload($r)) {
239 Sesse::pr0n::Templates::print_template($r, "overloadmode");
242 print_thumbsize($r, $event, \%settings, \%defsettings);
243 print_viewres($r, $event, \%settings, \%defsettings);
244 print_pagelimit($r, $event, \%settings, \%defsettings);
245 print_infobox($r, $event, \%settings, \%defsettings);
246 print_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
247 print_fullscreen($r, $event, \%settings, \%defsettings);
248 print_nextprev($r, $event, $where, \%settings, \%defsettings);
250 if (1 || $event ne '+all') {
251 # Find the equipment used
252 my $eq = $dbh->prepare("
254 TRIM(model.value) AS model,
255 coalesce(TRIM(lens_spec.value), TRIM(lens.value)) AS lens,
257 FROM ( SELECT * FROM images WHERE vhost=? $where ) i
258 LEFT JOIN exif_info model ON i.id=model.image
259 LEFT JOIN ( SELECT * FROM exif_info WHERE key='Lens' ) lens ON i.id=lens.image
260 LEFT JOIN ( SELECT * FROM exif_info WHERE key='LensSpec') lens_spec ON i.id=lens_spec.image
261 WHERE model.key='Model'
264 or die "Couldn't prepare to find equipment: $!";
265 $eq->execute($r->get_server_name)
266 or die "Couldn't find equipment: $!";
269 my %cameras_seen = ();
270 while (my $ref = $eq->fetchrow_hashref) {
271 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
273 # Some compact cameras seem to add lens info sometimes and not at other
274 # times; if we have seen a camera with at least one specific lens earlier,
275 # just combine entries without a lens with the previous one.
277 $equipment[$#equipment]->{'num'} += $ref->{'num'};
280 push @equipment, $ref;
281 $cameras_seen{$ref->{'model'}} = 1;
285 if (scalar @equipment > 0) {
286 Sesse::pr0n::Templates::print_template($r, "equipment-start");
287 for my $e (@equipment) {
288 my $eqspec = $e->{'model'};
289 $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
290 $eqspec = HTML::Entities::encode_entities($eqspec);
292 my %newsettings = %settings;
295 if (defined($model) && defined($lens)) {
296 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
297 $newsettings{'model'} = undef;
298 $newsettings{'lens'} = undef;
299 $newsettings{'start'} = 1;
301 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
302 $newsettings{'model'} = $e->{'model'};
303 $newsettings{'lens'} = defined($e->{'lens'}) ? $e->{'lens'} : '';
304 $newsettings{'start'} = 1;
307 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
309 # This isn't correct for all languages. Fix if we ever need to care. :-)
310 if ($e->{'num'} == 1) {
311 Sesse::pr0n::Templates::print_template($r, "equipment-item-singular", { eqspec => $eqspec, filterurl => $url, action => $action });
313 Sesse::pr0n::Templates::print_template($r, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
316 Sesse::pr0n::Templates::print_template($r, "equipment-end");
322 my $img_num = (defined($start) && defined($num)) ? $start : 1;
324 # Print out all thumbnails
326 $r->print(" <form method=\"post\" action=\"/rotate\">\n");
327 $r->print(" <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
330 while (my $ref = $q->fetchrow_hashref()) {
332 my $takenby = $ref->{'takenby'};
334 if (defined($ref->{'day'})) {
335 $day = ", " . $ref->{'day'};
338 my $groupkey = $takenby . $day;
340 if ($groupkey ne $lastupl) {
341 $r->print(" </p>\n\n") if ($lastupl ne "" && $rot != 1);
342 $lastupl = $groupkey;
344 my %newsettings = %settings;
347 if (defined($author)) {
348 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
349 $newsettings{'author'} = undef;
350 $newsettings{'start'} = 1;
352 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
353 $newsettings{'author'} = $ref->{'takenby'};
354 $newsettings{'start'} = 1;
357 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
360 Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $takenby, action => $action, filterurl => $url, date => $day });
361 print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
362 $r->print("</h2>\n");
365 $r->print(" <p class=\"photos\">\n");
369 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
370 my $width = $ref->{'width'};
371 my $height = $ref->{'height'};
373 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
374 $imgsz = " width=\"$width\" height=\"$height\"";
377 my $filename = $ref->{'filename'};
378 my $uri = $infobox . $filename;
379 if (defined($xres) && defined($yres) && $xres != -1) {
380 $uri = "${xres}x$yres/$infobox$filename";
381 } elsif (defined($xres) && $xres == -1) {
382 $uri = "original/$infobox$filename";
387 $prefix = "/" . $ref->{'event'} . "/";
395 $r->print("<a href=\"$prefix$uri\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
398 $r->print(" 90 <input type=\"checkbox\" name=\"rot-" .
399 $ref->{'id'} . "-90\" />\n");
400 $r->print(" 180 <input type=\"checkbox\" name=\"rot-" .
401 $ref->{'id'} . "-180\" />\n");
402 $r->print(" 270 <input type=\"checkbox\" name=\"rot-" .
403 $ref->{'id'} . "-270\" />\n");
404 $r->print(" " .
405 " Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
412 $r->print(" <input type=\"submit\" value=\"Rotate\" />\n");
413 $r->print(" </form>\n");
415 $r->print(" </p>\n");
418 print_nextprev($r, $event, $where, \%settings, \%defsettings);
419 Sesse::pr0n::Common::footer($r);
422 return Apache2::Const::OK;
428 return 1 if (!defined($a) && !defined($b));
429 return 0 unless (defined($a) && defined($b));
434 my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
436 my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
438 $r->print(" <p>$title:\n");
440 for my $a (@$alternatives) {
442 my %newsettings = %$settings;
446 ($text, $v1, $v2) = @$a;
448 $newsettings{$var1} = $v1;
449 $newsettings{$var2} = $v2;
453 # Parse the current alternative
454 my ($v1, $v2) = split /x/, $a;
456 $newsettings{$var1} = $v1;
457 $newsettings{$var2} = $v2;
462 # Check if these settings are current (print only label)
463 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
464 eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
467 Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
471 $r->print(" </p>\n");
474 sub print_thumbsize {
475 my ($r, $event, $settings, $defsettings) = @_;
476 my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
478 print_changes($r, $event, 'thumbsize', $settings, $defsettings,
479 'thumbxres', 'thumbyres', \@alternatives);
482 my ($r, $event, $settings, $defsettings) = @_;
483 my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1280x960 1400x1050 1600x1200);
484 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
485 chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
486 push @alternatives, [ $unlimited, undef, undef ];
487 push @alternatives, [ $original, -1, -1 ];
489 print_changes($r, $event, 'viewres', $settings, $defsettings,
490 'xres', 'yres', \@alternatives);
493 sub print_pagelimit {
494 my ($r, $event, $settings, $defsettings) = @_;
496 my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
498 $r->print(" <p>$title:\n");
501 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
502 my @alternatives = qw(10 50 100 250 500);
503 push @alternatives, $unlimited;
505 for my $num (@alternatives) {
506 my %newsettings = %$settings;
508 if ($num !~ /^\d+$/) { # unlimited
509 $newsettings{'num'} = -1;
511 $newsettings{'num'} = $num;
515 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
518 Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
522 $r->print(" </p>\n");
526 my ($r, $event, $settings, $defsettings) = @_;
528 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
529 chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
530 chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
532 $r->print(" <p>$title:\n");
534 my %newsettings = %$settings;
536 if ($settings->{'infobox'} == 1) {
539 $newsettings{'infobox'} = 1;
540 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
545 if ($settings->{'infobox'} == 0) {
548 $newsettings{'infobox'} = 0;
549 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
556 my ($r, $event, $where, $settings, $defsettings) = @_;
557 my $start = $settings->{'start'};
558 my $num = $settings->{'num'};
559 my $dbh = Sesse::pr0n::Common::get_dbh();
561 $num = undef if (defined($num) && $num == -1);
562 return unless (defined($start) && defined($num));
564 # determine total number
565 my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where",
566 undef, $r->get_server_name)
567 or dberror($r, "image enumeration");
568 my $num_images = $ref->{'num_images'};
570 return if ($start == 1 && $start + $num >= $num_images);
572 my $end = $start + $num - 1;
573 if ($end > $num_images) {
577 $r->print(" <p class=\"nextprev\">\n");
581 my $newstart = $start - $num;
585 my $newend = $newstart + $num - 1;
586 if ($newend > $num_images) {
587 $newend = $num_images;
590 my %newsettings = %$settings;
591 $newsettings{'start'} = $newstart;
592 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
593 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
594 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
598 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
599 $r->print(" $title ($start-$end)\n");
602 if ($end < $num_images) {
603 my $newstart = $start + $num;
604 my $newend = $newstart + $num - 1;
605 if ($newend > $num_images) {
606 $newend = $num_images;
609 my %newsettings = %$settings;
610 $newsettings{'start'} = $newstart;
611 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
612 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
613 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
616 $r->print(" </p>\n");
620 my ($r, $event, $settings, $defsettings) = @_;
622 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
623 chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
624 chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
626 $r->print(" <p>$title:\n");
628 my %newsettings = %$settings;
630 if ($settings->{'all'} == 0) {
633 $newsettings{'all'} = 0;
634 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
639 if ($settings->{'all'} == 1) {
642 $newsettings{'all'} = 1;
643 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
649 sub print_fullscreen {
650 my ($r, $event, $settings, $defsettings) = @_;
652 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
654 my %newsettings = %$settings;
655 $newsettings{'fullscreen'} = 1;
658 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
662 sub print_fullscreen_fromhere {
663 my ($r, $event, $settings, $defsettings, $start) = @_;
665 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
667 my %newsettings = %$settings;
668 $newsettings{'fullscreen'} = 1;
669 $newsettings{'start'} = $start;
671 $r->print(" <span class=\"fsfromhere\">");
672 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
673 $r->print("</span>\n");