downloadfolder.inc.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Functions and main code for the download folder feature
  5. * @todo use ids instead of the path like the document tool
  6. * @package chamilo.work
  7. */
  8. $path = $_GET['path'];
  9. //prevent some stuff
  10. if (empty($path)) {
  11. $path = '/';
  12. }
  13. if (empty($_course) || empty($_course['path'])) {
  14. api_not_allowed();
  15. }
  16. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  17. //zip library for creation of the zipfile
  18. require api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  19. //Creating a ZIP file
  20. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  21. $zip_folder = new PclZip($temp_zip_file);
  22. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  23. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  24. //Put the files in the zip
  25. //2 possibilities: admins get all files and folders in the selected folder (except for the deleted ones)
  26. //normal users get only visible files that are in visible folders
  27. //admins are allowed to download invisible files
  28. if (api_is_allowed_to_edit()) {
  29. //set the path that will be used in the query
  30. if ($path == '/') {
  31. $querypath = ''; // to prevent ...path LIKE '//%'... in query
  32. } else {
  33. $querypath = $path;
  34. }
  35. //search for all files that are not deleted => visibility != 2
  36. $querypath = Database::escape_string($querypath);
  37. $query = Database::query("SELECT url FROM $tbl_student_publication AS work, $prop_table AS props
  38. WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility<>'2'");
  39. //add tem to the zip file
  40. while ($not_deleted_file = Database::fetch_assoc($query)) {
  41. $zip_folder->add($sys_course_path.$_course['path'].'/'.$not_deleted_file['url'], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/work');
  42. }
  43. } else {
  44. //for other users, we need to create a zipfile with only visible files and folders
  45. if ($path == '/') {
  46. $querypath = ''; // to prevent ...path LIKE '//%'... in query
  47. } else {
  48. $querypath = $path;
  49. }
  50. $querypath = Database::escape_string($querypath);
  51. //big problem: visible files that are in a hidden folder are included when we do a query for visiblity='v'!!!
  52. //so... I do it in a couple of steps:
  53. //1st: get all files that are visible in the given path
  54. $query = Database::query("SELECT url FROM $tbl_student_publication AS work, $prop_table AS props
  55. WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility='1' AND props.lastedit_user_id='".api_get_user_id()."'");
  56. //add them to an array
  57. $all_visible_files_path = array();
  58. while ($all_visible_files = Database::fetch_assoc($query)) {
  59. $all_visible_files_path[] = $all_visible_files['url'];
  60. }
  61. //2nd: get all folders that are invisible in the given path
  62. $query2 = Database::query("SELECT url FROM $tbl_student_publication AS work, $prop_table AS props
  63. WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility<>'1' AND props.lastedit_user_id='".api_get_user_id()."'");
  64. //if we get invisible folders, we have to filter out these results from all visible files we found
  65. if (Database::num_rows($query2) > 0) {
  66. //add tem to an array
  67. while ($invisible_folders = Database::fetch_assoc($query2)) {
  68. //3rd: get all files that are in the found invisible folder (these are "invisible" too)
  69. $query3 = Database::query("SELECT url FROM $tbl_student_publication AS work, $prop_table AS props
  70. WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".Database::escape_string($invisible_folders['path'])."/%' AND work.filetype='file' AND props.visibility='1' AND props.lastedit_user_id='".api_get_user_id()."'");
  71. //add tem to an array
  72. while ($files_in_invisible_folder = Database::fetch_assoc($query3)) {
  73. $files_in_invisible_folder_path[] = $files_in_invisible_folder['url'];
  74. }
  75. }
  76. //compare the array with visible files and the array with files in invisible folders
  77. //and keep the difference (= all visible files that are not in an invisible folder)
  78. $files_for_zipfile = diff((array) $all_visible_files_path, (array) $files_in_invisible_folder_path);
  79. } else {
  80. //no invisible folders found, so all visible files can be added to the zipfile
  81. $files_for_zipfile = $all_visible_files_path;
  82. }
  83. //add all files in our final array to the zipfile
  84. for ($i=0;$i<count($files_for_zipfile);$i++) {
  85. $zip_folder->add($sys_course_path.$_course['path'].'/'.$files_for_zipfile[$i], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/work'.$remove_dir);
  86. }
  87. }//end for other users
  88. //logging
  89. event_download(basename($path).'.zip (folder)');
  90. //start download of created file
  91. $name = basename($path).'.zip';
  92. if (Security::check_abs_path($temp_zip_file, api_get_path(SYS_ARCHIVE_PATH))) {
  93. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  94. @unlink($temp_zip_file);
  95. exit;
  96. }
  97. /* Extra function (only used here) */
  98. /**
  99. * Return the difference between two arrays, as an array of those key/values
  100. * Use this as array_diff doesn't give the
  101. *
  102. * @param array $arr1 first array
  103. * @param array $arr2 second array
  104. * @return difference between the two arrays
  105. */
  106. function diff($arr1, $arr2) {
  107. $res = array();
  108. $r = 0;
  109. foreach ($arr1 as $av) {
  110. if (!in_array($av, $arr2)) {
  111. $res[$r] = $av;
  112. $r++;
  113. }
  114. }
  115. return $res;
  116. }