80c9e4c5bec2f7b1e9022fdb7293e73a34522dde
[friendica-addons.git/.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 addon 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 use Friendica\App;
10 use Friendica\BaseModule;
11 use Friendica\Core\Addon;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Database\DBA;
16 use Friendica\Util\Security;
17
18 /**
19  * Installs the addon hook
20  */
21 function libravatar_install()
22 {
23         Addon::registerHook('load_config',   'addon/libravatar/libravatar.php', 'libravatar_load_config');
24         Addon::registerHook('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
25         Logger::log("registered libravatar in avatar_lookup hook");
26 }
27
28 /**
29  * Removes the addon hook
30  */
31 function libravatar_uninstall()
32 {
33         Addon::unregisterHook('load_config',   'addon/libravatar/libravatar.php', 'libravatar_load_config');
34         Addon::unregisterHook('avatar_lookup', 'addon/libravatar/libravatar.php', 'libravatar_lookup');
35         Logger::log("unregistered libravatar in avatar_lookup hook");
36 }
37
38 function libravatar_load_config(App $a)
39 {
40         $a->loadConfigFile(__DIR__. '/config/libravatar.ini.php');
41 }
42
43 /**
44  * Looks up the avatar at Libravatar and returns the URL.
45  *
46  * @param $a array
47  * @param &$b array
48  */
49 function libravatar_lookup($a, &$b)
50 {
51         $default_avatar = Config::get('libravatar', 'default_avatar');
52
53         if (! $default_avatar) {
54                 // if not set, look up if there was one from the gravatar addon
55                 $default_avatar = Config::get('gravatar', 'default_avatar');
56                 // setting default avatar if nothing configured
57                 if (!$default_avatar) {
58                         $default_avatar = 'identicon'; // default image will be a random pattern
59                 }
60         }
61
62         require_once 'Services/Libravatar.php';
63         $libravatar = new Services_Libravatar();
64         $libravatar->setSize($b['size']);
65         $libravatar->setDefault($default_avatar);
66         $avatar_url = $libravatar->getUrl($b['email']);
67
68         $b['url'] = $avatar_url;
69         $b['success'] = true;
70 }
71
72 /**
73  * Display admin settings for this addon
74  */
75 function libravatar_addon_admin(&$a, &$o)
76 {
77         $t = get_markup_template("admin.tpl", "addon/libravatar");
78
79         $default_avatar = Config::get('libravatar', 'default_avatar');
80
81         // set default values for first configuration
82         if (!$default_avatar) {
83                 $default_avatar = 'identicon'; // pseudo-random geometric pattern based on email hash
84         }
85
86         // Available options for the select boxes
87         $default_avatars = [
88                 'mm' => L10n::t('generic profile image'),
89                 'identicon' => L10n::t('random geometric pattern'),
90                 'monsterid' => L10n::t('monster face'),
91                 'wavatar' => L10n::t('computer generated face'),
92                 'retro' => L10n::t('retro arcade style face'),
93         ];
94
95         // Show warning if PHP version is too old
96         if (! version_compare(PHP_VERSION, '5.3.0', '>=')) {
97                 $o = '<h5>' .L10n::t('Warning') .'</h5><p>';
98                 $o .= L10n::t('Your PHP version %s is lower than the required PHP >= 5.3.', PHP_VERSION);
99                 $o .= '<br>' .L10n::t('This addon is not functional on your server.') .'<p><br>';
100                 return;
101         }
102
103         // Libravatar falls back to gravatar, so show warning about gravatar addon if enabled
104         $r = q("SELECT * FROM `addon` WHERE `name` = '%s' and `installed` = 1",
105                 DBA::escape('gravatar')
106         );
107         if (count($r)) {
108                 $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>';
109         }
110
111         // output Libravatar settings
112         $o .= '<input type="hidden" name="form_security_token" value="' . BaseModule::getFormSecurityToken("libravatarsave") .'">';
113         $o .= replace_macros( $t, [
114                 '$submit' => L10n::t('Save Settings'),
115                 '$default_avatar' => ['avatar', L10n::t('Default avatar image'), $default_avatar, L10n::t('Select default avatar image if none was found. See README'), $default_avatars],
116         ]);
117 }
118
119 /**
120  * Save admin settings
121  */
122 function libravatar_addon_admin_post(&$a)
123 {
124         BaseModule::checkFormSecurityToken('libravatarrsave');
125
126         $default_avatar = ((x($_POST, 'avatar')) ? notags(trim($_POST['avatar'])) : 'identicon');
127         Config::set('libravatar', 'default_avatar', $default_avatar);
128         info(L10n::t('Libravatar settings updated.') .EOL);
129 }