Could you implement webfm_file_download (hook_file_download) in the same way of cck filefield ?

Because if we use this technic to set private files, that doesn't works with this module

Thank you

Comments

sinasquax’s picture

I have implemented it but it's not finished (look at browser access if file is not attached to a node and check for uid of file, ...)


/**
 * Implementation of hook_file_download()
 */
function webfm_file_download($file) {
  // Get true path
  $file = file_create_path($file);

  // Check if file is attached to a node
  $result = db_query("SELECT a.nid, f.* FROM {webfm_attach} a LEFT JOIN {webfm_file} f ON a.fid = f.fid WHERE f.fpath = '%s'", $file);
  if ($file = db_fetch_object($result)) {
    // Check node access
	if (!(db_result(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $file->nid)))) {
	  return -1;
	}
  } else {
    // File is not attached, we don't have permissions (CHANGER ET VERIFIER AVEC LE BROWSER)
    return -1;
  }
  
  // Get file name
  $name = mime_header_encode(substr(strrchr($file->filename, '/'), 1));
  
  // Return file
  $disposition = ereg('^(text/|image/)', $file->fmime) ? 'inline' : 'attachment';
  return array(
    'Content-Type: ' . mime_header_encode($file->fmime) . '; name=' . $name,
    'Content-Length: ' . $file->fsize,
    'Content-Disposition: ' . $disposition . '; filename=' . $name,
    'Cache-Control: private',
  );
}

nhck’s picture

hook_webfm_send cannot take care of this? Also what about #437196: WebFM Folder Access rules based upon IP Ranges?

sinasquax’s picture

sinasquax’s picture

There is a bug in my function, here is the correct function :


/**
 * Implementation of hook_file_download()
 */
function webfm_file_download($file) {
  // Get true path
  $fpath = file_create_path($file);
  
  // Verify if file is managed by webfm
  $result = db_query("SELECT * FROM {webfm_file} WHERE fpath = '%s'", $fpath);
  if (!($file = db_fetch_object($result))) {
    // File not found, exit
    return;
  }
  
  // Check if file is attached to a node
  $result = db_query("SELECT a.nid, f.* FROM {webfm_attach} a LEFT JOIN {webfm_file} f ON a.fid = f.fid WHERE f.fpath = '%s'", $fpath);
  if ($file = db_fetch_object($result)) {
    // Check node access
	if (!(db_result(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $file->nid)))) {
	  return -1;
	}
  } else {
    // File is not attached, we don't have permissions (CHANGER ET VERIFIER AVEC LE BROWSER)
    return -1;
  }
  
  // Get file name
  $name = mime_header_encode(substr(strrchr($file->filename, '/'), 1));
  
  // Return file
  $disposition = ereg('^(text/|image/)', $file->fmime) ? 'inline' : 'attachment';
  return array(
    'Content-Type: ' . mime_header_encode($file->fmime) . '; name=' . $name,
    'Content-Length: ' . $file->fsize,
    'Content-Disposition: ' . $disposition . '; filename=' . $name,
    'Cache-Control: private',
  );
}

nhck’s picture

Status: Active » Closed (duplicate)

There is a patch over here: #591144: Implement hook_file_download Care to test it?