First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
<?php
/**
* Utility functions
*
* Web control interface script for Amarok
* Written by Dan Fuhry - 2008
*
* This script is in the public domain. Use it for good, not evil.
*/
/**
* Utility/reporting functions
*/
/**
* Report a fatal error and exit
* @param string Error message
*/
function burnout($msg)
{
echo "\x1B[31;1m[WebControl] fatal: \x1B[37;1m";
echo "$msg\x1B[0m\n";
exit(1);
}
/**
* Print a stylized status message, compatible with Linux consoles
* @param string Status message
*/
function status($msg)
{
echo "\x1B[32;1m[WebControl] \x1B[32;0m$msg\x1B[0m\n";
}
/**
* Performs an action with DCOP.
* @param string DCOP component, e.g. player, playlist, playlistbrowser, ...
* @param string Action to perform, e.g. stop, play, ...
* @param string additional parameters... (NOT IMPLEMENTED)
* @return mixed output of DCOP command
*/
function dcop_action($component, $action)
{
$tmpfile = tempnam('amaweb', '');
if ( !$tmpfile )
burnout('tempnam() failed us');
system("dcop amarok $component $action > $tmpfile");
$output = @file_get_contents($tmpfile);
@unlink($tmpfile);
$output = trim($output);
// detect type of output
if ( $output == 'true' )
return true;
else if ( $output == 'false' )
return false;
else if ( preg_match('/^-?[0-9]+/', $output) )
return intval($output);
else
return $output;
}
/**
* Rebuilds the copy of the playlist in RAM
*/
function rebuild_playlist()
{
// import what we need
global $homedir, $playlist;
// sync and load the playlist file
$playlist_file = dcop_action('playlist', 'saveCurrentPlaylist');
// start XML parser
try
{
$xml = simplexml_load_file($playlist_file);
}
catch ( Exception $e )
{
burnout("Caught exception trying to load playlist file:\n$e");
}
$attribs = $xml->attributes();
if ( @$attribs['product'] != 'Amarok' )
{
burnout('Playlist is not in Amarok format');
}
$playlist = array();
foreach ( $xml->children() as $child )
{
$attribs = $child->attributes();
$item = array(
'uri' => $attribs['uri'],
'title' => strval($child->Title),
'artist' => strval($child->Artist),
'album' => strval($child->Album),
'length' => seconds_to_str(intval($child->Length))
);
$playlist[] = $item;
}
}
/**
* Converts a number to minute:second format
* @param int Seconds
* @return string format: mm:ss
*/
function seconds_to_str($secs)
{
$seconds = $secs % 60;
$minutes = ( $secs - $seconds ) / 60;
$seconds = strval($seconds);
$minutes = strval($minutes);
if ( strlen($seconds) < 2 )
$seconds = "0$seconds";
if ( strlen($minutes) < 2 )
$minutes = "0$minutes";
return "$minutes:$seconds";
}