Hello All,

I'm trying to figure out how to display node file attachments (using standard upload.module) to unregistered users, but require registration in order to complete the download?

The following function looks like it displays the file download table in nodes, but I can't seem to figure out where it's called from (and where access permissions come into effect) for this function.

Anybody have any good ideas on how to accomplish this?

/**
 * Displays file attachments in table... from modules/upload.module
 */
function theme_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();
  foreach ($files as $file) {
    if ($file->list) {
      $href = check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))));
      $text = check_plain($file->description ? $file->description : $file->filename);
      $rows[] = array(l($text, $href), format_size($file->filesize));
    }
  }
  if (count($rows)) {
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}

Comments

conkdg’s picture

Forgot to mention that I'm running Drupal 4.7.2

pwolanin’s picture

It doesn't really matter where this is called from, you can override it in your theme's template.php file (assuming your theme is phptemplate-based). I'd try something like the below. A quick test on my scratch site seems to work, though I think anonymous users could still download if they can guess the path to the file.

/**
 * Displays file attachments in table... from modules/upload.module
 */
function phptemplate_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();
  foreach ($files as $file) {
    if ($file->list) {
      global $user;
      if ($user->uid) { // logged in users have uid > 0
        $href = check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))));
        $text = check_plain($file->description ? $file->description : $file->filename);
        $rows[] = array(l($text, $href), format_size($file->filesize));
        if (count($rows)) {
          return theme('table', $header, $rows, array('id' => 'attachments'));
        }
      }
  // for anonymous users only show the name, not the link
      $text = check_plain($file->description ? $file->description : $file->filename);
      $rows[] = array($text, format_size($file->filesize));
      if (count($rows)) {
        return theme('table', $header, $rows, array('id' => 'attachments'))."<p><em>You must <a href=\"/user\">login/register</a> in order to download files</em></p>";
      }
    }
  }
}

let me know if this works for you...

---
Work: BioRAFT

conkdg’s picture

pwolanin - This is just what I was looking for. Thank you VERY MUCH!

conkdg’s picture

Just FYI, this is the final function I ended up using. Since logged-in users are already going to see the files, I made this visible only to non logged-in users. I put this function in a module file, and called it from my node.tpl.php file just after the $content is displayed. Cheers!

function ww_phptemplate_upload_attachments($files) {
	/**
	* Displays file attachments in table for non-registered users, with link to login.
	*/
	global $user;
	if (!$user->uid) {	
		$header = array(t('Attachment'), t('Size'));
		$rows = array();
		foreach ($files as $file) {
			$text = check_plain($file->description ? $file->description : $file->filename);
			$rows[] = array($text, format_size($file->filesize));
		}//end foreach
		if (count($rows)) {
			return theme('table', $header, $rows, array('id' => 'attachments'))."<p><small><em>You must <a href=\"/user\">login/register</a> to download publications</em></small></p>";
		}
	}
}