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\System\Core;
29use Exception;
30use Requests;
31
32class AmpacheBitly
33{
34    public $name        = 'Bit.ly';
35    public $categories  = 'shortener';
36    public $description = 'URL shorteners on shared links with Bit.ly';
37    public $url         = 'http://bitly.com';
38    public $version     = '000002';
39    public $min_ampache = '360037';
40    public $max_ampache = '999999';
41
42    // These are internal settings used by this class, run this->load to fill them out
43    private $bitly_username;
44    private $bitly_api_key;
45
46    /**
47     * Constructor
48     * This function does nothing...
49     */
50    public function __construct()
51    {
52        $this->description = T_('URL shorteners on shared links with Bit.ly');
53
54        return true;
55    } // constructor
56
57    /**
58     * install
59     * This is a required plugin function. It inserts our preferences
60     * into Ampache
61     */
62    public function install()
63    {
64
65        // Check and see if it's already installed (they've just hit refresh, those dorks)
66        if (Preference::exists('bitly_username')) {
67            return false;
68        }
69
70        Preference::insert('bitly_username', T_('Bit.ly Username'), '', 75, 'string', 'plugins', $this->name);
71        Preference::insert('bitly_api_key', T_('Bit.ly API key'), '', 75, 'string', 'plugins', $this->name);
72
73        return true;
74    } // install
75
76    /**
77     * uninstall
78     * This is a required plugin function. It removes our preferences from
79     * the database returning it to its original form
80     */
81    public function uninstall()
82    {
83        Preference::delete('bitly_username');
84        Preference::delete('bitly_api_key');
85    } // uninstall
86
87    /**
88     * upgrade
89     * This is a recommended plugin function
90     */
91    public function upgrade()
92    {
93        return true;
94    } // upgrade
95
96    /**
97     * @param string $url
98     * @return string|false
99     */
100    public function shortener($url)
101    {
102        if (empty($this->bitly_username) || empty($this->bitly_api_key)) {
103            debug_event('bitly.plugin', 'Bit.ly username or api key missing', 3);
104
105            return false;
106        }
107
108        $apiurl = 'http://api.bit.ly/v3/shorten?login=' . $this->bitly_username . '&apiKey=' . $this->bitly_api_key . '&longUrl=' . urlencode($url) . '&format=json';
109        try {
110            debug_event('bitly.plugin', 'Bit.ly api call: ' . $apiurl, 5);
111            $request = Requests::get($apiurl, array(), Core::requests_options());
112
113            return json_decode($request->body)->data->url;
114        } catch (Exception $error) {
115            debug_event('bitly.plugin', 'Bit.ly api http exception: ' . $error->getMessage(), 1);
116
117            return false;
118        }
119    }
120
121    /**
122     * load
123     * This loads up the data we need into this object, this stuff comes
124     * from the preferences.
125     * @param User $user
126     * @return boolean
127     */
128    public function load($user)
129    {
130        $user->set_preferences();
131        $data = $user->prefs;
132        // load system when nothing is given
133        if (!strlen(trim($data['bitly_username'])) || !strlen(trim($data['bitly_api_key']))) {
134            $data                   = array();
135            $data['bitly_username'] = Preference::get_by_user(-1, 'bitly_username');
136            $data['bitly_api_key']  = Preference::get_by_user(-1, 'bitly_api_key');
137        }
138
139        if (strlen(trim($data['bitly_username']))) {
140            $this->bitly_username = trim($data['bitly_username']);
141        } else {
142            debug_event('bitly.plugin', 'No Bit.ly username, shortener skipped', 3);
143
144            return false;
145        }
146        if (strlen(trim($data['bitly_api_key']))) {
147            $this->bitly_api_key = trim($data['bitly_api_key']);
148        } else {
149            debug_event('bitly.plugin', 'No Bit.ly api key, shortener skipped', 3);
150
151            return false;
152        }
153
154        return true;
155    } // load
156}
157