downloadfolder.inc.php 6.7 KB

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