line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::ShareHelpers; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1042
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
55
|
|
4
|
1
|
|
|
1
|
|
9
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
54
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
31
|
use Mojo::ByteStream 'b'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
105
|
|
7
|
1
|
|
|
1
|
|
6
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.7'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $APP; # for app instance |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has url => sub { +{ |
14
|
|
|
|
|
|
|
'twitter' => 'http://twitter.com/share', |
15
|
|
|
|
|
|
|
'facebook' => 'http://facebook.com/sharer.php', |
16
|
|
|
|
|
|
|
'vkontakte' => 'http://vk.com/share.php', |
17
|
|
|
|
|
|
|
'mymailru' => 'http://connect.mail.ru/share', |
18
|
|
|
|
|
|
|
'google+' => 'http://plus.google.com', |
19
|
|
|
|
|
|
|
} }; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub register { |
22
|
1
|
|
|
1
|
1
|
46
|
my($self, $app) = @_; |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
3
|
$APP = $app; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
16
|
|
11
|
$app->helper( share_url => sub { $self->share_url ( @_ ) } ); |
|
16
|
|
|
|
|
209060
|
|
27
|
1
|
|
|
27
|
|
102
|
$app->helper( share_button => sub { $self->share_button ( @_ ) } ); |
|
27
|
|
|
|
|
59930
|
|
28
|
1
|
|
|
6
|
|
58
|
$app->helper( share_meta => sub { $self->share_meta ( @_ ) } ); |
|
6
|
|
|
|
|
45398
|
|
29
|
1
|
|
|
3
|
|
65
|
$app->helper( is_share_agent => sub { $self->is_share_agent( @_ ) } ); |
|
3
|
|
|
|
|
58740
|
|
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub share_url { |
33
|
16
|
|
|
16
|
0
|
28
|
my($self, $c) = (shift, shift); |
34
|
|
|
|
|
|
|
|
35
|
16
|
|
|
|
|
27
|
my $type = shift; |
36
|
16
|
100
|
|
|
|
43
|
return '' unless $self->_check_type( $type ); |
37
|
|
|
|
|
|
|
|
38
|
14
|
|
|
|
|
51
|
my %args = @_; |
39
|
|
|
|
|
|
|
|
40
|
14
|
|
|
|
|
21
|
my $param; |
41
|
14
|
100
|
|
|
|
68
|
if ($type eq 'twitter') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
42
|
4
|
|
|
|
|
46
|
$param->{$_} = $args{$_} for qw(url via text related count lang counturl); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
elsif ($type eq 'facebook') { |
45
|
2
|
|
|
|
|
9
|
$param->{u} = $args{url }; |
46
|
2
|
|
|
|
|
6
|
$param->{t} = $args{text}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
elsif ($type eq 'vkontakte') { |
49
|
2
|
|
|
|
|
7
|
$param->{url} = $args{url}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
elsif ($type eq 'mymailru') { |
52
|
6
|
|
|
|
|
18
|
$param->{share_url} = $args{url}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
elsif ($type eq 'google+') { |
55
|
0
|
|
|
|
|
0
|
$APP->log->error("Google Plus doen't have share URL, use share_button"); |
56
|
0
|
|
|
|
|
0
|
return ''; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
14
|
|
|
|
|
75
|
my @p = grep { $param->{$_} } sort keys %$param; |
|
40
|
|
|
|
|
137
|
|
60
|
18
|
|
|
|
|
812
|
return join '?', $self->url->{ $type }, |
61
|
14
|
100
|
|
|
|
945
|
@p ? join '&', map { $_ . '=' . b( $param->{$_} )->url_escape } @p : () |
62
|
|
|
|
|
|
|
; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub share_button { |
66
|
27
|
|
|
27
|
0
|
46
|
my($self, $c) = (shift, shift); |
67
|
|
|
|
|
|
|
|
68
|
27
|
|
|
|
|
32
|
my $type = shift; |
69
|
27
|
100
|
|
|
|
90
|
return '' unless $self->_check_type( $type ); |
70
|
|
|
|
|
|
|
|
71
|
25
|
|
|
|
|
81
|
my %args = @_; |
72
|
|
|
|
|
|
|
|
73
|
25
|
|
|
|
|
27
|
my $button; |
74
|
25
|
100
|
|
|
|
110
|
if ($type eq 'twitter') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
75
|
4
|
100
|
|
|
|
10
|
if ($args{iframe}) { |
76
|
2
|
|
|
|
|
32
|
my $url = $c->share_url( $type, @_ ); |
77
|
2
|
|
|
|
|
85
|
my($param) = $url =~ /.*\?(.*)/; |
78
|
|
|
|
|
|
|
|
79
|
2
|
|
|
|
|
12
|
$button = |
80
|
|
|
|
|
|
|
qq( |
81
|
|
|
|
|
|
|
qq(src="http://platform.twitter.com/widgets/tweet_button.html?$param">) |
82
|
|
|
|
|
|
|
; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
2
|
|
|
|
|
2
|
my $attr; push @$attr, qq(data-$_="$args{$_}") |
|
14
|
|
|
|
|
37
|
|
86
|
2
|
|
|
|
|
6
|
for grep { $args{$_} } qw(url via text related count lang counturl); |
87
|
2
|
|
|
|
|
7
|
my $param = join ' ', @$attr; |
88
|
|
|
|
|
|
|
|
89
|
2
|
|
|
|
|
9
|
$button = |
90
|
|
|
|
|
|
|
qq() . |
91
|
|
|
|
|
|
|
qq() |
92
|
|
|
|
|
|
|
; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
elsif ($type eq 'facebook') { |
96
|
4
|
100
|
|
|
|
11
|
if ($args{fb}) { |
97
|
2
|
|
|
|
|
11
|
my $attr = { type => $args{type}, href => $args{url}, class => $args{class} }; |
98
|
2
|
|
|
|
|
11
|
my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr; |
|
4
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
11
|
|
99
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
9
|
$button = |
101
|
|
|
|
|
|
|
qq() |
102
|
|
|
|
|
|
|
; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
else { |
105
|
2
|
|
|
|
|
10
|
my $attr = { type => $args{type}, share_url => $args{url} }; |
106
|
2
|
|
|
|
|
13
|
my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr; |
|
4
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
33
|
|
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
12
|
$button = |
109
|
|
|
|
|
|
|
qq($args{title}) . |
110
|
|
|
|
|
|
|
qq() |
111
|
|
|
|
|
|
|
; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
elsif ($type eq 'vkontakte') { |
115
|
4
|
100
|
|
|
|
13
|
my $url = $args{url} ? qq({url: "$args{url}"}) : 'false'; |
116
|
4
|
|
|
|
|
13
|
my $attr = { type => $args{type}, text => $args{title} }; |
117
|
4
|
|
|
|
|
14
|
my $param = join ', ', map { qq($_: "$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr; |
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
14
|
|
118
|
|
|
|
|
|
|
|
119
|
4
|
|
|
|
|
15
|
$button = |
120
|
|
|
|
|
|
|
qq() . |
121
|
|
|
|
|
|
|
qq() |
122
|
|
|
|
|
|
|
; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
elsif ($type eq 'mymailru') { |
125
|
1
|
|
|
1
|
|
1656
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
126
|
4
|
|
|
|
|
39
|
my $url = $c->share_url( $type, @_ ); |
127
|
|
|
|
|
|
|
|
128
|
4
|
|
50
|
|
|
163
|
$args{type } ||= ''; |
129
|
4
|
|
50
|
|
|
13
|
$args{title} ||= 'В Мой Мир'; |
130
|
|
|
|
|
|
|
|
131
|
4
|
|
|
|
|
19
|
$button = |
132
|
|
|
|
|
|
|
qq() . |
133
|
|
|
|
|
|
|
qq($args{title}) |
134
|
|
|
|
|
|
|
; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
elsif ($type eq 'google+') { |
137
|
9
|
|
|
|
|
37
|
my $attr = { size => $args{size}, href => $args{url}, count => $args{count}, callback => $args{callback} }; |
138
|
9
|
|
|
|
|
34
|
my $param = join ' ', 'class="g-plusone"', map { qq(data-$_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr; |
|
13
|
|
|
|
|
71
|
|
|
36
|
|
|
|
|
44
|
|
139
|
|
|
|
|
|
|
|
140
|
9
|
|
|
|
|
15
|
my $script = join ', ', map { qq($_: "$args{$_}") } grep { $args{$_} } qw(lang parsetags); |
|
4
|
|
|
|
|
14
|
|
|
18
|
|
|
|
|
28
|
|
141
|
|
|
|
|
|
|
|
142
|
9
|
100
|
|
|
|
47
|
$button = |
|
|
100
|
|
|
|
|
|
143
|
|
|
|
|
|
|
( |
144
|
|
|
|
|
|
|
$args{noscript} |
145
|
|
|
|
|
|
|
? '' |
146
|
|
|
|
|
|
|
: qq(\n) |
147
|
|
|
|
|
|
|
) . |
148
|
|
|
|
|
|
|
qq() |
149
|
|
|
|
|
|
|
; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
25
|
|
|
|
|
105
|
return $button; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub share_meta { |
156
|
6
|
|
|
6
|
0
|
12
|
my($self, $c) = (shift, shift); |
157
|
6
|
|
|
|
|
17
|
my %args = @_; |
158
|
|
|
|
|
|
|
|
159
|
6
|
|
|
|
|
15
|
$_ = b($_)->xml_escape->to_string for grep {$_} @args{qw(title description)}; |
|
12
|
|
|
|
|
33
|
|
160
|
|
|
|
|
|
|
|
161
|
6
|
50
|
|
|
|
47
|
return join "\n", |
162
|
|
|
|
|
|
|
@_ ? qq() : '', |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
$args{og} ? ( |
165
|
|
|
|
|
|
|
$args{fb_app_id} ? qq() : (), |
166
|
|
|
|
|
|
|
qq(), |
167
|
|
|
|
|
|
|
qq(), |
168
|
6
|
100
|
|
|
|
258
|
map { $args{$_} ? qq() : () } |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
169
|
|
|
|
|
|
|
qw(image title description) |
170
|
|
|
|
|
|
|
) : (), |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
$args{title} ? qq() : (), |
173
|
|
|
|
|
|
|
$args{description} ? qq() : (), |
174
|
|
|
|
|
|
|
$args{image} ? qq() : (), |
175
|
|
|
|
|
|
|
$args{url} ? ( |
176
|
|
|
|
|
|
|
qq(), |
177
|
|
|
|
|
|
|
qq(), |
178
|
|
|
|
|
|
|
) : (), |
179
|
|
|
|
|
|
|
; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub is_share_agent { |
183
|
3
|
|
|
3
|
0
|
10
|
my($self, $c) = (shift, shift); |
184
|
|
|
|
|
|
|
|
185
|
3
|
|
|
|
|
12
|
my $ua = $c->req->headers->user_agent; |
186
|
3
|
|
|
|
|
315
|
my $range = $c->req->headers->header('Range'); |
187
|
3
|
|
|
|
|
240
|
my $enc = $c->req->headers->header('Accept-Encoding'); |
188
|
|
|
|
|
|
|
|
189
|
3
|
100
|
66
|
|
|
331
|
my $agent = |
|
|
100
|
66
|
|
|
|
|
190
|
|
|
|
|
|
|
$ua =~ /facebookexternalhit/ && $range && $enc eq 'gzip' ? 'facebook' : |
191
|
|
|
|
|
|
|
$ua =~ /Mozilla/ && $range && $enc =~ /gzip/ ? 'vkontakte' : # XXX: add cp1251 |
192
|
|
|
|
|
|
|
'' |
193
|
|
|
|
|
|
|
; |
194
|
|
|
|
|
|
|
|
195
|
3
|
100
|
|
|
|
54
|
$APP->log->debug(qq(Found the share agent "$agent")) if $agent; |
196
|
|
|
|
|
|
|
|
197
|
3
|
|
|
|
|
74
|
return $agent; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub _check_type { |
201
|
43
|
|
|
43
|
|
207
|
my $self = shift; |
202
|
43
|
|
100
|
|
|
115
|
my $type = shift || ''; |
203
|
|
|
|
|
|
|
|
204
|
43
|
100
|
|
|
|
1371
|
if (!$type) { |
|
|
100
|
|
|
|
|
|
205
|
2
|
|
|
|
|
69
|
$APP->log->debug('Missed the share type'); |
206
|
2
|
|
|
|
|
94
|
return; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
elsif (! exists $self->url->{ $type }) { |
209
|
2
|
|
|
|
|
11
|
my $types = join ', ', sort keys %{$self->url}; |
|
2
|
|
|
|
|
155
|
|
210
|
2
|
|
|
|
|
141
|
$APP->log->debug(qq(Bad share type "$type", support types of share: $types)); |
211
|
2
|
|
|
|
|
79
|
return; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
else { |
214
|
39
|
|
|
|
|
346
|
return $type; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
1; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
__END__ |