Index: audio.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/audio/audio.install,v
retrieving revision 1.4
diff -u -r1.4 audio.install
--- audio.install	12 Apr 2006 23:02:52 -0000	1.4
+++ audio.install	16 May 2006 23:26:11 -0000
@@ -55,7 +55,7 @@
 }
 
 /**
- * Expand the tag lengths to 45 characters to allow the longest tag defined 
+ * Expand the tag lengths to 45 characters to allow the longest tag defined
  * by getID3.
  */
 function audio_update_11() {
@@ -74,6 +74,15 @@
 
   return $ret;
 }
+
+/**
+ * Add download and play permisions for anon and auth users by default.
+ */
+function audio_update_12() {
+  _audio_add_default_perms();
+  return array();
+}
+
 /**
  * Install the initial schema.
  */
@@ -127,4 +136,30 @@
       );
       break;
   }
+
+  _audio_add_default_perms();
+}
+
+/**
+ * Add permission to download and view audio to the anonymous and authenticated
+ * roles by default.
+ */
+function _audio_add_default_perms() {
+  $newperms = array('download audio', 'play audio');
+  $rids = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID);
+  foreach ($rids as $rid) {
+    $result = db_result(db_query("SELECT perm FROM {permission} WHERE rid = %d", $rid));
+    if ($result) {
+      $perms = explode(', ', $result);
+      foreach ($newperms as $newperm) {
+        if (!in_array($newperm, $perms)) {
+          $perms[] = $newperm;
+        }
+      }
+      db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", implode(', ', $perms), $rid);
+    }
+    else {
+      db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', $newperms));
+    }
+  }
 }
Index: audio.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/audio/audio.module,v
retrieving revision 1.51
diff -u -r1.51 audio.module
--- audio.module	21 Apr 2006 21:46:10 -0000	1.51
+++ audio.module	17 May 2006 00:05:36 -0000
@@ -130,7 +130,7 @@
  * Implementation of hook_perm
  */
 function audio_perm() {
-  return array('administer audio', 'create audio');
+  return array('administer audio', 'create audio', 'play audio', 'download audio');
 }
 
 /**
@@ -154,6 +154,43 @@
 }
 
 /**
+ * Is the current user allowed to download an audio node?
+ */
+function _audio_allow_download($node) {
+  if (isset($node->url_download) && $node->audio_fileinfo['downloadable']) {
+    // let other modules affect the download permissions
+    $result = module_invoke_all('audio', 'access', $node, 'download');
+    if (in_array(TRUE, $result)) {
+      return TRUE;
+    }
+    if (in_array(FALSE, $result)) {
+      return FALSE;
+    }
+    return user_access('download audio');
+  }
+  return FALSE;
+}
+
+/**
+ * Is the current user allowed to play an audio node?
+ */
+function _audio_allow_play($node) {
+  if (isset($node->url_play)) {
+    // let other modules affect the download permissions
+    $result = module_invoke_all('audio', 'access', $node, 'play');
+    if (in_array(TRUE, $result)) {
+      return TRUE;
+    }
+    if (in_array(FALSE, $result)) {
+      return FALSE;
+    }
+    return user_access('play audio');
+  }
+  return FALSE;
+}
+
+
+/**
  * Build a title for the node.
  */
 function audio_set_title($node) {
@@ -179,11 +216,13 @@
   $links = array();
 
   if ($type == 'node' && $node->type == 'audio') {
-    if ($node->url_download) {
+    if (_audio_allow_download($node)) {
       $links[] = l(t('download audio file'), $node->url_download, NULL);
       $links[] = t('%download_count downloads', array('%download_count' => $node->audio_fileinfo['download_count']));
     }
-    $links[] = t('%play_count plays', array('%play_count' => $node->audio_fileinfo['play_count']));
+    if (_audio_allow_play($node)) {
+      $links[] = t('%play_count plays', array('%play_count' => $node->audio_fileinfo['play_count']));
+    }
   }
 
   return $links;
@@ -201,7 +240,7 @@
 
       case 'rss item':
         $ret = array();
-        if ($node->audio_file && $node->url_download) {
+        if (_audio_allow_download($node)) {
           // NOTE: we'll try to reset the node's body to remove theming, this only
           // works with this patch: http://drupal.org/node/41703
           $body = db_result(db_query("SELECT r.body FROM {node} n INNER JOIN {node_revisions} r ON n.nid = r.nid WHERE n.vid=%d", $node->vid));
@@ -342,6 +381,9 @@
 
   // add the tags records
   _audio_save_tags_to_db($node);
+
+  // notify other modules
+  module_invoke_all('audio', 'insert', $node);
 }
 
 /**
@@ -385,6 +427,9 @@
 
   // add the tags records
   _audio_save_tags_to_db($node);
+
+  // notify other modules
+  module_invoke_all('audio', 'insert', $node);
 }
 
 /**
@@ -437,13 +482,18 @@
 
   // add the tags records
   _audio_save_tags_to_db($node);
+
+  // notify other modules
+  module_invoke_all('audio', 'update', $node);
 }
 
 /**
  * Implementation of hook_delete.
  */
 function audio_delete($node) {
-  $vids = array();
+  // notify other modules
+  module_invoke_all('audio', 'delete', $node);
+
   $result = db_query('SELECT vid FROM {audio} WHERE nid = %d', $node->nid);
   while ($o = db_fetch_object($result)) {
     if ($filepath = db_result(db_query('SELECT filepath FROM {audio_file} WHERE vid = %d', $o->vid))) {
@@ -459,6 +509,9 @@
  * Delete a single revision.
  */
 function audio_delete_revision($node) {
+  // notify other modules
+  module_invoke_all('audio', 'delete', $node);
+
   if ($filepath = db_result(db_query('SELECT filepath FROM {audio_file} WHERE vid = %d', $node->vid))) {
     file_delete($filepath);
   }
@@ -1126,7 +1179,7 @@
  */
 function audio_get_player($node) {
   // if we've got a URL, setup a player
-  if (isset($node->url_play)) {
+  if (_audio_allow_play($node)) {
     // try to find one that's type specific...
     $format = $node->audio_fileinfo['fileformat'];
     if (!$player = theme('audio_'. $format .'_player', $node)) {
@@ -1135,6 +1188,7 @@
     }
     return $player;
   }
+  return NULL;
 }
 
 /**
@@ -1192,10 +1246,13 @@
  */
 function audio_download($nid = false) {
   if ($node = node_load($nid)) {
-    if (db_result(db_query('SELECT downloadable FROM {audio} a WHERE a.vid=%d', $node->vid))) {
+    if (_audio_allow_download($node)) {
       // increment the play count
       db_query('UPDATE {audio} a SET a.download_count = a.download_count + 1 WHERE a.vid = %d', $node->vid);
 
+      // notify other modules
+      module_invoke_all('audio', 'download', $node);
+
       $headers = array(
         'Content-Type: '. $node->audio_file->filemime,
         'Content-Length: '. $node->audio_file->filesize,
@@ -1205,7 +1262,9 @@
     }
     drupal_access_denied();
   }
-  drupal_not_found();
+  else {
+    drupal_not_found();
+  }
 }
 
 /**
@@ -1213,23 +1272,31 @@
  */
 function audio_play($nid = false) {
   if ($node = node_load($nid)) {
-    // increment the play count
-    db_query('UPDATE {audio} a SET a.play_count = a.play_count + 1 WHERE a.vid = %d', $node->vid);
+    if (_audio_allow_play($node)) {
+      // increment the play count
+      db_query('UPDATE {audio} a SET a.play_count = a.play_count + 1 WHERE a.vid = %d', $node->vid);
 
-    $headers = array(
-      "Pragma: public", // required
-      "Expires: 0",
-      "Cache-Control: must-revalidate, post-check=0, pre-check=0, private",
-      'Content-Type: '. $node->audio_file->filemime,
-      'Content-Length: '. $node->audio_file->filesize,
-      'Content-Disposition: inline;',
-      'Content-Transfer-Encoding: binary',
-    );
-    // required for IE, otherwise Content-disposition is ignored
-    ini_set('zlib.output_compression', 'Off');
-    file_transfer($node->audio_file->filepath, $headers);
+      // notify other modules
+      module_invoke_all('audio', 'play', $node);
+
+      $headers = array(
+        "Pragma: public", // required
+        "Expires: 0",
+        "Cache-Control: must-revalidate, post-check=0, pre-check=0, private",
+        'Content-Type: '. $node->audio_file->filemime,
+        'Content-Length: '. $node->audio_file->filesize,
+        'Content-Disposition: inline;',
+        'Content-Transfer-Encoding: binary',
+      );
+      // required for IE, otherwise Content-disposition is ignored
+      ini_set('zlib.output_compression', 'Off');
+      file_transfer($node->audio_file->filepath, $headers);
+    }
+    drupal_access_denied();
+  }
+  else {
+    drupal_not_found();
   }
-  drupal_not_found();
 }
 
 

