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