Revert to stable version 3.5.4
[friendica-addons.git] / libravatar / libravatar.php
1 <?php
2 /**
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)
5  * Version: 1.1
6  * Author: Klaus Weidenbach <http://friendica.dszdw.net/profile/klaus>
7  */
8
9 /**
10  * Installs the plugin hook
11  */
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
16                 return false;
17         }
18         register_hook('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
19
20         logger("registered libravatar in avatar_lookup hook");
21 }
22
23 /**
24  * Removes the plugin hook
25  */
26 function libravatar_uninstall() {
27         unregister_hook('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
28
29         logger("unregistered libravatar in avatar_lookup hook");
30 }
31
32 /**
33  * Looks up the avatar at Libravatar and returns the URL.
34  *
35  * @param $a array
36  * @param &$b array
37  */
38 function libravatar_lookup($a, &$b) {
39         $default_avatar = get_config('libravatar', 'default_img');
40
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
47         }
48
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']);
54
55         $b['url'] = $avatar_url;
56         $b['success'] = true;
57 }
58
59 /**
60  * Display admin settings for this addon
61  */
62 function libravatar_plugin_admin (&$a, &$o) {
63         $t = get_markup_template( "admin.tpl", "addon/libravatar" );
64
65         $default_avatar = get_config('libravatar', 'default_img');
66
67         // set default values for first configuration
68         if(! $default_avatar)
69                 $default_avatar = 'identicon'; // pseudo-random geometric pattern based on email hash
70
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'),
78         );
79
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>';
85                 return;
86         }
87
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",
90                 dbesc('gravatar')
91         );
92         if (count($r)) {
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>';
94         }
95
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('Save Settings'),
100                 '$default_avatar' => array('avatar', t('Default avatar image'), $default_avatar, t('Select default avatar image if none was found. See README'), $default_avatars),
101         ));
102 }
103
104 /**
105  * Save admin settings
106  */
107 function libravatar_plugin_admin_post (&$a) {
108         check_form_security_token('libravatarrsave');
109
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);
113 }
114 ?>