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' 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" || $s eq "xres" || $s eq "yres") && defined($val) && $val == -1) {
99 if ($s eq "model" || $s eq "lens" || $s eq "author") {
100 $settings{$s} = Sesse::pr0n::Common::pretty_unescape($val);
104 my $thumbxres = $settings{'thumbxres'};
105 my $thumbyres = $settings{'thumbyres'};
106 my $xres = $settings{'xres'};
107 my $yres = $settings{'yres'};
108 my $start = $settings{'start'};
109 my $num = $settings{'num'};
110 my $all = $settings{'all'};
111 my $infobox = $settings{'infobox'} ? '' : 'nobox/';
112 my $rot = $settings{'rot'};
113 my $sel = $settings{'sel'};
114 my $model = $settings{'model'};
115 my $lens = $settings{'lens'};
116 my $author = $settings{'author'};
118 # Construct SQL for this filter
120 $where .= ' AND selected=\'t\'';
122 if (defined($model) && defined($lens)) {
123 my $mq = $dbh->quote($model);
124 my $lq = $dbh->quote($lens);
128 $where .= " AND model IS NULL";
130 $where .= " AND model=$mq";
135 $where .= " AND lens IS NULL";
137 $where .= " AND lens=$lq";
140 if (defined($author)) {
141 my $aq = $dbh->quote($author);
143 $where .= " AND takenby=$aq";
146 if (defined($num) && $num == -1) {
152 if ($event eq '+all' || defined($tag)) {
153 $ref = $dbh->selectrow_hashref("SELECT EXTRACT(EPOCH FROM MAX(last_update)) AS last_update FROM events WHERE vhost=?",
154 undef, $r->get_server_name)
155 or error($r, "Could not list events", 404, "File not found");
157 $name = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
158 $r->set_last_modified($ref->{'last_update'});
160 $ref = $dbh->selectrow_hashref("SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE vhost=? AND event=?",
161 undef, $r->get_server_name, $event)
162 or error($r, "Could not find event $event", 404, "File not found");
164 $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
165 $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
166 $r->set_last_modified($ref->{'last_update'});
169 # If the client can use cache, do so
170 if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
174 # Count the number of selected images.
175 $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE vhost=? $where AND selected=\'t\'", undef, $r->get_server_name);
176 my $num_selected = $ref->{'num_selected'};
178 # Find all images related to this event.
179 my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : "";
181 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")
182 or dberror($r, "prepare()");
183 $q->execute($r->get_server_name)
184 or dberror($r, "image enumeration");
186 # Print the page itself
187 if ($settings{'fullscreen'}) {
188 $r->content_type("text/html; charset=utf-8");
191 my $title = Sesse::pr0n::Templates::process_template($r, "tag-title", { tag => $tag });
192 Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => $title });
194 Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => "$name [$event]" });
198 while (my $ref = $q->fetchrow_hashref()) {
199 my $width = defined($ref->{'width'}) ? $ref->{'width'} : -1;
200 my $height = defined($ref->{'height'}) ? $ref->{'height'} : -1;
201 push @files, [ $ref->{'event'}, $ref->{'filename'}, $width, $height ];
204 for my $i (0..$#files) {
205 my $line = sprintf " [ \"%s\", \"%s\", %d, %d ]", @{$files[$i]};
206 $line .= "," unless ($i == $#files);
207 $r->print($line . "\n");
210 my %settings_no_fullscreen = %settings;
211 $settings_no_fullscreen{'fullscreen'} = 0;
213 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
214 Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
217 $returnurl =~ s/&/&/g;
219 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
220 vhost => $r->get_server_name,
221 returnurl => $returnurl,
222 start => $settings{'start'} - 1,
223 sel => $settings{'sel'},
228 my $title = Sesse::pr0n::Templates::process_template($r, "tag-title", { tag => $tag });
229 Sesse::pr0n::Common::header($r, $title);
231 Sesse::pr0n::Common::header($r, "$name [$event]");
233 if (defined($date)) {
234 Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
237 if (Sesse::pr0n::Overload::is_in_overload($r)) {
238 Sesse::pr0n::Templates::print_template($r, "overloadmode");
241 print_thumbsize($r, $event, \%settings, \%defsettings);
242 print_viewres($r, $event, \%settings, \%defsettings);
243 print_pagelimit($r, $event, \%settings, \%defsettings);
244 print_infobox($r, $event, \%settings, \%defsettings);
245 print_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
246 print_fullscreen($r, $event, \%settings, \%defsettings);
247 print_nextprev($r, $event, $where, \%settings, \%defsettings);
249 if (1 || $event ne '+all') {
250 # Find the equipment used
251 my $eq = $dbh->prepare("
260 or die "Couldn't prepare to find equipment: $!";
261 $eq->execute($r->get_server_name)
262 or die "Couldn't find equipment: $!";
265 my %cameras_seen = ();
266 while (my $ref = $eq->fetchrow_hashref) {
267 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
269 # Some compact cameras seem to add lens info sometimes and not at other
270 # times; if we have seen a camera with at least one specific lens earlier,
271 # just combine entries without a lens with the previous one.
273 $equipment[$#equipment]->{'num'} += $ref->{'num'};
276 push @equipment, $ref;
277 $cameras_seen{$ref->{'model'}} = 1;
281 if (scalar @equipment > 0) {
282 Sesse::pr0n::Templates::print_template($r, "equipment-start");
283 for my $e (@equipment) {
284 my $eqspec = $e->{'model'};
285 $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
286 $eqspec = HTML::Entities::encode_entities($eqspec);
288 my %newsettings = %settings;
291 if (defined($model) && defined($lens)) {
292 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
293 $newsettings{'model'} = undef;
294 $newsettings{'lens'} = undef;
295 $newsettings{'start'} = 1;
297 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
298 $newsettings{'model'} = $e->{'model'};
299 $newsettings{'lens'} = defined($e->{'lens'}) ? $e->{'lens'} : '';
300 $newsettings{'start'} = 1;
303 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
305 # This isn't correct for all languages. Fix if we ever need to care. :-)
306 if ($e->{'num'} == 1) {
307 Sesse::pr0n::Templates::print_template($r, "equipment-item-singular", { eqspec => $eqspec, filterurl => $url, action => $action });
309 Sesse::pr0n::Templates::print_template($r, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
312 Sesse::pr0n::Templates::print_template($r, "equipment-end");
318 my $img_num = (defined($start) && defined($num)) ? $start : 1;
320 # Print out all thumbnails
322 $r->print(" <form method=\"post\" action=\"/rotate\">\n");
323 $r->print(" <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
326 while (my $ref = $q->fetchrow_hashref()) {
328 my $takenby = $ref->{'takenby'};
330 if (defined($ref->{'day'})) {
331 $day = ", " . $ref->{'day'};
334 my $groupkey = $takenby . $day;
336 if ($groupkey ne $lastupl) {
337 $r->print(" </p>\n\n") if ($lastupl ne "" && $rot != 1);
338 $lastupl = $groupkey;
340 my %newsettings = %settings;
343 if (defined($author)) {
344 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
345 $newsettings{'author'} = undef;
346 $newsettings{'start'} = 1;
348 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
349 $newsettings{'author'} = $ref->{'takenby'};
350 $newsettings{'start'} = 1;
353 my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
356 Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $takenby, action => $action, filterurl => $url, date => $day });
357 print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
358 $r->print("</h2>\n");
361 $r->print(" <p class=\"photos\">\n");
365 if (defined($ref->{'width'}) && defined($ref->{'height'})) {
366 my $width = $ref->{'width'};
367 my $height = $ref->{'height'};
369 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
370 $imgsz = " width=\"$width\" height=\"$height\"";
373 my $filename = $ref->{'filename'};
374 my $uri = $infobox . $filename;
375 if (defined($xres) && defined($yres) && $xres != -1) {
376 $uri = "${xres}x$yres/$infobox$filename";
377 } elsif (defined($xres) && $xres == -1) {
378 $uri = "original/$infobox$filename";
383 $prefix = "/" . $ref->{'event'} . "/";
391 $r->print("<a href=\"$prefix$uri\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
394 $r->print(" 90 <input type=\"checkbox\" name=\"rot-" .
395 $ref->{'id'} . "-90\" />\n");
396 $r->print(" 180 <input type=\"checkbox\" name=\"rot-" .
397 $ref->{'id'} . "-180\" />\n");
398 $r->print(" 270 <input type=\"checkbox\" name=\"rot-" .
399 $ref->{'id'} . "-270\" />\n");
400 $r->print(" " .
401 " Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
408 $r->print(" <input type=\"submit\" value=\"Rotate\" />\n");
409 $r->print(" </form>\n");
411 $r->print(" </p>\n");
414 print_nextprev($r, $event, $where, \%settings, \%defsettings);
415 Sesse::pr0n::Common::footer($r);
418 return Apache2::Const::OK;
424 return 1 if (!defined($a) && !defined($b));
425 return 0 unless (defined($a) && defined($b));
430 my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
432 my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
434 $r->print(" <p>$title:\n");
436 for my $a (@$alternatives) {
438 my %newsettings = %$settings;
442 ($text, $v1, $v2) = @$a;
444 $newsettings{$var1} = $v1;
445 $newsettings{$var2} = $v2;
449 # Parse the current alternative
450 my ($v1, $v2) = split /x/, $a;
452 $newsettings{$var1} = $v1;
453 $newsettings{$var2} = $v2;
458 # Check if these settings are current (print only label)
459 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
460 eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
463 Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
467 $r->print(" </p>\n");
470 sub print_thumbsize {
471 my ($r, $event, $settings, $defsettings) = @_;
472 my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
474 print_changes($r, $event, 'thumbsize', $settings, $defsettings,
475 'thumbxres', 'thumbyres', \@alternatives);
478 my ($r, $event, $settings, $defsettings) = @_;
479 my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1152x864 1280x960 1400x1050 1600x1200);
480 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
481 chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
482 push @alternatives, [ $unlimited, undef, undef ];
483 push @alternatives, [ $original, -1, -1 ];
485 print_changes($r, $event, 'viewres', $settings, $defsettings,
486 'xres', 'yres', \@alternatives);
489 sub print_pagelimit {
490 my ($r, $event, $settings, $defsettings) = @_;
492 my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
494 $r->print(" <p>$title:\n");
497 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
498 my @alternatives = qw(10 50 100 250 500);
499 push @alternatives, $unlimited;
501 for my $num (@alternatives) {
502 my %newsettings = %$settings;
504 if ($num !~ /^\d+$/) { # unlimited
505 $newsettings{'num'} = -1;
507 $newsettings{'num'} = $num;
511 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
514 Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
518 $r->print(" </p>\n");
522 my ($r, $event, $settings, $defsettings) = @_;
524 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
525 chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
526 chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
528 $r->print(" <p>$title:\n");
530 my %newsettings = %$settings;
532 if ($settings->{'infobox'} == 1) {
535 $newsettings{'infobox'} = 1;
536 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
541 if ($settings->{'infobox'} == 0) {
544 $newsettings{'infobox'} = 0;
545 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
552 my ($r, $event, $where, $settings, $defsettings) = @_;
553 my $start = $settings->{'start'};
554 my $num = $settings->{'num'};
555 my $dbh = Sesse::pr0n::Common::get_dbh();
557 $num = undef if (defined($num) && $num == -1);
558 return unless (defined($start) && defined($num));
560 # determine total number
561 my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where",
562 undef, $r->get_server_name)
563 or dberror($r, "image enumeration");
564 my $num_images = $ref->{'num_images'};
566 return if ($start == 1 && $start + $num >= $num_images);
568 my $end = $start + $num - 1;
569 if ($end > $num_images) {
573 $r->print(" <p class=\"nextprev\">\n");
577 my $newstart = $start - $num;
581 my $newend = $newstart + $num - 1;
582 if ($newend > $num_images) {
583 $newend = $num_images;
586 my %newsettings = %$settings;
587 $newsettings{'start'} = $newstart;
588 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
589 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
590 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
594 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
595 $r->print(" $title ($start-$end)\n");
598 if ($end < $num_images) {
599 my $newstart = $start + $num;
600 my $newend = $newstart + $num - 1;
601 if ($newend > $num_images) {
602 $newend = $num_images;
605 my %newsettings = %$settings;
606 $newsettings{'start'} = $newstart;
607 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
608 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
609 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
612 $r->print(" </p>\n");
616 my ($r, $event, $settings, $defsettings) = @_;
618 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
619 chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
620 chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
622 $r->print(" <p>$title:\n");
624 my %newsettings = %$settings;
626 if ($settings->{'all'} == 0) {
629 $newsettings{'all'} = 0;
630 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
635 if ($settings->{'all'} == 1) {
638 $newsettings{'all'} = 1;
639 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
645 sub print_fullscreen {
646 my ($r, $event, $settings, $defsettings) = @_;
648 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
650 my %newsettings = %$settings;
651 $newsettings{'fullscreen'} = 1;
654 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
658 sub print_fullscreen_fromhere {
659 my ($r, $event, $settings, $defsettings, $start) = @_;
661 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
663 my %newsettings = %$settings;
664 $newsettings{'fullscreen'} = 1;
665 $newsettings{'start'} = $start;
667 $r->print(" <span class=\"fsfromhere\">");
668 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
669 $r->print("</span>\n");