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\Preference;
27use Ampache\Repository\Model\User;
28use Ampache\Module\Util\OAuth\OAuthConsumer;
29use Ampache\Module\Util\OAuth\OAuthRequest;
30use Ampache\Module\Util\OAuth\OAuthSignatureMethod_HMAC_SHA1;
31
32class Ampache7digital
33{
34    public $name        = '7digital';
35    public $categories  = 'preview';
36    public $description = 'Song preview from 7digital';
37    public $url         = 'http://www.7digital.com';
38    public $version     = '000001';
39    public $min_ampache = '370015';
40    public $max_ampache = '999999';
41
42    private $api_key;
43    private $secret;
44
45    /**
46     * Constructor
47     * This function does nothing...
48     */
49    public function __construct()
50    {
51        $this->description = T_('Song preview from 7digital');
52
53        return true;
54    } // constructor
55
56    /**
57     * install
58     * This is a required plugin function. It inserts our preferences
59     * into Ampache
60     */
61    public function install()
62    {
63        if (Preference::exists('7digital_api_key')) {
64            return false;
65        }
66        Preference::insert('7digital_api_key', T_('7digital consumer key'), '', 75, 'string', 'plugins', $this->name);
67        Preference::insert('7digital_secret_api_key', T_('7digital secret'), '', 75, 'string', 'plugins', $this->name);
68
69        return true;
70    } // install
71
72    /**
73     * uninstall
74     * This is a required plugin function. It removes our preferences from
75     * the database returning it to its original form
76     */
77    public function uninstall()
78    {
79        Preference::delete('7digital_api_key');
80        Preference::delete('7digital_secret_api_key');
81
82        return true;
83    } // uninstall
84
85    /**
86     * upgrade
87     * This is a recommended plugin function
88     */
89    public function upgrade()
90    {
91        return true;
92    } // upgrade
93
94    /**
95     * Get song preview.
96     * @param string $track_mbid
97     * @param string $artist_name
98     * @param string $title
99     * @return array
100     */
101    public function get_song_preview($track_mbid, $artist_name, $title)
102    {
103        return array();
104    }
105
106    /**
107     * @param $file
108     * @return boolean
109     */
110    public function stream_song_preview($file)
111    {
112        if (strpos($file, "7digital") !== false) {
113            $consumer = new OAuthConsumer($this->api_key, $this->secret, null);
114            $request  = OAuthRequest::from_consumer_and_token($consumer, null, 'GET', $file);
115            $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, null);
116            $url = $request->to_url();
117
118            header("Location: " . $url);
119
120            return false;
121        }
122
123        return false;
124    }
125
126    /**
127     * load
128     * This loads up the data we need into this object, this stuff comes
129     * from the preferences.
130     * @param User $user
131     * @return boolean
132     */
133    public function load($user)
134    {
135        $user->set_preferences();
136        $data = $user->prefs;
137        // load system when nothing is given
138        if (!strlen(trim($data['7digital_api_key'])) || !strlen(trim($data['7digital_secret_api_key']))) {
139            $data                            = array();
140            $data['7digital_api_key']        = Preference::get_by_user(-1, '7digital_api_key');
141            $data['7digital_secret_api_key'] = Preference::get_by_user(-1, '7digital_secret_api_key');
142        }
143
144        if (strlen(trim($data['7digital_api_key']))) {
145            $this->api_key = trim($data['7digital_api_key']);
146        } else {
147            debug_event(self::class, 'No 7digital api key, song preview plugin skipped', 3);
148
149            return false;
150        }
151        if (strlen(trim($data['7digital_secret_api_key']))) {
152            $this->secret = trim($data['7digital_secret_api_key']);
153        } else {
154            debug_event(self::class, 'No 7digital secret, song preview plugin skipped', 3);
155
156            return false;
157        }
158
159        return true;
160    } // load
161}
162