From ea9248eec491ac4ba670af7ea2fd3107c776f81d Mon Sep 17 00:00:00 2001
From: Roy Spliet <r.spliet@student.tudelft.nl>
Date: Mon, 24 Oct 2011 17:02:17 +0200
Subject: [PATCH] Drupal/Filetree: Add private file downloader

For now copy-pasted from IMCE. Should have access control though eventually. Also adds an option to force a file download instead of opening in a browser window, but this only works with the Private file downloader

Signed-off-by: Roy Spliet <r.spliet@student.tudelft.nl>
---
 filetree.info    |    5 ++-
 filetree.install |    7 ++++++
 filetree.module  |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/filetree.info b/filetree.info
index 5c834a5..4c087de 100755
--- a/filetree.info
+++ b/filetree.info
@@ -2,6 +2,7 @@ name = File Tree
 description = Renders an interactive list from a folder of files.
 package = Input filters
 core = 7.x
-configure = admin/config/content/formats
+configure = admin/config/media/filetree
 scripts[] = filetree.js
-stylesheets[all][] = filetree.css
\ No newline at end of file
+stylesheets[all][] = filetree.css
+
diff --git a/filetree.install b/filetree.install
index 8e068b9..96f3179 100644
--- a/filetree.install
+++ b/filetree.install
@@ -4,9 +4,16 @@
  * Implements hook_install().
  */
 function filetree_install() {
+  variable_set('filetree_private_download_mgr',0);
+  variable_set('filetree_private_download_force',0);
   drupal_set_message(t('File Tree has been installed. Before this does anything, File Tree needs to be added to one or more text formats. Visit the <a href="!url">text format administration</a> to set up this filter.', array('!url' => url('admin/config/content/formats'))), 'warning');
 }
 
+function filetree_uninstall() {
+  variable_del('filetree_private_download_mgr');
+  variable_del('filetree_private_download_force');
+}
+
 /**
  * Upgrade File Tree to Drupal 7.
  */
diff --git a/filetree.module b/filetree.module
index 3f22c74..48386a3 100755
--- a/filetree.module
+++ b/filetree.module
@@ -333,3 +333,66 @@ function theme_filetree($variables) {
 
   return '<div id="' . $id . '" class="' . implode(' ', $classes) . '">' . $output . '</div>';
 }
+
+/**
+ * Implements hook_file_download().
+ * Support private downloads if not disabled.
+ * For now a copy from IMCE
+ */
+function filetree_file_download($uri) {
+	$serve = file_uri_scheme($uri) == 'private' && variable_get('filetree_private_download_mgr') == 1 &&  file_exists($uri) && strpos(basename($uri), '.');
+	/* XXX: Also check permissions here */
+	if ($serve) {
+		$retval = array(
+			'Content-Type' => file_get_mimetype($uri),
+			'Content-Length' => filesize($uri),
+		);
+		if(variable_get('filetree_private_download_force') == 1) 
+			$retval['Content-Disposition'] = 'attachment; filename='.basename($uri);
+		return $retval;
+	}
+}
+
+function filetree_menu() {
+	$items = array();
+	
+	$items['admin/config/media/filetree'] = array(
+		'title' => t('Settings'),
+		'description' => t('Settings for the filetree module'),
+		'page callback' => 'drupal_get_form',
+		'page arguments' => array('filetree_admin_form'),
+		'access arguments' => array('administer modules'),
+		'type' => MENU_NORMAL_ITEM
+	);
+	
+	return $items;
+}
+
+function filetree_admin_form($form,&$form_state) {
+	$form = array();
+	$form['filetree_private_download_mgr'] = array(
+		'#type' => 'checkbox',
+		'#title' => t('Enable private file downloader (note: this imposes a security risk)'),
+		'#required' => FALSE,
+		'#default_value' => variable_get('filetree_private_download_mgr')
+	);
+	
+	$form['filetree_private_download_force'] = array(
+		'#type' => 'checkbox',
+		'#title' => t('Force private file download (instead of opening in a browser window)'),
+		'#required' => FALSE,
+		'#default_value' => variable_get('filetree_private_download_force')
+	);
+	
+	return system_settings_form($form);
+}
+
+function filetree_help($path, $arg) {
+	switch ($path) {
+	case 'admin/config/media/filetree':
+		$help = t("The filetree filter can be turned on for one or more Text formats on the ") . l(t("Text formats settings page"), "admin/config/content/formats") . ".";
+		break;
+	}
+	
+	return $help;
+}
\ No newline at end of file
-- 
1.7.6.4

