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