Overview

Packages

  • Service
    • Gravatar

Classes

  • Horde_Service_Gravatar
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Horde_Service_Gravatar abstracts communication with Services supporting the
  4:  * Gravatar API (http://www.gravatar.com/site/implement/).
  5:  *
  6:  * PHP version 5
  7:  *
  8:  * @category Horde
  9:  * @package  Service_Gravatar
 10:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 11:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 12:  * @link     http://pear.horde.org/index.php?package=Service_Gravatar
 13:  */
 14: 
 15: /**
 16:  * Horde_Service_Gravatar abstracts communication with Services supporting the
 17:  * Gravatar API (http://www.gravatar.com/site/implement/).
 18:  *
 19:  * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
 20:  *
 21:  * See the enclosed file COPYING for license information (LGPL). If you
 22:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 23:  *
 24:  * @category Horde
 25:  * @package  Service_Gravatar
 26:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 27:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 28:  * @link     http://pear.horde.org/index.php?package=Service_Gravatar
 29:  */
 30: class Horde_Service_Gravatar
 31: {
 32:     /** The default Gravatar base URL */
 33:     const STANDARD = 'http://www.gravatar.com';
 34: 
 35:     /** The Gravatar base URL in SSL context */
 36:     const SECURE  = 'https://secure.gravatar.com';
 37: 
 38:     /**
 39:      * The base Gravatar URL.
 40:      *
 41:      * @var string
 42:      */
 43:     private $_base;
 44: 
 45:     /**
 46:      * The HTTP client to access the server.
 47:      *
 48:      * @var Horde_Http_Client
 49:      */
 50:     private $_client;
 51: 
 52:     /**
 53:      * Constructor.
 54:      *
 55:      * The default Gravatar base URL is Horde_Service_Gravatar::STANDARD. If you
 56:      * need URLs in an HTTPS context you should provide the base URL parameter
 57:      * as Horde_Service_Gravatar::SECURE. In case you wish to access another URL
 58:      * offering the Gravatar API you can specify the base URL of this service as
 59:      * $base.
 60:      *
 61:      * @param string            $base   The base Gravatar URL.
 62:      * @param Horde_Http_Client $client The HTTP client to access the server.
 63:      */
 64:     public function __construct(
 65:         $base = self::STANDARD,
 66:         Horde_Http_Client $client = null
 67:     )
 68:     {
 69:         $this->_base   = $base;
 70:         if ($client === null) {
 71:             $client = new Horde_Http_Client();
 72:         }
 73:         $this->_client = $client;
 74:     }
 75: 
 76:     /**
 77:      * Return the Gravatar ID for the specified mail address.
 78:      *
 79:      * @param string $mail The mail address.
 80:      *
 81:      * @return string The Gravatar ID.
 82:      *
 83:      * @throws InvalidArgumentException In case the mail address is no string.
 84:      */
 85:     public function getId($mail)
 86:     {
 87:         if (!is_string($mail)) {
 88:             throw new InvalidArgumentException('The mail address must be a string!');
 89:         }
 90:         return md5(strtolower(trim($mail)));
 91:     }
 92: 
 93:     /**
 94:      * Return the Gravatar image URL for the specified mail address. The
 95:      * returned URL can be directly used with an <img/> tag e.g. <img
 96:      * src="http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50" />
 97:      *
 98:      * @param string $mail   The mail address.
 99:      * @param integer $size  An optinoal size parameter. Valid values are
100:      *                       between 1 and 512.
101:      *
102:      * @return string The image URL.
103:      *
104:      * @throws InvalidArgumentException In case the mail address is no string.
105:      */
106:     public function getAvatarUrl($mail, $size = null)
107:     {
108:         if (!empty($size) && ($size < 1 || $size > 512)) {
109:             throw InvalidArgumentException('The size parameter is out of bounds');
110:         }
111:         return $this->_base . '/avatar/' . $this->getId($mail) . (!empty($size) ? '?s=' . $size : '');
112:     }
113: 
114:     /**
115:      * Return the Gravatar profile URL.
116:      *
117:      * @param string $mail The mail address.
118:      *
119:      * @return string The profile URL.
120:      *
121:      * @throws InvalidArgumentException In case the mail address is no string.
122:      */
123:     public function getProfileUrl($mail)
124:     {
125:         return $this->_base . '/' . $this->getId($mail);
126:     }
127: 
128:     /**
129:      * Fetch the Gravatar profile information.
130:      *
131:      * @param string $mail The mail address.
132:      *
133:      * @return string The profile information.
134:      *
135:      * @throws InvalidArgumentException In case the mail address is no string.
136:      */
137:     public function fetchProfile($mail)
138:     {
139:         return $this->_client->get($this->getProfileUrl($mail) . '.json')
140:             ->getBody();
141:     }
142: 
143:     /**
144:      * Return the Gravatar profile information as an array.
145:      *
146:      * @param string $mail The mail address.
147:      *
148:      * @return array The profile information.
149:      *
150:      * @throws InvalidArgumentException In case the mail address is no string.
151:      */
152:     public function getProfile($mail)
153:     {
154:         return json_decode($this->fetchProfile($mail), true);
155:     }
156: 
157:     /**
158:      * Fetch the avatar image.
159:      *
160:      * @param string $mail   The mail address.
161:      * @param integer $size  An optional size parameter.
162:      *
163:      * @return resource The image as stream resource.
164:      *
165:      * @throws InvalidArgumentException In case the mail address is no string.
166:      */
167:     public function fetchAvatar($mail, $size = null)
168:     {
169:         return $this->_client->get($this->getAvatarUrl($mail, $size))->getStream();
170:     }
171: 
172: }
API documentation generated by ApiGen