forked from henrikbjorn/GravatarBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGravatarApi.php
79 lines (68 loc) · 2.06 KB
/
GravatarApi.php
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
<?php
namespace Bundle\GravatarBundle;
/**
* Simple wrapper to the gravatar API
* http://en.gravatar.com/site/implement/url
*
* Usage:
* \Bundle\GravatarBundle\GravatarApi::getUrl('[email protected]', 80, 'g', 'mm');
*
* @author Thibault Duplessis <[email protected]>
* @author Henrik Bjørnskov <[email protected]>
*/
class GravatarApi
{
/**
* @var array $default array of default options that can be overriden with getters and in the construct.
*/
protected $defaults = array(
'size' => 80,
'rating' => 'g',
'default' => null,
);
/**
* Constructor
*
* @param array $options the array is merged with the defaults.
* @return void
*/
public function __construct(array $options = array())
{
$this->defaults = array_merge($this->defaults, $options);
}
/**
* Returns a url for a gravatar.
*
* @param string $email
* @param integer $size
* @param string $rating
* @param string $default
* @return string
*/
public function getUrl($email, $size = null, $rating = null, $default = null)
{
$hash = md5(strtolower($email));
$map = array(
's' => $size ?: $this->defaults['size'],
'r' => $rating ?: $this->defaults['rating'],
'd' => $default ?: $this->defaults['default'],
);
return 'http://www.gravatar.com/avatar/' . $hash . '?' . http_build_query(array_filter($map));
}
/**
* Checks if a gravatar exists for the email. It does this by checking for 404 Not Found in the
* body returned.
*
* @param string $email
* @return boolean
*/
public function exists($email)
{
$path = $this->getUrl($email, null, null, '404');
$sock = fsockopen('gravatar.com', 80, $errorNo, $error);
fputs($sock, "HEAD " . $path . " HTTP/1.0\r\n\r\n");
$header = fgets($sock, 128);
fclose($sock);
return trim($header) == 'HTTP/1.1 404 Not Found' ? false : true;
}
}