forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGravatar.php
425 lines (363 loc) · 9.95 KB
/
Gravatar.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Serghei Iakovlev <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Avatar;
use InvalidArgumentException;
use Phalcon\Config;
/**
* Phalcon Gravatar.
*
* This component provides an easy way to retrieve a user's profile image
* from Gravatar site based on a given email address.
*
* If the email address cannot be matched with a Gravatar account, an alternative will
* be returned based on the Gravatar::$defaultImage setting. Users with gravatars can
* have a default image if you want to.
*
* @link https://en.gravatar.com/site/implement/
* @package Phalcon\Avatar
*/
class Gravatar implements Avatarable
{
/**
* The gravatar service URL
* @type string
*/
const HTTP_URL = 'http://www.gravatar.com/avatar/';
/**
* The secure gravatar service URL
* @type string
*/
const HTTPS_URL = 'https://secure.gravatar.com/avatar/';
/**
* The minimum size allowed for the Gravatar
* @type int
*/
const MIN_AVATAR_SIZE = 0;
/**
* The maximum size allowed for the Gravatar
* @type int
*/
const MAX_AVATAR_SIZE = 2048;
/**
* @type string
*/
const RATING_G = 'g';
/**
* @type string
*/
const RATING_PG = 'pg';
/**
* @type string
*/
const RATING_R = 'r';
/**
* @type string
*/
const RATING_X = 'x';
/**
* The default image.
*
* Possible values:
* - String of the gravatar-recognized default image "type" to use
* - URL
* - false if using the default gravatar image
*
* @var mixed
*/
private $defaultImage = false;
/**
* Gravatar defaults
* @var array
*/
private $validDefaults = [
'404' => true,
'mm' => true,
'identicon' => true,
'monsterid' => true,
'wavatar' => true,
'retro' => true,
'blank' => true
];
/**
* Gravatar rating
* @var array
*/
private $validRatings = [
self::RATING_G => true,
self::RATING_PG => true,
self::RATING_R => true,
self::RATING_X => true
];
/**
* The size to use for avatars
* @var int
*/
private $size = 80;
/**
* The maximum rating to allow for the avatar
* @var string
*/
private $rating = self::RATING_G;
/**
* Should we use the secure (HTTPS) URL base?
* @var bool
*/
private $secureURL = false;
/**
* The default image shall be shown even if user that has an gravatar profile.
* @var bool
*/
private $forceDefault = false;
public function __construct($config)
{
if ($config instanceof Config) {
$config = $config->toArray();
}
if (!is_array($config)) {
throw new InvalidArgumentException('Config must be either an array or \Phalcon\Config instance');
}
if (isset($config['default_image'])) {
$this->setDefaultImage($config['default_image']);
}
if (isset($config['rating'])) {
$this->setRating($config['rating']);
}
if (isset($config['size'])) {
$this->setSize($config['size']);
}
if (isset($config['use_https']) && $config['use_https']) {
$this->enableSecureURL();
}
}
/**
* {@inheritdoc}
*
* Possible $image formats:
* - a string specifying a recognized gravatar "default"
* - a string containing a valid image URL
* - boolean false for the gravatar default
*
* @param mixed $image The default image to use
* @return Gravatar
*
* @throws InvalidArgumentException
*/
public function setDefaultImage($image)
{
if (false === $image) {
$this->defaultImage = false;
return $this;
}
$default = strtolower(trim($image));
if (!isset($this->validDefaults[$default])) {
if (!filter_var($image, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
throw new InvalidArgumentException(
'The default image specified is not a recognized gravatar "default" and is not a valid URL'
);
} else {
$this->defaultImage = $image;
}
} else {
$this->defaultImage = $default;
}
return $this;
}
/**
* {@inheritdoc}
*
* @return mixed
*/
public function getDefaultImage()
{
return $this->defaultImage;
}
/**
* {@inheritdoc}
*
* By default, images from Gravatar.com will be returned as 80x80px
*
* @param int $size The avatar size to use
* @return Gravatar
*
* @throws InvalidArgumentException
*/
public function setSize($size)
{
$options = [
'options' => [
'min_range' => static::MIN_AVATAR_SIZE,
'max_range' => static::MAX_AVATAR_SIZE
]
];
if (false === filter_var($size, FILTER_VALIDATE_INT, $options)) {
throw new InvalidArgumentException(sprintf(
"Can't set Gravatar size. Size must be an integer within %s and %s pixels",
static::MIN_AVATAR_SIZE,
static::MAX_AVATAR_SIZE
));
}
$this->size = (int) $size;
return $this;
}
/**
* {@inheritdoc}
*
* @return int
*/
public function getSize()
{
return $this->size;
}
/**
* Set the maximum allowed rating for avatars
*
* @param string $rating The maximum rating to use for avatars ('g', 'pg', 'r', 'x')
* @return Gravatar
*
* @throws InvalidArgumentException
*/
public function setRating($rating)
{
$rating = strtolower(trim($rating));
if (!isset($this->validRatings[$rating])) {
$allowed = array_keys($this->validRatings);
$last = array_pop($allowed);
$allowed = join(',', $allowed);
throw new InvalidArgumentException(
sprintf("Invalid rating '%s' specified. Available for use only: %s or %s", $rating, $allowed, $last)
);
}
$this->rating = $rating;
return $this;
}
/**
* Get the current maximum allowed rating for avatars
*
* The string representing the current maximum allowed rating ('g', 'pg', 'r', 'x').
*
* @return int
*/
public function getRating()
{
return $this->rating;
}
/**
* Check if we are using the secure protocol for the image URLs
*
* @return bool
*/
public function isUseSecureURL()
{
return $this->secureURL;
}
/**
* Enable the use of the secure protocol for image URLs
*
* @return Gravatar
*/
public function enableSecureURL()
{
$this->secureURL = true;
return $this;
}
/**
* Disable the use of the secure protocol for image URLs
*
* @return Gravatar
*/
public function disableSecureURL()
{
$this->secureURL = false;
return $this;
}
/**
* Get the email hash to use
*
* @param string $email The email to get the hash for
* @return string
*/
public function getEmailHash($email)
{
return md5(strtolower(trim($email)));
}
/**
* Forces Gravatar to display default image
*
* @return Gravatar
*/
public function enableForceDefault()
{
$this->forceDefault = true;
return $this;
}
/**
* Disable forces default image
*
* @return Gravatar
*/
public function disableForceDefault()
{
$this->forceDefault = false;
return $this;
}
/**
* Check if need to force the default image to always load
*
* @return bool
*/
public function isUseForceDefault()
{
return $this->forceDefault;
}
/**
* {@inheritdoc}
*
* @param string $identity The email to get the gravatar for
* @return string
*/
public function getAvatar($identity)
{
return $this->buildURL($identity);
}
/**
* Build the Gravatar URL based on the configuration and provided email address
*
* @param string $email The email to get the gravatar for
* @return string
*/
protected function buildURL($email)
{
$url = static::HTTP_URL;
if ($this->secureURL) {
$url = static::HTTPS_URL;
}
$url .= $this->getEmailHash($email);
$query = [
's' => $this->getSize(),
'r' => $this->getRating()
];
if ($this->defaultImage) {
$query = array_merge($query, ['d' => $this->defaultImage]);
}
if ($this->forceDefault) {
$query = array_merge($query, ['f' => 'y']);
}
$url .= '?'.http_build_query($query, '', '&');
return $url;
}
}