2cb1125277e3be0e92d8df47b29792902b7d2a6c
[pr0n] / perl / Sesse / pr0n / Listing.pm
1 package Sesse::pr0n::Listing;
2 use strict;
3 use warnings;
4
5 use Sesse::pr0n::Common qw(error dberror);
6 use HTML::TagCloud;
7
8 sub handler {
9         my $r = shift;
10         my $dbh = Sesse::pr0n::Common::get_dbh();
11
12         # Internal? (Ugly?)
13         if ($r->get_server_name =~ /internal/ || $r->get_server_name =~ /skoyen\.bilder\.knatten\.com/ || $r->get_server_name =~ /lia\.heimdal\.org/) {
14                 my $user = Sesse::pr0n::Common::check_access($r);
15                 if (!defined($user)) {
16                         return Apache2::Const::OK;
17                 }
18         }
19         
20         # Fix common error: pr0n.sesse.net/+foo -> pr0n.sesse.net/+foo/
21         if ($r->uri !~ /\/$/) {
22                 $r->headers_out->{'location'} = $r->uri . "/";
23                 return Apache2::Const::REDIRECT;
24         }
25         
26         # find the last modification
27         my $ref = $dbh->selectrow_hashref('SELECT EXTRACT(EPOCH FROM last_update) AS last_update FROM last_picture_cache WHERE vhost=? ORDER BY last_update DESC LIMIT 1',
28                 undef, $r->get_server_name)
29                 or error($r, "Could not find any events", 404, "File not found");
30         $r->set_last_modified($ref->{'last_update'});
31         $r->content_type('text/html; charset=utf-8');
32                                 
33         # If the client can use cache, do so
34         if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
35                 return $rc;
36         }
37         
38         if ($r->uri =~ /^\/\+tags\/?/) {
39                 # Tag cloud
40                 my $q = $dbh->prepare('SELECT tag,COUNT(*) AS frequency FROM tags t JOIN images i ON t.image=i.id WHERE vhost=? GROUP BY tag ORDER BY COUNT(*) DESC LIMIT 75')
41                         or dberror($r, "Couldn't list events");
42                 $q->execute($r->get_server_name)
43                         or dberror($r, "Couldn't get events");
44                 
45                 Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'tag-listing'));
46                 Sesse::pr0n::Templates::print_template($r, 'mainmenu-tags');
47
48                 my $cloud = HTML::TagCloud->new;
49
50                 while (my $ref = $q->fetchrow_hashref()) {
51                         my $tag = Encode::decode_utf8($ref->{'tag'});
52                         my $html = HTML::Entities::encode_entities($tag);    # is this right?
53                         my $uri = Sesse::pr0n::Common::pretty_escape($tag);  # and this?
54
55                         $cloud->add($html, "/+tags/$uri/", $ref->{'frequency'});
56                 }
57
58                 $r->print($cloud->html_and_css());
59                 Sesse::pr0n::Common::footer($r);
60
61                 $q->finish();
62         } else {
63                 # main listing
64 #               my $q = $dbh->prepare('SELECT t1.id,t1.date,t1.name FROM events t1 LEFT JOIN images t2 ON t1.id=t2.event WHERE t1.vhost=? GROUP BY t1.id,t1.date,t1.name ORDER BY COALESCE(MAX(t2.date),\'1970-01-01 00:00:00\'),t1.id') or
65 #                       dberror($r, "Couldn't list events");
66                 my $q = $dbh->prepare('SELECT event,date,name FROM events e JOIN last_picture_cache c USING (vhost,event) WHERE vhost=? ORDER BY last_picture DESC NULLS LAST')
67                         or dberror($r, "Couldn't list events");
68                 $q->execute($r->get_server_name)
69                         or dberror($r, "Couldn't get events");
70                 
71                 Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'event-listing'));
72
73                 # See if there are any tags related to this vhost
74                 my $ref = $dbh->selectrow_hashref('SELECT * FROM tags t JOIN images i ON t.image=i.id WHERE vhost=? LIMIT 1',
75                         undef, $r->get_server_name);
76                 if (defined($ref)) {
77                         Sesse::pr0n::Templates::print_template($r, 'mainmenu-events');
78                 }
79
80                 my $allcaption = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
81                 $r->print("    <ul>\n");
82                 $r->print("      <li><a href=\"+all/\">$allcaption</a></li>\n");
83                 $r->print("    </ul>\n");
84                 
85                 $r->print("    <ul>\n");
86
87                 while (my $ref = $q->fetchrow_hashref()) {
88                         my $id = $ref->{'event'};
89                         my $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
90                         my $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
91                         
92                         $r->print("      <li><a href=\"$id/\">$name</a> ($date)</li>\n");
93                 }
94
95                 $r->print("    </ul>\n");
96                 Sesse::pr0n::Common::footer($r);
97
98                 $q->finish();
99         }
100
101         return Apache2::Const::OK;
102 }
103
104 1;
105
106