1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ballen\Gravel; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Gravel |
7
|
|
|
* |
8
|
|
|
* Gravel is a PHP library which provides easy access to get and display Gravatars. |
9
|
|
|
* |
10
|
|
|
* @author Bobby Allen <[email protected]> |
11
|
|
|
* @license http://opensource.org/licenses/MIT |
12
|
|
|
* @link https://github.com/allebb/gravel |
13
|
|
|
* @link http://www.bobbyallen.me |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class Gravatar |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* * |
21
|
|
|
* The standard 'HTTP' (non-secure) URL to gravatar.com. |
22
|
|
|
*/ |
23
|
|
|
const HTTP_GRAVATAR_URL = 'http://www.gravatar.com/'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The secure 'HTTPS' URL to gravatar.com. |
27
|
|
|
*/ |
28
|
|
|
const HTTPS_GRAVATAR_URL = 'https://secure.gravatar.com/'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Instructs not to load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response. |
32
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
33
|
|
|
*/ |
34
|
|
|
const DEFAULT_404 = "404"; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A simple, cartoon-style silhouetted outline of a person "Mystery-man" (does not vary by email hash) |
38
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
39
|
|
|
*/ |
40
|
|
|
const DEFAULT_MYSTERYMAN = "mm"; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* A geometric pattern based on an email hash |
44
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
45
|
|
|
*/ |
46
|
|
|
const DEFAULT_IDENTICON = "identicon"; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* A generated 'monster' with different colors, faces, etc |
50
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
51
|
|
|
*/ |
52
|
|
|
const DEFAULT_MONSTER = "monsterid"; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Generated faces with differing features and backgrounds |
56
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
57
|
|
|
*/ |
58
|
|
|
const DEFAULT_WAVATAR = "wavatar"; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Awesome generated, 8-bit arcade-style pixelated faces |
62
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
63
|
|
|
*/ |
64
|
|
|
const DEFAULT_RETRO = "retro"; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* A transparent PNG image (border added to HTML below for demonstration purposes) |
68
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
69
|
|
|
*/ |
70
|
|
|
const DEFAULT_BLANK = "blank"; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Suitable for display on all websites with any audience type. |
74
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
75
|
|
|
*/ |
76
|
|
|
const RATING_G = 'g'; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* May contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence. |
80
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
81
|
|
|
*/ |
82
|
|
|
const RATING_PG = 'pg'; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* May contain such things as harsh profanity, intense violence, nudity, or hard drug use. |
86
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
87
|
|
|
*/ |
88
|
|
|
const RATING_R = 'r'; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* May contain hardcore sexual imagery or extremely disturbing violence. |
92
|
|
|
* @see https://en.gravatar.com/site/implement/images/ |
93
|
|
|
*/ |
94
|
|
|
const RATING_X = 'x'; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The email address of which you want to return the Gravatar for. |
98
|
|
|
* @var string Email address for the user to return the Gravatar for. |
99
|
|
|
*/ |
100
|
|
|
private $email; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The size of the Gravatar to return. |
104
|
|
|
* @var int The size of the Gravatar to display. |
105
|
|
|
*/ |
106
|
|
|
private $size = 120; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Options for the default avatar to return when the avatar does not meet |
110
|
|
|
* the rating threshold or when no gravar is found for the user. |
111
|
|
|
* @var string Default gravatar image. |
112
|
|
|
*/ |
113
|
|
|
private $default_avatar = self::DEFAULT_404; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the threshold of the Gravatar image. |
117
|
|
|
* @var string Rating setting. (Default is set to 'g') |
118
|
|
|
*/ |
119
|
|
|
private $rating = self::RATING_G; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Enable Gravatar URL over HTTPS (good for sites using HTTPS!) |
123
|
|
|
* @var bool Use the HTTPS protocol to display avatar images. (Default is HTTPS) |
124
|
|
|
*/ |
125
|
|
|
private $secure = true; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Override the generated Gravatar URL with a custom one. |
129
|
|
|
* @var null|string Custom avatar URL |
130
|
|
|
*/ |
131
|
|
|
private $overrideAvatarUrl = null; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Class constructor. |
135
|
|
|
* @param string $email Optionally construct the object with the email address. |
136
|
|
|
*/ |
137
|
16 |
|
public function __construct($email = null) |
138
|
|
|
{ |
139
|
16 |
|
if (!is_null($email)) { |
140
|
14 |
|
$this->setEmail($email); |
141
|
|
|
} |
142
|
16 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Generates the Hash for the email address suppiled. |
146
|
|
|
* @return string md5 hash of the email supplied. |
147
|
|
|
*/ |
148
|
14 |
|
private function generateAddressHash() |
149
|
|
|
{ |
150
|
14 |
|
return md5($this->email); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set sthe email address for the person. |
155
|
|
|
* @param string $email The Email address of the Gravatar account. |
156
|
|
|
* @return Gravatar |
157
|
|
|
*/ |
158
|
16 |
|
public function setEmail($email) |
159
|
|
|
{ |
160
|
16 |
|
$this->email = trim(strtolower($email)); |
161
|
16 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set the avatar size you would like to get back. |
166
|
|
|
* @param int $size The size of the Gravatar to get back. (Default is 120) |
167
|
|
|
* @return Gravatar |
168
|
|
|
*/ |
169
|
2 |
|
public function setSize($size = 120) |
170
|
|
|
{ |
171
|
2 |
|
$this->size = (int)$size; |
172
|
2 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Options for the default avatar to return when the avatar does not meet |
177
|
|
|
* the rating threshold or when no gravar is found for the user. Valid options |
178
|
|
|
* are: |
179
|
|
|
* |
180
|
|
|
* '404' : do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response |
181
|
|
|
* 'mm' : (mystery-man) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash) |
182
|
|
|
* 'identicon': a geometric pattern based on an email hash |
183
|
|
|
* 'monsterid': a generated 'monster' with different colors, faces, etc |
184
|
|
|
* 'wavatar' : generated faces with differing features and backgrounds |
185
|
|
|
* 'retro' : awesome generated, 8-bit arcade-style pixelated faces |
186
|
|
|
* 'blank' : a transparent PNG image (border added to HTML below for demonstration purposes) |
187
|
|
|
* @param string $option The prefix of the default avatar to return if no valid Gravatar is found for the supplied email address. |
188
|
|
|
* @return Gravatar |
189
|
|
|
*/ |
190
|
2 |
|
public function setDefaultAvatar($option) |
191
|
|
|
{ |
192
|
2 |
|
$this->default_avatar = $option; |
193
|
2 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set the rating threshold, will not return a Gravatar unless its in this band. |
198
|
|
|
* Valid options are ('g' is default!) |
199
|
|
|
* |
200
|
|
|
* 'g' : suitable for display on all websites with any audience type. |
201
|
|
|
* 'pg': may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence. |
202
|
|
|
* 'r' : may contain such things as harsh profanity, intense violence, nudity, or hard drug use. |
203
|
|
|
* 'x' : may contain hardcore sexual imagery or extremely disturbing violence. |
204
|
|
|
* @param string $rating |
205
|
|
|
* @return Gravatar |
206
|
|
|
*/ |
207
|
2 |
|
public function setRating($rating) |
208
|
|
|
{ |
209
|
2 |
|
$this->rating = $rating; |
210
|
2 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Set the protocol to be used for the image URL to HTTPS. |
215
|
|
|
* @return Gravatar |
216
|
|
|
*/ |
217
|
2 |
|
public function setUseHTTPS() |
218
|
|
|
{ |
219
|
2 |
|
$this->secure = true; |
220
|
2 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set the protocol to be used for the image URL to HTTP. |
225
|
|
|
* @return Gravatar |
226
|
|
|
*/ |
227
|
2 |
|
public function setUseHTTP() |
228
|
|
|
{ |
229
|
2 |
|
$this->secure = false; |
230
|
2 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Overrides the generated Gravatar with a custom avatar URL. |
235
|
|
|
* @param string $url |
236
|
|
|
* @return Gravatar |
237
|
|
|
*/ |
238
|
2 |
|
public function setCustomAvatarUrl($url) |
239
|
|
|
{ |
240
|
2 |
|
$this->overrideAvatarUrl = $url; |
241
|
2 |
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Builds and returns the final Gravatar URL. |
246
|
|
|
* @return string The URL to the Gravatar or Custom Avatar Image. |
247
|
|
|
*/ |
248
|
16 |
|
public function buildGravatarUrl() |
249
|
|
|
{ |
250
|
|
|
|
251
|
16 |
|
if ($this->overrideAvatarUrl !== null) { |
252
|
2 |
|
return $this->overrideAvatarUrl; |
253
|
|
|
} |
254
|
|
|
|
255
|
14 |
|
$base_url = self::HTTPS_GRAVATAR_URL; |
256
|
14 |
|
if (!$this->secure) { |
257
|
2 |
|
$base_url = self::HTTP_GRAVATAR_URL; |
258
|
|
|
} |
259
|
14 |
|
return $base_url . 'avatar/' . $this->generateAddressHash() . '?s=' . $this->size . '&r=' . $this->rating . '&d=' . $this->default_avatar; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns the Gravar URL of the current instance. |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
4 |
|
public function __toString() |
267
|
|
|
{ |
268
|
4 |
|
return $this->buildGravatarUrl(); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|