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