1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22declare(strict_types=0);
23
24namespace Ampache\Plugin;
25
26use Ampache\Repository\Model\User;
27use Ampache\Module\System\Core;
28
29class AmpacheLibravatar
30{
31    public $name        = 'Libravatar';
32    public $categories  = 'avatar';
33    public $description = 'Users avatar\'s with Libravatar';
34    public $url         = 'https://www.libravatar.org';
35    public $version     = '000001';
36    public $min_ampache = '360040';
37    public $max_ampache = '999999';
38
39    /**
40     * Constructor
41     * This function does nothing...
42     */
43    public function __construct()
44    {
45        $this->description = T_("Users avatar's with Libravatar");
46
47        return true;
48    } // constructor
49
50    /**
51     * install
52     * This is a required plugin function. It inserts our preferences
53     * into Ampache
54     */
55    public function install()
56    {
57        return true;
58    } // install
59
60    /**
61     * uninstall
62     * This is a required plugin function. It removes our preferences from
63     * the database returning it to its original form
64     */
65    public function uninstall()
66    {
67        return true;
68    } // uninstall
69
70    /**
71     * upgrade
72     * This is a recommended plugin function
73     */
74    public function upgrade()
75    {
76        return true;
77    } // upgrade
78
79    /**
80     * @param $user
81     * @param integer $size
82     * @return string
83     */
84    public function get_avatar_url($user, $size = 80)
85    {
86        $url = "";
87        if (!empty($user->email)) {
88            // Federated Servers are not supported here without libravatar.org. Should query DNS server first.
89            if (filter_has_var(INPUT_SERVER, 'HTTPS') && Core::get_server('HTTPS') !== 'off') {
90                $url = "https://seccdn.libravatar.org";
91            } else {
92                $url = "http://cdn.libravatar.org";
93            }
94            $url .= "/avatar/";
95            $url .= md5(strtolower(trim($user->email)));
96            $url .= "?s=" . $size . "&r=g";
97            $url .= "&d=identicon";
98        }
99
100        return $url;
101    }
102
103    /**
104     * load
105     * This loads up the data we need into this object, this stuff comes
106     * from the preferences.
107     * @param User $user
108     * @return boolean
109     */
110    public function load($user)
111    {
112        $user->set_preferences();
113
114        return true;
115    } // load
116}
117