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();
15 $r->uri =~ m#^/([a-zA-Z0-9-]+)/?$#
16 or error($r, "Could not extract event");
19 # Fix common error: pr0n.sesse.net/event -> pr0n.sesse.net/event/
20 if ($r->uri !~ m#/$#) {
21 $r->headers_out->{'location'} = "/$event/";
22 return Apache2::Const::REDIRECT;
26 if ($r->get_server_name =~ /internal/ || $r->get_server_name =~ /skoyen\.bilder\.knatten\.com/) {
27 my $user = Sesse::pr0n::Common::check_access($r);
28 if (!defined($user)) {
29 return Apache2::Const::OK;
33 # Read the appropriate settings from the query string into the settings hash
48 # Any NEF files => default to processing
49 my $ref = $dbh->selectrow_hashref('SELECT * FROM images WHERE event=? AND LOWER(filename) LIKE \'%.nef\' LIMIT 1',
51 and $defsettings{'xres'} = $defsettings{'yres'} = undef;
53 # Reduce the front page load when in overload mode.
54 if (Sesse::pr0n::Overload::is_in_overload($r)) {
55 $defsettings{'num'} = 100;
58 my %settings = %defsettings;
60 for my $s qw(thumbxres thumbyres xres yres start num all infobox rot sel fullscreen) {
61 my $val = $apr->param($s);
62 if (defined($val) && $val =~ /^(\d+)$/) {
65 if (($s eq "num" || $s eq "xres" || $s eq "yres") && defined($val) && $val == -1) {
70 my $thumbxres = $settings{'thumbxres'};
71 my $thumbyres = $settings{'thumbyres'};
72 my $xres = $settings{'xres'};
73 my $yres = $settings{'yres'};
74 my $start = $settings{'start'};
75 my $num = $settings{'num'};
76 my $all = $settings{'all'};
77 my $infobox = $settings{'infobox'} ? '' : 'nobox/';
78 my $rot = $settings{'rot'};
79 my $sel = $settings{'sel'};
81 if (defined($num) && $num == -1) {
85 $ref = $dbh->selectrow_hashref('SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE id=? AND vhost=?',
86 undef, $event, $r->get_server_name)
87 or error($r, "Could not find event $event", 404, "File not found");
89 my $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
90 my $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
91 $r->set_last_modified($ref->{'last_update'});
93 # If the client can use cache, do so
94 if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
98 # Count the number of selected images.
99 $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE event=? AND selected=\'t\'", undef, $event);
100 my $num_selected = $ref->{'num_selected'};
102 # Find all images related to this event.
104 my $where = ($all == 0) ? ' AND selected=\'t\'' : '';
106 if (defined($start) && defined($num) && !$settings{'fullscreen'}) {
107 $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE event=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename LIMIT $num OFFSET " . ($start-1))
108 or dberror($r, "prepare()");
110 $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE event=? $where ORDER BY (date - INTERVAL '6 hours')::date,takenby,date,filename")
111 or dberror($r, "prepare()");
114 or dberror($r, "image enumeration");
116 # Print the page itself
117 if ($settings{'fullscreen'}) {
118 $r->content_type("text/html; charset=utf-8");
119 Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => "$name [$event]" });
120 while (my $ref = $q->fetchrow_hashref()) {
121 $r->print(" \"" . $infobox . $ref->{'filename'} . "\",\n");
124 my %settings_no_fullscreen = %settings;
125 $settings_no_fullscreen{'fullscreen'} = 0;
127 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
128 Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
131 $returnurl =~ s/&/&/g;
133 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
134 vhost => $r->get_server_name,
136 start => $settings{'start'} - 1,
137 returnurl => $returnurl,
138 sel => $settings{'sel'},
141 Sesse::pr0n::Common::header($r, "$name [$event]");
142 Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
144 if (Sesse::pr0n::Overload::is_in_overload($r)) {
145 Sesse::pr0n::Templates::print_template($r, "overloadmode");
148 print_thumbsize($r, $event, \%settings, \%defsettings);
149 print_viewres($r, $event, \%settings, \%defsettings);
150 print_pagelimit($r, $event, \%settings, \%defsettings);
151 print_infobox($r, $event, \%settings, \%defsettings);
152 print_nextprev($r, $event, \%settings, \%defsettings);
153 print_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
154 print_fullscreen($r, $event, \%settings, \%defsettings);
156 # Find the equipment used
157 my $eq = $dbh->prepare('
159 TRIM(model.value) AS model,
160 coalesce(TRIM(lens_spec.value), TRIM(lens.value)) AS lens,
163 LEFT JOIN exif_info model ON i.id=model.image
164 LEFT JOIN ( SELECT * FROM exif_info WHERE tag=\'Lens\' ) lens ON i.id=lens.image
165 LEFT JOIN ( SELECT * FROM exif_info WHERE tag=\'LensSpec\') lens_spec ON i.id=lens_spec.image
166 WHERE event=? AND model.tag=\'Model\'
169 or die "Couldn't prepare to find equipment: $!";
171 or die "Couldn't find equipment: $!";
174 my %cameras_seen = ();
175 while (my $ref = $eq->fetchrow_hashref) {
176 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
178 # Some compact cameras seem to add lens info sometimes and not at other
179 # times; if we have seen a camera with at least one specific lens earlier,
180 # just combine entries without a lens with the previous one.
182 $equipment[$#equipment]->{'num'} += $ref->{'num'};
185 push @equipment, $ref;
186 $cameras_seen{$ref->{'model'}} = 1;
190 if (scalar @equipment > 0) {
191 Sesse::pr0n::Templates::print_template($r, "equipment-start");
192 for my $e (@equipment) {
193 my $eqspec = $e->{'model'};
194 $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
196 # This isn't correct for all languages. Fix if we ever need to care. :-)
197 if ($e->{'num'} == 1) {
198 Sesse::pr0n::Templates::print_template($r, "equipment-item-singular", { eqspec => $eqspec });
200 Sesse::pr0n::Templates::print_template($r, "equipment-item", { eqspec => $eqspec, num => $e->{'num'} });
203 Sesse::pr0n::Templates::print_template($r, "equipment-end");
209 my $img_num = $start;
211 # Print out all thumbnails
213 $r->print(" <form method=\"post\" action=\"/rotate\">\n");
214 $r->print(" <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
216 while (my $ref = $q->fetchrow_hashref()) {
218 my $takenby = $ref->{'takenby'};
219 if (defined($ref->{'day'})) {
220 $takenby .= ", " . $ref->{'day'};
223 if ($takenby ne $lastupl) {
226 Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
227 print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
228 $r->print("</h2>\n");
230 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
231 my $width = $ref->{'width'};
232 my $height = $ref->{'height'};
234 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
235 $imgsz = " width=\"$width\" height=\"$height\"";
238 my $filename = $ref->{'filename'};
239 my $uri = $infobox . $filename;
240 if (defined($xres) && defined($yres) && $xres != -1) {
241 $uri = "${xres}x$yres/$infobox$filename";
242 } elsif (defined($xres) && $xres == -1) {
243 $uri = "original/$infobox$filename";
246 $r->print(" <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
247 $r->print(" 90 <input type=\"checkbox\" name=\"rot-" .
248 $ref->{'id'} . "-90\" />\n");
249 $r->print(" 180 <input type=\"checkbox\" name=\"rot-" .
250 $ref->{'id'} . "-180\" />\n");
251 $r->print(" 270 <input type=\"checkbox\" name=\"rot-" .
252 $ref->{'id'} . "-270\" />\n");
253 $r->print(" " .
254 " Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
258 $r->print(" <input type=\"submit\" value=\"Rotate\" />\n");
259 $r->print(" </form>\n");
260 } elsif ($sel == 1) {
261 $r->print(" <form method=\"post\" action=\"/select\">\n");
262 $r->print(" <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
264 while (my $ref = $q->fetchrow_hashref()) {
266 my $takenby = $ref->{'takenby'};
267 if (defined($ref->{'day'})) {
268 $takenby .= ", " . $ref->{'day'};
271 if ($takenby ne $lastupl) {
274 Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
275 print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
276 $r->print("</h2>\n");
278 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
279 my $width = $ref->{'width'};
280 my $height = $ref->{'height'};
282 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
283 $imgsz = " width=\"$width\" height=\"$height\"";
286 my $filename = $ref->{'filename'};
287 my $uri = $infobox . $filename;
288 if (defined($xres) && defined($yres) && $xres != -1) {
289 $uri = "${xres}x$yres/$infobox$filename";
290 } elsif (defined($xres) && $xres == -1) {
291 $uri = "original/$infobox$filename";
294 my $selected = $ref->{'selected'} ? ' checked="checked"' : '';
296 $r->print(" <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
297 $r->print(" <input type=\"checkbox\" name=\"sel-" .
298 $ref->{'id'} . "\"$selected /></p>\n");
302 $r->print(" <input type=\"submit\" value=\"Select\" />\n");
303 $r->print(" </form>\n");
305 while (my $ref = $q->fetchrow_hashref()) {
307 my $takenby = $ref->{'takenby'};
308 if (defined($ref->{'day'})) {
309 $takenby .= ", " . $ref->{'day'};
312 if ($takenby ne $lastupl) {
313 $r->print(" </p>\n\n") if ($lastupl ne "");
316 Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
317 print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
318 $r->print("</h2>\n");
319 $r->print(" <p class=\"photos\">\n");
321 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
322 my $width = $ref->{'width'};
323 my $height = $ref->{'height'};
325 ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
326 $imgsz = " width=\"$width\" height=\"$height\"";
329 my $filename = $ref->{'filename'};
330 my $uri = $infobox . $filename;
331 if (defined($xres) && defined($yres) && $xres != -1) {
332 $uri = "${xres}x$yres/$infobox$filename";
333 } elsif (defined($xres) && $xres == -1) {
334 $uri = "original/$infobox$filename";
337 $r->print(" <a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
341 $r->print(" </p>\n");
344 print_nextprev($r, $event, \%settings, \%defsettings);
345 Sesse::pr0n::Common::footer($r);
348 return Apache2::Const::OK;
354 return 1 if (!defined($a) && !defined($b));
355 return 0 unless (defined($a) && defined($b));
360 my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
362 my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
364 $r->print(" <p>$title:\n");
366 for my $a (@$alternatives) {
368 my %newsettings = %$settings;
372 ($text, $v1, $v2) = @$a;
374 $newsettings{$var1} = $v1;
375 $newsettings{$var2} = $v2;
379 # Parse the current alternative
380 my ($v1, $v2) = split /x/, $a;
382 $newsettings{$var1} = $v1;
383 $newsettings{$var2} = $v2;
388 # Check if these settings are current (print only label)
389 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
390 eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
393 Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
397 $r->print(" </p>\n");
400 sub print_thumbsize {
401 my ($r, $event, $settings, $defsettings) = @_;
402 my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
404 print_changes($r, $event, 'thumbsize', $settings, $defsettings,
405 'thumbxres', 'thumbyres', \@alternatives);
408 my ($r, $event, $settings, $defsettings) = @_;
409 my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1280x960);
410 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
411 chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
412 push @alternatives, [ $unlimited, undef, undef ];
413 push @alternatives, [ $original, -1, -1 ];
415 print_changes($r, $event, 'viewres', $settings, $defsettings,
416 'xres', 'yres', \@alternatives);
419 sub print_pagelimit {
420 my ($r, $event, $settings, $defsettings) = @_;
422 my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
424 $r->print(" <p>$title:\n");
427 chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
428 my @alternatives = qw(10 50 100 500);
429 push @alternatives, $unlimited;
431 for my $num (@alternatives) {
432 my %newsettings = %$settings;
434 if ($num !~ /^\d+$/) { # unlimited
435 $newsettings{'num'} = -1;
437 $newsettings{'num'} = $num;
441 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
444 Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
448 $r->print(" </p>\n");
452 my ($r, $event, $settings, $defsettings) = @_;
454 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
455 chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
456 chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
458 $r->print(" <p>$title:\n");
460 my %newsettings = %$settings;
462 if ($settings->{'infobox'} == 1) {
465 $newsettings{'infobox'} = 1;
466 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
471 if ($settings->{'infobox'} == 0) {
474 $newsettings{'infobox'} = 0;
475 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
482 my ($r, $event, $settings, $defsettings) = @_;
483 my $start = $settings->{'start'};
484 my $num = $settings->{'num'};
485 my $dbh = Sesse::pr0n::Common::get_dbh();
487 $num = undef if (defined($num) && $num == -1);
488 return unless (defined($start) && defined($num));
490 # determine total number
491 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS num_images FROM images WHERE event=?',
493 or dberror($r, "image enumeration");
494 my $num_images = $ref->{'num_images'};
496 return if ($start == 1 && $start + $num >= $num_images);
498 my $end = $start + $num - 1;
499 if ($end > $num_images) {
507 my $newstart = $start - $num;
511 my $newend = $newstart + $num - 1;
512 if ($newend > $num_images) {
513 $newend = $num_images;
516 my %newsettings = %$settings;
517 $newsettings{'start'} = $newstart;
518 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
519 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
520 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
524 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
525 $r->print(" $title ($start-$end)\n");
528 if ($end < $num_images) {
529 my $newstart = $start + $num;
530 my $newend = $newstart + $num - 1;
531 if ($newend > $num_images) {
532 $newend = $num_images;
535 my %newsettings = %$settings;
536 $newsettings{'start'} = $newstart;
537 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
538 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
539 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
542 $r->print(" </p>\n");
546 my ($r, $event, $settings, $defsettings) = @_;
548 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
549 chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
550 chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
552 $r->print(" <p>$title:\n");
554 my %newsettings = %$settings;
556 if ($settings->{'all'} == 0) {
559 $newsettings{'all'} = 0;
560 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
565 if ($settings->{'all'} == 1) {
568 $newsettings{'all'} = 1;
569 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
575 sub print_fullscreen {
576 my ($r, $event, $settings, $defsettings) = @_;
578 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
580 my %newsettings = %$settings;
581 $newsettings{'fullscreen'} = 1;
584 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
588 sub print_fullscreen_fromhere {
589 my ($r, $event, $settings, $defsettings, $start) = @_;
591 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
593 my %newsettings = %$settings;
594 $newsettings{'fullscreen'} = 1;
595 $newsettings{'start'} = $start;
597 $r->print(" <p class=\"fsfromhere\">");
598 Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);