Fix a warning.
[pr0n] / perl / Sesse / pr0n / Index.pm
1 package Sesse::pr0n::Index;
2 use strict;
3 use warnings;
4
5 use Sesse::pr0n::Common qw(error dberror);
6 use Apache2::Request;
7 use POSIX;
8
9 sub handler {
10         my $r = shift;
11         my $apr = Apache2::Request->new($r);
12         my $dbh = Sesse::pr0n::Common::get_dbh();
13
14         # Find the event
15         $r->uri =~ m#^/([a-zA-Z0-9-]+)/?$#
16                 or error($r, "Could not extract event");
17         my $event = $1;
18
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;
23         }
24
25         # Internal? (Ugly?) 
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;
30                 }
31         }
32
33         # Read the appropriate settings from the query string into the settings hash
34         my %defsettings = (
35                 thumbxres => 80,
36                 thumbyres => 64,
37                 xres => -1,
38                 yres => -1,
39                 start => 1,
40                 num => -1,
41                 all => 1,
42                 infobox => 1,
43                 rot => 0,
44                 sel => 0,
45                 fullscreen => 0,
46         );
47         
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',
50                 undef, $event)
51                 and $defsettings{'xres'} = $defsettings{'yres'} = undef;
52         
53         # Reduce the front page load when in overload mode.
54         if (Sesse::pr0n::Overload::is_in_overload($r)) {
55                 $defsettings{'num'} = 100;
56         }
57                 
58         my %settings = %defsettings;
59
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+)$/) {
63                         $settings{$s} = $val;
64                 }
65                 if (($s eq "num" || $s eq "xres" || $s eq "yres") && defined($val) && $val == -1) {
66                         $settings{$s} = $val;
67                 }
68         }
69
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'};
80
81         if (defined($num) && $num == -1) {
82                 $num = undef;
83         }
84
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");
88
89         my $name = $ref->{'name'};
90         my $date = $ref->{'date'};
91         $r->set_last_modified($ref->{'last_update'});
92                                 
93         # If the client can use cache, do so
94         if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
95                 return $rc;
96         }
97         
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'};
101
102         # Find all images related to this event.
103         my $q;
104         my $where = ($all == 0) ? ' AND selected=\'t\'' : '';
105
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()");
109         } else {
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()");
112         }
113         $q->execute($event)
114                 or dberror($r, "image enumeration");
115
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");
122                 }
123
124                 my %settings_no_fullscreen = %settings;
125                 $settings_no_fullscreen{'fullscreen'} = 0;
126
127                 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
128                         Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
129
130                 # *whistle*
131                 $returnurl =~ s/&/&/g;
132
133                 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
134                         vhost => $r->get_server_name,
135                         event => $event,
136                         start => $settings{'start'} - 1,
137                         returnurl => $returnurl,
138                         sel => $settings{'sel'},
139                 });
140         } else {
141                 Sesse::pr0n::Common::header($r, "$name [$event]");
142                 Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
143
144                 if (Sesse::pr0n::Overload::is_in_overload($r)) {
145                         Sesse::pr0n::Templates::print_template($r, "overloadmode");
146                 }
147
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);
155
156                 my $toclose = 0;
157                 my $lastupl = "";
158                 
159                 # Print out all thumbnails
160                 if ($rot == 1) {
161                         $r->print("    <form method=\"post\" action=\"/rotate\">\n");
162                 
163                         while (my $ref = $q->fetchrow_hashref()) {
164                                 my $imgsz = "";
165                                 my $takenby = $ref->{'takenby'};
166                                 if (defined($ref->{'day'})) {
167                                          $takenby .= ", " . $ref->{'day'};
168                                 }
169
170                                 if ($takenby ne $lastupl) {
171                                         $lastupl = $takenby;
172                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
173                                 }
174                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
175                                         my $width = $ref->{'width'};
176                                         my $height = $ref->{'height'};
177                                                 
178                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
179                                         $imgsz = " width=\"$width\" height=\"$height\"";
180                                 }
181
182                                 my $filename = $ref->{'filename'};
183                                 my $uri = $infobox . $filename;
184                                 if (defined($xres) && defined($yres) && $xres != -1) {
185                                         $uri = "${xres}x$yres/$infobox$filename";
186                                 } elsif (defined($xres) && $xres == -1) {
187                                         $uri = "original/$infobox$filename";
188                                 }
189
190                                 $r->print("    <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
191                                 $r->print("      90 <input type=\"checkbox\" name=\"rot-" .
192                                         $ref->{'id'} . "-90\" />\n");
193                                 $r->print("      180 <input type=\"checkbox\" name=\"rot-" .
194                                         $ref->{'id'} . "-180\" />\n");
195                                 $r->print("      270 <input type=\"checkbox\" name=\"rot-" .
196                                         $ref->{'id'} . "-270\" />\n");
197                                 $r->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
198                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
199                         }
200                         $r->print("      <input type=\"submit\" value=\"Rotate\" />\n");
201                         $r->print("    </form>\n");
202                 } elsif ($sel == 1) {
203                         $r->print("    <form method=\"post\" action=\"/select\">\n");
204                         $r->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
205                 
206                         while (my $ref = $q->fetchrow_hashref()) {
207                                 my $imgsz = "";
208                                 my $takenby = $ref->{'takenby'};
209                                 if (defined($ref->{'day'})) {
210                                          $takenby .= ", " . $ref->{'day'};
211                                 }
212
213                                 if ($takenby ne $lastupl) {
214                                         $lastupl = $takenby;
215                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
216                                 }
217                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
218                                         my $width = $ref->{'width'};
219                                         my $height = $ref->{'height'};
220                                                 
221                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
222                                         $imgsz = " width=\"$width\" height=\"$height\"";
223                                 }
224
225                                 my $filename = $ref->{'filename'};
226                                 my $uri = $infobox . $filename;
227                                 if (defined($xres) && defined($yres) && $xres != -1) {
228                                         $uri = "${xres}x$yres/$infobox$filename";
229                                 } elsif (defined($xres) && $xres == -1) {
230                                         $uri = "original/$infobox$filename";
231                                 }
232
233                                 my $selected = $ref->{'selected'} ? ' checked="checked"' : '';
234
235                                 $r->print("    <p><a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
236                                 $r->print("      <input type=\"checkbox\" name=\"sel-" .
237                                         $ref->{'id'} . "\"$selected /></p>\n");
238                         }
239                         $r->print("      <input type=\"submit\" value=\"Select\" />\n");
240                         $r->print("    </form>\n");
241                 } else {
242                         while (my $ref = $q->fetchrow_hashref()) {
243                                 my $imgsz = "";
244                                 my $takenby = $ref->{'takenby'};
245                                 if (defined($ref->{'day'})) {
246                                          $takenby .= ", " . $ref->{'day'};
247                                 }
248
249                                 if ($takenby ne $lastupl) {
250                                         $r->print("    </p>\n\n") if ($lastupl ne "");
251                                         $lastupl = $takenby;
252                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
253                                         $r->print("    <p>\n");
254                                 }
255                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
256                                         my $width = $ref->{'width'};
257                                         my $height = $ref->{'height'};
258                                                 
259                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
260                                         $imgsz = " width=\"$width\" height=\"$height\"";
261                                 }
262
263                                 my $filename = $ref->{'filename'};
264                                 my $uri = $infobox . $filename;
265                                 if (defined($xres) && defined($yres) && $xres != -1) {
266                                         $uri = "${xres}x$yres/$infobox$filename";
267                                 } elsif (defined($xres) && $xres == -1) {
268                                         $uri = "original/$infobox$filename";
269                                 }
270                                 
271                                 $r->print("      <a href=\"$uri\"><img src=\"${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
272                         }
273                         $r->print("    </p>\n");
274                 }
275
276                 print_nextprev($r, $event, \%settings, \%defsettings);
277                 Sesse::pr0n::Common::footer($r);
278         }
279
280         return Apache2::Const::OK;
281 }
282
283 sub eq_with_undef {
284         my ($a, $b) = @_;
285         
286         return 1 if (!defined($a) && !defined($b));
287         return 0 unless (defined($a) && defined($b));
288         return ($a eq $b);
289 }
290
291 sub print_changes {
292         my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
293
294         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
295         chomp $title;
296         $r->print("    <p>$title:\n");
297
298         for my $a (@$alternatives) {
299                 my $text;
300                 my %newsettings = %$settings;
301
302                 if (ref $a) {
303                         my ($v1, $v2);
304                         ($text, $v1, $v2) = @$a;
305                         
306                         $newsettings{$var1} = $v1;
307                         $newsettings{$var2} = $v2;
308                 } else {
309                         $text = $a;
310
311                         # Parse the current alternative
312                         my ($v1, $v2) = split /x/, $a;
313
314                         $newsettings{$var1} = $v1;
315                         $newsettings{$var2} = $v2;
316                 }
317
318                 $r->print("      ");
319
320                 # Check if these settings are current (print only label)
321                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
322                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
323                         $r->print($text);
324                 } else {
325                         Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
326                 }
327                 $r->print("\n");
328         }
329         $r->print("    </p>\n");
330 }
331
332 sub print_thumbsize {
333         my ($r, $event, $settings, $defsettings) = @_;
334         my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
335
336         print_changes($r, $event, 'thumbsize', $settings, $defsettings,
337                       'thumbxres', 'thumbyres', \@alternatives);
338 }
339 sub print_viewres {
340         my ($r, $event, $settings, $defsettings) = @_;
341         my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1280x960);
342         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
343         chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
344         push @alternatives, [ $unlimited, undef, undef ];
345         push @alternatives, [ $original, -1, -1 ];
346
347         print_changes($r, $event, 'viewres', $settings, $defsettings,
348                       'xres', 'yres', \@alternatives);
349 }
350
351 sub print_pagelimit {
352         my ($r, $event, $settings, $defsettings) = @_;
353         
354         my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
355         chomp $title;
356         $r->print("    <p>$title:\n");
357         
358         # Get choices
359         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
360         my @alternatives = qw(10 50 100 500);
361         push @alternatives, $unlimited;
362         
363         for my $num (@alternatives) {
364                 my %newsettings = %$settings;
365
366                 if ($num !~ /^\d+$/) { # unlimited
367                         $newsettings{'num'} = -1;
368                 } else {
369                         $newsettings{'num'} = $num;
370                 }
371
372                 $r->print("      ");
373                 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
374                         $r->print($num);
375                 } else {
376                         Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
377                 }
378                 $r->print("\n");
379         }
380         $r->print("    </p>\n");
381 }
382
383 sub print_infobox {
384         my ($r, $event, $settings, $defsettings) = @_;
385
386         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
387         chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
388         chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
389
390         $r->print("    <p>$title:\n");
391
392         my %newsettings = %$settings;
393
394         if ($settings->{'infobox'} == 1) {
395                 $r->print($on);
396         } else {
397                 $newsettings{'infobox'} = 1;
398                 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
399         }
400
401         $r->print(' ');
402
403         if ($settings->{'infobox'} == 0) {
404                 $r->print($off);
405         } else {
406                 $newsettings{'infobox'} = 0;
407                 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
408         }
409         
410         $r->print('</p>');
411 }
412
413 sub print_nextprev {
414         my ($r, $event, $settings, $defsettings) = @_;
415         my $start = $settings->{'start'};
416         my $num = $settings->{'num'};
417         my $dbh = Sesse::pr0n::Common::get_dbh();
418
419         $num = undef if (defined($num) && $num == -1);
420         return unless (defined($start) && defined($num));
421
422         # determine total number
423         my $ref = $dbh->selectrow_hashref('SELECT count(*) AS num_images FROM images WHERE event=?',
424                 undef, $event)
425                 or dberror($r, "image enumeration");
426         my $num_images = $ref->{'num_images'};
427
428         return if ($start == 1 && $start + $num >= $num_images);
429
430         my $end = $start + $num - 1;
431         if ($end > $num_images) {
432                 $end = $num_images;
433         }
434
435         $r->print("    <p>\n");
436
437         # Previous
438         if ($start > 1) {
439                 my $newstart = $start - $num;
440                 if ($newstart < 1) {
441                         $newstart = 1;
442                 }
443                 my $newend = $newstart + $num - 1;
444                 if ($newend > $num_images) {
445                         $newend = $num_images;
446                 }
447
448                 my %newsettings = %$settings;
449                 $newsettings{'start'} = $newstart;
450                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
451                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
452                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
453         }
454
455         # This
456         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
457         $r->print("    $title ($start-$end)\n");
458
459         # Next
460         if ($end < $num_images) {
461                 my $newstart = $start + $num;
462                 my $newend = $newstart + $num - 1;
463                 if ($newend > $num_images) {
464                         $newend = $num_images;
465                 }
466                 
467                 my %newsettings = %$settings;
468                 $newsettings{'start'} = $newstart;
469                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
470                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
471                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
472         }
473
474         $r->print("    </p>\n");
475 }
476
477 sub print_selected {
478         my ($r, $event, $settings, $defsettings) = @_;
479
480         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
481         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
482         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
483
484         $r->print("    <p>$title:\n");
485
486         my %newsettings = %$settings;
487
488         if ($settings->{'all'} == 0) {
489                 $r->print($sel);
490         } else {
491                 $newsettings{'all'} = 0;
492                 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
493         }
494
495         $r->print(' ');
496
497         if ($settings->{'all'} == 1) {
498                 $r->print($all);
499         } else {
500                 $newsettings{'all'} = 1;
501                 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
502         }
503         
504         $r->print('</p>');
505 }
506
507 sub print_fullscreen {
508         my ($r, $event, $settings, $defsettings) = @_;
509
510         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
511
512         my %newsettings = %$settings;
513         $newsettings{'fullscreen'} = 1;
514
515         $r->print("    <p>");
516         Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
517         $r->print("</p>\n");
518 }
519         
520 1;
521
522