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