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