## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License version 3, 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 warranties of
## MERCHANTABILITY, SATISFACTORY QUALITY, 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, see .
#### END LICENSE
#
// load the ampache API functions
require_once('ampachenowplaying-api.php');
function widget_ampache_now_playing($args) {
extract($args);
global $wpdb;
_create_initial_tables();
$autoupdate = get_option('ampachenowplaying_autoupdate', false);
echo $before_widget;
echo $before_title;
_e("Ampache Now Playing");
echo $after_title;
echo '

Loading...
'; // base64 encoded ajax gif
?>
".WP_CONTENT_DIR."";
if ($show_album_art) { // if the user selected to show the album art
$ampache_table = $table_prefix . "ampachenowplaying";
$uploads_dir = WP_CONTENT_DIR . '/uploads/';
if (! file_exists($uploads_dir) ) {
mkdir($uploads_dir);
}
$full_ampache_dir = $uploads_dir . 'ampachenowplaying';
if (! file_exists($full_ampache_dir) ) { // create the album_art cache folder if not exists
mkdir($full_ampache_dir);
}
// get the song id and check if we have it in the DB, if showing the last song that was playing it WILL be in the db
$song_id = _ampachenowplaying_get_song_id($song_info['link']);
$album_id = _ampachenowplaying_get_album_id_from_db($song_id);
if (!$album_id) { // the album ID for the current song is NOT in the db, so authenticate and get it
$auth = _ampachenowplaying_authenticate($user, $pass, $ampache_base);
if ($auth) { // authentication was successfull
$album_id = _ampachenowplaying_get_album_id($song_id, $ampache_base, $auth);
if ($album_id) { // if the album ID was returned successfully, save it to the db
$wpdb->query($wpdb->prepare("INSERT INTO $ampache_table (song_id, album_id)
VALUES ( %d, %d )", $song_id, $album_id));
}
}
}
if ($album_id) { // if the album id was found either in the DB or by authenticating, get the album art
$artwork_file = $full_ampache_dir . DIRECTORY_SEPARATOR . $album_id . '.jpg';
if (file_exists($artwork_file)) { // the album artwork exists locally on drupal
$artwork_url = WP_CONTENT_URL . '/uploads/ampachenowplaying/' . basename($artwork_file);
} else { // the artwork doesn't exist locally, pull it from ampache
if (! isset($auth)) { // if not already authenticated, authenticate
$auth = _ampachenowplaying_authenticate($user, $pass, $ampache_base);
}
if (isset($auth)) { // make sure we have an authenticated session
$artwork_file = _ampachenowplaying_get_album_artwork($album_id, $ampache_base, $auth, $full_ampache_dir);
$artwork_url = WP_CONTENT_URL . '/uploads/ampachenowplaying/' . basename($artwork_file);
}
}
}
} // END IF statement for show_album_art
// now print the results
$show_agent = get_option('ampachenowplaying_show_agent', FALSE);
$show_time = get_option('ampachenowplaying_show_date' , FALSE);
if ($show_album_art && $artwork_url) { // show the album art
$google_link = 'http://www.google.com/search?q=' . _add_pluses($song_info['title']);
$msg .= "";
}
$msg .= '';
$msg .= _convert_to_html($song_info['title']) . "
";
if ($show_agent) { $msg .= "
" . _convert_to_html($song_info['comments']); }
if ($show_time ) { $msg .= "
" . _convert_to_html($song_info['date']); }
$msg .= '
';
return $msg;
}
/**
* Helper Function for _fetch_rss
* This function takes the rss feed, parses it
* and returns and array with information about the current song.
* If the request is within the threshold (default of 10 seconds)
* this function will return an array constructed with data from
* the cache, to avoid pulling data from Ampache over and over again.
*
* @param $rss
* A string with the full url of the now_playing rss
* @return $song_info
* An array with song information
* -1 -- can't connect to ampache
* -2 -- nothing playing
* -3 -- hide block (return null for the block)
*/
function _ampachenowplaying_grab_now_playing($rss) {
$show_now_playing = get_option('ampachenowplaying_show_now_playing', 'Not Playing');
// check to see if the RSS was grabbed within the threshold, if it was, return the cache
$current_time = time();
$last_rss_time = get_option('ampachenowplaying_last_rss_time', NULL);
$threshold = get_option('ampachenowplaying_rss_threshold', 10);
if ($last_rss_time && $threshold != 0) { // this is NOT the first time getting rss, and caching is enabled
$time_since_last_rss = $current_time - $last_rss_time;
if ($time_since_last_rss <= $threshold) { // less than threshold -- return the cache
if (get_option('ampachenowplaying_nothing_playing', FALSE)) { // nothing playing
switch ($show_now_playing) {
case 'Not Playing': // show nothing playing
return -2;
case 'Hide Block': // hide the block
return -3;
}
} else { // something playing, or 'show last song' is enabled
return _ampachenowplaying_grab_from_cache();
}
}
} // END IF for being within the threshold
// rss is either older than threshold, or this is the first time this module is running- time to update
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
$doc = new DOMDocument();
$request = new WP_Http;
$msg = $request->request($rss, array( 'method' => 'GET') );
// try to load the DOMdocument, and fail if the URL is invalid
if ( ! isset($msg->errors) && $msg['response']['code'] == 200 && @$doc->loadXML($msg['body'])) { // http request successful and xml loaded int $dom
$arrFeeds = array();
// parse the XML for songs, grab only the first (newest)
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'comments' => $node->getElementsByTagName('comments')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
$song_info = $arrFeeds[0];
} else { // couldn't connect to ampache
switch ($show_now_playing) {
case 'Not Playing':
return -1;
case 'Hide Block' :
return -3;
case 'Last Song':
return _ampachenowplaying_grab_from_cache();
}
}
// if the code makes it this far, the RSS was pulled successfully, so set the last access time to now
update_option('ampachenowplaying_last_rss_time', $current_time);
if (!$song_info) { // nothing is currently playing
update_option('ampachenowplaying_nothing_playing', TRUE);
switch ($show_now_playing) {
case 'Not Playing':
return -2;
case 'Hide Block':
return -3;
case 'Last Song':
return _ampachenowplaying_grab_from_cache();
}
} else { // something is playing
update_option('ampachenowplaying_nothing_playing', FALSE);
// save the variables from the RSS document
update_option('ampachenowplaying_nowplaying_title', $song_info['title']);
update_option('ampachenowplaying_nowplaying_comments', $song_info['comments']);
update_option('ampachenowplaying_nowplaying_link', $song_info['link']);
update_option('ampachenowplaying_nowplaying_date', $song_info['date']);
}
return $song_info;
}
/**
* This function grabs the information from the cache instead of the rss and returns it as an array
*
* @param none
* @return $song_info
* The same array that gets created from parsing the now_playing rss
*/
function _ampachenowplaying_grab_from_cache() {
$song_info = array (
'title' => get_option('ampachenowplaying_nowplaying_title', NULL),
'comments' => get_option('ampachenowplaying_nowplaying_comments', NULL),
'link' => get_option('ampachenowplaying_nowplaying_link', NULL),
'date' => get_option('ampachenowplaying_nowplaying_date', NULL),
);
if ($song_info['title']) { // the value isn't null
return $song_info;
}
// no song has played and been cached yet
return NULL;
}
/**
* This function takes the link extracted from the RSS
* and extracts the current playing song_id
*
* @param $link
* A string with the link extracted from the now playing rss
* @return $song_id
* An int with the song_id
*/
function _ampachenowplaying_get_song_id($link) {
// parse a link that looks like this
// http://example.com/ampache/song.php?action=show_song&song_id=8776
$song_id = preg_replace('/^.*song_id=/', '', $link);
$song_id = preg_replace('/&.*/', '', $song_id);
$song_id = preg_replace('/[^0-9]/', '', $song_id); // sanatize
return (int)$song_id;
}
/**
* This function takes the song_id and checks the database
* to see if it is already linked to an album.
*
* @param $song_id
* An int with the song_id
* @return $album_id
* An int with the album_id or NULL if it doesn't exist
*/
function _ampachenowplaying_get_album_id_from_db($song_id) {
global $table_prefix, $wpdb;
$ampache_table = $table_prefix . "ampachenowplaying";
$query = $wpdb->get_results($wpdb->prepare("SELECT album_id FROM $ampache_table
WHERE song_id = %d", $song_id), ARRAY_A);
#$album_id = $album_id['album_id'];
if ( ! $query) {
return NULL;
}
$album_id = $query[0]['album_id'];
if ( ! $album_id) {
return NULL;
}
return (int)$album_id;
}
/**
* This function checks to see if the ampachenowplaying table exists,
* and if it doesn't it creates it.
* This table links song_id's to album_id's
*/
function _create_initial_tables() {
global $table_prefix, $wpdb;
# Create the 'name' of our table which is prefixed by the standard WP table prefix (which you specified when you installed WP)
$ampache_table = $table_prefix . "ampachenowplaying";
# Check to see if the table exists already, if not, then create it
if($wpdb->get_var("show tables like '$ampache_table'") != $ampache_table) {
$sql0 = "CREATE TABLE `". $ampache_table . "` ( ";
$sql0 .= " `song_id` int NOT NULL,";
$sql0 .= " `album_id` int NOT NULL,";
$sql0 .= " PRIMARY KEY (`song_id`) ";
$sql0 .= ");";
#We need to include this file so we have access to the dbDelta function below (which is used to create the table)
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
dbDelta($sql0);
}
}
/**
* Returns a string fit for a google URL
*
* @param string
* @return string
*/
function _add_pluses($string) {
return urlencode($string);
}
function _convert_to_html($string) {
return htmlentities($string);
}
/**
* This gets called by admin_menu for the admin menu (settings tab)
**/
function ampachenowplaying_admin_menu() {
add_options_page('Ampache Now Playing', 'Ampache Now Playing', 'manage_options', 'ampachenowplaying', 'ampachenowplaying_options_page');
}
function ampachenowplaying_options_page() {
include('ampachenowplaying-settings.php');
}
?>