line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::ShareHelpers; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1193
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
24
|
use Mojo::ByteStream 'b'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
74
|
|
7
|
1
|
|
|
1
|
|
5
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.6'; |
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
|
56
|
my($self, $app) = @_; |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
2
|
$APP = $app; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
16
|
|
12
|
$app->helper( share_url => sub { $self->share_url ( @_ ) } ); |
|
16
|
|
|
|
|
163680
|
|
27
|
1
|
|
|
27
|
|
115
|
$app->helper( share_button => sub { $self->share_button ( @_ ) } ); |
|
27
|
|
|
|
|
58089
|
|
28
|
1
|
|
|
6
|
|
86
|
$app->helper( share_meta => sub { $self->share_meta ( @_ ) } ); |
|
6
|
|
|
|
|
55912
|
|
29
|
1
|
|
|
3
|
|
88
|
$app->helper( is_share_agent => sub { $self->is_share_agent( @_ ) } ); |
|
3
|
|
|
|
|
62452
|
|
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub share_url { |
33
|
16
|
|
|
16
|
0
|
30
|
my($self, $c) = (shift, shift); |
34
|
|
|
|
|
|
|
|
35
|
16
|
|
|
|
|
23
|
my $type = shift; |
36
|
16
|
100
|
|
|
|
43
|
return '' unless $self->_check_type( $type ); |
37
|
|
|
|
|
|
|
|
38
|
14
|
|
|
|
|
52
|
my %args = @_; |
39
|
|
|
|
|
|
|
|
40
|
14
|
|
|
|
|
18
|
my $param; |
41
|
14
|
100
|
|
|
|
72
|
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
|
|
|
|
|
7
|
$param->{u} = $args{url }; |
46
|
2
|
|
|
|
|
8
|
$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
|
|
|
|
|
16
|
$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
|
|
|
|
|
63
|
my @p = grep { $param->{$_} } sort keys %$param; |
|
40
|
|
|
|
|
99
|
|
60
|
18
|
|
|
|
|
585
|
return join '?', $self->url->{ $type }, |
61
|
14
|
100
|
|
|
|
564
|
@p ? join '&', map { $_ . '=' . b( $param->{$_} )->url_escape } @p : () |
62
|
|
|
|
|
|
|
; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub share_button { |
66
|
27
|
|
|
27
|
0
|
50
|
my($self, $c) = (shift, shift); |
67
|
|
|
|
|
|
|
|
68
|
27
|
|
|
|
|
33
|
my $type = shift; |
69
|
27
|
100
|
|
|
|
57
|
return '' unless $self->_check_type( $type ); |
70
|
|
|
|
|
|
|
|
71
|
25
|
|
|
|
|
91
|
my %args = @_; |
72
|
|
|
|
|
|
|
|
73
|
25
|
|
|
|
|
35
|
my $button; |
74
|
25
|
100
|
|
|
|
113
|
if ($type eq 'twitter') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
75
|
4
|
100
|
|
|
|
9
|
if ($args{iframe}) { |
76
|
2
|
|
|
|
|
28
|
my $url = $c->share_url( $type, @_ ); |
77
|
2
|
|
|
|
|
80
|
my($param) = $url =~ /.*\?(.*)/; |
78
|
|
|
|
|
|
|
|
79
|
2
|
|
|
|
|
9
|
$button = |
80
|
|
|
|
|
|
|
qq( |
81
|
|
|
|
|
|
|
qq(src="http://platform.twitter.com/widgets/tweet_button.html?$param">) |
82
|
|
|
|
|
|
|
; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
2
|
|
|
|
|
4
|
my $attr; push @$attr, qq(data-$_="$args{$_}") |
|
14
|
|
|
|
|
38
|
|
86
|
2
|
|
|
|
|
5
|
for grep { $args{$_} } qw(url via text related count lang counturl); |
87
|
2
|
|
|
|
|
8
|
my $param = join ' ', @$attr; |
88
|
|
|
|
|
|
|
|
89
|
2
|
|
|
|
|
10
|
$button = |
90
|
|
|
|
|
|
|
qq() . |
91
|
|
|
|
|
|
|
qq() |
92
|
|
|
|
|
|
|
; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
elsif ($type eq 'facebook') { |
96
|
4
|
100
|
|
|
|
11
|
if ($args{fb}) { |
97
|
2
|
|
|
|
|
12
|
my $attr = { type => $args{type}, href => $args{url}, class => $args{class} }; |
98
|
2
|
|
|
|
|
7
|
my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } keys %$attr; |
|
4
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
11
|
|
99
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
10
|
$button = |
101
|
|
|
|
|
|
|
qq() |
102
|
|
|
|
|
|
|
; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
else { |
105
|
2
|
|
|
|
|
10
|
my $attr = { type => $args{type}, share_url => $args{url} }; |
106
|
2
|
|
|
|
|
8
|
my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } keys %$attr; |
|
4
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
72
|
|
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
10
|
$button = |
109
|
|
|
|
|
|
|
qq($args{title}) . |
110
|
|
|
|
|
|
|
qq() |
111
|
|
|
|
|
|
|
; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
elsif ($type eq 'vkontakte') { |
115
|
4
|
100
|
|
|
|
15
|
my $url = $args{url} ? qq({url: "$args{url}"}) : 'false'; |
116
|
4
|
|
|
|
|
14
|
my $attr = { type => $args{type}, text => $args{title} }; |
117
|
4
|
|
|
|
|
11
|
my $param = join ', ', map { qq($_: "$attr->{$_}") } grep { $attr->{$_} } keys %$attr; |
|
8
|
|
|
|
|
24
|
|
|
8
|
|
|
|
|
15
|
|
118
|
|
|
|
|
|
|
|
119
|
4
|
|
|
|
|
19
|
$button = |
120
|
|
|
|
|
|
|
qq() . |
121
|
|
|
|
|
|
|
qq() |
122
|
|
|
|
|
|
|
; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
elsif ($type eq 'mymailru') { |
125
|
1
|
|
|
1
|
|
1547
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
126
|
4
|
|
|
|
|
32
|
my $url = $c->share_url( $type, @_ ); |
127
|
|
|
|
|
|
|
|
128
|
4
|
|
50
|
|
|
118
|
$args{type } ||= ''; |
129
|
4
|
|
50
|
|
|
11
|
$args{title} ||= 'В Мой Мир'; |
130
|
|
|
|
|
|
|
|
131
|
4
|
|
|
|
|
23
|
$button = |
132
|
|
|
|
|
|
|
qq() . |
133
|
|
|
|
|
|
|
qq($args{title}) |
134
|
|
|
|
|
|
|
; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
elsif ($type eq 'google+') { |
137
|
9
|
|
|
|
|
45
|
my $attr = { size => $args{size}, href => $args{url}, count => $args{count}, callback => $args{callback} }; |
138
|
9
|
|
|
|
|
29
|
my $param = join ' ', 'class="g-plusone"', map { qq(data-$_="$attr->{$_}") } grep { $attr->{$_} } keys %$attr; |
|
13
|
|
|
|
|
63
|
|
|
36
|
|
|
|
|
63
|
|
139
|
|
|
|
|
|
|
|
140
|
9
|
|
|
|
|
20
|
my $script = join ', ', map { qq($_: "$args{$_}") } grep { $args{$_} } qw(lang parsetags); |
|
4
|
|
|
|
|
18
|
|
|
18
|
|
|
|
|
38
|
|
141
|
|
|
|
|
|
|
|
142
|
9
|
100
|
|
|
|
70
|
$button = |
|
|
100
|
|
|
|
|
|
143
|
|
|
|
|
|
|
( |
144
|
|
|
|
|
|
|
$args{noscript} |
145
|
|
|
|
|
|
|
? '' |
146
|
|
|
|
|
|
|
: qq(\n) |
147
|
|
|
|
|
|
|
) . |
148
|
|
|
|
|
|
|
qq() |
149
|
|
|
|
|
|
|
; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
25
|
|
|
|
|
121
|
return $button; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub share_meta { |
156
|
6
|
|
|
6
|
0
|
12
|
my($self, $c) = (shift, shift); |
157
|
6
|
|
|
|
|
22
|
my %args = @_; |
158
|
|
|
|
|
|
|
|
159
|
6
|
|
|
|
|
17
|
$_ = b($_)->xml_escape->to_string for grep {$_} @args{qw(title description)}; |
|
12
|
|
|
|
|
37
|
|
160
|
|
|
|
|
|
|
|
161
|
6
|
50
|
|
|
|
76
|
return join "\n", |
162
|
|
|
|
|
|
|
@_ ? qq() : '', |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
$args{og} ? ( |
165
|
|
|
|
|
|
|
$args{fb_app_id} ? qq() : (), |
166
|
|
|
|
|
|
|
qq(), |
167
|
|
|
|
|
|
|
qq(), |
168
|
6
|
100
|
|
|
|
257
|
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
|
9
|
my($self, $c) = (shift, shift); |
184
|
|
|
|
|
|
|
|
185
|
3
|
|
|
|
|
14
|
my $ua = $c->req->headers->user_agent; |
186
|
3
|
|
|
|
|
370
|
my $range = $c->req->headers->header('Range'); |
187
|
3
|
|
|
|
|
429
|
my $enc = $c->req->headers->header('Accept-Encoding'); |
188
|
|
|
|
|
|
|
|
189
|
3
|
100
|
66
|
|
|
377
|
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
|
|
|
|
55
|
$APP->log->debug(qq(Found the share agent "$agent")) if $agent; |
196
|
|
|
|
|
|
|
|
197
|
3
|
|
|
|
|
69
|
return $agent; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub _check_type { |
201
|
43
|
|
|
43
|
|
84
|
my $self = shift; |
202
|
43
|
|
100
|
|
|
115
|
my $type = shift || ''; |
203
|
|
|
|
|
|
|
|
204
|
43
|
100
|
|
|
|
997
|
if (!$type) { |
|
|
100
|
|
|
|
|
|
205
|
2
|
|
|
|
|
43
|
$APP->log->debug('Missed the share type'); |
206
|
2
|
|
|
|
|
60
|
return; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
elsif (! exists $self->url->{ $type }) { |
209
|
2
|
|
|
|
|
8
|
my $types = join ', ', sort keys %{$self->url}; |
|
2
|
|
|
|
|
37
|
|
210
|
2
|
|
|
|
|
50
|
$APP->log->debug(qq(Bad share type "$type", support types of share: $types)); |
211
|
2
|
|
|
|
|
50
|
return; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
else { |
214
|
39
|
|
|
|
|
334
|
return $type; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
1; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
__END__ |