* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://www.gravatar.com/site/implement/
*/
class Horde_Service_Gravatar
{
/** The default Gravatar base URL */
const STANDARD = 'http://www.gravatar.com';
/** The Gravatar base URL in SSL context */
const SECURE = 'https://secure.gravatar.com';
/**
* The base Gravatar URL.
*
* @var string
*/
private $_base;
/**
* The HTTP client to access the server.
*
* @var Horde_Http_Client
*/
private $_client;
/**
* Constructor.
*
* The default Gravatar base URL is Horde_Service_Gravatar::STANDARD. If
* you need URLs in an HTTPS context you should provide the base URL
* parameter as Horde_Service_Gravatar::SECURE. In case you wish to access
* another URL offering the Gravatar API you can specify the base URL of
* this service as $base.
*
* @param string $base The base Gravatar URL.
* @param Horde_Http_Client $client The HTTP client to access the server.
*/
public function __construct(
$base = self::STANDARD,
Horde_Http_Client $client = null
)
{
$this->_base = $base;
if ($client === null) {
$client = new Horde_Http_Client();
}
$this->_client = $client;
}
/**
* Return the Gravatar ID for the specified mail address.
*
* @param string $mail The mail address.
*
* @return string The Gravatar ID.
*/
public function getId($mail)
{
if (!is_string($mail)) {
throw new InvalidArgumentException('The mail address must be a string!');
}
return md5(Horde_String::lower(trim($mail)));
}
/**
* Return the Gravatar image URL for the specified mail address. The
* returned URL can be directly used with an IMG tag e.g.:
* <img src="http://www.gravatar.com/avatar/hash" />
*
* @param string $mail The mail address.
* @param mixed $opts Additional options. If an integer, treated as the
* 'size' option. If an array, the following options
* are available:
*
* - default: (string) Default behavior. Valid values are '404', 'mm',
* 'identicon', 'monsterid', 'wavatar', 'retro', 'blank', or
* a URL-encoded URL to use as the default image.
* - rating: (string) Rating. Valid values are 'g', 'pg', 'r', and 'x'.
* - size: (integer) Image size. Valid values are between 1 and 512.
*
*
* @return Horde_Url The image URL.
*/
public function getAvatarUrl($mail, $opts = array())
{
if (is_integer($opts)) {
$opts = array('size' => $opts);
}
if (!empty($opts['size']) &&
(($opts['size'] < 1) || ($opts['size'] > 512))) {
throw InvalidArgumentException('The size parameter is out of bounds');
}
$url = new Horde_Url($this->_base . '/avatar/' . $this->getId($mail));
if (!empty($opts['default'])) {
$url->add('d', $opts['default']);
}
if (!empty($opts['rating'])) {
$url->add('r', $opts['rating']);
}
if (!empty($opts['size'])) {
$url->add('s', $opts['size']);
}
return $url;
}
/**
* Return the Gravatar profile URL.
*
* @param string $mail The mail address.
*
* @return string The profile URL.
*/
public function getProfileUrl($mail)
{
return $this->_base . '/' . $this->getId($mail);
}
/**
* Fetch the Gravatar profile information.
*
* @param string $mail The mail address.
*
* @return string The profile information.
*/
public function fetchProfile($mail)
{
return $this->_client->get($this->getProfileUrl($mail) . '.json')
->getBody();
}
/**
* Return the Gravatar profile information as an array.
*
* @param string $mail The mail address.
*
* @return array The profile information.
*/
public function getProfile($mail)
{
return json_decode($this->fetchProfile($mail), true);
}
/**
* Fetch the avatar image.
*
* @param string $mail The mail address.
* @param mixed $opts Additional options. See getAvatarUrl().
*
* @return resource The image as stream resource, or null if the server
* returned an error.
*/
public function fetchAvatar($mail, $opts = array())
{
$get = $this->_client->get($this->getAvatarUrl($mail, $opts));
return ($get->code == 404)
? null
: $get->getStream();
}
}
Horde_Service_Gravatar-1.0.1/test/Horde/Service/Gravatar/AllTests.php 0000664 0001750 0001750 00000000132 12654070240 023614 0 ustar jan jan run();
Horde_Service_Gravatar-1.0.1/test/Horde/Service/Gravatar/bootstrap.php 0000664 0001750 0001750 00000000143 12654070240 024100 0 ustar jan jan
* @category Horde
* @copyright 2011-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Service_Gravatar
* @package Service_Gravatar
*/
class Horde_Service_Gravatar_GravatarTest
extends PHPUnit_Framework_TestCase
{
public function testReturn()
{
$g = new Horde_Service_Gravatar();
$this->assertInternalType('string', $g->getId('test'));
}
public function testAddress()
{
$g = new Horde_Service_Gravatar();
$this->assertEquals(
'0c17bf66e649070167701d2d3cd71711',
$g->getId('test@example.org')
);
}
/**
* @dataProvider provideAddresses
*/
public function testAddresses($mail, $id)
{
$g = new Horde_Service_Gravatar();
$this->assertEquals($id, $g->getId($mail));
}
public function provideAddresses()
{
return array(
array('test@example.org', '0c17bf66e649070167701d2d3cd71711'),
array('x@example.org', 'ae46d8cbbb834a85db7287f8342d0c42'),
array('test@example.com', '55502f40dc8b7c769880b10874abc9d0'),
);
}
public function testIgnoreCase()
{
$g = new Horde_Service_Gravatar();
$this->assertEquals(
'0c17bf66e649070167701d2d3cd71711',
$g->getId('Test@EXAMPLE.orG')
);
}
public function testTrimming()
{
$g = new Horde_Service_Gravatar();
$this->assertEquals(
'0c17bf66e649070167701d2d3cd71711',
$g->getId(' Test@Example.orG ')
);
}
/**
* @expectedException InvalidArgumentException
*/
public function testInvalidMail()
{
$g = new Horde_Service_Gravatar();
$g->getId(0.0);
}
public function testAvatarUrl()
{
$g = new Horde_Service_Gravatar();
$this->assertEquals(
'http://www.gravatar.com/avatar/0c17bf66e649070167701d2d3cd71711',
$g->getAvatarUrl(' Test@Example.orG ')
);
}
public function testAvatarUrlWithSize()
{
$g = new Horde_Service_Gravatar();
$this->assertEquals(
'http://www.gravatar.com/avatar/0c17bf66e649070167701d2d3cd71711?s=50',
$g->getAvatarUrl('test@example.org', 50));
}
public function testProfileUrl()
{
$g = new Horde_Service_Gravatar();
$this->assertEquals(
'http://www.gravatar.com/0c17bf66e649070167701d2d3cd71711',
$g->getProfileUrl(' Test@Example.orG ')
);
}
public function testFlexibleBase()
{
$g = new Horde_Service_Gravatar(Horde_Service_Gravatar::SECURE);
$this->assertEquals(
'https://secure.gravatar.com/0c17bf66e649070167701d2d3cd71711',
$g->getProfileUrl(' Test@Example.orG ')
);
}
public function testFetchProfile()
{
$g = $this->_getMockedGravatar('RESPONSE');
$this->assertEquals(
'RESPONSE',
$g->fetchProfile('test@example.org')
);
}
public function testGetProfile()
{
$g = $this->_getMockedGravatar('{"test":"example"}');
$this->assertEquals(
array('test' => 'example'),
$g->getProfile('test@example.org')
);
}
private function _getMockedGravatar($response_string)
{
$response = $this->getMock('Horde_Http_Response', array('getBody'));
$response->expects($this->once())
->method('getBody')
->will($this->returnValue($response_string));
$mock = $this->getMock('Horde_Http_Client', array('get'));
$mock->expects($this->once())
->method('get')
->will($this->returnValue($response));
return new Horde_Service_Gravatar(
Horde_Service_Gravatar::STANDARD,
$mock
);
}
public function testFetchImage()
{
$g = $this->_getStubbedGravatar('RESPONSE');
$this->assertEquals(
'RESPONSE',
stream_get_contents($g->fetchAvatar('test@example.org'))
);
}
private function _getStubbedGravatar($response_string)
{
$body = new Horde_Support_StringStream($response_string);
$response = new Horde_Http_Response_Mock('', $body->fopen());
$request = new Horde_Http_Request_Mock();
$request->setResponse($response);
return new Horde_Service_Gravatar(
Horde_Service_Gravatar::STANDARD,
new Horde_Http_Client(array('request' => $request))
);
}
}
Horde_Service_Gravatar-1.0.1/test/Horde/Service/Gravatar/phpunit.xml 0000664 0001750 0001750 00000000056 12654070240 023566 0 ustar jan jan
Horde_Service_Gravatar-1.0.1/test/Horde/Service/Gravatar/ServerTest.php 0000664 0001750 0001750 00000002703 12654070240 024175 0 ustar jan jan
* @category Horde
* @copyright 2011-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Service_Gravatar
* @package Service_Gravatar
*/
class Horde_Service_Gravatar_ServerTest extends Horde_Test_Case
{
private $_server;
public function setUp()
{
$config = self::getConfig('SERVICE_GRAVATAR_TEST_CONFIG');
if ($config && !empty($config['service']['gravatar']['server'])) {
$this->_server = $config['service']['gravatar']['server'];
} else {
$this->markTestSkipped('Configuration is missing and remote server tests are disabled.');
}
}
public function testGetProfile()
{
$g = new Horde_Service_Gravatar($this->_server);
$profile = $g->getProfile('wrobel@horde.org');
$this->assertEquals(
'http://gravatar.com/abc1xyz2',
$profile['entry'][0]['profileUrl']
);
}
}