1
|
|
|
<?php namespace Arcanedev\Gravatar; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Gravatar\Helpers\NumberChecker; |
4
|
|
|
use Arcanedev\Html\Elements\Img; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Gravatar |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Gravatar |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Gravatar implements Contracts\Gravatar |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Traits |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use Concerns\HashEmail; |
21
|
|
|
|
22
|
|
|
/* ----------------------------------------------------------------- |
23
|
|
|
| Constants |
24
|
|
|
| ----------------------------------------------------------------- |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
const BASE_URL = 'http://www.gravatar.com/avatar/'; |
28
|
|
|
const SECURE_URL = 'https://secure.gravatar.com/avatar/'; |
29
|
|
|
|
30
|
|
|
/* ----------------------------------------------------------------- |
31
|
|
|
| Properties |
32
|
|
|
| ----------------------------------------------------------------- |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The default image to use (gravatar-recognized type, url or false = default gravatar image) |
37
|
|
|
* |
38
|
|
|
* @var mixed |
39
|
|
|
*/ |
40
|
|
|
protected $defaultImage = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The maximum rating to allow for the avatar. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $rating = 'g'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The avatar size. |
51
|
|
|
* |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
protected $size = 80; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Should we use the secure (HTTPS) URL base ? |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
protected $secure = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* A temporary internal cache of the URL parameters. |
65
|
|
|
* |
66
|
|
|
* @var array|null |
67
|
|
|
*/ |
68
|
|
|
protected $cachedParams = null; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Supported image defaults. |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
private $supportedImages = ['404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro']; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Supported image ratings. |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
private $supportedRatings = ['g', 'pg', 'r', 'x']; |
83
|
|
|
|
84
|
|
|
/* ----------------------------------------------------------------- |
85
|
|
|
| Constructor |
86
|
|
|
| ----------------------------------------------------------------- |
87
|
|
|
*/ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Make a gravatar instance. |
91
|
|
|
* |
92
|
|
|
* @param string $default |
93
|
|
|
* @param int $size |
94
|
|
|
* @param string $rating |
95
|
|
|
*/ |
96
|
68 |
|
public function __construct($default = 'mm', $size = 80, $rating = 'g') |
97
|
|
|
{ |
98
|
68 |
|
$this->setDefaultImage($default); |
99
|
68 |
|
$this->setSize($size); |
100
|
68 |
|
$this->setRating($rating); |
101
|
68 |
|
$this->enableSecure(); |
102
|
68 |
|
} |
103
|
|
|
|
104
|
|
|
/* ----------------------------------------------------------------- |
105
|
|
|
| Getters & Setters |
106
|
|
|
| ----------------------------------------------------------------- |
107
|
|
|
*/ |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the current default image setting. |
111
|
|
|
* |
112
|
|
|
* @return string|false |
113
|
|
|
*/ |
114
|
32 |
|
public function getDefaultImage() |
115
|
|
|
{ |
116
|
32 |
|
return $this->defaultImage; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the default image to use for avatars. |
121
|
|
|
* |
122
|
|
|
* @param string|false $image |
123
|
|
|
* |
124
|
|
|
* @return \Arcanedev\Gravatar\Gravatar |
125
|
|
|
* |
126
|
|
|
* @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException |
127
|
|
|
*/ |
128
|
68 |
|
public function setDefaultImage($image) |
129
|
|
|
{ |
130
|
68 |
|
if ($image !== false) { |
131
|
68 |
|
$this->cachedParams = null; |
132
|
|
|
|
133
|
68 |
|
$image = in_array(strtolower($image), $this->supportedImages) |
134
|
68 |
|
? strtolower($image) |
135
|
68 |
|
: $this->checkImageUrl($image); |
136
|
|
|
} |
137
|
|
|
|
138
|
68 |
|
$this->defaultImage = $image; |
139
|
|
|
|
140
|
68 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the currently set avatar size. |
145
|
|
|
* |
146
|
|
|
* @return int |
147
|
|
|
*/ |
148
|
40 |
|
public function getSize() |
149
|
|
|
{ |
150
|
40 |
|
return $this->size; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set the avatar size to use. |
155
|
|
|
* |
156
|
|
|
* @param integer $size - The avatar size to use, must be less than 512 and greater than 0. |
157
|
|
|
* |
158
|
|
|
* @return \Arcanedev\Gravatar\Gravatar |
159
|
|
|
* |
160
|
|
|
* @throws \Arcanedev\Gravatar\Exceptions\InvalidImageSizeException |
161
|
|
|
*/ |
162
|
68 |
|
public function setSize($size) |
163
|
|
|
{ |
164
|
68 |
|
$this->cachedParams = null; |
165
|
|
|
|
166
|
68 |
|
$this->checkSize($size); |
167
|
|
|
|
168
|
68 |
|
$this->size = $size; |
169
|
|
|
|
170
|
68 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the current maximum allowed rating for avatars. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
36 |
|
public function getRating() |
179
|
|
|
{ |
180
|
36 |
|
return $this->rating; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the maximum allowed rating for avatars. |
185
|
|
|
* |
186
|
|
|
* @param string $rating |
187
|
|
|
* |
188
|
|
|
* @return \Arcanedev\Gravatar\Gravatar |
189
|
|
|
* |
190
|
|
|
* @throws \Arcanedev\Gravatar\Exceptions\InvalidImageRatingException |
191
|
|
|
*/ |
192
|
68 |
|
public function setRating($rating) |
193
|
|
|
{ |
194
|
68 |
|
$this->cachedParams = null; |
195
|
|
|
|
196
|
68 |
|
$rating = strtolower($rating); |
197
|
|
|
|
198
|
68 |
|
if ( ! in_array($rating, $this->supportedRatings)) |
199
|
4 |
|
throw Exceptions\InvalidImageRatingException::make($rating); |
200
|
|
|
|
201
|
68 |
|
$this->rating = $rating; |
202
|
|
|
|
203
|
68 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Check if we are using the secure protocol for the image URLs. |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
40 |
|
public function isSecured() |
212
|
|
|
{ |
213
|
40 |
|
return $this->secure; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/* ----------------------------------------------------------------- |
217
|
|
|
| Main Methods |
218
|
|
|
| ----------------------------------------------------------------- |
219
|
|
|
*/ |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get Gravatar image source. |
223
|
|
|
* |
224
|
|
|
* @param string $email |
225
|
|
|
* @param int|null $size |
226
|
|
|
* @param string|null $rating |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
12 |
|
public function src($email, $size = null, $rating = null) |
231
|
|
|
{ |
232
|
12 |
|
if (is_null($size)) { |
233
|
4 |
|
$size = $this->getSize(); |
234
|
|
|
} |
235
|
|
|
|
236
|
12 |
|
$size = max(1, min(512, $size)); |
237
|
|
|
|
238
|
12 |
|
$this->setSize($size); |
239
|
|
|
|
240
|
12 |
|
if ( ! is_null($rating)) { |
241
|
4 |
|
$this->setRating($rating); |
242
|
|
|
} |
243
|
|
|
|
244
|
12 |
|
return $this->get($email); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get the avatar URL based on the provided email address. |
249
|
|
|
* |
250
|
|
|
* @param string $email |
251
|
|
|
* @param bool $hash |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
32 |
|
public function get($email, $hash = true) |
256
|
|
|
{ |
257
|
32 |
|
$url = $this->isSecured() ? static::SECURE_URL : static::BASE_URL; |
258
|
32 |
|
$url .= empty($email) |
259
|
16 |
|
? str_repeat('0', 32) |
260
|
32 |
|
: ($hash ? static::hashEmail($email) : $email); |
261
|
|
|
|
262
|
32 |
|
$params = $this->getParams($email); |
263
|
|
|
|
264
|
32 |
|
return $url.'?'.http_build_query($params); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get Gravatar image tag. |
269
|
|
|
* |
270
|
|
|
* @param string $email |
271
|
|
|
* @param string|null $alt |
272
|
|
|
* @param array $attributes |
273
|
|
|
* @param string|null $rating |
274
|
|
|
* |
275
|
|
|
* @return \Arcanedev\Html\Elements\Img |
276
|
|
|
*/ |
277
|
8 |
|
public function image($email, $alt = null, array $attributes = [], $rating = null) |
278
|
|
|
{ |
279
|
8 |
|
$dimensions = array_values( |
280
|
8 |
|
Arr::only($attributes, ['width', 'height']) |
281
|
|
|
); |
282
|
|
|
|
283
|
8 |
|
$size = count($dimensions) |
284
|
4 |
|
? min(512, max($dimensions)) |
285
|
8 |
|
: $this->getSize(); |
286
|
|
|
|
287
|
8 |
|
return Img::make() |
288
|
8 |
|
->src($this->src($email, $size, $rating)) |
289
|
8 |
|
->attributeUnless(is_null($alt), 'alt', $alt) |
290
|
8 |
|
->attributes($attributes); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get profile's data. |
295
|
|
|
* |
296
|
|
|
* @param string $email |
297
|
|
|
* @param mixed|null $default |
298
|
|
|
* |
299
|
|
|
* @return array|mixed |
300
|
|
|
*/ |
301
|
4 |
|
public function profile($email, $default = null) |
302
|
|
|
{ |
303
|
4 |
|
return (new Profile)->get($email, $default); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Enable the use of the secure protocol for image URLs. |
308
|
|
|
* |
309
|
|
|
* @return \Arcanedev\Gravatar\Gravatar |
310
|
|
|
*/ |
311
|
68 |
|
public function enableSecure() |
312
|
|
|
{ |
313
|
68 |
|
$this->secure = true; |
314
|
|
|
|
315
|
68 |
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Disable the use of the secure protocol for image URLs. |
320
|
|
|
* |
321
|
|
|
* @return \Arcanedev\Gravatar\Gravatar |
322
|
|
|
*/ |
323
|
4 |
|
public function disableSecure() |
324
|
|
|
{ |
325
|
4 |
|
$this->secure = false; |
326
|
|
|
|
327
|
4 |
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Check if email has a gravatar. |
332
|
|
|
* |
333
|
|
|
* @param string $email |
334
|
|
|
* |
335
|
|
|
* @return bool |
336
|
|
|
*/ |
337
|
4 |
|
public function exists($email) |
338
|
|
|
{ |
339
|
4 |
|
$this->setDefaultImage('404'); |
340
|
|
|
|
341
|
4 |
|
$headers = get_headers($this->get($email), 1); |
342
|
|
|
|
343
|
4 |
|
return strpos($headers[0], '200') ? true : false; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/* ----------------------------------------------------------------- |
347
|
|
|
| Check Methods |
348
|
|
|
| ----------------------------------------------------------------- |
349
|
|
|
*/ |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Check image url. |
353
|
|
|
* |
354
|
|
|
* @param string $image |
355
|
|
|
* |
356
|
|
|
* @return string |
357
|
|
|
* |
358
|
|
|
* @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException |
359
|
|
|
*/ |
360
|
8 |
|
private function checkImageUrl($image) |
361
|
|
|
{ |
362
|
8 |
|
if ( ! filter_var($image, FILTER_VALIDATE_URL)) { |
363
|
4 |
|
throw Exceptions\InvalidImageUrlException::make(); |
364
|
|
|
} |
365
|
|
|
|
366
|
4 |
|
return $image; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/* ----------------------------------------------------------------- |
370
|
|
|
| Other Methods |
371
|
|
|
| ----------------------------------------------------------------- |
372
|
|
|
*/ |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Get params. |
376
|
|
|
* |
377
|
|
|
* @param string $email |
378
|
|
|
* |
379
|
|
|
* @return array |
380
|
|
|
*/ |
381
|
32 |
|
private function getParams($email) |
382
|
|
|
{ |
383
|
32 |
|
$params = $this->cachedParams; |
384
|
|
|
|
385
|
32 |
|
if (is_null($this->cachedParams)) { |
386
|
32 |
|
$params['s'] = $this->getSize(); |
387
|
32 |
|
$params['r'] = $this->getRating(); |
388
|
|
|
|
389
|
32 |
|
if ($this->getDefaultImage() !== false) { |
390
|
28 |
|
$params['d'] = $this->getDefaultImage(); |
391
|
|
|
} |
392
|
|
|
|
393
|
32 |
|
$this->cachedParams = $params; |
394
|
|
|
} |
395
|
|
|
|
396
|
32 |
|
if (empty($email)) { |
397
|
16 |
|
$params['f'] = 'y'; |
398
|
|
|
} |
399
|
|
|
|
400
|
32 |
|
return (array) $params; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Check the image size. |
405
|
|
|
* |
406
|
|
|
* @param int $size |
407
|
|
|
*/ |
408
|
68 |
|
private function checkSize(&$size) |
409
|
|
|
{ |
410
|
68 |
|
if ( ! NumberChecker::isIntValue($size)) { |
411
|
4 |
|
throw new Exceptions\InvalidImageSizeException( |
412
|
4 |
|
'Avatar size specified must be an integer.' |
413
|
|
|
); |
414
|
|
|
} |
415
|
|
|
|
416
|
68 |
|
$size = (int) $size; |
417
|
|
|
|
418
|
68 |
|
if ( ! NumberChecker::isIntBetween($size, 0, 512)) { |
419
|
4 |
|
throw new Exceptions\InvalidImageSizeException( |
420
|
4 |
|
'Avatar size must be within 0 pixels and 512 pixels.' |
421
|
|
|
); |
422
|
|
|
} |
423
|
68 |
|
} |
424
|
|
|
} |
425
|
|
|
|