Kaynağa Gözat

Fixing dropbox download see BT#7680

Julio Montoya 11 yıl önce
ebeveyn
işleme
530382a993

+ 11 - 6
main/dropbox/dropbox_download.php

@@ -26,10 +26,13 @@ require_once api_get_path(LIBRARY_PATH).'document.lib.php';
 
 
 /*	DOWNLOAD A FOLDER */
 /*	DOWNLOAD A FOLDER */
 $course_id = api_get_course_int_id();
 $course_id = api_get_course_int_id();
-
 $user_id = api_get_user_id();
 $user_id = api_get_user_id();
 
 
-if (isset($_GET['cat_id']) AND is_numeric($_GET['cat_id']) AND $_GET['action'] == 'downloadcategory' AND isset($_GET['sent_received'])) {
+if (isset($_GET['cat_id']) AND
+    is_numeric($_GET['cat_id']) AND
+    $_GET['action'] == 'downloadcategory' AND
+    isset($_GET['sent_received'])
+) {
     /** step 1: constructing the sql statement.
     /** step 1: constructing the sql statement.
     Due to the nature off the classes of the dropbox the categories for sent files are stored in the table
     Due to the nature off the classes of the dropbox the categories for sent files are stored in the table
     dropbox_file while the categories for the received files are stored in dropbox_post.
     dropbox_file while the categories for the received files are stored in dropbox_post.
@@ -40,7 +43,8 @@ if (isset($_GET['cat_id']) AND is_numeric($_GET['cat_id']) AND $_GET['action'] =
     if ($_GET['sent_received'] == 'sent') {
     if ($_GET['sent_received'] == 'sent') {
         // here we also incorporate the person table to make sure that deleted sent documents are not included.
         // here we also incorporate the person table to make sure that deleted sent documents are not included.
         $sql = "SELECT DISTINCT file.id, file.filename, file.title
         $sql = "SELECT DISTINCT file.id, file.filename, file.title
-                FROM ".$dropbox_cnf['tbl_file']." file INNER JOIN ".$dropbox_cnf['tbl_person']." person
+                FROM ".$dropbox_cnf['tbl_file']." file
+                INNER JOIN ".$dropbox_cnf['tbl_person']." person
                 ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
                 ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
                 WHERE
                 WHERE
                     file.uploader_id = $user_id AND
                     file.uploader_id = $user_id AND
@@ -50,7 +54,8 @@ if (isset($_GET['cat_id']) AND is_numeric($_GET['cat_id']) AND $_GET['action'] =
 
 
     if ($_GET['sent_received'] == 'received') {
     if ($_GET['sent_received'] == 'received') {
         $sql = "SELECT DISTINCT file.id, file.filename, file.title
         $sql = "SELECT DISTINCT file.id, file.filename, file.title
-                FROM ".$dropbox_cnf['tbl_file']." file INNER JOIN ".$dropbox_cnf['tbl_person']." person
+                FROM ".$dropbox_cnf['tbl_file']." file
+                INNER JOIN ".$dropbox_cnf['tbl_person']." person
                 ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
                 ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
                 INNER JOIN ".$dropbox_cnf['tbl_post']." post
                 INNER JOIN ".$dropbox_cnf['tbl_post']." post
                 ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
                 ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
@@ -58,7 +63,7 @@ if (isset($_GET['cat_id']) AND is_numeric($_GET['cat_id']) AND $_GET['action'] =
                     post.cat_id = ".intval($_GET['cat_id'])." AND
                     post.cat_id = ".intval($_GET['cat_id'])." AND
                     post.dest_user_id = $user_id" ;
                     post.dest_user_id = $user_id" ;
     }
     }
-
+    $files_to_download = array();
     $result = Database::query($sql);
     $result = Database::query($sql);
     while ($row = Database::fetch_array($result)) {
     while ($row = Database::fetch_array($result)) {
         $files_to_download[] = $row['id'];
         $files_to_download[] = $row['id'];
@@ -152,4 +157,4 @@ if (!$allowed_to_download) {
     exit();
     exit();
 }
 }
 //@todo clean this file the code below is useless there are 2 exits in previous conditions ... maybe a bad copy/paste/merge?
 //@todo clean this file the code below is useless there are 2 exits in previous conditions ... maybe a bad copy/paste/merge?
-exit;
+exit;

+ 36 - 18
main/dropbox/dropbox_functions.inc.php

@@ -1034,8 +1034,8 @@ function store_feedback()
 }
 }
 
 
 /**
 /**
-* This function downloads all the files of the inputarray into one zip
-* @param $array an array containing all the ids of the files that have to be downloaded.
+* This function downloads all the files of the input array into one zip
+* @param array $fileList containing all the ids of the files that have to be downloaded.
 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
 * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
 * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
 * @todo integrate some cleanup function that removes zip files that are older than 2 days
 * @todo integrate some cleanup function that removes zip files that are older than 2 days
@@ -1044,34 +1044,52 @@ function store_feedback()
 * @author Julio Montoya  Addin c_id support
 * @author Julio Montoya  Addin c_id support
 * @version march 2006
 * @version march 2006
 */
 */
-function zip_download($array) {
+function zip_download($fileList)
+{
 	$_course = api_get_course_info();
 	$_course = api_get_course_info();
     $dropbox_cnf = getDropboxConf();
     $dropbox_cnf = getDropboxConf();
-
     $course_id = api_get_course_int_id();
     $course_id = api_get_course_int_id();
-	$array = array_map('intval', $array);
+    $fileList = array_map('intval', $fileList);
 
 
 	// note: we also have to add the check if the user has received or sent this file.
 	// note: we also have to add the check if the user has received or sent this file.
 	$sql = "SELECT DISTINCT file.filename, file.title, file.author, file.description
 	$sql = "SELECT DISTINCT file.filename, file.title, file.author, file.description
-			FROM ".$dropbox_cnf['tbl_file']." file INNER JOIN ".$dropbox_cnf['tbl_person']." person
+			FROM ".$dropbox_cnf['tbl_file']." file
+			INNER JOIN ".$dropbox_cnf['tbl_person']." person
             ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
             ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
             INNER JOIN ".$dropbox_cnf['tbl_post']." post
             INNER JOIN ".$dropbox_cnf['tbl_post']." post
             ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
             ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
-			WHERE   file.id IN (".implode(', ',$array).") AND
-                    file.id = person.file_id AND
-                    (person.user_id = '".api_get_user_id()."' OR post.dest_user_id = '".api_get_user_id()."' ) ";
+			WHERE
+			    file.id IN (".implode(', ', $fileList).") AND
+                file.id = person.file_id AND
+                (
+                    person.user_id = '".api_get_user_id()."' OR
+                    post.dest_user_id = '".api_get_user_id()."'
+                ) ";
 	$result = Database::query($sql);
 	$result = Database::query($sql);
+
 	$files = array();
 	$files = array();
 	while ($row = Database::fetch_array($result)) {
 	while ($row = Database::fetch_array($result)) {
-		$files[$row['filename']] = array('filename' => $row['filename'],'title' => $row['title'], 'author' => $row['author'], 'description' => $row['description']);
+		$files[$row['filename']] = array(
+            'filename' => $row['filename'],
+            'title' => $row['title'],
+            'author' => $row['author'],
+            'description' => $row['description']
+        );
 	}
 	}
 
 
 	// Step 3: create the zip file and add all the files to it
 	// Step 3: create the zip file and add all the files to it
 	$temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
 	$temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
-	$zip_folder = new PclZip($temp_zip_file);
+    Session::write('dropbox_files_to_download', $files);
+	$zip = new PclZip($temp_zip_file);
 	foreach ($files as $value) {
 	foreach ($files as $value) {
-		$zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/'.$value['filename'], PCLZIP_OPT_REMOVE_ALL_PATH, PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
+        $zip->add(
+            api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/'.$value['filename'],
+            PCLZIP_OPT_REMOVE_ALL_PATH,
+            PCLZIP_CB_PRE_ADD,
+            'my_pre_add_callback'
+        );
 	}
 	}
+    Session::erase('dropbox_files_to_download');
 	$name = 'dropbox-'.api_get_utc_datetime().'.zip';
 	$name = 'dropbox-'.api_get_utc_datetime().'.zip';
 	DocumentManager::file_send_for_download($temp_zip_file, true, $name);
 	DocumentManager::file_send_for_download($temp_zip_file, true, $name);
 	@unlink($temp_zip_file);
 	@unlink($temp_zip_file);
@@ -1080,19 +1098,19 @@ function zip_download($array) {
 
 
 /**
 /**
 * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
 * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
-* @param $p_event a variable of PCLZip
-* @param $p_header a variable of PCLZip
+* @param array $p_event a variable of PCLZip
+* @param array $p_header a variable of PCLZip
 *
 *
 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
 * @version march 2006
 * @version march 2006
 */
 */
-function my_pre_add_callback($p_event, &$p_header) {
-	global $files;
+function my_pre_add_callback($p_event, &$p_header)
+{
+    $files = Session::read('dropbox_files_to_download');
 	$p_header['stored_filename'] = $files[$p_header['stored_filename']]['title'];
 	$p_header['stored_filename'] = $files[$p_header['stored_filename']]['title'];
 	return 1;
 	return 1;
 }
 }
 
 
-
 /**
 /**
  * @desc Generates the contents of a html file that gives an overview of all the files in the zip file.
  * @desc Generates the contents of a html file that gives an overview of all the files in the zip file.
  *		This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
  *		This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
@@ -1222,4 +1240,4 @@ function get_last_tool_access($tool, $course_code = '', $user_id='')
 	$result = Database::query($sql);
 	$result = Database::query($sql);
 	$row = Database::fetch_array($result);
 	$row = Database::fetch_array($result);
 	return $row['access_date'];
 	return $row['access_date'];
-}
+}