Skip to content
Newer
Older
100644 301 lines (263 sloc) 7.85 KB
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
1 <?php
2 /**
3 *
4 *===================================================================
5 *
6 * GravatarLib - Gravatar PHP 5.3 OOP Library
7 *-------------------------------------------------------------------
8 * @category gravatarlib
9 * @package gravatarlib
3f5966d @damianb Move this over to the emberlabs organization
damianb authored
10 * @author emberlabs.org
11 * @copyright (c) 2011 emberlabs.org
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
12 * @license MIT License
3f5966d @damianb Move this over to the emberlabs organization
damianb authored
13 * @link https://github.com/emberlabs/gravatarlib
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
14 *
15 *===================================================================
16 *
17 * This source file is subject to the MIT license that is bundled
18 * with this package in the file LICENSE.
19 *
20 */
21
7ba6734 @damianb case pickiness
damianb authored
22 namespace emberlabs\GravatarLib;
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
23 use \InvalidArgumentException;
24
25 /**
26 * GravatarLib - A lightweight library for working with gravatars en masse,
27 * used for setting a ton of options and then just passing in only the essential data needed.
28 *
29 *
30 * @category gravatarlib
31 * @package gravatarlib
3f5966d @damianb Move this over to the emberlabs organization
damianb authored
32 * @author emberlabs.org
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
33 * @license MIT License
3f5966d @damianb Move this over to the emberlabs organization
damianb authored
34 * @link https://github.com/emberlabs/gravatarlib
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
35 */
36 class Gravatar
37 {
38 /**
39 * @var integer - The size to use for avatars.
40 */
41 protected $size = 80;
42
43 /**
44 * @var mixed - The default image to use - either a string of the gravatar-recognized default image "type" to use, a URL, or false if using the...default gravatar default image (hah)
45 */
46 protected $default_image = false;
47
48 /**
49 * @var string - The maximum rating to allow for the avatar.
50 */
51 protected $max_rating = 'g';
52
53 /**
54 * @var boolean - Should we use the secure (HTTPS) URL base?
55 */
56 protected $use_secure_url = false;
57
13f22bb @damianb make a micro-optimization, no usage changes; just internal
damianb authored
58 /**
59 * @var string - A temporary internal cache of the URL parameters to use.
60 */
61 protected $param_cache = NULL;
62
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
63 /**#@+
64 * @var string - URL constants for the avatar images
65 */
66 const HTTP_URL = 'http://www.gravatar.com/avatar/';
67 const HTTPS_URL = 'https://secure.gravatar.com/avatar/';
68 /**#@-*/
69
70 /**
71 * Get the currently set avatar size.
72 * @return integer - The current avatar size in use.
73 */
74 public function getAvatarSize()
75 {
76 return $this->size;
77 }
78
79 /**
80 * Set the avatar size to use.
81 * @param integer $size - The avatar size to use, must be less than 512 and greater than 0.
7ba6734 @damianb case pickiness
damianb authored
82 * @return \emberlabs\GravatarLib\Gravatar - Provides a fluent interface.
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
83 *
84 * @throws \InvalidArgumentException
85 */
86 public function setAvatarSize($size)
87 {
13f22bb @damianb make a micro-optimization, no usage changes; just internal
damianb authored
88 // Wipe out the param cache.
89 $this->param_cache = NULL;
90
3c5e6ac @damianb Clarify an exception, and use a better check in setAvatarSize
damianb authored
91 if(!is_int($size) && !ctype_digit($size))
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
92 {
93 throw new InvalidArgumentException('Avatar size specified must be an integer');
94 }
95
3c5e6ac @damianb Clarify an exception, and use a better check in setAvatarSize
damianb authored
96 $this->size = (int) $size;
97
98 if($this->size > 512 || $this->size < 0)
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
99 {
3c5e6ac @damianb Clarify an exception, and use a better check in setAvatarSize
damianb authored
100 throw new InvalidArgumentException('Avatar size must be within 0 pixels and 512 pixels');
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
101 }
102
103 return $this;
104 }
105
106 /**
107 * Get the current default image setting.
108 * @return mixed - False if no default image set, string if one is set.
109 */
110 public function getDefaultImage()
111 {
112 return $this->default_image;
113 }
114
115 /**
116 * Set the default image to use for avatars.
117 * @param mixed $image - The default image to use. Use boolean false for the gravatar default, a string containing a valid image URL, or a string specifying a recognized gravatar "default".
7ba6734 @damianb case pickiness
damianb authored
118 * @return \emberlabs\GravatarLib\Gravatar - Provides a fluent interface.
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
119 *
120 * @throws \InvalidArgumentException
121 */
122 public function setDefaultImage($image)
123 {
124 // Quick check against boolean false.
125 if($image === false)
126 {
127 $this->default_image = false;
128
129 return $this;
130 }
131
13f22bb @damianb make a micro-optimization, no usage changes; just internal
damianb authored
132 // Wipe out the param cache.
133 $this->param_cache = NULL;
134
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
135 // Check $image against recognized gravatar "defaults", and if it doesn't match any of those we need to see if it is a valid URL.
136 $_image = strtolower($image);
e9117c5 @damianb New image default set and handle forcing default avatars
damianb authored
137 $valid_defaults = array('404' => 1, 'mm' => 1, 'identicon' => 1, 'monsterid' => 1, 'wavatar' => 1, 'retro' => 1);
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
138 if(!isset($valid_defaults[$_image]))
139 {
140 if(!filter_var($image, FILTER_VALIDATE_URL))
141 {
142 throw new InvalidArgumentException('The default image specified is not a recognized gravatar "default" and is not a valid URL');
143 }
144 else
145 {
8692c31 @damianb Use rawurlencode() on the provided default image if it is a valid URL.
damianb authored
146 $this->default_image = rawurlencode($image);
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
147 }
148 }
149 else
150 {
151 $this->default_image = $_image;
152 }
153
154 return $this;
155 }
156
157 /**
158 * Get the current maximum allowed rating for avatars.
159 * @return string - The string representing the current maximum allowed rating ('g', 'pg', 'r', 'x').
160 */
161 public function getMaxRating()
162 {
163 return $this->max_rating;
164 }
165
166 /**
167 * Set the maximum allowed rating for avatars.
168 * @param string $rating - The maximum rating to use for avatars ('g', 'pg', 'r', 'x').
7ba6734 @damianb case pickiness
damianb authored
169 * @return \emberlabs\GravatarLib\Gravatar - Provides a fluent interface.
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
170 *
171 * @throws \InvalidArgumentException
172 */
173 public function setMaxRating($rating)
174 {
13f22bb @damianb make a micro-optimization, no usage changes; just internal
damianb authored
175 // Wipe out the param cache.
176 $this->param_cache = NULL;
177
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
178 $rating = strtolower($rating);
179 $valid_ratings = array('g' => 1, 'pg' => 1, 'r' => 1, 'x' => 1);
180 if(!isset($valid_ratings[$rating]))
181 {
cd15a07 @damianb Fix the exception message when setting an invalid maximum content rating
damianb authored
182 throw new InvalidArgumentException(sprintf('Invalid rating "%s" specified, only "g", "pg", "r", or "x" are allowed to be used.', $rating));
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
183 }
184
185 $this->max_rating = $rating;
186
187 return $this;
188 }
189
190 /**
191 * Check if we are using the secure protocol for the image URLs.
192 * @return boolean - Are we supposed to use the secure protocol?
193 */
194 public function usingSecureImages()
195 {
196 return $this->use_secure_url;
197 }
198
199 /**
200 * Enable the use of the secure protocol for image URLs.
7ba6734 @damianb case pickiness
damianb authored
201 * @return \emberlabs\GravatarLib\Gravatar - Provides a fluent interface.
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
202 */
203 public function enableSecureImages()
204 {
205 $this->use_secure_url = true;
206
207 return $this;
208 }
209
210 /**
211 * Disable the use of the secure protocol for image URLs.
7ba6734 @damianb case pickiness
damianb authored
212 * @return \emberlabs\GravatarLib\Gravatar - Provides a fluent interface.
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
213 */
214 public function disableSecureImages()
215 {
216 $this->use_secure_url = false;
217
218 return $this;
219 }
220
221 /**
222 * Build the avatar URL based on the provided email address.
223 * @param string $email - The email to get the gravatar for.
5b28315 @damianb ...but what if I already hashed the email address? LOOK NO FURTHER!
damianb authored
224 * @param string $hash_email - Should we hash the $email variable? (Useful if the email address has a hash stored already)
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
225 * @return string - The XHTML-safe URL to the gravatar.
226 */
5b28315 @damianb ...but what if I already hashed the email address? LOOK NO FURTHER!
damianb authored
227 public function buildGravatarURL($email, $hash_email = true)
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
228 {
229 // Start building the URL, and deciding if we're doing this via HTTPS or HTTP.
230 if($this->usingSecureImages())
231 {
232 $url = static::HTTPS_URL;
233 }
234 else
235 {
236 $url = static::HTTP_URL;
237 }
238
239 // Tack the email hash onto the end.
e9117c5 @damianb New image default set and handle forcing default avatars
damianb authored
240 if($hash_email == true && !empty($email))
5b28315 @damianb ...but what if I already hashed the email address? LOOK NO FURTHER!
damianb authored
241 {
242 $url .= $this->getEmailHash($email);
243 }
e9117c5 @damianb New image default set and handle forcing default avatars
damianb authored
244 elseif(!empty($email))
5b28315 @damianb ...but what if I already hashed the email address? LOOK NO FURTHER!
damianb authored
245 {
246 $url .= $email;
247 }
e9117c5 @damianb New image default set and handle forcing default avatars
damianb authored
248 else
249 {
250 $url .= str_repeat('0', 32);
251 }
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
252
13f22bb @damianb make a micro-optimization, no usage changes; just internal
damianb authored
253 // Check to see if the param_cache property has been populated yet
254 if($this->param_cache === NULL)
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
255 {
13f22bb @damianb make a micro-optimization, no usage changes; just internal
damianb authored
256 // Time to figure out our request params
257 $params = array();
258 $params[] = 's=' . $this->getAvatarSize();
259 $params[] = 'r=' . $this->getMaxRating();
260 if($this->getDefaultImage())
261 {
262 $params[] = 'd=' . $this->getDefaultImage();
263 }
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
264
13f22bb @damianb make a micro-optimization, no usage changes; just internal
damianb authored
265 // Stuff the request params into the param_cache property for later reuse
266 $this->params_cache = (!empty($params)) ? '?' . implode('&amp;', $params) : '';
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
267 }
268
e9117c5 @damianb New image default set and handle forcing default avatars
damianb authored
269 // Handle "null" gravatar requests.
270 $tail = '';
271 if(empty($email))
272 {
273 $tail = !empty($this->params_cache) ? '&amp;f=y' : '?f=y';
274 }
275
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
276 // And we're done.
e9117c5 @damianb New image default set and handle forcing default avatars
damianb authored
277 return $url . $this->params_cache . $tail;
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
278 }
279
280 /**
281 * Get the email hash to use (after cleaning the string).
282 * @param string $email - The email to get the hash for.
283 * @return string - The hashed form of the email, post cleaning.
284 */
285 public function getEmailHash($email)
286 {
287 // Using md5 as per gravatar docs.
288 return hash('md5', strtolower(trim($email)));
289 }
290
291 /**
292 * ...Yeah, it's just an alias of buildGravatarURL. This is just to make it easier to use as a twig asset.
7ba6734 @damianb case pickiness
damianb authored
293 * @see \emberlabs\GravatarLib\Gravatar::buildGravatarURL()
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
294 */
5b28315 @damianb ...but what if I already hashed the email address? LOOK NO FURTHER!
damianb authored
295 public function get($email, $hash_email = true)
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
296 {
297 // Just an alias. Makes it easy to use this as a twig asset.
5b28315 @damianb ...but what if I already hashed the email address? LOOK NO FURTHER!
damianb authored
298 return $this->buildGravatarURL($email, $hash_email);
0cf1a19 @damianb initial commit, commit readme, license, library, and gitignore file
damianb authored
299 }
300 }
Something went wrong with that request. Please try again.