3 * Name: Libravatar Support
4 * Description: If there is no avatar image for a new user or contact this addon will look for one at Libravatar. Please disable Gravatar addon if you use this one. (requires PHP >= 5.3)
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 libravatar_install()
25 Hook::register('load_config', 'addon/libravatar/libravatar.php', 'libravatar_load_config');
26 Hook::register('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
27 Logger::log("registered libravatar in avatar_lookup hook");
31 * Removes the addon hook
33 function libravatar_uninstall()
35 Hook::unregister('load_config', 'addon/libravatar/libravatar.php', 'libravatar_load_config');
36 Hook::unregister('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
37 Logger::log("unregistered libravatar in avatar_lookup hook");
40 function libravatar_load_config(App $a, ConfigFileLoader $loader)
42 $a->getConfigCache()->load($loader->loadAddonConfig('libravatar'));
46 * Looks up the avatar at Libravatar and returns the URL.
51 function libravatar_lookup($a, &$b)
53 $default_avatar = Config::get('libravatar', 'default_avatar');
55 if (! $default_avatar) {
56 // if not set, look up if there was one from the gravatar addon
57 $default_avatar = Config::get('gravatar', 'default_avatar');
58 // setting default avatar if nothing configured
59 if (!$default_avatar) {
60 $default_avatar = 'identicon'; // default image will be a random pattern
64 require_once 'Services/Libravatar.php';
65 $libravatar = new Services_Libravatar();
66 $libravatar->setSize($b['size']);
67 $libravatar->setDefault($default_avatar);
68 $avatar_url = $libravatar->getUrl($b['email']);
70 $b['url'] = $avatar_url;
75 * Display admin settings for this addon
77 function libravatar_addon_admin(&$a, &$o)
79 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/libravatar");
81 $default_avatar = Config::get('libravatar', 'default_avatar');
83 // set default values for first configuration
84 if (!$default_avatar) {
85 $default_avatar = 'identicon'; // pseudo-random geometric pattern based on email hash
88 // Available options for the select boxes
90 'mm' => L10n::t('generic profile image'),
91 'identicon' => L10n::t('random geometric pattern'),
92 'monsterid' => L10n::t('monster face'),
93 'wavatar' => L10n::t('computer generated face'),
94 'retro' => L10n::t('retro arcade style face'),
97 // Show warning if PHP version is too old
98 if (! version_compare(PHP_VERSION, '5.3.0', '>=')) {
99 $o = '<h5>' .L10n::t('Warning') .'</h5><p>';
100 $o .= L10n::t('Your PHP version %s is lower than the required PHP >= 5.3.', PHP_VERSION);
101 $o .= '<br>' .L10n::t('This addon is not functional on your server.') .'<p><br>';
105 // Libravatar falls back to gravatar, so show warning about gravatar addon if enabled
106 $r = q("SELECT * FROM `addon` WHERE `name` = '%s' and `installed` = 1",
107 DBA::escape('gravatar')
110 $o = '<h5>' .L10n::t('Information') .'</h5><p>' .L10n::t('Gravatar addon is installed. Please disable the Gravatar addon.<br>The Libravatar addon will fall back to Gravatar if nothing was found at Libravatar.') .'</p><br><br>';
113 // output Libravatar settings
114 $o .= '<input type="hidden" name="form_security_token" value="' . BaseModule::getFormSecurityToken("libravatarsave") .'">';
115 $o .= Renderer::replaceMacros( $t, [
116 '$submit' => L10n::t('Save Settings'),
117 '$default_avatar' => ['avatar', L10n::t('Default avatar image'), $default_avatar, L10n::t('Select default avatar image if none was found. See README'), $default_avatars],
122 * Save admin settings
124 function libravatar_addon_admin_post(&$a)
126 BaseModule::checkFormSecurityToken('libravatarrsave');
128 $default_avatar = (!empty($_POST['avatar']) ? Strings::escapeTags(trim($_POST['avatar'])) : 'identicon');
129 Config::set('libravatar', 'default_avatar', $default_avatar);
130 info(L10n::t('Libravatar settings updated.') .EOL);