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 $datesort = 'DESC NULLS LAST';
21 } elsif ($r->uri =~ /^\/\+tags\/([a-zA-Z0-9-]+)\/?$/) {
23 $event = "+tags/$tag";
26 $datesort = 'DESC NULLS LAST';
29 $r->uri =~ /^\/([a-zA-Z0-9-]+)\/?$/
30 or error($r, "Could not extract event");
34 $datesort = 'ASC NULLS LAST';
37 # Fix common error: pr0n.sesse.net/event -> pr0n.sesse.net/event/
38 if ($r->uri !~ /\/$/) {
39 $r->headers_out->{'location'} = $r->uri . "/";
40 return Apache2::Const::REDIRECT;
44 if ($r->get_server_name =~ /internal/ || $r->get_server_name =~ /skoyen\.bilder\.knatten\.com/ || $r->get_server_name =~ /lia\.heimdal\.org/) {
45 my $user = Sesse::pr0n::Common::check_access($r);
46 if (!defined($user)) {
47 return Apache2::Const::OK;
51 # Read the appropriate settings from the query string into the settings hash
71 my $tq = $dbh->quote($tag);
72 $where = " AND id IN ( SELECT image FROM tags WHERE tag=$tq )";
73 } elsif ($event eq '+all') {
76 $where = ' AND event=' . $dbh->quote($event);
79 # Any NEF files => default to processing
80 my $ref = $dbh->selectrow_hashref("SELECT * FROM images WHERE vhost=? $where AND ( LOWER(filename) LIKE '%.nef' OR LOWER(filename) LIKE '%.cr2' ) LIMIT 1",
81 undef, $r->get_server_name)
82 and $defsettings{'xres'} = $defsettings{'yres'} = undef;
84 # Reduce the front page load when in overload mode.
85 if (Sesse::pr0n::Overload::is_in_overload($r)) {
86 $defsettings{'num'} = 100;
89 my %settings = %defsettings;
91 for my $s (qw(thumbxres thumbyres xres yres start num all infobox rot sel fullscreen model lens author)) {
92 my $val = $apr->param($s);
93 if (defined($val) && $val =~ /^(\d+)$/) {
96 if ($s eq "num" && defined($val) && $val == -1) {
99 if (($s eq "xres" || $s eq "yres") && defined($val) && ($val == -1 || $val == -2)) {
100 $settings{$s} = $val;
102 if (($s eq "model" || $s eq "lens" || $s eq "author") && defined($val)) {
103 $settings{$s} = Sesse::pr0n::Common::pretty_unescape($val);
107 my $thumbxres = $settings{'thumbxres'};
108 my $thumbyres = $settings{'thumbyres'};
109 my $xres = $settings{'xres'};
110 my $yres = $settings{'yres'};
111 my $start = $settings{'start'};
112 my $num = $settings{'num'};
113 my $all = $settings{'all'};
114 my $infobox = $settings{'infobox'} ? '' : 'nobox/';
115 my $rot = $settings{'rot'};
116 my $sel = $settings{'sel'};
117 my $model = $settings{'model'};
118 my $lens = $settings{'lens'};
119 my $author = $settings{'author'};
121 # Construct SQL for this filter
123 $where .= ' AND selected=\'t\'';
125 if (defined($model) && defined($lens)) {
126 my $mq = $dbh->quote($model);
127 my $lq = $dbh->quote($lens);
131 $where .= " AND model IS NULL";
133 $where .= " AND model=$mq";
138 $where .= " AND lens IS NULL";
140 $where .= " AND lens=$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 last_picture_cache 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 NATURAL JOIN last_picture_cache 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 (date - INTERVAL '6 hours')::date $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 my $width = defined($ref->{'width'}) ? $ref->{'width'} : -1;
203 my $height = defined($ref->{'height'}) ? $ref->{'height'} : -1;
204 push @files, [ $ref->{'event'}, $ref->{'filename'}, $width, $height ];
207 for my $i (0..$#files) {
208 my $line = sprintf " [ \"%s\", \"%s\", %d, %d ]", @{$files[$i]};
209 $line .= "," unless ($i == $#files);
210 $r->print($line . "\n");
213 my %settings_no_fullscreen = %settings;
214 $settings_no_fullscreen{'fullscreen'} = 0;
216 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
217 Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
220 $returnurl =~ s/&/&/g;
222 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
223 vhost => $r->get_server_name,
224 returnurl => $returnurl,
225 start => $settings{'start'} - 1,
226 sel => $settings{'sel'},
231 my $title = Sesse::pr0n::Templates::process_template($r, "tag-title", { tag => $tag });
232 Sesse::pr0n::Common::header($r, $title);
234 Sesse::pr0n::Common::header($r, "$name [$event]");
236 if (defined($date)) {
237 Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
240 if (Sesse::pr0n::Overload::is_in_overload($r)) {
241 Sesse::pr0n::Templates::print_template($r, "overloadmode");
244 print_thumbsize($r, $event, \%settings, \%defsettings);
245 print_viewres($r, $event, \%settings, \%defsettings);
246 print_pagelimit($r, $event, \%settings, \%defsettings);
247 print_infobox($r, $event, \%settings, \%defsettings);
248 print_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
249 print_fullscreen($r, $event, \%settings, \%defsettings);
250 print_nextprev($r, $event, $where, \%settings, \%defsettings);
252 if (1 || $event ne '+all') {
253 # Find the equipment used
254 my $eq = $dbh->prepare("
263 or die "Couldn't prepare to find equipment: $!";
264 $eq->execute($r->get_server_name)
265 or die "Couldn't find equipment: $!";
268 my %cameras_seen = ();
269 while (my $ref = $eq->fetchrow_hashref) {
270 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
272 # Some compact cameras seem to add lens info sometimes and not at other
273 # times; if we have seen a camera with at least one specific lens earlier,
274 # just combine entries without a lens with the previous one.
276 $equipment[$#equipment]->{'num'} += $ref->{'num'};
279 push @equipment, $ref;
280 $cameras_seen{$ref->{'model'}} = 1;
284 if (scalar @equipment > 0) {
285 Sesse::pr0n::Templates::print_template($r, "equipment-start");
286 for my $e (@equipment) {
287 my $eqspec = $e->{'model'};
288 $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
289 $eqspec = HTML::Entities::encode_entities($eqspec);
291 my %newsettings = %settings;
294 if (defined($model) && defined($lens)) {
295 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
296 $newsettings{'model'} = undef;
297 $newsettings{'lens'} = undef;
298 $newsettings{'start'} = 1;
300 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
301 $newsettings{'model'} = $e->{'model'};
302 $newsettings{'lens'} = defined($e->{'lens'}) ? $e->{'lens'} : '';
303 $newsettings{'start'} = 1;
306 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
308 # This isn't correct for all languages. Fix if we ever need to care. :-)
309 if ($e->{'num'} == 1) {
310 Sesse::pr0n::Templates::print_template($r, "equipment-item-singular", { eqspec => $eqspec, filterurl => $url, action => $action });
312 Sesse::pr0n::Templates::print_template($r, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
315 Sesse::pr0n::Templates::print_template($r, "equipment-end");
321 my $img_num = (defined($start) && defined($num)) ? $start : 1;
323 # Print out all thumbnails
325 $r->print(" <form method=\"post\" action=\"/rotate\">\n");
326 $r->print(" <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
329 while (my $ref = $q->fetchrow_hashref()) {
331 my $takenby = $ref->{'takenby'};
333 if (defined($ref->{'day'})) {
334 $day = ", " . $ref->{'day'};
337 my $groupkey = $takenby . $day;
339 if ($groupkey ne $lastupl) {
340 $r->print(" </p>\n\n") if ($lastupl ne "" && $rot != 1);
341 $lastupl = $groupkey;
343 my %newsettings = %settings;
346 if (defined($author)) {
347 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
348 $newsettings{'author'} = undef;
349 $newsettings{'start'} = 1;
351 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
352 $newsettings{'author'} = $ref->{'takenby'};
353 $newsettings{'start'} = 1;
356 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
359 Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $takenby, action => $action, filterurl => $url, date => $day });
360 print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
361 $r->print("</h2>\n");
364 $r->print(" <p class=\"photos\">\n");
368 if (defined($ref->{'width'}) && defined($ref->{'height'})) {
369 my $width = $ref->{'width'};
370 my $height = $ref->{'height'};
372 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
373 $imgsz = " width=\"$width\" height=\"$height\"";
376 my $filename = $ref->{'filename'};
377 my $uri = $infobox . $filename;
378 if (defined($xres) && defined($yres) && $xres != -1 && $xres != -2) {
379 $uri = "${xres}x$yres/$infobox$filename";
380 } elsif (defined($xres) && $xres == -1) {
381 $uri = "original/$infobox$filename";
386 $prefix = "/" . $ref->{'event'} . "/";
394 $r->print("<a href=\"$prefix$uri\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
397 $r->print(" 90 <input type=\"checkbox\" name=\"rot-" .
398 $ref->{'id'} . "-90\" />\n");
399 $r->print(" 180 <input type=\"checkbox\" name=\"rot-" .
400 $ref->{'id'} . "-180\" />\n");
401 $r->print(" 270 <input type=\"checkbox\" name=\"rot-" .
402 $ref->{'id'} . "-270\" />\n");
403 $r->print(" " .
404 " Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
411 $r->print(" <input type=\"submit\" value=\"Rotate\" />\n");
412 $r->print(" </form>\n");
414 $r->print(" </p>\n");
417 print_nextprev($r, $event, $where, \%settings, \%defsettings);
418 Sesse::pr0n::Common::footer($r);
421 return Apache2::Const::OK;
427 return 1 if (!defined($a) && !defined($b));
428 return 0 unless (defined($a) && defined($b));
433 my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
435 my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
437 $r->print(" <p>$title:\n");
439 for my $a (@$alternatives) {
441 my %newsettings = %$settings;
445 ($text, $v1, $v2) = @$a;
447 $newsettings{$var1} = $v1;
448 $newsettings{$var2} = $v2;
452 # Parse the current alternative
453 my ($v1, $v2) = split /x/, $a;
455 $newsettings{$var1} = $v1;
456 $newsettings{$var2} = $v2;
461 # Check if these settings are current (print only label)
462 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
463 eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
466 Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
470 $r->print(" </p>\n");
473 sub print_thumbsize {
474 my ($r, $event, $settings, $defsettings) = @_;
475 my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
477 print_changes($r, $event, 'thumbsize', $settings, $defsettings,
478 'thumbxres', 'thumbyres', \@alternatives);
481 my ($r, $event, $settings, $defsettings) = @_;
482 my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1152x864 1280x960 1400x1050 1600x1200 1920x1440 2048x1536);
483 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
484 chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
485 push @alternatives, [ $unlimited, -2, -2 ];
486 push @alternatives, [ $original, -1, -1 ];
488 print_changes($r, $event, 'viewres', $settings, $defsettings,
489 'xres', 'yres', \@alternatives);
492 sub print_pagelimit {
493 my ($r, $event, $settings, $defsettings) = @_;
495 my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
497 $r->print(" <p>$title:\n");
500 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
501 my @alternatives = qw(10 50 100 250 500);
502 push @alternatives, $unlimited;
504 for my $num (@alternatives) {
505 my %newsettings = %$settings;
507 if ($num !~ /^\d+$/) { # unlimited
508 $newsettings{'num'} = -1;
510 $newsettings{'num'} = $num;
514 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
517 Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
521 $r->print(" </p>\n");
525 my ($r, $event, $settings, $defsettings) = @_;
527 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
528 chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
529 chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
531 $r->print(" <p>$title:\n");
533 my %newsettings = %$settings;
535 if ($settings->{'infobox'} == 1) {
538 $newsettings{'infobox'} = 1;
539 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
544 if ($settings->{'infobox'} == 0) {
547 $newsettings{'infobox'} = 0;
548 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
555 my ($r, $event, $where, $settings, $defsettings) = @_;
556 my $start = $settings->{'start'};
557 my $num = $settings->{'num'};
558 my $dbh = Sesse::pr0n::Common::get_dbh();
560 $num = undef if (defined($num) && $num == -1);
561 return unless (defined($start) && defined($num));
563 # determine total number
564 my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where",
565 undef, $r->get_server_name)
566 or dberror($r, "image enumeration");
567 my $num_images = $ref->{'num_images'};
569 return if ($start == 1 && $start + $num >= $num_images);
571 my $end = $start + $num - 1;
572 if ($end > $num_images) {
576 $r->print(" <p class=\"nextprev\">\n");
580 my $newstart = $start - $num;
584 my $newend = $newstart + $num - 1;
585 if ($newend > $num_images) {
586 $newend = $num_images;
589 my %newsettings = %$settings;
590 $newsettings{'start'} = $newstart;
591 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
592 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
593 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
597 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
598 $r->print(" $title ($start-$end)\n");
601 if ($end < $num_images) {
602 my $newstart = $start + $num;
603 my $newend = $newstart + $num - 1;
604 if ($newend > $num_images) {
605 $newend = $num_images;
608 my %newsettings = %$settings;
609 $newsettings{'start'} = $newstart;
610 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
611 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
612 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
615 $r->print(" </p>\n");
619 my ($r, $event, $settings, $defsettings) = @_;
621 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
622 chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
623 chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
625 $r->print(" <p>$title:\n");
627 my %newsettings = %$settings;
629 if ($settings->{'all'} == 0) {
632 $newsettings{'all'} = 0;
633 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
638 if ($settings->{'all'} == 1) {
641 $newsettings{'all'} = 1;
642 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
648 sub print_fullscreen {
649 my ($r, $event, $settings, $defsettings) = @_;
651 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
653 my %newsettings = %$settings;
654 $newsettings{'fullscreen'} = 1;
657 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
661 sub print_fullscreen_fromhere {
662 my ($r, $event, $settings, $defsettings, $start) = @_;
664 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
666 my %newsettings = %$settings;
667 $newsettings{'fullscreen'} = 1;
668 $newsettings{'start'} = $start;
670 $r->print(" <span class=\"fsfromhere\">");
671 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
672 $r->print("</span>\n");