3 * Name: Gravatar Support
4 * Description: If there is no avatar image for a new user or contact this addon will look for one at Gravatar.
6 * Author: Klaus Weidenbach <http://friendica.dszdw.net/profile/klaus>
10 use Friendica\BaseModule;
11 use Friendica\Core\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
16 use Friendica\Database\DBA;
17 use Friendica\Util\Config\ConfigFileLoader;
18 use Friendica\Util\Strings;
21 * Installs the addon hook
23 function gravatar_install() {
24 Hook::register('load_config', 'addon/gravatar/gravatar.php', 'gravatar_load_config');
25 Hook::register('avatar_lookup', 'addon/gravatar/gravatar.php', 'gravatar_lookup');
27 Logger::log("registered gravatar in avatar_lookup hook");
31 * Removes the addon hook
33 function gravatar_uninstall() {
34 Hook::unregister('load_config', 'addon/gravatar/gravatar.php', 'gravatar_load_config');
35 Hook::unregister('avatar_lookup', 'addon/gravatar/gravatar.php', 'gravatar_lookup');
37 Logger::log("unregistered gravatar in avatar_lookup hook");
40 function gravatar_load_config(App $a, ConfigFileLoader $loader)
42 $a->getConfigCache()->load($loader->loadAddonConfig('gravatar'));
46 * Looks up the avatar at gravatar.com and returns the URL.
51 function gravatar_lookup($a, &$b) {
52 $default_avatar = Config::get('gravatar', 'default_avatar');
53 $rating = Config::get('gravatar', 'rating');
55 // setting default value if nothing configured
57 $default_avatar = 'identicon'; // default image will be a random pattern
59 $rating = 'g'; // suitable for display on all websites with any audience type
61 $hash = md5(trim(strtolower($b['email'])));
63 $url = 'https://secure.gravatar.com/avatar/' .$hash .'.jpg';
64 $url .= '?s=' .$b['size'] .'&r=' .$rating;
65 if ($default_avatar != "gravatar")
66 $url .= '&d=' .$default_avatar;
73 * Display admin settings for this addon
75 function gravatar_addon_admin (&$a, &$o) {
76 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/gravatar/" );
78 $default_avatar = Config::get('gravatar', 'default_avatar');
79 $rating = Config::get('gravatar', 'rating');
81 // set default values for first configuration
83 $default_avatar = 'identicon'; // pseudo-random geometric pattern based on email hash
85 $rating = 'g'; // suitable for display on all websites with any audience type
87 // Available options for the select boxes
89 'mm' => L10n::t('generic profile image'),
90 'identicon' => L10n::t('random geometric pattern'),
91 'monsterid' => L10n::t('monster face'),
92 'wavatar' => L10n::t('computer generated face'),
93 'retro' => L10n::t('retro arcade style face'),
102 // Check if Libravatar is enabled and show warning
103 $r = q("SELECT * FROM `addon` WHERE `name` = '%s' and `installed` = 1",
104 DBA::escape('libravatar')
107 $o = '<h5>' .L10n::t('Information') .'</h5><p>' .L10n::t('Libravatar addon is installed, too. Please disable Libravatar addon or this Gravatar addon.<br>The Libravatar addon will fall back to Gravatar if nothing was found at Libravatar.') .'</p><br><br>';
110 // output Gravatar settings
111 $o .= '<input type="hidden" name="form_security_token" value="' . BaseModule::getFormSecurityToken("gravatarsave") .'">';
112 $o .= Renderer::replaceMacros( $t, [
113 '$submit' => L10n::t('Save Settings'),
114 '$default_avatar' => ['avatar', L10n::t('Default avatar image'), $default_avatar, L10n::t('Select default avatar image if none was found at Gravatar. See README'), $default_avatars],
115 '$rating' => ['rating', L10n::t('Rating of images'), $rating, L10n::t('Select the appropriate avatar rating for your site. See README'), $ratings],
120 * Save admin settings
122 function gravatar_addon_admin_post (&$a) {
123 BaseModule::checkFormSecurityToken('gravatarsave');
125 $default_avatar = (!empty($_POST['avatar']) ? Strings::escapeTags(trim($_POST['avatar'])) : 'identicon');
126 $rating = (!empty($_POST['rating']) ? Strings::escapeTags(trim($_POST['rating'])) : 'g');
127 Config::set('gravatar', 'default_avatar', $default_avatar);
128 Config::set('gravatar', 'rating', $rating);
129 info(L10n::t('Gravatar settings updated.') .EOL);