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