downloadfolder.inc.php 6.6 KB

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