functions.php
changeset 5 9b96265b5918
parent 4 cde92f6ec29f
child 6 5f35ebc4f9bb
equal deleted inserted replaced
4:cde92f6ec29f 5:9b96265b5918
     2 
     2 
     3 /**
     3 /**
     4  * Utility functions
     4  * Utility functions
     5  * 
     5  * 
     6  * Web control interface script for Amarok
     6  * Web control interface script for Amarok
     7  * Written by Dan Fuhry - 2008
     7  * Copyright (C) 2008 Dan Fuhry
     8  *
     8  *
     9  * This script is in the public domain. Use it for good, not evil.
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    10  */
    14  */
    11 
    15 
    12 /**
    16 /**
    13  * Utility/reporting functions
    17  * Utility/reporting functions
    14  */
    18  */
    76   global $homedir, $playlist;
    80   global $homedir, $playlist;
    77   // sync and load the playlist file
    81   // sync and load the playlist file
    78   $playlist_file = dcop_action('playlist', 'saveCurrentPlaylist');
    82   $playlist_file = dcop_action('playlist', 'saveCurrentPlaylist');
    79   // check MD5 - if it's not changed, exit to save CPU cycles
    83   // check MD5 - if it's not changed, exit to save CPU cycles
    80   global $playlist_last_md5;
    84   global $playlist_last_md5;
    81   if ( $playlist_last_md5 == @md5_file($playlist_file) )
    85   $effective_md5 = md5_playlist_file($playlist_file);
       
    86   if ( $playlist_last_md5 == $effective_md5 )
    82   {
    87   {
    83     return true;
    88     return true;
    84   }
    89   }
    85   $playlist_last_md5 = @md5_file($playlist_file);
    90   $playlist_last_md5 = $effective_md5;
    86   // start XML parser
    91   // start XML parser
    87   try
    92   try
    88   {
    93   {
    89     $xml = simplexml_load_file($playlist_file);
    94     $xml = simplexml_load_file($playlist_file);
    90   }
    95   }
   109         'length' => seconds_to_str(intval($child->Length)),
   114         'length' => seconds_to_str(intval($child->Length)),
   110         'length_int' => intval($child->Length)
   115         'length_int' => intval($child->Length)
   111       );
   116       );
   112     $playlist[] = $item;
   117     $playlist[] = $item;
   113   }
   118   }
       
   119 }
       
   120 
       
   121 /**
       
   122  * Builds the correct MD5 check for the specified playlist XML file. This is designed to base on the list of actual tracks, disregarding
       
   123  * the rest of the text in the XML file.
       
   124  * @param string Path to playlist
       
   125  * @return string hash
       
   126  */
       
   127 
       
   128 function md5_playlist_file($file)
       
   129 {
       
   130   $contents = @file_get_contents($file);
       
   131   if ( empty($contents) )
       
   132     return false;
       
   133   $count = preg_match_all('/uniqueid="([a-fA-F0-9]+?)"/', $contents, $matches);
       
   134   $matches = implode("", $matches[1]);
       
   135   return md5($matches);
   114 }
   136 }
   115 
   137 
   116 /**
   138 /**
   117  * Converts a number to minute:second format
   139  * Converts a number to minute:second format
   118  * @param int Seconds
   140  * @param int Seconds