1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-04 18:29:40 +02:00
ampache/modules/plugins/Bitly.plugin.php
2014-01-15 23:58:13 +01:00

136 lines
3.9 KiB
PHP

<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
*
* LICENSE: GNU General Public License, version 2 (GPLv2)
* Copyright 2001 - 2013 Ampache.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License v2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
class AmpacheBitly {
public $name = 'Bit.ly';
public $description = 'Url shorteners on shared links with Bit.ly';
public $url = 'http://bitly.com/';
public $version = '000001';
public $min_ampache = '360037';
public $max_ampache = '999999';
// These are internal settings used by this class, run this->load to
// fill them out
private $bitly_username;
private $bitly_api;
/**
* Constructor
* This function does nothing...
*/
public function __construct() {
return true;
} // constructor
/**
* install
* This is a required plugin function. It inserts our preferences
* into Ampache
*/
public function install() {
// Check and see if it's already installed (they've just hit refresh, those dorks)
if (Preference::exists('bitly_username')) { return false; }
Preference::insert('bitly_username','Bit.ly username','','25','string','plugins');
Preference::insert('bitly_api','Bit.ly api key','','25','string','plugins');
return true;
} // install
/**
* uninstall
* This is a required plugin function. It removes our preferences from
* the database returning it to its original form
*/
public function uninstall() {
Preference::delete('bitly_username');
Preference::delete('bitly_api');
} // uninstall
/**
* upgrade
* This is a recommended plugin function
*/
public function upgrade() {
return true;
} // upgrade
public function shortener($url) {
if (empty($this->bitly_username) || empty($this->bitly_api)) {
debug_event($this->name, 'Bit.ly username or api key missing', '3');
return false;
}
$shorturl = '';
$apiurl = 'http://api.bit.ly/v3/shorten?login=' . $this->bitly_username . '&apiKey=' . $this->bitly_api . '&longUrl=' . urlencode($url) . '&format=json';
try {
debug_event($this->name, 'Bit.ly api call: ' . $apiurl, '5');
$request = Requests::get($apiurl);
$shorturl = json_decode($request->body)->data->url;
} catch (Exception $e) {
debug_event($this->name, 'Bit.ly api http exception: ' . $e->getMessage(), '1');
return false;
}
return $shorturl;
}
/**
* load
* This loads up the data we need into this object, this stuff comes
* from the preferences.
*/
public function load($user) {
$user->set_preferences();
$data = $user->prefs;
if (strlen(trim($data['bitly_username']))) {
$this->bitly_username = trim($data['bitly_username']);
}
else {
debug_event($this->name,'No Bit.ly username, shortener skipped','3');
return false;
}
if (strlen(trim($data['bitly_api']))) {
$this->bitly_api = trim($data['bitly_api']);
}
else {
debug_event($this->name,'No Bit.ly api key, shortener skipped','3');
return false;
}
return true;
} // load
} // end AmpacheBitly
?>