downloadfolder.inc.php 8.1 KB

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