Source for file Libravatar.php
Documentation is available at Libravatar.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* PHP support for the Libravatar.org service.
* Copyright (c) 2011 Services_Libravatar committers.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* @package Services_Libravatar
* @author Melissa Draper <melissa@meldraweb.com>
* @copyright 2011 Services_Libravatar committers.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://pear.php.net/package/Services_Libravatar
* @since File available since Release 0.1.0
* PHP support for the Libravatar.org service.
* Using this class is easy. After including or requiring
* Services/Libravatar.php simply do:
* $libravatar = new Services_Libravatar();
* $url = $libravatar->getUrl('melissa@meldraweb.com');
* This would populate $url with the string:
* http://cdn.libravatar.org/avatar/4db84629c121f2d443d33bdb9fd149bc
* A complicated lookup using all the options is:
* $libravatar = new Services_Libravatar();
* $libravatar->setSize(40);
* $libravatar->setAlgorithm('sha256');
* $libravatar->setHttps(true);
* $libravatar->setDefault(
* 'http://upload.wikimedia.org/wikipedia/commons/a/af/Tux.png'
* $url = $libravatar->getUrl('melissa@meldraweb.com');
* @package Services_Libravatar
* @author Melissa Draper <melissa@meldraweb.com>
* @copyright 2011 Services_Libravatar committers.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Release: 0.2.2
* @link http://pear.php.net/package/Services_Libravatar
* @since Class available since Release 0.1.0
* Hashing algorithm to use
* @see processAlgorithm()
* Default image URL to use
* If HTTPS URLs should be used
* Composes a URL for the identifier and options passed in
* Compose a full URL as specified by the Libravatar API, based on the
* email address or openid URL passed in, and the options specified.
* @param string $identifier a string of either an email address
* @param array $options an array of (bool) https, (string) algorithm
* (string) size, (string) default.
* @return string A string of a full URL for an avatar image
* @since Method available since Release 0.2.0
* @deprecated Use getUrl() instead
public function url($identifier, $options = array ())
return $this->getUrl($identifier, $options);
* Composes a URL for the identifier and options passed in
* Compose a full URL as specified by the Libravatar API, based on the
* email address or openid URL passed in, and the options specified.
* @param string $identifier a string of either an email address
* @param array $options an array of (bool) https, (string) algorithm
* (string) size, (string) default.
* @return string A string of a full URL for an avatar image
* @since Method available since Release 0.2.0
* @throws InvalidArgumentException When an invalid option is passed
public function getUrl($identifier, $options = array ())
// If no identifier has been passed, set it to a null.
// This way, there'll always be something returned.
if (isset ($options['https'])) {
$https = (bool) $options['https'];
if (isset ($options['algorithm'])) {
if (isset ($options['default'])) {
if (isset ($options['size'])) {
// Get the domain so we can determine the SRV stuff for federation
// If https has been specified in $options, make sure we make the
$service = $this->srvGet($domain, $https);
$protocol = $https ? 'https' : 'http';
$params['default'] = $default;
if (count($params) > 0 ) {
// Compose the URL from the pieces we generated
$url = $protocol . '://' . $service . '/avatar/' . $identifierHash
* Checks the options array and verify that only allowed options are in it.
* @param array $options Array of options for getUrl()
* @throws Exception When an invalid option is used
//this short options are deprecated!
if (isset ($options['s'])) {
$options['size'] = $options['s'];
if (isset ($options['d'])) {
$options['default'] = $options['d'];
foreach ($options as $key => $value) {
if (!isset ($allowedOptions[$key])) {
throw new InvalidArgumentException (
'Invalid option in array: ' . $key
* Normalizes the identifier (E-mail address or OpenID)
* @param string $identifier E-Mail address or OpenID
* @return string Normalized identifier
if (filter_var($identifier, FILTER_VALIDATE_EMAIL )) {
return self ::normalizeOpenId ($identifier);
* Create a hash of the identifier.
* Create a hash of the email address or openid passed in. Algorithm
* used for email address ONLY can be varied. Either md5 or sha256
* are supported by the Libravatar API. Will be ignored for openid.
* @param string $identifier A string of the email address or openid URL
* @param string $hash A string of the hash algorithm type to make
* Uses the php implementation of hash()
* MD5 preferred for Gravatar fallback
* @return string A string hash of the identifier.
* @since Method available since Release 0.1.0
if (filter_var($identifier, FILTER_VALIDATE_EMAIL ) || $identifier === null ) {
// If email, we can select our algorithm. Default to md5 for
return hash($hash, $identifier);
//no email, so the identifier has to be an OpenID
return hash('sha256', $identifier);
* Normalizes an identifier (URI or XRI)
* @param mixed $identifier URI or XRI to be normalized
* @return string Normalized Identifier.
* Empty string when the OpenID is invalid.
* @internal Adapted from OpenID::normalizeIdentifier()
if (in_array($identifier[0 ], array ('=', '@', '+', '$', '!'))) {
if (!preg_match('@^http[s]?://@i', $identifier)) {
$identifier = 'http://' . $identifier;
if (strpos($identifier, '/', 8 ) === false ) {
if (!filter_var($identifier, FILTER_VALIDATE_URL )) {
//http://openid.net/specs/openid-authentication-2_0.html#normalization
return $parts['scheme'] . '://'
. (isset ($parts['user']) ? $parts['user'] : '')
. (isset ($parts['pass']) ? ':' . $parts['pass'] : '')
. (isset ($parts['user']) || isset ($parts['pass']) ? '@' : '')
&& $parts['scheme'] === 'http' && $parts['port'] != 80 )
|| (isset ($parts['port'])
&& $parts['scheme'] === 'https' && $parts['port'] != 443 )
? ':' . $parts['port'] : ''
. (isset ($parts['query']) ? '?' . $parts['query'] : '');
//leave out fragment as requested by the spec
* Grab the domain from the identifier.
* Extract the domain from the Email or OpenID.
* @param string $identifier A string of the email address or openid URL
* @return string A string of the domain to use
* @since Method available since Release 0.1.0
if ($identifier === null ) {
// What are we, email or openid? Split ourself up and get the
if (filter_var($identifier, FILTER_VALIDATE_EMAIL )) {
$email = explode('@', $identifier);
if (!isset ($url['host'])) {
if (isset ($url['port']) && $url['scheme'] === 'http'
|| isset ($url['port']) && $url['scheme'] === 'https'
$domain .= ':' . $url['port'];
* Get the SRV record, filtered by priority and weight. If our domain
* has no SRV records, fall back to Libravatar.org
* @param string $domain A string of the domain we extracted from the
* provided identifier with domainGet()
* @param boolean $https Whether or not to look for https records
* @return string The target URL.
* @since Method available since Release 0.1.0
protected function srvGet($domain, $https = false )
// Are we going secure? Set up a fallback too.
if (isset ($https) && $https === true ) {
$subdomain = '_avatars-sec._tcp.';
$subdomain = '_avatars._tcp.';
// No domain means invalid email address/openid
return $fallback . 'libravatar.org';
// Lets try get us some records based on the choice of subdomain
// and the domain we had passed in.
// Did we get anything? No?
// Then let's try Libravatar.org.
return $fallback . 'libravatar.org';
// Sort by the priority. We must get the lowest.
usort($srv, array ($this, 'comparePriority'));
// Try to adhere to RFC2782's weighting algorithm, page 3
// "arrange all SRV RRs (that have not been ordered yet) in any order,
// except that all those with weight 0 are placed at the beginning of
if ($s['pri'] == $top['pri']) {
// "Compute the sum of the weights of those RRs"
$sum += (int) $s['weight'];
// "and with each RR associate the running sum in the selected
// "Then choose a uniform random number between 0 and the sum computed
// "and select the RR whose running sum value is the first in the selected
// order which is greater than or equal to the random number selected"
foreach ($pri as $k => $v) {
* Sorting function for record priorities.
* @param mixed $a A mixed value passed by usort()
* @param mixed $b A mixed value passed by usort()
* @return mixed The result of the comparison
* @since Method available since Release 0.1.0
return $a['pri'] - $b['pri'];
* Automatically set the https option depending on the current connection
* If the current connection is HTTPS, the https options is activated.
* If it is not HTTPS, the https option is deactivated.
isset ($_SERVER['HTTPS']) && $_SERVER['HTTPS']
* Verify and cast the email address hashing algorithm to use.
* @param string $algorithm Algorithm to use, "sha256" or "md5".
* @return string Algorithm
* @throws InvalidArgumentException When an unsupported algorithm is given
$algorithm = (string) $algorithm;
if ($algorithm !== 'md5' && $algorithm !== 'sha256') {
throw new InvalidArgumentException (
'Only md5 and sha256 hashing supported'
* Verify and cast the default URL to use when no avatar image can be found.
* If none is set, the libravatar logo is returned.
* @param string $url Full URL to use OR one of the following:
* - "404" - give a "404 File not found" instead of an image
* @return string Default URL
* @throws InvalidArgumentException When an invalid URL is given
throw new InvalidArgumentException ('Invalid default avatar URL');
* Verify and cast the required size of the images.
* @param integer $size Size (width and height in pixels) of the image.
* NULL for the default width.
* @throws InvalidArgumentException When a size <= 0 is given
throw new InvalidArgumentException ('Size has to be larger than 0');
* Set the email address hashing algorithm to use.
* To keep gravatar compatibility, use "md5".
* @param string $algorithm Algorithm to use, "sha256" or "md5".
* @throws InvalidArgumentException When an unsupported algorithm is given
* Set the default URL to use when no avatar image can be found.
* If none is set, the gravatar logo is returned.
* @param string $url Full URL to use OR one of the following:
* - "404" - give a "404 File not found" instead of an image
* @throws InvalidArgumentException When an invalid URL is given
* Set if HTTPS URLs shall be returned.
* @param boolean $useHttps If HTTPS url shall be returned
$this->https = (bool) $useHttps;
* Set the required size of the images.
* Every avatar image is square sized, which means you need to set only number.
* @param integer $size Size (width and height) of the image
* @throws InvalidArgumentException When a size <= 0 is given
* c-hanging-comment-ender-p: nil
Documentation generated on Thu, 20 Sep 2012 13:00:02 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|