id = Dba::escape(Stream::get_session());
if (!Stream::session_exists($this->id)) {
debug_event('stream_playlist', 'Stream::session_exists failed', 2);
return false;
}
$this->user = intval($GLOBALS['user']->id);
$sql = "SELECT * FROM `stream_playlist` WHERE `sid`='" .
$this->id . "' ORDER BY `id`";
$db_results = Dba::read($sql);
while ($row = Dba::fetch_assoc($db_results)) {
$this->urls[] = new Stream_URL($row);
}
return true;
}
private function _add_url($url) {
$this->urls[] = $url;
$sql = 'INSERT INTO `stream_playlist` ';
$fields[] = '`sid`';
$values[] = Dba::escape($this->id);
foreach ($url->properties as $field) {
if ($url->$field) {
$fields[] = '`' . $field . '`';
$values[] = Dba::escape($url->$field);
}
}
$sql .= '(' . implode(', ', $fields) . ') ';
$sql .= "VALUES('" . implode("', '", $values) . "')";
return Dba::write($sql);
}
public static function gc() {
$sql = 'DELETE FROM `stream_playlist` ' .
'USING `stream_playlist` LEFT JOIN `session_stream` ' .
'ON `session_stream`.`id`=`stream_playlist`.`sid` ' .
'WHERE `session_stream`.`id` IS NULL';
return Dba::write($sql);
}
/**
* _media_to_urlarray
* Formats the URL and media information and adds it to the object
*/
private static function _media_to_urlarray($media) {
$urls = array();
foreach($media as $medium) {
debug_event('stream_playlist', 'Adding ' . json_encode($media), 5);
$url = array();
$type = $medium['object_type'];
$array['type'] = $type;
$object = new $type($medium['object_id']);
$object->format();
//FIXME: play_url shouldn't be static
$url['url'] = $type::play_url($object->id);
// Set a default which can be overridden
$url['author'] = 'Ampache';
$url['time'] = $object->time;
switch($type) {
case 'song':
$url['title'] = $object->title;
$url['author'] = $object->f_artist_full;
$url['info_url'] = $object->f_link;
$url['image_url'] = Art::url($object->album, 'album');
$url['album'] = $object->f_album_full;
break;
case 'video':
$url['title'] = 'Video - ' . $object->title;
$url['author'] = $object->f_artist_full;
break;
case 'radio':
$url['title'] = 'Radio - ' . $object->name .
' [' . $object->frequency .
'] (' . $object->site_url . ')';
break;
case 'random':
$url['title'] = 'Random URL';
break;
default:
$url['title'] = 'URL-Add';
$url['time'] = -1;
break;
}
$urls[] = new Stream_URL($url);
}
return $urls;
}
public function generate_playlist($type, $redirect = false) {
if (!count($this->urls)) {
debug_event('stream_playlist', 'Error: Empty URL array for ' . $this->id, 2);
return false;
}
debug_event('stream_playlist', 'generating a ' . $type, 5);
$ext = $type;
switch($type) {
case 'democratic':
case 'localplay':
case 'xspf_player':
// These are valid, but witchy
$redirect = false;
unset($ext);
break;
case 'asx':
$ct = 'video/x-ms-wmv';
break;
case 'pls':
$ct = 'audio/x-scpls';
break;
case 'ram':
$ct = 'audio/x-pn-realaudio ram';
break;
case 'simple_m3u':
$ext = 'm3u';
$ct = 'audio/x-mpegurl';
break;
case 'xspf':
$ct = 'application/xspf+xml';
break;
case 'm3u':
default:
// Assume M3U if the pooch is screwed
$ext = $type = 'm3u';
$ct = 'audio/x-mpegurl';
break;
}
if ($redirect) {
// Our ID is the SID, so we always want to include it
Config::set('require_session', true, true);
header('Location: ' . Stream::get_base_url() . 'uid=' . scrub_out($this->user) . '&type=playlist&playlist_type=' . scrub_out($type));
exit;
}
if (isset($ext)) {
header('Cache-control: public');
header('Content-Disposition: filename=ampache_playlist.' . $ext);
header('Content-Type: ' . $ct . ';');
}
$this->{'create_' . $type}();
}
/**
* add
* Adds an array of media
*/
public function add($media = array()) {
$urls = $this->_media_to_urlarray($media);
foreach ($urls as $url) {
$this->_add_url($url);
}
}
/**
* add_urls
* Add an array of urls. This is used for things that aren't coming
* from media objects
*/
public function add_urls($urls = array()) {
if (!is_array($urls)) { return false; }
foreach ($urls as $url) {
$this->_add_url(new Stream_URL(array(
'url' => $url,
'title' => 'URL-Add',
'author' => 'Ampache',
'time' => '-1'
)));
}
}
/**
* create_simplem3u
* this creates a simple m3u without any of the extended information
*/
public function create_simple_m3u() {
foreach ($this->urls as $url) {
echo $url->url . "\n";
}
} // simple_m3u
/**
* create_m3u
* creates an m3u file, this includes the EXTINFO and as such can be
* large with very long playlsits
*/
public function create_m3u() {
echo "#EXTM3U\n";
foreach ($this->urls as $url) {
echo '#EXTINF:' . $url->time, ',' . $url->author .
' - ' . $url->title . "\n";
echo $url->url . "\n";
}
} // create_m3u
/**
* create_pls
*/
public function create_pls() {
echo "[playlist]\n";
echo 'NumberOfEntries=' . count($this->urls) . "\n";
foreach ($this->urls as $url) {
$i++;
echo 'File' . $i . '='. $url->url . "\n";
echo 'Title' . $i . '=' . $url->author . ' - ' .
$url->title . "\n";
echo 'Length' . $i . '=' . $url->time . "\n";
}
echo "Version=2\n";
} // create_pls
/**
* create_asx
* This should really only be used if all of the content is ASF files.
*/
public function create_asx() {
echo '