This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
forked from henrikbjorn/GravatarBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGravatarApi.php
222 lines (196 loc) · 5.13 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
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
<?php
namespace Bundle\GravatarBundle;
/**
* Simple wrapper to the gravatar API
* http://en.gravatar.com/site/implement/url
*
* Usage:
* \Bundle\GravatarBundle\GravatarApi::getThumbnailUrl('henrik@bearwoods.dk', 80, 'g', 'mm');
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Henrik Bjørnskov <henrik@bearwoods.dk>
* @author Benjamin Zikarsky <benjamin@zikarsky.de>
*/
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,
'format' => ''
);
/**
* @var \Zend\Http\Client $httpClient
*/
protected $httpClient = null;
/**
* @var \Zend\Cache\Frontend $cache
*/
protected $cache = 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 thumbnail url for a gravatar.
*
* @param string $email
* @param integer $size
* @param string $rating
* @param string $default
* @return string
*/
public function getThumbnailUrl($email, $size = null, $rating = null, $default = null)
{
$hash = $this->hash($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));
}
/**
* Returns a thumbnail url for a gravatar. Deprecated in favour of getThumbnailUrl
*
* @deprecated
* @param string $email
* @param integer $size
* @param string $rating
* @param string $default
* @return string
*/
public function getUrl($email, $size = null, $rating = null, $default = null)
{
return $this->getThumbnailUrl($email, $size, $rating, $default);
}
/**
* Get an url to a grvatar profile
*
* @param string $email
* @param string $format
* @param array $params (optional)
* @return string
*/
public function getProfileUrl($email, $format = null, array $params = array())
{
$url = 'http://www.gravatar.com/' . $this->hash($email);
// add an optional format
$format = $format ?: $this->defaults['format'];
if (strlen($format)) {
$url .= '.' . $format;
}
// add optional paramaters
$query = http_build_query(array_filter($params));
if ($query) {
$url .= '?' . $query;
}
return $url;
}
/**
* Get a Gravatar profile or null if the email does not have a profile
*
* @param string $email
* @return null|array
*/
public function getProfile($email)
{
$url = $this->getProfileUrl($email, 'php');
$cacheId = md5($url);
$cache = $this->getCache();
// check cache
if ($cache->test($cacheId)) {
return unserialize($cache->load($cacheId));
}
// request profile
$response = $this->getHttpClient()
->setUri($url)
->request('GET');
// check profile existence
if ($response->getStatus() != 200) {
return null;
}
// cache relies on "Expires" header
if ($response->getHeader('Expires')) {
$serverTime = new \DateTime($response->getHeader('Date'));
$serverExpires = new \DateTime($response->getHeader('Expires'));
$expiresIn = $serverTime->diff($serverExpires)->s;
$cache->save($response->getBody(), $cacheId, array(), $expiresIn);
}
return unserialize($response->getBody());
}
/**
* Returns the hash to a given email-address
*
* @param string email
* @return string
*/
public function hash($email)
{
return md5(strtolower(trim($email)));
}
/**
* Checks if a gravatar exists for the email.
*
* @param string $email
* @return boolean
*/
public function exists($email)
{
return 200 == $this->getHttpClient()
->setUri($this->getProfileUrl($email, 'json'))
->request('HEAD')
->getStatus();
}
/**
* Set the cache
*
* @param \Zend\Cache\Frontend $cache
*/
public function setCache(\Zend\Cache\Frontend $cache)
{
$this->cache = $cache;
}
/**
* Returns the cache
*
* @return \Zend\Cache\Frontend
*/
public function getCache()
{
if (!$this->cache) {
$this->setCache(\Zend\Cache\Cache::factory('Core', 'BlackHole'));
}
return $this->cache;
}
/**
* Set the HttpClient
*
* @param \Zend\Http\Client
*/
public function setHttpClient(\Zend\Http\Client $client)
{
$this->httpClient = $client;
}
/**
* Get the HttpClient
*
* @return \Zend\Http\Client
*/
public function getHttpClient()
{
if (!$this->httpClient) {
$this->setHttpClient(new \Zend\Http\Client());
}
return $this->httpClient;
}
}