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
<?php namespace Creativeorange\Gravatar;
use Creativeorange\Gravatar\Exceptions\InvalidEmailException;
/**
* Class Gravatar
* @package Creativeorange\Gravatar
*/
class Gravatar {
/**
* Gravatar base url
*
* @var string
*/
private $publicBaseUrl = 'http://www.gravatar.com/avatar/';
/**
* Gravatar secure base url
*
* @var string
*/
private $secureBaseUrl = 'https://secure.gravatar.com/avatar/';
/**
* Email address to check
*
* @var string
*/
private $email;
/**
* @var array
*/
private $config;
/**
* @var string
*/
private $fallback;
/**
* Override the default image fallback set in the config.
* Can either be a public URL to an image or a valid themed image.
* For more info, visit http://en.gravatar.com/site/implement/images/#default-image
*
* @param string $fallback
* @return $this
*/
public function fallback ($fallback)
{
if (
filter_var($fallback, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)
||
in_array( $fallback, array('mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank') )
)
$this->fallback = $fallback;
else
$this->fallback = false;
return $this;
}
/**
* Check if Gravatar has an avatar for the given email address
*
* @param $email
* @return bool
* @throws InvalidEmailException
*/
public function exists ($email)
{
$this->checkEmail($email);
$this->email = $email;
$this->setConfig(['fallback' => 404]);
$headers = get_headers($this->buildUrl());
return (boolean) strpos($headers[0], '200');
}
/**
* Get the gravatar url
*
* @param $email
* @param string $configGroup
* @return string
* @throws InvalidEmailException
*/
public function get($email, $configGroup = 'default')
{
$this->checkEmail($email);
$this->setConfig($configGroup);
$this->email = $email;
return $this->buildUrl();
}
/**
* Helper function for setting the config based on either:
* 1. The name of a config group
* 2. A custom array
* 3. The default group in the config
*
* @param string|array|null $group
* @return $this
*/
private function setConfig ($group = null)
{
if (is_string($group) && $group != 'default')
$this->config = array_dot( array_replace_recursive( config('gravatar.default'), config('gravatar.'.$group) ) );
elseif (is_array($group))
$this->config = array_dot( array_replace_recursive( config('gravatar.default'), $group) );
else
$this->config = array_dot( config('gravatar.default') );
return $this;
}
/**
* Helper function to retrieve config settings.
*
* @param $value
* @param null $default
* @return null
*/
protected function c ($value, $default = null)
{
return array_key_exists($value, $this->config) ? $this->config[$value] : $default;
}
/**
* Helper function to md5 hash the email address
*
* @return string
*/
private function hashEmail()
{
return md5( strtolower( trim( $this->email ) ) );
}
/**
* @return string
*/
private function getExtension ()
{
$v = $this->c('forceExtension');
return $v ? '.' . $v : '';
}
/**
* @return string
*/
private function buildUrl()
{
$url = $this->c('secure') === true ? $this->secureBaseUrl : $this->publicBaseUrl;
$url .= $this->hashEmail();
$url .= $this->getExtension();
$url .= $this->getUrlParameters();
return $url;
}
/**
* @return string
*/
private function getUrlParameters ()
{
$build = array ();
foreach ( get_class_methods($this) as $method)
{
if ( substr($method, -strlen('Parameter')) !== 'Parameter')
continue;
if ($called = call_user_func(array($this, $method)))
$build = array_replace($build, $called);
}
return '?'.http_build_query($build);
}
/**
* @return array|null
*/
private function sizeParameter ()
{
if ( ! $this->c('size') || ! is_integer($this->c('size')) )
return null;
return array('s' => $this->c('size'));
}
/**
* @return array|null
*/
private function defaultParameter ()
{
$this->fallback = $this->c( 'fallback' );
if ( ! $this->fallback )
return null;
return array( 'd' => $this->fallback );
}
/**
* @return array|null
*/
private function ratingParameter ()
{
$rating = $this->c('maximumRating');
if ( ! $rating || ! in_array($rating, array('g','pg','r','x') ) )
return null;
return array('r' => $rating);
}
/**
* @return array|null
*/
private function forceDefaultParameter ()
{
if ($this->c('forceDefault') === true)
return array('forcedefault' => 'y');
return null;
}
/**
* Check if the provided email address is valid
*
* @param $email
* @throws InvalidEmailException
*/
private function checkEmail($email)
{
if ( ! filter_var($email, FILTER_VALIDATE_EMAIL))
throw new InvalidEmailException ('Please specify a valid email address');
}
}