functions.php
changeset 5 9b96265b5918
parent 4 cde92f6ec29f
child 6 5f35ebc4f9bb
--- a/functions.php	Sun Mar 23 22:03:57 2008 -0400
+++ b/functions.php	Sun Mar 23 22:44:52 2008 -0400
@@ -4,9 +4,13 @@
  * Utility functions
  * 
  * Web control interface script for Amarok
- * Written by Dan Fuhry - 2008
+ * Copyright (C) 2008 Dan Fuhry
  *
- * This script is in the public domain. Use it for good, not evil.
+ * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
  */
 
 /**
@@ -78,11 +82,12 @@
   $playlist_file = dcop_action('playlist', 'saveCurrentPlaylist');
   // check MD5 - if it's not changed, exit to save CPU cycles
   global $playlist_last_md5;
-  if ( $playlist_last_md5 == @md5_file($playlist_file) )
+  $effective_md5 = md5_playlist_file($playlist_file);
+  if ( $playlist_last_md5 == $effective_md5 )
   {
     return true;
   }
-  $playlist_last_md5 = @md5_file($playlist_file);
+  $playlist_last_md5 = $effective_md5;
   // start XML parser
   try
   {
@@ -114,6 +119,23 @@
 }
 
 /**
+ * Builds the correct MD5 check for the specified playlist XML file. This is designed to base on the list of actual tracks, disregarding
+ * the rest of the text in the XML file.
+ * @param string Path to playlist
+ * @return string hash
+ */
+
+function md5_playlist_file($file)
+{
+  $contents = @file_get_contents($file);
+  if ( empty($contents) )
+    return false;
+  $count = preg_match_all('/uniqueid="([a-fA-F0-9]+?)"/', $contents, $matches);
+  $matches = implode("", $matches[1]);
+  return md5($matches);
+}
+
+/**
  * Converts a number to minute:second format
  * @param int Seconds
  * @return string format: mm:ss