4ec1426eeb163fa166b2074b4a112122eefd9d49
[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         my ($event, $abspath, $datesort, $tag);
15         if ($r->uri =~ /^\/\+all\/?/) {
16                 $event = '+all';
17                 $abspath = 1;
18                 $tag = undef; 
19
20                 # augh, this needs 8.3, so we'll have to fiddle around a bit instead
21                 # $datesort = 'DESC NULLS LAST';
22                 $datesort = 'DESC';
23         } elsif ($r->uri =~ /^\/\+tags\/([a-zA-Z0-9-]+)\/?$/) {
24                 $tag = $1;
25                 $event = "+tags/$tag";
26                 $abspath = 1;
27                 
28                 $datesort = 'DESC';
29         } else {
30                 # Find the event
31                 $r->uri =~ /^\/([a-zA-Z0-9-]+)\/?$/
32                         or error($r, "Could not extract event");
33                 $event = $1;
34                 $abspath = 0;
35                 $tag = undef;
36                 $datesort = 'ASC';
37         }
38
39         # Fix common error: pr0n.sesse.net/event -> pr0n.sesse.net/event/
40         if ($r->uri !~ /\/$/) {
41                 $r->headers_out->{'location'} = $r->uri . "/";
42                 return Apache2::Const::REDIRECT;
43         }
44
45         # Internal? (Ugly?) 
46         if ($r->get_server_name =~ /internal/ || $r->get_server_name =~ /skoyen\.bilder\.knatten\.com/ || $r->get_server_name =~ /lia\.heimdal\.org/) {
47                 my $user = Sesse::pr0n::Common::check_access($r);
48                 if (!defined($user)) {
49                         return Apache2::Const::OK;
50                 }
51         }
52
53         # Read the appropriate settings from the query string into the settings hash
54         my %defsettings = (
55                 thumbxres => 80,
56                 thumbyres => 64,
57                 xres => -1,
58                 yres => -1,
59                 start => 1,
60                 num => 100,
61                 all => 1,
62                 infobox => 1,
63                 rot => 0,
64                 sel => 0,
65                 fullscreen => 0,
66                 model => undef,
67                 lens => undef
68         );
69         
70         my $where;
71         if (defined($tag)) {
72                 my $tq = $dbh->quote($tag);
73                 $where = " AND id IN ( SELECT image FROM tags WHERE tag=$tq )";
74         } elsif ($event eq '+all') {
75                 $where = '';
76         } else {
77                 $where = ' AND event=' . $dbh->quote($event);
78         }
79         
80         # Any NEF files => default to processing
81         my $ref = $dbh->selectrow_hashref("SELECT * FROM images WHERE vhost=? $where AND LOWER(filename) LIKE '%.nef' LIMIT 1",
82                 undef, $r->get_server_name)
83                 and $defsettings{'xres'} = $defsettings{'yres'} = undef;
84         
85         # Reduce the front page load when in overload mode.
86         if (Sesse::pr0n::Overload::is_in_overload($r)) {
87                 $defsettings{'num'} = 100;
88         }
89                 
90         my %settings = %defsettings;
91
92         for my $s qw(thumbxres thumbyres xres yres start num all infobox rot sel fullscreen model lens) {
93                 my $val = $apr->param($s);
94                 if (defined($val) && $val =~ /^(\d+)$/) {
95                         $settings{$s} = $val;
96                 }
97                 if (($s eq "num" || $s eq "xres" || $s eq "yres") && defined($val) && $val == -1) {
98                         $settings{$s} = $val;
99                 }
100                 if ($s eq "model" || $s eq "lens") {
101                         $settings{$s} = Sesse::pr0n::Common::pretty_unescape($val);
102                 }
103         }
104
105         my $thumbxres = $settings{'thumbxres'};
106         my $thumbyres = $settings{'thumbyres'};
107         my $xres = $settings{'xres'};
108         my $yres = $settings{'yres'};
109         my $start = $settings{'start'};
110         my $num = $settings{'num'};
111         my $all = $settings{'all'};
112         my $infobox = $settings{'infobox'} ? '' : 'nobox/';
113         my $rot = $settings{'rot'};
114         my $sel = $settings{'sel'};
115         my $model = $settings{'model'};
116         my $lens = $settings{'lens'};
117
118         # Construct SQL for this filter
119         if ($all == 0) {
120                 $where .= ' AND selected=\'t\'';        
121         }
122         if (defined($model) && defined($lens)) {
123                 my $mq = $dbh->quote($model);
124                 my $lq = $dbh->quote($lens);
125
126                 if ($model eq '') {
127                         # no defined model
128                         $where .= " AND id NOT IN ( SELECT image FROM exif_info WHERE key='Model' AND TRIM(value)<>'' )";
129                 } else {
130                         $where .= " AND id IN ( SELECT image FROM exif_info WHERE key='Model' AND TRIM(value)=$mq )";
131                 }
132         
133                 # This doesn't match 1:1 if there's both lens and lensspec, but it should be OK in practice
134                 if ($lens eq '') {
135                         # no defined lens
136                         $where .= " AND id NOT IN ( SELECT image FROM exif_info WHERE (key='Lens' OR key='LensSpec') AND TRIM(value)<>'' )";
137                 } else {
138                         $where .= " AND id IN ( SELECT image FROM exif_info WHERE (key='Lens' OR key='LensSpec') AND TRIM(value)=$lq )";
139                 }
140         }
141
142         if (defined($num) && $num == -1) {
143                 $num = undef;
144         }
145
146         my ($date, $name);
147
148         if ($event eq '+all' || defined($tag)) {
149                 $ref = $dbh->selectrow_hashref("SELECT EXTRACT(EPOCH FROM MAX(last_update)) AS last_update FROM events WHERE vhost=?",
150                         undef, $r->get_server_name)
151                         or error($r, "Could not list events", 404, "File not found");
152                 $date = undef;
153                 $name = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
154                 $r->set_last_modified($ref->{'last_update'});
155         } else {
156                 $ref = $dbh->selectrow_hashref("SELECT name,date,EXTRACT(EPOCH FROM last_update) AS last_update FROM events WHERE vhost=? AND event=?",
157                         undef, $r->get_server_name, $event)
158                         or error($r, "Could not find event $event", 404, "File not found");
159
160                 $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
161                 $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
162                 $r->set_last_modified($ref->{'last_update'});
163         }
164                                 
165         # If the client can use cache, do so
166         if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
167                 return $rc;
168         }
169         
170         # Count the number of selected images.
171         $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS num_selected FROM images WHERE vhost=? $where AND selected=\'t\'", undef, $r->get_server_name);
172         my $num_selected = $ref->{'num_selected'};
173
174         # Find all images related to this event.
175         my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : "";
176
177         my $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE vhost=? $where ORDER BY COALESCE((date - INTERVAL '6 hours')::date, '1970-01-01') $datesort,takenby,date,filename $limit")
178                 or dberror($r, "prepare()");
179         $q->execute($r->get_server_name)
180                 or dberror($r, "image enumeration");
181
182         # Print the page itself
183         if ($settings{'fullscreen'}) {
184                 $r->content_type("text/html; charset=utf-8");
185
186                 if (defined($tag)) {
187                         my $title = Sesse::pr0n::Templates::process_template($r, "tag-title", { tag => $tag });
188                         Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => $title });
189                 } else {
190                         Sesse::pr0n::Templates::print_template($r, "fullscreen-header", { title => "$name [$event]" });
191                 }
192
193                 my @files = ();
194                 while (my $ref = $q->fetchrow_hashref()) {
195                         push @files, [ $ref->{'event'}, $ref->{'filename'} ];
196                 }
197                 
198                 for my $i (0..$#files) {
199                         $r->print("        [ \"" . $files[$i]->[0] . "\", \"" . $files[$i]->[1] . "\" ]");
200                         $r->print(",") unless ($i == $#files);
201                         $r->print("\n");
202                 }
203
204                 my %settings_no_fullscreen = %settings;
205                 $settings_no_fullscreen{'fullscreen'} = 0;
206
207                 my $returnurl = "http://" . $r->get_server_name . "/" . $event . "/" .
208                         Sesse::pr0n::Common::get_query_string(\%settings_no_fullscreen, \%defsettings);
209
210                 # *whistle*
211                 $returnurl =~ s/&amp;/&/g;
212
213                 Sesse::pr0n::Templates::print_template($r, "fullscreen-footer", {
214                         vhost => $r->get_server_name,
215                         start => $settings{'start'} - 1,
216                         returnurl => $returnurl,
217                         sel => $settings{'sel'},
218                         infobox => $infobox
219                 });
220         } else {
221                 if (defined($tag)) {
222                         my $title = Sesse::pr0n::Templates::process_template($r, "tag-title", { tag => $tag });
223                         Sesse::pr0n::Common::header($r, $title);
224                 } else {
225                         Sesse::pr0n::Common::header($r, "$name [$event]");
226                 }
227                 if (defined($date)) {
228                         Sesse::pr0n::Templates::print_template($r, "date", { date => $date });
229                 }
230
231                 if (Sesse::pr0n::Overload::is_in_overload($r)) {
232                         Sesse::pr0n::Templates::print_template($r, "overloadmode");
233                 }
234
235                 print_thumbsize($r, $event, \%settings, \%defsettings);
236                 print_viewres($r, $event, \%settings, \%defsettings);
237                 print_pagelimit($r, $event, \%settings, \%defsettings);
238                 print_infobox($r, $event, \%settings, \%defsettings);
239                 print_selected($r, $event, \%settings, \%defsettings) if ($num_selected > 0);
240                 print_fullscreen($r, $event, \%settings, \%defsettings);
241                 print_nextprev($r, $event, $where, \%settings, \%defsettings);
242         
243                 if (1 || $event ne '+all') {
244                         # Find the equipment used
245                         my $eq = $dbh->prepare("
246                                 SELECT 
247                                         TRIM(model.value) AS model,
248                                         coalesce(TRIM(lens_spec.value), TRIM(lens.value)) AS lens,
249                                         COUNT(*) AS num
250                                 FROM ( SELECT * FROM images WHERE vhost=? $where ) i
251                                         LEFT JOIN exif_info model ON i.id=model.image
252                                         LEFT JOIN ( SELECT * FROM exif_info WHERE key='Lens' ) lens ON i.id=lens.image
253                                         LEFT JOIN ( SELECT * FROM exif_info WHERE key='LensSpec') lens_spec ON i.id=lens_spec.image
254                                 WHERE model.key='Model'
255                                 GROUP BY 1,2
256                                 ORDER BY 1,2")
257                                 or die "Couldn't prepare to find equipment: $!";
258                         $eq->execute($r->get_server_name)
259                                 or die "Couldn't find equipment: $!";
260
261                         my @equipment = ();
262                         my %cameras_seen = ();
263                         while (my $ref = $eq->fetchrow_hashref) {
264                                 if (!defined($ref->{'lens'}) && exists($cameras_seen{$ref->{'model'}})) {
265                                         #
266                                         # Some compact cameras seem to add lens info sometimes and not at other
267                                         # times; if we have seen a camera with at least one specific lens earlier,
268                                         # just combine entries without a lens with the previous one.
269                                         #
270                                         $equipment[$#equipment]->{'num'} += $ref->{'num'};
271                                         next;
272                                 }
273                                 push @equipment, $ref;
274                                 $cameras_seen{$ref->{'model'}} = 1;
275                         }
276                         $eq->finish;
277
278                         if (scalar @equipment > 0) {
279                                 Sesse::pr0n::Templates::print_template($r, "equipment-start");
280                                 for my $e (@equipment) {
281                                         my $eqspec = $e->{'model'};
282                                         $eqspec .= ', ' . $e->{'lens'} if (defined($e->{'lens'}));
283                                         $eqspec = HTML::Entities::encode_entities($eqspec);
284
285                                         my %newsettings = %settings;
286
287                                         my $action;
288                                         if (defined($model) && defined($lens)) {
289                                                 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "unfilter"));
290                                                 $newsettings{'model'} = undef;
291                                                 $newsettings{'lens'} = undef;
292                                         } else {
293                                                 chomp ($action = Sesse::pr0n::Templates::fetch_template($r, "filter"));
294                                                 $newsettings{'model'} = $e->{'model'};
295                                                 $newsettings{'lens'} = defined($e->{'lens'}) ? $e->{'lens'} : '';
296                                         }
297                                         
298                                         my $url = "/$event/" . Sesse::pr0n::Common::get_query_string(\%newsettings, \%defsettings);
299
300                                         # This isn't correct for all languages. Fix if we ever need to care. :-)
301                                         if ($e->{'num'} == 1) {
302                                                 Sesse::pr0n::Templates::print_template($r, "equipment-item-singular", { eqspec => $eqspec, filterurl => $url, action => $action });
303                                         } else {
304                                                 Sesse::pr0n::Templates::print_template($r, "equipment-item", { eqspec => $eqspec, num => $e->{'num'}, filterurl => $url, action => $action });
305                                         }
306                                 }
307                                 Sesse::pr0n::Templates::print_template($r, "equipment-end");
308                         }
309                 }
310
311                 my $toclose = 0;
312                 my $lastupl = "";
313                 my $img_num = (defined($start) && defined($num)) ? $start : 1;
314                 
315                 # Print out all thumbnails
316                 if ($rot == 1) {
317                         $r->print("    <form method=\"post\" action=\"/rotate\">\n");
318                         $r->print("      <input type=\"hidden\" name=\"event\" value=\"$event\" />\n");
319         
320                         while (my $ref = $q->fetchrow_hashref()) {
321                                 my $imgsz = "";
322                                 my $takenby = $ref->{'takenby'};
323                                 if (defined($ref->{'day'})) {
324                                          $takenby .= ", " . $ref->{'day'};
325                                 }
326
327                                 if ($takenby ne $lastupl) {
328                                         $lastupl = $takenby;
329                                         $r->print("    <h2>");
330                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
331                                         print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
332                                         $r->print("</h2>\n");
333                                 }
334                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
335                                         my $width = $ref->{'width'};
336                                         my $height = $ref->{'height'};
337                                                 
338                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
339                                         $imgsz = " width=\"$width\" height=\"$height\"";
340                                 }
341
342                                 my $filename = $ref->{'filename'};
343                                 my $uri = $infobox . $filename;
344                                 if (defined($xres) && defined($yres) && $xres != -1) {
345                                         $uri = "${xres}x$yres/$infobox$filename";
346                                 } elsif (defined($xres) && $xres == -1) {
347                                         $uri = "original/$infobox$filename";
348                                 }
349
350                                 my $prefix = "";
351                                 if ($abspath) {
352                                         $prefix = "/" . $ref->{'event'} . "/";
353                                 }
354
355                                 $r->print("    <p><a href=\"$prefix$uri\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
356                                 $r->print("      90 <input type=\"checkbox\" name=\"rot-" .
357                                         $ref->{'id'} . "-90\" />\n");
358                                 $r->print("      180 <input type=\"checkbox\" name=\"rot-" .
359                                         $ref->{'id'} . "-180\" />\n");
360                                 $r->print("      270 <input type=\"checkbox\" name=\"rot-" .
361                                         $ref->{'id'} . "-270\" />\n");
362                                 $r->print("      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
363                                         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Del <input type=\"checkbox\" name=\"del-" . $ref->{'id'} . "\" /></p>\n");
364                         
365                                 ++$img_num;
366                         }
367                         $r->print("      <input type=\"submit\" value=\"Rotate\" />\n");
368                         $r->print("    </form>\n");
369                 } else {
370                         while (my $ref = $q->fetchrow_hashref()) {
371                                 my $imgsz = "";
372                                 my $takenby = $ref->{'takenby'};
373                                 if (defined($ref->{'day'})) {
374                                          $takenby .= ", " . $ref->{'day'};
375                                 }
376
377                                 if ($takenby ne $lastupl) {
378                                         $r->print("    </p>\n\n") if ($lastupl ne "");
379                                         $lastupl = $takenby;
380                                         $r->print("    <h2>");
381                                         Sesse::pr0n::Templates::print_template($r, "submittedby", { author => $lastupl });
382                                         print_fullscreen_fromhere($r, $event, \%settings, \%defsettings, $img_num);
383                                         $r->print("</h2>\n");
384                                         $r->print("    <p class=\"photos\">\n");
385                                 }
386                                 if ($ref->{'width'} != -1 && $ref->{'height'} != -1) {
387                                         my $width = $ref->{'width'};
388                                         my $height = $ref->{'height'};
389                                                 
390                                         ($width, $height) = Sesse::pr0n::Common::scale_aspect($width, $height, $thumbxres, $thumbyres);
391                                         $imgsz = " width=\"$width\" height=\"$height\"";
392                                 }
393
394                                 my $filename = $ref->{'filename'};
395                                 my $uri = $infobox . $filename;
396                                 if (defined($xres) && defined($yres) && $xres != -1) {
397                                         $uri = "${xres}x$yres/$infobox$filename";
398                                 } elsif (defined($xres) && $xres == -1) {
399                                         $uri = "original/$infobox$filename";
400                                 }
401                                 
402                                 my $prefix = "";
403                                 if ($abspath) {
404                                         $prefix = "/" . $ref->{'event'} . "/";
405                                 }
406                                 
407                                 $r->print("      <a href=\"$prefix$uri\"><img src=\"$prefix${thumbxres}x${thumbyres}/$filename\" alt=\"\"$imgsz /></a>\n");
408                                 
409                                 ++$img_num;
410                         }
411                         $r->print("    </p>\n");
412                 }
413
414                 print_nextprev($r, $event, $where, \%settings, \%defsettings);
415                 Sesse::pr0n::Common::footer($r);
416         }
417
418         return Apache2::Const::OK;
419 }
420
421 sub eq_with_undef {
422         my ($a, $b) = @_;
423         
424         return 1 if (!defined($a) && !defined($b));
425         return 0 unless (defined($a) && defined($b));
426         return ($a eq $b);
427 }
428
429 sub print_changes {
430         my ($r, $event, $template, $settings, $defsettings, $var1, $var2, $alternatives) = @_;
431
432         my $title = Sesse::pr0n::Templates::fetch_template($r, $template);
433         chomp $title;
434         $r->print("    <p>$title:\n");
435
436         for my $a (@$alternatives) {
437                 my $text;
438                 my %newsettings = %$settings;
439
440                 if (ref $a) {
441                         my ($v1, $v2);
442                         ($text, $v1, $v2) = @$a;
443                         
444                         $newsettings{$var1} = $v1;
445                         $newsettings{$var2} = $v2;
446                 } else {
447                         $text = $a;
448
449                         # Parse the current alternative
450                         my ($v1, $v2) = split /x/, $a;
451
452                         $newsettings{$var1} = $v1;
453                         $newsettings{$var2} = $v2;
454                 }
455
456                 $r->print("      ");
457
458                 # Check if these settings are current (print only label)
459                 if (eq_with_undef($settings->{$var1}, $newsettings{$var1}) &&
460                     eq_with_undef($settings->{$var2}, $newsettings{$var2})) {
461                         $r->print($text);
462                 } else {
463                         Sesse::pr0n::Common::print_link($r, $text, "/$event/", \%newsettings, $defsettings);
464                 }
465                 $r->print("\n");
466         }
467         $r->print("    </p>\n");
468 }
469
470 sub print_thumbsize {
471         my ($r, $event, $settings, $defsettings) = @_;
472         my @alternatives = qw(80x64 120x96 160x128 240x192 320x256);
473
474         print_changes($r, $event, 'thumbsize', $settings, $defsettings,
475                       'thumbxres', 'thumbyres', \@alternatives);
476 }
477 sub print_viewres {
478         my ($r, $event, $settings, $defsettings) = @_;
479         my @alternatives = qw(320x256 512x384 640x480 800x600 1024x768 1280x960);
480         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'viewres-unlimited'));
481         chomp (my $original = Sesse::pr0n::Templates::fetch_template($r, 'viewres-original'));
482         push @alternatives, [ $unlimited, undef, undef ];
483         push @alternatives, [ $original, -1, -1 ];
484
485         print_changes($r, $event, 'viewres', $settings, $defsettings,
486                       'xres', 'yres', \@alternatives);
487 }
488
489 sub print_pagelimit {
490         my ($r, $event, $settings, $defsettings) = @_;
491         
492         my $title = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage');
493         chomp $title;
494         $r->print("    <p>$title:\n");
495         
496         # Get choices
497         chomp (my $unlimited = Sesse::pr0n::Templates::fetch_template($r, 'imgsperpage-unlimited'));
498         my @alternatives = qw(10 50 100 500);
499         push @alternatives, $unlimited;
500         
501         for my $num (@alternatives) {
502                 my %newsettings = %$settings;
503
504                 if ($num !~ /^\d+$/) { # unlimited
505                         $newsettings{'num'} = -1;
506                 } else {
507                         $newsettings{'num'} = $num;
508                 }
509
510                 $r->print("      ");
511                 if (eq_with_undef($settings->{'num'}, $newsettings{'num'})) {
512                         $r->print($num);
513                 } else {
514                         Sesse::pr0n::Common::print_link($r, $num, "/$event/", \%newsettings, $defsettings);
515                 }
516                 $r->print("\n");
517         }
518         $r->print("    </p>\n");
519 }
520
521 sub print_infobox {
522         my ($r, $event, $settings, $defsettings) = @_;
523
524         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'infobox'));
525         chomp (my $on = Sesse::pr0n::Templates::fetch_template($r, 'infobox-on'));
526         chomp (my $off = Sesse::pr0n::Templates::fetch_template($r, 'infobox-off'));
527
528         $r->print("    <p>$title:\n");
529
530         my %newsettings = %$settings;
531
532         if ($settings->{'infobox'} == 1) {
533                 $r->print($on);
534         } else {
535                 $newsettings{'infobox'} = 1;
536                 Sesse::pr0n::Common::print_link($r, $on, "/$event/", \%newsettings, $defsettings);
537         }
538
539         $r->print(' ');
540
541         if ($settings->{'infobox'} == 0) {
542                 $r->print($off);
543         } else {
544                 $newsettings{'infobox'} = 0;
545                 Sesse::pr0n::Common::print_link($r, $off, "/$event/", \%newsettings, $defsettings);
546         }
547         
548         $r->print('</p>');
549 }
550
551 sub print_nextprev {
552         my ($r, $event, $where, $settings, $defsettings) = @_;
553         my $start = $settings->{'start'};
554         my $num = $settings->{'num'};
555         my $dbh = Sesse::pr0n::Common::get_dbh();
556
557         $num = undef if (defined($num) && $num == -1);
558         return unless (defined($start) && defined($num));
559
560         # determine total number
561         my $ref = $dbh->selectrow_hashref("SELECT count(*) AS num_images FROM images WHERE vhost=? $where",
562                 undef, $r->get_server_name)
563                 or dberror($r, "image enumeration");
564         my $num_images = $ref->{'num_images'};
565
566         return if ($start == 1 && $start + $num >= $num_images);
567
568         my $end = $start + $num - 1;
569         if ($end > $num_images) {
570                 $end = $num_images;
571         }
572
573         $r->print("    <p class=\"nextprev\">\n");
574
575         # Previous
576         if ($start > 1) {
577                 my $newstart = $start - $num;
578                 if ($newstart < 1) {
579                         $newstart = 1;
580                 }
581                 my $newend = $newstart + $num - 1;
582                 if ($newend > $num_images) {
583                         $newend = $num_images;
584                 }
585
586                 my %newsettings = %$settings;
587                 $newsettings{'start'} = $newstart;
588                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'prevpage'));
589                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'prevaccesskey'));
590                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)\n", "/$event/", \%newsettings, $defsettings, $accesskey);
591         }
592
593         # This
594         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'thispage'));
595         $r->print("    $title ($start-$end)\n");
596
597         # Next
598         if ($end < $num_images) {
599                 my $newstart = $start + $num;
600                 my $newend = $newstart + $num - 1;
601                 if ($newend > $num_images) {
602                         $newend = $num_images;
603                 }
604                 
605                 my %newsettings = %$settings;
606                 $newsettings{'start'} = $newstart;
607                 chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'nextpage'));
608                 chomp (my $accesskey = Sesse::pr0n::Templates::fetch_template($r, 'nextaccesskey'));
609                 Sesse::pr0n::Common::print_link($r, "$title ($newstart-$newend)", "/$event/", \%newsettings, $defsettings, $accesskey);
610         }
611
612         $r->print("    </p>\n");
613 }
614
615 sub print_selected {
616         my ($r, $event, $settings, $defsettings) = @_;
617
618         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'show'));
619         chomp (my $all = Sesse::pr0n::Templates::fetch_template($r, 'show-all'));
620         chomp (my $sel = Sesse::pr0n::Templates::fetch_template($r, 'show-selected'));
621
622         $r->print("    <p>$title:\n");
623
624         my %newsettings = %$settings;
625
626         if ($settings->{'all'} == 0) {
627                 $r->print($sel);
628         } else {
629                 $newsettings{'all'} = 0;
630                 Sesse::pr0n::Common::print_link($r, $sel, "/$event/", \%newsettings, $defsettings);
631         }
632
633         $r->print(' ');
634
635         if ($settings->{'all'} == 1) {
636                 $r->print($all);
637         } else {
638                 $newsettings{'all'} = 1;
639                 Sesse::pr0n::Common::print_link($r, $all, "/$event/", \%newsettings, $defsettings);
640         }
641         
642         $r->print('</p>');
643 }
644
645 sub print_fullscreen {
646         my ($r, $event, $settings, $defsettings) = @_;
647
648         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen'));
649
650         my %newsettings = %$settings;
651         $newsettings{'fullscreen'} = 1;
652
653         $r->print("    <p>");
654         Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
655         $r->print("</p>\n");
656 }
657
658 sub print_fullscreen_fromhere {
659         my ($r, $event, $settings, $defsettings, $start) = @_;
660
661         chomp (my $title = Sesse::pr0n::Templates::fetch_template($r, 'fullscreen-fromhere'));
662
663         my %newsettings = %$settings;
664         $newsettings{'fullscreen'} = 1;
665         $newsettings{'start'} = $start;
666
667         $r->print("    <p class=\"fsfromhere\">");
668         Sesse::pr0n::Common::print_link($r, $title, "/$event/", \%newsettings, $defsettings);
669         $r->print("</p>\n");
670 }
671         
672 1;
673
674