-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravatar.class.php
336 lines (308 loc) · 8.49 KB
/
gravatar.class.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* Class to handle gravatar.com requests
*
* @author Knut Kohl <[email protected]>
* @copyright 2011 Knut Kohl
* @licence GNU General Public License - http://www.gnu.org/licenses/gpl.txt
* @version 0.1.0
*/
class Gravatar {
const URL_HTTP = 'http://www.gravatar.com/';
const URL_HTTPS = 'https://secure.gravatar.com/';
// -------------------------------------------------------------------------
// PUBLIC
// -------------------------------------------------------------------------
/**
* Class constructor
*
* Set defaults
*
* @return void
*/
public function __construct() {
$this->reset();
} // function __construct()
/**
* Set avatar size in pixels, defaults to 80px
*
* @param string $size Size in pixels [1-512]
* @return instance $this
*/
public function setSize( $size ) {
if ($s >= 1 AND $s <= 512) {
$this->_size = (int) $size;
}
return $this;
} // function setSize()
/**
* Get avatar size in pixels
*
* @return int
*/
public function getSize() {
return $this->_size;
}
/**
* Set avatar size in pixels, defaults to 80px
*
* @param string $size Size in pixels [1-512]
* @return instance $this
*/
public function setUseExtension( $use=TRUE ) {
$this->_useextension = (bool) $use;
return $this->_size;
} // function setUseExtension()
/**
* Get avatar size in pixels
*
* @return int
*/
public function getUseExtension() {
return $this->_useextension;
} // function getUseExtension()
/**
* Set default imageset to use, defaults to 'mm'
*
* Defaults:
* 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response
* mm: (mystery-man) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
* identicon: a geometric pattern based on an email hash
* monsterid: a generated 'monster' with different colors, faces, etc
* wavatar: generated faces with differing features and backgrounds
* retro: awesome generated, 8-bit arcade-style pixelated faces
*
* If an other value is provided, assume it is a URL and urlencode() it
*
* Force Default
* If for some reason you wanted to force the default image to always load,
* you can do that by setting this value to 'y'
*
* @param string $default Default imageset to use [404|mm|identicon|monsterid|wavatar|y]
* @return instance $this
*/
public function setImageset( $default ) {
if (in_array($default, array('404','mm','identicon','monsterid','wavatar'))) {
$this->_default = $default;
} else {
$this->_default = urlencode($default);
}
return $this;
}
/**
* Get default imageset to use
*
* @return string
*/
public function getImageset() {
return $this->_default;
}
/**
* Set maximum rating (inclusive), defaults to 'g'
*
* You may specify one of the following ratings to request images up to and
* including that rating:
* g: suitable for display on all websites with any audience type
* pg: may contain rude gestures, provocatively dressed individuals,
* the lesser swear words, or mild violence
* r: may contain such things as harsh profanity, intense violence, nudity,
* or hard drug use
* x: may contain hardcore sexual imagery or extremely disturbing violence
*
* @param string $rating Maximum rating (inclusive) [g|pg|r|x]
* @return instance $this
*/
public function setMaxRating( $rating ) {
if (in_array($r, array('g','pg','r','x'))) {
$this->_rating = $rating;
}
return $this;
}
/**
* Get maximum rating (inclusive)
*
* @return string
*/
public function getMaxRating() {
return $this->_rating;
}
/**
* Use secure connection
*
* @param bool $secure
* @return instance $this
*/
public function setSecure( $secure ) {
$this->_secure = (bool) $secure;
return $this;
}
/**
* Get usage of secure connection
*
* @return instance $this
*/
public function getSecure() {
return $this->_secure;
}
/**
* Set extra parameters for request URL
*
* @param string $param
* @param string $value
* @return instance $this
*/
public function setParam( $param, $value=NULL ) {
if (isset($value))
$this->_params[$param] = $value;
else
unset($this->_params[$param]);
return $this;
}
/**
* Build URL to retrieve infos from gravatar.com
*
* Data Formats
* - JSON
* - XML
* - PHP
* - VCF/vCard
* - QR Code (contain a link to the main profile page)
*
* @param string $email Email adress
* @param string $format Requested result format [json|xml|php|vcf|qr],
* defaults to 'php'
* @param bool $params Add parameters to URL (for QR code)
* @return string
*/
public function getInfoURL( $email, $format='php', $params=FALSE ) {
$url = $this->_secure ? self::URL_HTTPS : self::URL_HTTP;
$url .= md5(trim(strtolower($email))).'.'.$format;
$url .= $this->_buildParams($params);
return $url;
} // function getInfoURL()
/**
* Get account info
*
* Data Formats
* - JSON
* - XML
* - PHP
* - VCF/vCard
*
* Possible info data (json,xml,php):
* - id
* - hash
* - requestHash
* - profileUrl
* - preferredUsername
* - thumbnailUrl
* - urls (array)
* - photos (array)
* - value
* - type
* - name
* - displayName
*
* @param string $email Email adress
* @param string $format Requested result format [json|xml|php|vcf],
* defaults to 'php'
* @return array|string Returns "No valid user" in case of an error
*/
public function getInfo( $email, $format='php' ) {
$temp = @file_get_contents($this->getInfoURL($email, $format));
$utemp = @unserialize($temp); // works ONLY for php format!
if (isset($utemp['entry'][0]) AND is_array($utemp['entry'][0])) {
$utemp = $utemp['entry'][0];
return array_change_key_case($utemp, CASE_LOWER);
} else {
return $temp; // "User not found" or non-(php)serialized data
}
} // function getInfo()
/**
* @param string $email Email adress
* @param bool $img TRUE to return a complete IMG tag; FALSE for just the URL
* @param array $atts Optional, additional key/value attributes to include
* in the IMG tag
* @return string
*/
public function getGravatarURL( $email, $img=FALSE, $atts=array() ) {
$url = $this->_secure ? self::URL_HTTPS : self::URL_HTTP;
$url .= 'avatar/' . md5(trim(strtolower($email)));
if ($this->_usextension) {
$url .= '.jpg';
}
$url .= $this->_buildParams(TRUE);
if ($img) {
$url = '<img width="'.$this->_size.'" height="'.$this->_size.'" src="'.$url.'"';
foreach ($atts as $key=>$val) {
$url .= ' '.$key.'="'.$val.'"';
}
$url .= '/>';
}
return $url;
} // function getGravatarURL()
/**
* Set to defaults
*
* @return instance $this
*/
public function reset() {
$this->_secure = FALSE;
$this->_size = 80;
$this->_usextension = TRUE;
$this->_default = 'mm';
$this->_rating = 'g';
$this->_params = array();
} // function reset()
// -------------------------------------------------------------------------
// PROTECTED
// -------------------------------------------------------------------------
/**
* @var string
*/
protected $_secure;
/**
* Size in pixels, defaults to 80px [1-512]
*
* @var array
*/
protected $_size;
/**
* Use .jpg on gravatar requests
*
* @var bool
*/
protected $_usextension;
/**
* Default imageset to use [404|mm|identicon|monsterid|wavatar]
*
* @var array
*/
protected $_default;
/**
* Maximum rating (inclusive) [g|pg|r|x]
*
* @var array
*/
protected $_rating;
/**
* @var array
*/
protected $_params;
/**
* @param bool $img TRUE to return a complete IMG tag; FALSE for just the URL
* @return string
*/
protected function _buildParams( $gravatar ) {
$return = '?';
if ($gravatar) {
$return .= '?s=' . $this->_size . '&r=' . $this->_rating . '&'
// if default is set to y, use parameter 'f' (force)
. (($this->_default != 'y') ? 'd' : 'f') . '=' . $this->_default;
}
foreach ($this->_params as $key=>$value) {
$return .= '&'.$key.'='.$value;
}
return trim($return, '?');
}
}