]> git.decadent.org.uk Git - memories.git/blob - Memories.pm
Use configured lock directory, not hard-coded.
[memories.git] / Memories.pm
1 package Memories;
2 use strict;
3 our $VERSION = "1.2";
4 use Maypole::Application qw(Authentication::UserSessionCookie);
5 use HTML::TagCloud;
6 use URI;
7 use Memories::Config;
8 use Memories::DBI;
9 use Memories::Photo;
10 use Memories::Comment;
11 use Memories::Tag;
12 use Memories::SystemTag;
13 use Memories::User;
14 use Memories::Album;
15 use URI::Escape;
16 use Calendar::Simple;
17 use XML::RSS;
18 use Tagtools;
19
20 Memories->config->auth->{ user_field } = "name";
21 Memories->config->model("Maypole::Model::CDBI::Plain");
22 Memories->setup([qw/ Memories::Photo Memories::User Memories::Tag
23 Memories::Album Memories::SystemTag/]);
24
25 sub message {
26     my ($self, $message) = @_;
27     push @{$self->{template_args}{messages}}, $message;
28 }
29
30 sub check_credentials {
31     my $r = shift;
32     my ($uid, $user) = $r->SUPER::check_credentials;
33     #if (!$uid) { return (-1, undef) }
34     return ($uid, $user);
35 }
36
37 sub do_rss {
38     my $r = shift;
39     $r->model_class->process($r);
40     my $photos = $r->get_photos;
41     my $rss = XML::RSS->new(version => "2.0");
42     $rss->channel(
43         title => ($r->config->{application_name}. " : ".ucfirst($r->action)." ".ucfirst($r->table)." ".($r->objects||[])->[0]) ,
44         link  => $r->config->{uri_base}."/".$r->path
45     );
46     for my $item (@$photos) { 
47         my $link = $r->config->{uri_base}."photo/view/".$item->id;
48         $rss->add_item( title => $item->title, link => $link,
49             description => 
50     "<a href=\"$link\">
51     <img src=\"". $item->thumb_url."\" alt=\"".$item->title."\"></a>",
52             dc => { subject => join " ", $item->tags },
53             pubDate => $item->uploaded->strftime("%a, %d %b %Y %H:%M:%S %z")
54         )
55     }
56     $r->output($rss->as_string);
57     $r->content_type("application/rss+xml");
58     return
59 }
60
61 sub get_photos {
62     my $r = shift;
63     my $maybe_photos = $r->{objects}||[];
64     return (@$maybe_photos && $maybe_photos->[0]->isa("Memories::Photo")) 
65             ? $maybe_photos :
66             ($r->{template_args}->{photos} || []);
67 }
68
69 sub last_search {
70     my $r = shift;
71     my $photos = $r->get_photos; 
72     $r->{session}{last_search} = join ",", map { $_->id } @$photos 
73         if @$photos > 1;
74 }
75
76 sub additional_data { 
77     my $r = shift;
78     if ($r->params->{view_cal}) { 
79     $r->{template_args}{view_cal} = eval {
80             Time::Piece->strptime($r->{params}{view_cal}, "%Y-%m-%d") }; 
81     }
82     $r->{template_args}{now} = Time::Piece->new;
83     if ($r->session) {
84         (tied %{$r->session})->{lock_manager}->clean(Memories->config->{auth}{session_args}{LockDirectory}, 3600) #remove files older than 1 hour
85     }
86     return $r->do_rss if ($r->params->{format} =~ /rss/)
87 }
88
89 use Maypole::Constants;
90 sub authenticate {
91    my ($self, $r) = @_;
92    return DECLINED if $self->path =~/static|store/; # XXX
93    $r->get_user;
94    if (!$r->user and $self->path =~ /upload/) { $r->template("login"); }
95    # Don't let 'em go until they've fixed it
96    if ($r->session and $r->session->{quarantined} and $self->path !~ /js$/) { 
97        $r->table("photo"); $r->action("quarantine");
98        $r->model_class("Memories::Photo");
99    }
100    return OK; 
101 }
102
103 for my $how (qw(random recent interesting popular)) {
104     no strict;
105     my $method = "search_$how";
106     *{"_$how"} = sub { Memories::Photo->$method };
107     *{$how} = sub { shift->do_cached(\&{"_$how"})};
108 }
109
110 sub tag_select {
111     my ($r, $tags) = @_;
112     my %counter;
113     my @photos = Memories::Photo->sth_to_objects(Memories::Tag->multi_search(@$tags));
114     for (map {$_->tags} @photos) { 
115         $counter{$_->name}++; 
116     } 
117     delete $counter{$_->name} for @$tags;
118     my @super;
119
120     my $cloud = HTML::TagCloud->new();
121     my $base = $r->config->uri_base.$r->path."/";
122     my $tags;
123     for my $name (sort {$a cmp $b} keys %counter) {
124         if ($counter{$name} == @photos) {
125             push @super, $name;
126         } else {
127             $cloud->add($name, $base.uri_escape($name), $counter{$name});
128             $tags++;
129         }
130     }
131     my %res;
132     if (@super) { $res{super} = \@super }
133     if ($tags) { $res{cloud} = $cloud }
134     \%res;
135 }
136 1;