|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TGravatar class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Brad Anderson <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
* @package Prado\Web\UI\WebControls |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Web\UI\WebControls; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\TPropertyValue; |
|
14
|
|
|
use Prado\Exceptions\TInvalidDataValueException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* TGravatar class. |
|
18
|
|
|
* |
|
19
|
|
|
* TGravatar extends TImage and outputs a gravatar ImageUrl based |
|
20
|
|
|
* upon an email address, size, rating, and default style of the gravatar. |
|
21
|
|
|
* |
|
22
|
|
|
* {@link setDefault} allows for various styles of gravatar: |
|
23
|
|
|
* • mp - Mystery Person |
|
24
|
|
|
* • identicon - identicon style |
|
25
|
|
|
* • monsterid - monster style |
|
26
|
|
|
* • wavatar - wavatar style |
|
27
|
|
|
* • retro - Retra style |
|
28
|
|
|
* • robohash - Robohash style |
|
29
|
|
|
* • blank - a blank space |
|
30
|
|
|
* • 404 - not found page error |
|
31
|
|
|
* • _url - provide your own default URL (to be url encoded for you) |
|
32
|
|
|
* |
|
33
|
|
|
* A rating of the gravatar can be provided as g, pg, r, and x. |
|
34
|
|
|
* |
|
35
|
|
|
* The size must be between 1 and 512, inclusive. |
|
36
|
|
|
* |
|
37
|
|
|
* {@link TParameterizeBehavior} can be attached to TGravatar to give |
|
38
|
|
|
* default values for various properties like Default and Rating. |
|
39
|
|
|
* |
|
40
|
|
|
* See {@link https://gravatar.com} for more information. |
|
41
|
|
|
* |
|
42
|
|
|
* @author Brad Anderson <[email protected]> |
|
43
|
|
|
* @package Prado\Web\UI\WebControls |
|
44
|
|
|
* @since 4.2.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
class TGravatar extends TImage |
|
47
|
|
|
{ |
|
48
|
|
|
public const HTTP_URL = 'http://www.gravatar.com/avatar/'; |
|
49
|
|
|
public const HTTPS_URL = 'https://secure.gravatar.com/avatar/'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string the URL to the gravatar |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getImageUrl() |
|
55
|
|
|
{ |
|
56
|
|
|
$params = []; |
|
57
|
|
|
$params['s'] = $this->getSize(); |
|
58
|
|
|
$params['r'] = $this->getRating(); |
|
59
|
|
|
$params['d'] = $this->getDefaultImageStyle(); |
|
60
|
|
|
|
|
61
|
|
|
return ($this->getUseSecureUrl() ? self::HTTPS_URL : self::HTTP_URL) . md5(strtolower(trim($this->getEmail()))) . '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return null|string one of: mp, identicon, monsterid, wavatar, retro, robohash, blank, 404, _url_; |
|
66
|
|
|
* this defaults to null |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getDefaultImageStyle() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->getViewState('default'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param null|string $default one of: mp, identicon, monsterid, wavatar, retro, robohash, blank, 404, _url_ |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setDefaultImageStyle($default) |
|
77
|
|
|
{ |
|
78
|
|
|
$default = TPropertyValue::ensureString($default); |
|
79
|
|
|
$lowerDefault = strtolower($default); |
|
80
|
|
|
if ($valid = in_array($lowerDefault, ['mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank', '404', ''])) { |
|
81
|
|
|
$default = $lowerDefault; |
|
82
|
|
|
} |
|
83
|
|
|
if (!$valid && !preg_match('/^https?:\/\//i', $default)) { |
|
84
|
|
|
throw new TInvalidDataValueException('gravatar_bad_default', $default); |
|
85
|
|
|
} |
|
86
|
|
|
if (!$default) { |
|
87
|
|
|
$default = null; |
|
88
|
|
|
} |
|
89
|
|
|
$this->setViewState('default', $default); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* When the Size is not set or is null, the default size of a gravatar |
|
94
|
|
|
* from the gravatar website is 80. |
|
95
|
|
|
* @return null|int the pixel size of the gravatar, [1..512], default null (results in 80) |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getSize() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->getViewState('size'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param null|int $size the pixel size of the gravatar, [1..512] |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setSize($size) |
|
106
|
|
|
{ |
|
107
|
|
|
$_size = TPropertyValue::ensureInteger($size); |
|
108
|
|
|
if (($_size > 512 || $_size < 1) && $size !== null && $size !== '') { |
|
109
|
|
|
throw new TInvalidDataValueException('gravatar_bad_size', $size); |
|
110
|
|
|
} |
|
111
|
|
|
if (!$size) { |
|
|
|
|
|
|
112
|
|
|
$_size = null; |
|
113
|
|
|
} |
|
114
|
|
|
$this->setViewState('size', $_size); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return null|string the rating of the icon ['g', 'pg', 'r', 'x', ''], default null |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getRating() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getViewState('rating'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param null|string $rating the rating of the icon ['g', 'pg', 'r', 'x', ''] |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setRating($rating) |
|
129
|
|
|
{ |
|
130
|
|
|
$rating = strtolower(TPropertyValue::ensureString($rating)); |
|
131
|
|
|
$rating = TPropertyValue::ensureEnum($rating, ['g', 'pg', 'r', 'x', '']); |
|
132
|
|
|
if (!$rating) { |
|
133
|
|
|
$rating = null; |
|
134
|
|
|
} |
|
135
|
|
|
$this->setViewState('rating', $rating); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return string the email address associated with the gravatar icon |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getEmail() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->getViewState('email', ''); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param $email string the email address associated with the gravatar icon |
|
148
|
|
|
*/ |
|
149
|
|
|
public function setEmail($email) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->setViewState('email', TPropertyValue::ensureString($email), ''); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return bool whether or not to use the secure HTTPS url, defaults to the connection being used |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getUseSecureUrl() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->getViewState('use_secure_url', $this->getRequest()->getIsSecureConnection()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param bool $useSecureUrl whether or not to use the secure HTTPS url |
|
164
|
|
|
*/ |
|
165
|
|
|
public function setUseSecureUrl($useSecureUrl) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->setViewState('use_secure_url', TPropertyValue::ensureBoolean($useSecureUrl)); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: