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