Update function names
[friendica-addons.git] / gravatar / gravatar.php
1 <?php
2 /**
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.
5  * Version: 1.1
6  * Author: Klaus Weidenbach <http://friendica.dszdw.net/profile/klaus>
7  */
8 use Friendica\Core\Addon;
9 use Friendica\Core\Config;
10
11 /**
12  * Installs the addon hook
13  */
14 function gravatar_install() {
15         Addon::registerHook('avatar_lookup', 'addon/gravatar/gravatar.php', 'gravatar_lookup');
16
17         logger("registered gravatar in avatar_lookup hook");
18 }
19
20 /**
21  * Removes the addon hook
22  */
23 function gravatar_uninstall() {
24         Addon::unregisterHook('avatar_lookup', 'addon/gravatar/gravatar.php', 'gravatar_lookup');
25
26         logger("unregistered gravatar in avatar_lookup hook");
27 }
28
29 /**
30  * Looks up the avatar at gravatar.com and returns the URL.
31  *
32  * @param $a array
33  * @param &$b array
34  */
35 function gravatar_lookup($a, &$b) {
36         $default_avatar = Config::get('gravatar', 'default_img');
37         $rating = Config::get('gravatar', 'rating');
38
39         // setting default value if nothing configured
40         if(! $default_avatar)
41                 $default_avatar = 'identicon'; // default image will be a random pattern
42         if(! $rating)
43                 $rating = 'g'; // suitable for display on all websites with any audience type
44
45         $hash = md5(trim(strtolower($b['email'])));
46
47         $url = 'https://secure.gravatar.com/avatar/' .$hash .'.jpg';
48         $url .= '?s=' .$b['size'] .'&r=' .$rating;
49         if ($default_avatar != "gravatar")
50                 $url .= '&d=' .$default_avatar;
51
52         $b['url'] = $url;
53         $b['success'] = true;
54 }
55
56 /**
57  * Display admin settings for this addon
58  */
59 function gravatar_addon_admin (&$a, &$o) {
60         $t = get_markup_template( "admin.tpl", "addon/gravatar/" );
61
62         $default_avatar = Config::get('gravatar', 'default_img');
63         $rating = Config::get('gravatar', 'rating');
64
65         // set default values for first configuration
66         if(! $default_avatar)
67                 $default_avatar = 'identicon'; // pseudo-random geometric pattern based on email hash
68         if(! $rating)
69                 $rating = 'g'; // suitable for display on all websites with any audience type
70
71         // Available options for the select boxes
72         $default_avatars = [
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         $ratings = [
80                 'g' => 'g',
81                 'pg' => 'pg',
82                 'r' => 'r',
83                 'x' => 'x'
84         ];
85
86         // Check if Libravatar is enabled and show warning
87         $r = q("SELECT * FROM `addon` WHERE `name` = '%s' and `installed` = 1",
88                 dbesc('libravatar')
89         );
90         if (count($r)) {
91                 $o = '<h5>' .t('Information') .'</h5><p>' .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>';
92         }
93
94         // output Gravatar settings
95         $o .= '<input type="hidden" name="form_security_token" value="' .get_form_security_token("gravatarsave") .'">';
96         $o .= replace_macros( $t, [
97                 '$submit' => t('Save Settings'),
98                 '$default_avatar' => ['avatar', t('Default avatar image'), $default_avatar, t('Select default avatar image if none was found at Gravatar. See README'), $default_avatars],
99                 '$rating' => ['rating', t('Rating of images'), $rating, t('Select the appropriate avatar rating for your site. See README'), $ratings],
100         ]);
101 }
102
103 /**
104  * Save admin settings
105  */
106 function gravatar_addon_admin_post (&$a) {
107         check_form_security_token('gravatarsave');
108
109         $default_avatar = ((x($_POST, 'avatar')) ? notags(trim($_POST['avatar'])) : 'identicon');
110         $rating = ((x($_POST, 'rating')) ? notags(trim($_POST['rating'])) : 'g');
111         Config::set('gravatar', 'default_img', $default_avatar);
112         Config::set('gravatar', 'rating', $rating);
113         info( t('Gravatar settings updated.') .EOL);
114 }
115 ?>