3 * Name: Libravatar Support
4 * Description: If there is no avatar image for a new user or contact this plugin 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 * Installs the plugin hook
12 function libravatar_install() {
13 if (! version_compare(PHP_VERSION, '5.3.0', '>=')) {
14 info(t('Could NOT install Libravatar successfully.<br>It requires PHP >= 5.3') .EOL);
15 // avoid registering the hook
18 register_hook('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
20 logger("registered libravatar in avatar_lookup hook");
24 * Removes the plugin hook
26 function libravatar_uninstall() {
27 unregister_hook('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
29 logger("unregistered libravatar in avatar_lookup hook");
33 * Looks up the avatar at Libravatar and returns the URL.
38 function libravatar_lookup($a, &$b) {
39 $default_avatar = get_config('libravatar', 'default_img');
41 if (! $default_avatar) {
42 // if not set, look up if there was one from the gravatar addon
43 $default_avatar = get_config('gravatar', 'default_img');
44 // setting default avatar if nothing configured
45 if (! $default_avatar)
46 $default_avatar = 'identicon'; // default image will be a random pattern
49 require_once 'Services/Libravatar.php';
50 $libravatar = new Services_Libravatar();
51 $libravatar->setSize($b['size']);
52 $libravatar->setDefault($default_avatar);
53 $avatar_url = $libravatar->getUrl($b['email']);
55 $b['url'] = $avatar_url;
60 * Display admin settings for this addon
62 function libravatar_plugin_admin (&$a, &$o) {
63 $t = get_markup_template( "admin.tpl", "addon/libravatar" );
65 $default_avatar = get_config('libravatar', 'default_img');
67 // set default values for first configuration
69 $default_avatar = 'identicon'; // pseudo-random geometric pattern based on email hash
71 // Available options for the select boxes
72 $default_avatars = array(
73 'mm' => t('generic profile image'),
74 'identicon' => t('random geometric pattern'),
75 'monsterid' => t('monster face'),
76 'wavatar' => t('computer generated face'),
77 'retro' => t('retro arcade style face'),
80 // Show warning if PHP version is too old
81 if (! version_compare(PHP_VERSION, '5.3.0', '>=')) {
82 $o = '<h5>' .t('Warning') .'</h5><p>';
83 $o .= sprintf(t('Your PHP version %s is lower than the required PHP >= 5.3.'), PHP_VERSION);
84 $o .= '<br>' .t('This addon is not functional on your server.') .'<p><br>';
88 // Libravatar falls back to gravatar, so show warning about gravatar addon if enabled
89 $r = q("SELECT * FROM `addon` WHERE `name` = '%s' and `installed` = 1",
93 $o = '<h5>' .t('Information') .'</h5><p>' .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>';
96 // output Libravatar settings
97 $o .= '<input type="hidden" name="form_security_token" value="' .get_form_security_token("libravatarsave") .'">';
98 $o .= replace_macros( $t, array(
99 '$submit' => t('Submit'),
100 '$default_avatar' => array('avatar', t('Default avatar image'), $default_avatar, t('Select default avatar image if none was found. See README'), $default_avatars),
105 * Save admin settings
107 function libravatar_plugin_admin_post (&$a) {
108 check_form_security_token('libravatarrsave');
110 $default_avatar = ((x($_POST, 'avatar')) ? notags(trim($_POST['avatar'])) : 'identicon');
111 set_config('libravatar', 'default_img', $default_avatar);
112 info(t('Libravatar settings updated.') .EOL);