Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f710546

Browse files
committedAug 15, 2012
Allow custom default avatar icons
Following implementation of #13992, it was no longer possible for administrators to define a custom default avatar, as Mantis forced use of Gravatar's 'identicon'. This commit reintroduces that functionality, and allows use of either one of Gravatar's default images or a custom one (identified by an URL), through appropriate configuration of $g_show_avatar. Fixes #14032
1 parent 6890b5a commit f710546

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed
 

‎config_defaults_inc.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -936,12 +936,24 @@
936936

937937
/**
938938
* Show user avatar
939-
* the current implementation is based on http://www.gravatar.com
940-
* users will need to register there the same address used in
941-
* this MantisBT installation to have their avatar shown
939+
*
940+
* The current implementation is based on http://www.gravatar.com
941+
* Users will need to register there the same email address used in this
942+
* MantisBT installation to have their avatar shown.
942943
* Please note: upon registration or avatar change, it takes some time for
943944
* the updated gravatar images to show on sites
944-
* @global int $g_show_avatar
945+
*
946+
* The config can be either set to OFF (avatars disabled) or set to a string
947+
* defining the default avatar to be used when none is associated with the
948+
* user's email. Valid values:
949+
* - OFF (default)
950+
* - One of Gravatar's defaults (mm, identicon, monsterid, wavatar, retro)
951+
* @link http://en.gravatar.com/site/implement/images/
952+
* - An URL to the default image to be used (for example,
953+
* "http:/path/to/unknown.jpg" or "%path%images/no_avatar.png")
954+
*
955+
* @global int|string $g_show_avatar
956+
* @see $g_show_avatar_threshold
945957
*/
946958
$g_show_avatar = OFF;
947959

‎core/print_api.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function print_successful_redirect( $p_redirect_to ) {
144144

145145
# Print avatar image for the given user ID
146146
function print_avatar( $p_user_id, $p_size = 80 ) {
147-
if ( OFF == config_get( 'show_avatar' ) ) {
147+
if ( OFF === config_get( 'show_avatar' ) ) {
148148
return;
149149
}
150150

‎core/user_api.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -794,28 +794,29 @@ function user_get_name( $p_user_id ) {
794794
* @return array|bool an array( URL, width, height ) or false when the given user has no avatar
795795
*/
796796
function user_get_avatar( $p_user_id, $p_size = 80 ) {
797-
$t_email = utf8_strtolower( trim( user_get_email( $p_user_id ) ) );
798-
if( is_blank( $t_email ) ) {
799-
$t_result = false;
800-
} else {
801-
$t_size = $p_size;
797+
$t_default_avatar = config_get( 'show_avatar' );
802798

803-
if( http_is_protocol_https() ) {
804-
$t_gravatar_domain = 'https://secure.gravatar.com/';
805-
} else {
806-
$t_gravatar_domain = 'http://www.gravatar.com/';
807-
}
799+
if( OFF === $t_default_avatar ) {
800+
# Avatars are not used
801+
return false;
802+
}
808803

809-
$t_avatar_url = $t_gravatar_domain . 'avatar/' . md5( $t_email ) . '?d=identicon&r=G&s=' . $t_size;
804+
# Default avatar is either one of Gravatar's options, or
805+
# assumed to be an URL to a default avatar image
806+
$t_default_avatar = urlencode( $t_default_avatar );
807+
$t_rating = 'G';
810808

811-
$t_result = array(
812-
$t_avatar_url,
813-
$t_size,
814-
$t_size,
815-
);
809+
$t_email_hash = md5( utf8_strtolower( trim( user_get_email( $p_user_id ) ) ) );
810+
811+
# Build Gravatar URL
812+
if( http_is_protocol_https() ) {
813+
$t_avatar_url = 'https://secure.gravatar.com/';
814+
} else {
815+
$t_avatar_url = 'http://www.gravatar.com/';
816816
}
817+
$t_avatar_url .= "avatar/$t_email_hash?d=$t_default_avatar&r=$t_rating&s=$p_size";
817818

818-
return $t_result;
819+
return array( $t_avatar_url, $p_size, $p_size );
819820
}
820821

821822
# --------------------

‎docbook/administration_guide/en/configuration.sgml

+32-4
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,38 @@
896896
<varlistentry>
897897
<term>$g_show_avatar</term>
898898
<listitem>
899-
<para>Show user avatar (default OFF); the current implementation is based on http://www.gravatar.com,
900-
users will need to register there with the same email address used in
901-
this MantisBT installation to have their avatar shown.</para>
902-
<note><para>Upon registration or avatar change, it takes some time for the updated gravatar images to show on sites.</para></note>
899+
<para>Show the user's avatar
900+
</para>
901+
<para>The current implementation is based on
902+
<ulink url="http://www.gravatar.com">Gravatar</ulink>;
903+
Users will need to register there the same email address
904+
used in this MantisBT installation to have their avatar
905+
shown.
906+
Please note: upon registration or avatar change, it may
907+
take some time for the updated gravatar images to show
908+
on sites
909+
</para>
910+
<para>The config can be either set to OFF (avatars disabled),
911+
or to a string defining the default avatar to be used
912+
when none is associated with the user's email.
913+
Valid values are:
914+
<itemizedlist>
915+
<listitem><para>OFF (default)</para>
916+
</listitem>
917+
<listitem><para>One of Gravatar's defaults
918+
(mm, identicon, monsterid, wavatar, retro);
919+
refer to
920+
<ulink url="http://en.gravatar.com/site/implement/images/">
921+
Image Requests documentation
922+
</ulink>
923+
for further details.
924+
</para></listitem>
925+
<listitem><para>An URL to the default image to be used
926+
(for example, "http:/path/to/unknown.jpg"
927+
or "%path%images/no_avatar.png").
928+
</para></listitem>
929+
</itemizedlist>
930+
</para>
903931
</listitem>
904932
</varlistentry>
905933
<varlistentry>

4 commit comments

Comments
 (4)

atrol commented on Aug 15, 2012

@atrol
Member

After upgrading old installations with setting $g_show_avatar = ON; will still work, but it's random behavior (seems that gravatar just ignores parameter ?d=1)

Did you check that mixed types (int|string) are working also with database configuration (adm_config_report.php)?
I had a short look and found only one other setting which uses mixed types ($g_limit_email_domain)

I prefer the simpler old approach to have to separate customization options for it.

dregad commented on Aug 16, 2012

@dregad
MemberAuthor

Hi @atrol

As usual, many thanks for your careful and thorough review :)

After upgrading old installations with setting $g_show_avatar = ON; will still work, but it's random behavior

I was actually thinking about the very same thing last night, and am in fact finalizing a patch to address exactly that, i.e. avoid regression when using legacy configuration (if ON, then Mantis will use the "identicon" default)

seems that gravatar just ignores parameter ?d=1

In my tests, behavior is not random, and d=1 is not ignored by Gravatar, it just uses the default "rotated G" image. Did you notice something different ?

Did you check that mixed types (int|string) are working also with database configuration (adm_config_report.php)?

Yes I did, and found no issues with that

I prefer the simpler old approach to have to separate customization options for it.

Both have their advantages - here we have one single config controlling everything in a rather simple and straightforward manner, which has the added benefit of reducing the number of global variables.

dregad commented on Aug 16, 2012

@dregad
MemberAuthor

After upgrading old installations with setting $g_show_avatar = ON

Fixed - see fd20db7

atrol commented on Aug 16, 2012

@atrol
Member

In my tests, behavior is not random, and d=1 is not ignored by Gravatar, it just uses the default "rotated G" image. Did you notice something different ?

With "ignored" I meant that
http://www.gravatar.com/avatar/00000000000000000000000000000000 and
http://www.gravatar.com/avatar/00000000000000000000000000000000?d=1
are producing the same result (the rotated G image)

With "random behavior" I meant that the behavior is not documented.
It's just the re-engineered reproducible way it works at the moment.
You can't expect that d=1 produces the rotated G.
Maybe Gravatar will change the implementation and d=1 will generate
an error bitmap in future versions.

Possibly the better word for it is accidental and not random?

Both have their advantages - here we have one single config controlling
everything in a rather simple and straightforward manner, which has the
added benefit of reducing the number of global variables.

I agree,
I wrote because most of the MantisBT code is not using this coding style and
probably because I am not experienced in untyped programming languages and dealing
with === and !== .

I am quite sure that a lot of developers that started with typed languages
have their problems with it.
I am a bit aware of type problems since the following fix
3cca927
That's why I don't like dynamic or mixed typing.

Fixed - see fd20db7

Thanks

Please sign in to comment.