/
Gravatar.php
180 lines (162 loc) · 4.97 KB
/
Gravatar.php
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
<?php
/**
* MantisBT - A PHP based bugtracking system
*
* MantisBT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* MantisBT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright Copyright MantisBT Team - mantisbt-dev@lists.sourceforge.net
*/
/**
* Mantis Gravatar Plugin
*
* This is an avatar provider plugin that is based on http://www.gravatar.com.
* Users will need to register there the same email address used in this
* MantisBT installation to have their avatar shown.
*
* Please note: upon registration or avatar change, it takes some time for
* the updated gravatar images to show on sites
*/
class GravatarPlugin extends MantisPlugin {
/**
* Gravatar URLs
*/
const GRAVATAR_URL = 'http://www.gravatar.com/';
const GRAVATAR_URL_SECURE = 'https://secure.gravatar.com/';
/**
* Default Gravatar image types
*
* @link http://en.gravatar.com/site/implement/images/
*/
const GRAVATAR_DEFAULT_MYSTERYMAN = 'mm';
const GRAVATAR_DEFAULT_IDENTICON = 'identicon';
const GRAVATAR_DEFAULT_MONSTERID = 'monsterid';
const GRAVATAR_DEFAULT_WAVATAR = 'wavatar';
const GRAVATAR_DEFAULT_RETRO = 'retro';
const GRAVATAR_DEFAULT_BLANK = 'blank';
/**
* Gravatar Ratings
*
* @link http://en.gravatar.com/site/implement/images/
*/
const GRAVATAR_RATING_G = 'G';
const GRAVATAR_RATING_PG = 'PG';
const GRAVATAR_RATING_R = 'R';
const GRAVATAR_RATING_X = 'X';
/**
* A method that populates the plugin information and minimum requirements.
* @return void
*/
function register() {
$this->name = plugin_lang_get( 'title' );
$this->description = lang_get( 'description' );
$this->page = '';
$this->version = '1.0';
$this->requires = array(
'MantisCore' => '1.3.0',
);
$this->author = 'Victor Boctor';
$this->contact = 'vboctor@mantisbt.org';
$this->url = 'http://www.mantisbt.org';
}
/**
* Default plugin configuration.
* @return array
*/
function config() {
return array(
/**
* The rating of the avatar to show: 'G', 'PG', 'R', 'X'
* @link http://en.gravatar.com/site/implement/images/
*/
'rating' => self::GRAVATAR_RATING_G,
/**
* The kind of avatar to use:
*
* - One of Gravatar's defaults (mm, identicon, monsterid, wavatar, retro)
* @link http://en.gravatar.com/site/implement/images/
* - An URL to the default image to be used (for example,
* "http:/path/to/unknown.jpg" or "%path%images/avatar.png")
*/
'default_avatar' => self::GRAVATAR_DEFAULT_IDENTICON
);
}
/**
* Register event hooks for plugin.
*/
function hooks() {
return array(
'EVENT_USER_AVATAR' => 'user_get_avatar',
'EVENT_CORE_HEADERS' => 'csp_headers',
);
}
/**
* Register gravatar url as an img-src for CSP header
*/
function csp_headers() {
if( config_get( 'show_avatar' ) !== OFF ) {
http_csp_add( 'img-src', self::getAvatarUrl() );
}
}
/**
* Return the user avatar image URL
* in this first implementation, only gravatar.com avatars are supported
*
* This function returns an array( URL, width, height ) or an empty array when the given user has no avatar.
*
* @param string $p_event The name for the event.
* @param integer $p_user_id A valid user identifier.
* @param integer $p_size The required number of pixel in the image to retrieve the link for.
*
* @return object An instance of class Avatar or null.
*/
function user_get_avatar( $p_event, $p_user_id, $p_size = 80 ) {
$t_default_avatar = plugin_config_get( 'default_avatar' );
# Default avatar is either one of Gravatar's options, or
# assumed to be an URL to a default avatar image
$t_default_avatar = urlencode( $t_default_avatar );
$t_rating = plugin_config_get( 'rating' );
if( user_exists( $p_user_id ) ) {
$t_email_hash = md5( strtolower( trim( user_get_email( $p_user_id ) ) ) );
} else {
$t_email_hash = md5( 'generic-avatar-since-user-not-found' );
}
# Build Gravatar URL
$t_avatar_url = self::getAvatarUrl() .
'avatar/' . $t_email_hash . '?' .
http_build_query(
array(
'd' => $t_default_avatar,
'r' => $t_rating,
's' => $p_size,
),
'', '&'
);
$t_avatar = new Avatar();
$t_avatar->image = $t_avatar_url;
return $t_avatar;
}
/**
* Gets the gravatar base URL
*
* @return string The gravatar URL.
*/
private static function getAvatarUrl() {
if( http_is_protocol_https() ) {
$t_avatar_url = self::GRAVATAR_URL_SECURE;
} else {
$t_avatar_url = self::GRAVATAR_URL;
}
return $t_avatar_url;
}
}