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