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 ?>