/usr/share/perl5/Libravatar/URL.pm is in libgravatar-url-perl 1.06-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | package Libravatar::URL;
use strict;
use warnings;
use Gravatar::URL qw(gravatar_url);
use Digest::SHA qw(sha256_hex);
use Carp;
our $VERSION = '1.06';
use parent 'Exporter';
our @EXPORT = qw(
libravatar_url
);
my $Libravatar_Http_Base = "http://cdn.libravatar.org/avatar";
my $Libravatar_Https_Base = "https://seccdn.libravatar.org/avatar";
=head1 NAME
Libravatar::URL - Make URLs for Libravatars from an email address
=head1 SYNOPSIS
use Libravatar::URL;
my $url = libravatar_url( email => 'larry@example.org' );
=head1 DESCRIPTION
See L<http://www.libravatar.org> for more information.
=head1 Functions
=head3 B<libravatar_url>
# By email
my $url = libravatar_url( email => $email, %options );
# By OpenID
my $url = libravatar_url( openid => $openid, %options );
Constructs a URL to fetch the Libravatar for the given $email address or $openid URL.
C<%options> are optional. C<libravatar_url> will accept all the
options of L<Gravatar::URL/gravatar_url> except for C<rating> and C<border>.
The available options are...
=head4 size
Specifies the desired width and height of the avatar (they are square).
Valid values are from 1 to 512 inclusive. Any size other than 80 may
cause the original image to be downsampled using bicubic resampling
before output.
size => 40, # 40 x 40 image
=head4 default
The url to use if the user has no avatar.
default => "http://www.example.org/nobody.jpg"
Relative URLs will be relative to the base (ie. libravatar.org), not your web site.
Libravatar defines special values that you may use as a default to
produce dynamic default images. These are "identicon", "monsterid",
"wavatar" and "retro". "404" will cause the URL to return an HTTP 404 "Not Found"
error instead and "mm" will display the same "mystery man" image for everybody.
See L<http://www.libravatar.org/api> for more info.
If omitted, Libravatar will serve up their default image, the orange butterfly.
=head4 base
This is the URL of the location of the Libravatar server you wish to
grab avatars from. Defaults to
L<http://cdn.libravatar.org/avatar/> for HTTP and
L<https://seccdn.libravatar.org/avatar/> for HTTPS.
=head4 short_keys
If true, use short key names when constructing the URL. "s" instead
of "size", "d" instead of "default" and so on.
short_keys defaults to true.
=head4 https
If true, serve avatars over HTTPS instead of HTTP.
You should select this option if your site is served over HTTPS to
avoid browser warnings about the presence of insecure content.
https defaults to false.
=cut
my %defaults = (
short_keys => 1,
);
# Extract the domain component of an email address
sub email_domain {
my ( $email ) = @_;
return undef unless $email;
if ( $email =~ m/@([^@]+)$/ ) {
return $1;
}
return undef;
}
# Extract the domain component of an OpenID URI
sub openid_domain {
my ( $openid ) = @_;
return undef unless $openid;
if ( $openid =~ m@^(http|https)://([^/]+)@i ) {
return $2;
}
return undef;
}
# Return the right (target, port) pair from a list of SRV records
sub srv_hostname {
my @records = @_;
return ( undef, undef ) unless scalar(@records) > 0;
if ( 1 == scalar(@records) ) {
my $rr = shift @records;
return ( $rr->target, $rr->port );
}
# Keep only the servers in the top priority
my @priority_records;
my $total_weight = 0;
my $top_priority = $records[0]->priority; # highest priority = lowest number
foreach my $rr (@records) {
if ( $rr->priority > $top_priority ) {
# ignore the record ($rr has lower priority)
next;
}
elsif ( $rr->priority < $top_priority ) {
# reset the array ($rr has higher priority)
$top_priority = $rr->priority;
$total_weight = 0;
@priority_records = ();
}
$total_weight += $rr->weight;
if ( $rr->weight > 0 ) {
push @priority_records, [ $total_weight, $rr ];
}
else {
# Zero-weigth elements must come first
unshift @priority_records, [ 0, $rr ];
}
}
if ( 1 == scalar(@priority_records) ) {
my $record = shift @priority_records;
my ( $weighted_index, $rr ) = @$record;
return ( $rr->target, $rr->port );
}
# Select first record according to RFC2782 weight ordering algorithm (page 3)
my $random_number = int(rand($total_weight + 1));
foreach my $record (@priority_records) {
my ( $weighted_index, $rr ) = @$record;
if ( $weighted_index >= $random_number ) {
return ( $rr->target, $rr->port );
}
}
die 'There is something wrong with our SRV weight ordering algorithm';
}
# Convert (target, port) to a full avatar base URL
sub build_url {
my ( $target, $port, $https ) = @_;
return undef unless $target;
my $url = $https ? 'https' : 'http' . '://' . $target;
if ( $port && !$https && ($port != 80) or $port && $https && ($port != 443) ) {
$url .= ':' . $port;
}
$url .= '/avatar';
return $url;
}
sub sanitize_target {
my ( $target, $port ) = @_;
unless ( defined $target && $target =~ m/^[0-9a-zA-Z\-.]+$/ ) {
return ( undef, undef );
}
unless ( defined $port && $port =~ m/^[0-9]{1,5}$/ ) {
return ( undef, undef );
}
return ( $target, $port )
}
sub federated_url {
my %args = @_;
my $domain;
if ( exists $args{email} ) {
$domain = email_domain($args{email});
}
elsif ( exists $args{openid} ) {
$domain = openid_domain($args{openid});
}
return undef unless $domain;
require Net::DNS::Resolver;
my $fast_resolver = Net::DNS::Resolver->new(retry => 1, tcp_timeout => 1, udp_timeout => 1, dnssec => 1);
my $srv_prefix = $args{https} ? '_avatars-sec' : '_avatars';
my $packet = $fast_resolver->query($srv_prefix . '._tcp.' . $domain, 'SRV');
if ( $packet and $packet->answer ) {
my ( $target, $port ) = sanitize_target(srv_hostname($packet->answer));
return build_url($target, $port, $args{https});
}
return undef;
}
sub lowercase_openid {
my $openid = shift;
if ( $openid =~ m@^([^:]+://[^/]+)(.*)@ ) {
$openid = (lc $1) . $2;
}
return $openid;
}
sub libravatar_url {
my %args = @_;
my $custom_base = defined $args{base};
exists $args{email} or exists $args{openid} or exists $args{id} or
croak "Cannot generate a Libravatar URI without an email address, an OpenID or a gravatar id";
if ( exists $args{email} and (exists $args{openid} or exists $args{id}) or
exists $args{openid} and (exists $args{email} or exists $args{id}) or
exists $args{id} and (exists $args{email} or exists $args{openid}) ) {
croak "Two or more identifiers (email, OpenID or gravatar id) were given. libravatar_url() only takes one";
}
$defaults{base_http} = $Libravatar_Http_Base;
$defaults{base_https} = $Libravatar_Https_Base;
Gravatar::URL::_apply_defaults(\%args, \%defaults);
if ( !$custom_base ) {
my $federated_url = federated_url(%args);
if ( $federated_url ) {
$args{base} = $federated_url;
}
}
if ( exists $args{openid} ) {
$args{id} = sha256_hex(lowercase_openid($args{openid}));
undef $args{openid};
}
return gravatar_url(%args);
}
=head1 LICENSE
Copyright 2011, Francois Marier <fmarier@gmail.com>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/artistic.html>
=head1 SEE ALSO
L<http://www.libravatar.org> - The Libravatar web site
L<http://www.libravatar.org/api> - The Libravatar API documentation
=cut
1;
|