functions.php
changeset 0 c63de9eb7045
child 2 860ba7141641
equal deleted inserted replaced
-1:000000000000 0:c63de9eb7045
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Utility functions
       
     5  * 
       
     6  * Web control interface script for Amarok
       
     7  * Written by Dan Fuhry - 2008
       
     8  *
       
     9  * This script is in the public domain. Use it for good, not evil.
       
    10  */
       
    11 
       
    12 /**
       
    13  * Utility/reporting functions
       
    14  */
       
    15  
       
    16 /**
       
    17  * Report a fatal error and exit
       
    18  * @param string Error message
       
    19  */
       
    20 
       
    21 function burnout($msg)
       
    22 {
       
    23   echo "\x1B[31;1m[WebControl] fatal: \x1B[37;1m";
       
    24   echo "$msg\x1B[0m\n";
       
    25   exit(1);
       
    26 }
       
    27 
       
    28 /**
       
    29  * Print a stylized status message, compatible with Linux consoles
       
    30  * @param string Status message
       
    31  */
       
    32 
       
    33 function status($msg)
       
    34 {
       
    35   echo "\x1B[32;1m[WebControl] \x1B[32;0m$msg\x1B[0m\n";
       
    36 }
       
    37 
       
    38 /**
       
    39  * Performs an action with DCOP.
       
    40  * @param string DCOP component, e.g. player, playlist, playlistbrowser, ...
       
    41  * @param string Action to perform, e.g. stop, play, ...
       
    42  * @param string additional parameters... (NOT IMPLEMENTED)
       
    43  * @return mixed output of DCOP command
       
    44  */
       
    45 
       
    46 function dcop_action($component, $action)
       
    47 {
       
    48   $tmpfile = tempnam('amaweb', '');
       
    49   if ( !$tmpfile )
       
    50     burnout('tempnam() failed us');
       
    51   system("dcop amarok $component $action > $tmpfile");
       
    52   $output = @file_get_contents($tmpfile);
       
    53   @unlink($tmpfile);
       
    54   $output = trim($output);
       
    55   
       
    56   // detect type of output
       
    57   if ( $output == 'true' )
       
    58     return true;
       
    59   else if ( $output == 'false' )
       
    60     return false;
       
    61   else if ( preg_match('/^-?[0-9]+/', $output) )
       
    62     return intval($output);
       
    63   else
       
    64     return $output;
       
    65 }
       
    66 
       
    67 /**
       
    68  * Rebuilds the copy of the playlist in RAM
       
    69  */
       
    70 
       
    71 function rebuild_playlist()
       
    72 {
       
    73   // import what we need
       
    74   global $homedir, $playlist;
       
    75   // sync and load the playlist file
       
    76   $playlist_file = dcop_action('playlist', 'saveCurrentPlaylist');
       
    77   // start XML parser
       
    78   try
       
    79   {
       
    80     $xml = simplexml_load_file($playlist_file);
       
    81   }
       
    82   catch ( Exception $e )
       
    83   {
       
    84     burnout("Caught exception trying to load playlist file:\n$e");
       
    85   }
       
    86   $attribs = $xml->attributes();
       
    87   if ( @$attribs['product'] != 'Amarok' )
       
    88   {
       
    89     burnout('Playlist is not in Amarok format');
       
    90   }
       
    91   $playlist = array();
       
    92   foreach ( $xml->children() as $child )
       
    93   {
       
    94     $attribs = $child->attributes();
       
    95     $item = array(
       
    96         'uri'    => $attribs['uri'],
       
    97         'title'  => strval($child->Title),
       
    98         'artist' => strval($child->Artist),
       
    99         'album'  => strval($child->Album),
       
   100         'length' => seconds_to_str(intval($child->Length))
       
   101       );
       
   102     $playlist[] = $item;
       
   103   }
       
   104 }
       
   105 
       
   106 /**
       
   107  * Converts a number to minute:second format
       
   108  * @param int Seconds
       
   109  * @return string format: mm:ss
       
   110  */
       
   111 
       
   112 function seconds_to_str($secs)
       
   113 {
       
   114   $seconds = $secs % 60;
       
   115   $minutes = ( $secs - $seconds ) / 60;
       
   116   $seconds = strval($seconds);
       
   117   $minutes = strval($minutes);
       
   118   if ( strlen($seconds) < 2 )
       
   119     $seconds = "0$seconds";
       
   120   if ( strlen($minutes) < 2 )
       
   121     $minutes = "0$minutes";
       
   122   return "$minutes:$seconds";
       
   123 }
       
   124