downloadfolder.inc.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $work_id = $_GET['id'];
  9. require_once '../inc/global.inc.php';
  10. $current_course_tool = TOOL_STUDENTPUBLICATION;
  11. //protection
  12. api_protect_course_script(true);
  13. require_once 'work.lib.php';
  14. $work_data = get_work_data_by_id($work_id);
  15. $groupId = api_get_group_id();
  16. if (empty($work_data)) {
  17. exit;
  18. }
  19. //prevent some stuff
  20. if (empty($path)) {
  21. $path = '/';
  22. }
  23. if (empty($_course) || empty($_course['path'])) {
  24. api_not_allowed();
  25. }
  26. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  27. //zip library for creation of the zipfile
  28. require_once 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. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  33. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  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. $files = array();
  39. $course_id = api_get_course_int_id();
  40. if (api_is_allowed_to_edit()) {
  41. //Search for all files that are not deleted => visibility != 2
  42. $sql = "SELECT DISTINCT url, title, description, insert_user_id, insert_date, contains_file
  43. FROM $tbl_student_publication AS work INNER JOIN $prop_table AS props
  44. ON (
  45. props.c_id = $course_id AND
  46. work.c_id = $course_id AND
  47. work.id = props.ref
  48. )
  49. WHERE props.tool='work' AND
  50. work.parent_id = $work_id AND
  51. work.filetype = 'file' AND
  52. props.visibility<>'2' AND
  53. work.active = 1 AND
  54. work.post_group_id = $groupId
  55. ";
  56. } else {
  57. $courseInfo = api_get_course_info();
  58. allowOnlySubscribedUser(api_get_user_id(), $work_id, $courseInfo['real_id']);
  59. $userCondition = null;
  60. // All users
  61. if ($courseInfo['show_score'] == 0) {
  62. // Do another filter
  63. } else {
  64. // Only teachers
  65. $userCondition = " AND props.insert_user_id = ".api_get_user_id();
  66. }
  67. //for other users, we need to create a zipfile with only visible files and folders
  68. $sql = "SELECT DISTINCT url, title, description, insert_user_id, insert_date, contains_file
  69. FROM $tbl_student_publication AS work INNER JOIN $prop_table AS props
  70. ON (props.c_id = $course_id AND
  71. work.c_id = $course_id AND
  72. work.id = props.ref)
  73. WHERE
  74. props.tool='work' AND
  75. work.accepted = 1 AND
  76. work.active = 1 AND
  77. work.parent_id = $work_id AND
  78. work.filetype = 'file' AND
  79. props.visibility = '1' AND
  80. work.post_group_id = $groupId
  81. $userCondition
  82. ";
  83. }
  84. $query = Database::query($sql);
  85. //add tem to the zip file
  86. while ($not_deleted_file = Database::fetch_assoc($query)) {
  87. $user_info = api_get_user_info($not_deleted_file['insert_user_id']);
  88. $insert_date = api_get_local_time($not_deleted_file['insert_date']);
  89. $insert_date = str_replace(array(':','-', ' '), '_', $insert_date);
  90. $filename = $insert_date.'_'.$user_info['username'].'_'.basename($not_deleted_file['title']);
  91. if (file_exists($sys_course_path.$_course['path'].'/'.$not_deleted_file['url']) && !empty($not_deleted_file['url'])) {
  92. $files[basename($not_deleted_file['url'])] = $filename;
  93. $zip_folder->add($sys_course_path.$_course['path'].'/'.$not_deleted_file['url'], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/work', PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
  94. }
  95. //Convert texts in html files
  96. if ($not_deleted_file['contains_file'] == 0) {
  97. $filename = trim($filename).".html";
  98. $work_temp = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().'_'.$filename;
  99. file_put_contents($work_temp, $not_deleted_file['description']);
  100. $files[basename($work_temp)] = $filename;
  101. $zip_folder->add($work_temp, PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_ARCHIVE_PATH), PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
  102. @unlink($work_temp);
  103. }
  104. }
  105. if (!empty($files)) {
  106. //logging
  107. event_download(basename($work_data['title']).'.zip (folder)');
  108. //start download of created file
  109. $name = basename($work_data['title']).'.zip';
  110. if (Security::check_abs_path($temp_zip_file, api_get_path(SYS_ARCHIVE_PATH))) {
  111. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  112. @unlink($temp_zip_file);
  113. exit;
  114. }
  115. } else {
  116. exit;
  117. }
  118. /* Extra function (only used here) */
  119. function my_pre_add_callback($p_event, &$p_header) {
  120. global $files;
  121. if (isset($files[basename($p_header['stored_filename'])])) {
  122. $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])];
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. /**
  128. * Return the difference between two arrays, as an array of those key/values
  129. * Use this as array_diff doesn't give the
  130. *
  131. * @param array $arr1 first array
  132. * @param array $arr2 second array
  133. * @return difference between the two arrays
  134. */
  135. function diff($arr1, $arr2) {
  136. $res = array();
  137. $r = 0;
  138. foreach ($arr1 as $av) {
  139. if (!in_array($av, $arr2)) {
  140. $res[$r] = $av;
  141. $r++;
  142. }
  143. }
  144. return $res;
  145. }