downloadfolder.inc.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Functions and main code for the download folder feature
  5. *
  6. * @package chamilo.document
  7. */
  8. /**
  9. * Code
  10. */
  11. set_time_limit(0);
  12. require_once '../inc/global.inc.php';
  13. $document_data = DocumentManager::get_document_data_by_id($_GET['id'], api_get_course_id());
  14. $path = $document_data['path'];
  15. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  16. if (empty($path)) {
  17. $path = '/';
  18. }
  19. if (empty($document_data)) {
  20. //api_not_allowed();
  21. }
  22. //a student should not be able to download a root shared directory
  23. if (($path == '/shared_folder' || $path=='/shared_folder_session_'.api_get_session_id()) && (!api_is_allowed_to_edit() || !api_is_platform_admin())){
  24. echo '<div align="center">';
  25. Display::display_error_message(get_lang('NotAllowedClickBack'));
  26. echo '</div>';
  27. exit;
  28. }
  29. //zip library for creation of the zipfile
  30. require api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  31. //Creating a ZIP file
  32. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  33. $zip_folder = new PclZip($temp_zip_file);
  34. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  35. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  36. $course_id = api_get_course_int_id();
  37. $session_id = api_get_session_id();
  38. //$to_group_id = api_get_group_id(); variable loaded in document.php
  39. // We need this path to clean it out of the zip file
  40. // I'm not using dirname as it gives too much problems (cfr.)
  41. $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
  42. // Put the files in the zip
  43. // 2 possibilities: Admins get all files and folders in the selected folder (except for the deleted ones)
  44. // Normal users get only visible files that are in visible folders
  45. // Admins are allowed to download invisible files
  46. if (api_is_allowed_to_edit()) {
  47. // Set the path that will be used in the query
  48. if ($path == '/') {
  49. $querypath = ''; // To prevent ...path LIKE '//%'... in query
  50. } else {
  51. $querypath = $path;
  52. }
  53. $querypath = Database::escape_string($querypath);
  54. // Search for all files that are not deleted => visibility != 2
  55. $sql = "SELECT path FROM $doc_table AS docs, $prop_table AS props
  56. WHERE props.tool ='".TOOL_DOCUMENT."' AND
  57. docs.id = props.ref AND
  58. docs.path LIKE '".$querypath."/%' AND
  59. docs.filetype = 'file' AND props.visibility<>'2' AND
  60. props.to_group_id = ".$to_group_id." AND
  61. props.c_id = ".$course_id." AND
  62. docs.c_id = ".$course_id." ";
  63. $query = Database::query($sql);
  64. // Add tem to the zip file
  65. while ($not_deleted_file = Database::fetch_assoc($query)) {
  66. $zip_folder->add($sys_course_path.$_course['path'].'/document'.$not_deleted_file['path'], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/document'.$remove_dir);
  67. }
  68. } else {
  69. // For other users, we need to create a zipfile with only visible files and folders
  70. if ($path == '/') {
  71. $querypath = ''; // To prevent ...path LIKE '//%'... in query
  72. } else {
  73. $querypath = $path;
  74. }
  75. // A big problem: Visible files that are in a hidden folder are included when we do a query for visiblity='v'
  76. // So... I do it in a couple of steps:
  77. // 1st: Get all files that are visible in the given path
  78. $querypath = Database::escape_string($querypath);
  79. $query = Database::query("SELECT path FROM $doc_table AS docs, $prop_table AS props
  80. WHERE docs.c_id = $course_id AND
  81. props.c_id = $course_id AND
  82. props.tool = '".TOOL_DOCUMENT."' AND
  83. docs.id = props.ref AND
  84. docs.path LIKE '".$querypath."/%' AND
  85. props.visibility = '1' AND
  86. docs.filetype = 'file' AND
  87. props.id_session IN ('0', '$session_id') AND
  88. props.to_group_id = ".$to_group_id);
  89. // Add them to an array
  90. while ($all_visible_files = Database::fetch_assoc($query)) {
  91. $all_visible_files_path[] = $all_visible_files['path'];
  92. }
  93. // 2nd: Get all folders that are invisible in the given path
  94. $query2 = Database::query("SELECT path FROM $doc_table AS docs, $prop_table AS props
  95. WHERE docs.c_id = $course_id AND
  96. props.c_id = $course_id AND
  97. props.tool = '".TOOL_DOCUMENT."' AND
  98. docs.id = props.ref AND
  99. docs.path LIKE '".$querypath."/%' AND
  100. props.visibility <> '1' AND
  101. props.id_session IN ('0', '$session_id') AND
  102. docs.filetype = 'folder'");
  103. // If we get invisible folders, we have to filter out these results from all visible files we found
  104. if (Database::num_rows($query2) > 0) {
  105. // Add tem to an array
  106. while ($invisible_folders = Database::fetch_assoc($query2)) {
  107. //3rd: Get all files that are in the found invisible folder (these are "invisible" too)
  108. //echo "<br /><br />invisible folders: ".$sys_course_path.$_course['path'].'/document'.$invisible_folders['path'].'<br />';
  109. $query3 = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props
  110. WHERE docs.c_id = $course_id AND
  111. props.c_id = $course_id AND
  112. props.tool ='".TOOL_DOCUMENT."' AND
  113. docs.id = props.ref AND
  114. docs.path LIKE '".$invisible_folders['path']."/%' AND
  115. docs.filetype ='file' AND
  116. props.id_session IN ('0', '$session_id') AND
  117. props.visibility ='1'");
  118. // Add tem to an array
  119. while ($files_in_invisible_folder = Database::fetch_assoc($query3)) {
  120. $files_in_invisible_folder_path[] = $files_in_invisible_folder['path'];
  121. //echo '<br /><br />files in invisible folders: '.$sys_course_path.$_course['path'].'/document'.$files_in_invisible_folder['path'].' <b>id '.$files_in_invisible_folder['id'].'</b><br />';
  122. }
  123. }
  124. // Compare the array with visible files and the array with files in invisible folders
  125. // and keep the difference (= all visible files that are not in an invisible folder)
  126. $files_for_zipfile = diff((array)$all_visible_files_path, (array)$files_in_invisible_folder_path);
  127. }
  128. // No invisible folders found, so all visible files can be added to the zipfile
  129. else {
  130. $files_for_zipfile = $all_visible_files_path;
  131. }
  132. // Add all files in our final array to the zipfile
  133. for ($i = 0; $i < count($files_for_zipfile); $i++) {
  134. $zip_folder->add($sys_course_path.$_course['path'].'/document'.$files_for_zipfile[$i], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/document'.$remove_dir);
  135. }
  136. } // end for other users
  137. // Launch event
  138. event_download(($path == '/') ? 'documents.zip (folder)' : basename($path).'.zip (folder)');
  139. // Start download of created file
  140. $name = ($path == '/') ? 'documents.zip' : $document_data['title'].'.zip';
  141. if (Security::check_abs_path($temp_zip_file, api_get_path(SYS_ARCHIVE_PATH))) {
  142. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  143. @unlink($temp_zip_file);
  144. exit;
  145. }
  146. /**
  147. * Returns the difference between two arrays, as an array of those key/values
  148. * Use this as array_diff doesn't give the
  149. *
  150. * @param array $arr1 first array
  151. * @param array $arr2 second array
  152. * @return difference between the two arrays
  153. */
  154. function diff($arr1, $arr2) {
  155. $res = array();
  156. $r = 0;
  157. foreach ($arr1 as & $av) {
  158. if (!in_array($av, $arr2)) {
  159. $res[$r] = $av;
  160. $r++;
  161. }
  162. }
  163. return $res;
  164. }