downloadfolder.inc.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Functions and main code for the download folder feature.
  5. *
  6. * @todo use ids instead of the path like the document tool
  7. *
  8. * @package chamilo.work
  9. */
  10. $work_id = $_GET['id'];
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. $current_course_tool = TOOL_STUDENTPUBLICATION;
  13. $_course = api_get_course_info();
  14. // Protection
  15. api_protect_course_script(true);
  16. require_once 'work.lib.php';
  17. $work_data = get_work_data_by_id($work_id);
  18. $groupId = api_get_group_id();
  19. if (empty($work_data)) {
  20. exit;
  21. }
  22. // Prevent some stuff.
  23. if (empty($path)) {
  24. $path = '/';
  25. }
  26. if (empty($_course) || empty($_course['path'])) {
  27. api_not_allowed();
  28. }
  29. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  30. // Creating a ZIP file
  31. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().'.zip';
  32. $zip_folder = new PclZip($temp_zip_file);
  33. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  34. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  35. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  36. // Put the files in the zip
  37. // 2 possibilities: admins get all files and folders in the selected folder (except for the deleted ones)
  38. // normal users get only visible files that are in visible folders
  39. //admins are allowed to download invisible files
  40. $files = [];
  41. $course_id = api_get_course_int_id();
  42. $sessionId = api_get_session_id();
  43. $sessionCondition = api_get_session_condition($sessionId, true, false, 'props.session_id');
  44. $filenameCondition = null;
  45. if (array_key_exists('filename', $work_data)) {
  46. $filenameCondition = ", filename";
  47. }
  48. $groupIid = 0;
  49. if ($groupId) {
  50. $groupInfo = GroupManager::get_group_properties($groupId);
  51. $groupIid = $groupInfo['iid'];
  52. }
  53. if (api_is_allowed_to_edit() || api_is_coach()) {
  54. //Search for all files that are not deleted => visibility != 2
  55. $sql = "SELECT DISTINCT
  56. url,
  57. title,
  58. description,
  59. insert_user_id,
  60. sent_date,
  61. contains_file
  62. $filenameCondition
  63. FROM $tbl_student_publication AS work
  64. INNER JOIN $prop_table AS props
  65. ON (work.id = props.ref AND props.c_id = work.c_id)
  66. INNER JOIN $tableUser as u
  67. ON (
  68. work.user_id = u.user_id
  69. )
  70. WHERE
  71. props.tool = 'work' AND
  72. props.c_id = $course_id AND
  73. work.c_id = $course_id AND
  74. work.parent_id = $work_id AND
  75. work.filetype = 'file' AND
  76. props.visibility <> '2' AND
  77. work.active IN (0, 1) AND
  78. work.post_group_id = $groupIid
  79. $sessionCondition
  80. ";
  81. } else {
  82. $courseInfo = api_get_course_info();
  83. protectWork($courseInfo, $work_id);
  84. $userCondition = null;
  85. // All users
  86. if ($courseInfo['show_score'] == 0) {
  87. // Do another filter
  88. } else {
  89. // Only teachers
  90. $userCondition = " AND props.insert_user_id = ".api_get_user_id();
  91. }
  92. //for other users, we need to create a zipfile with only visible files and folders
  93. $sql = "SELECT DISTINCT
  94. url,
  95. title,
  96. description,
  97. insert_user_id,
  98. sent_date,
  99. contains_file
  100. $filenameCondition
  101. FROM $tbl_student_publication AS work
  102. INNER JOIN $prop_table AS props
  103. ON (
  104. props.c_id = work.c_id AND
  105. work.id = props.ref
  106. )
  107. WHERE
  108. props.c_id = $course_id AND
  109. work.c_id = $course_id AND
  110. props.tool = 'work' AND
  111. work.accepted = 1 AND
  112. work.active = 1 AND
  113. work.parent_id = $work_id AND
  114. work.filetype = 'file' AND
  115. props.visibility = '1' AND
  116. work.post_group_id = $groupIid
  117. $userCondition
  118. ";
  119. }
  120. $query = Database::query($sql);
  121. //add tem to the zip file
  122. while ($not_deleted_file = Database::fetch_assoc($query)) {
  123. $user_info = api_get_user_info($not_deleted_file['insert_user_id']);
  124. $insert_date = api_get_local_time($not_deleted_file['sent_date']);
  125. $insert_date = str_replace([':', '-', ' '], '_', $insert_date);
  126. $title = basename($not_deleted_file['title']);
  127. if (!empty($filenameCondition)) {
  128. if (isset($not_deleted_file['filename']) && !empty($not_deleted_file['filename'])) {
  129. $title = $not_deleted_file['filename'];
  130. }
  131. }
  132. $filename = $insert_date.'_'.$user_info['username'].'_'.$title;
  133. // File exists
  134. if (file_exists($sys_course_path.$_course['path'].'/'.$not_deleted_file['url']) &&
  135. !empty($not_deleted_file['url'])
  136. ) {
  137. $files[basename($not_deleted_file['url'])] = $filename;
  138. $addStatus = $zip_folder->add(
  139. $sys_course_path.$_course['path'].'/'.$not_deleted_file['url'],
  140. PCLZIP_OPT_REMOVE_PATH,
  141. $sys_course_path.$_course['path'].'/work',
  142. PCLZIP_CB_PRE_ADD,
  143. 'my_pre_add_callback'
  144. );
  145. } else {
  146. // Convert texts in html files
  147. $filename = trim($filename).".html";
  148. $work_temp = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().'_'.$filename;
  149. file_put_contents($work_temp, $not_deleted_file['description']);
  150. $files[basename($work_temp)] = $filename;
  151. $addStatus = $zip_folder->add(
  152. $work_temp,
  153. PCLZIP_OPT_REMOVE_PATH,
  154. api_get_path(SYS_ARCHIVE_PATH),
  155. PCLZIP_CB_PRE_ADD,
  156. 'my_pre_add_callback'
  157. );
  158. @unlink($work_temp);
  159. }
  160. }
  161. if (!empty($files)) {
  162. $fileName = api_replace_dangerous_char($work_data['title']);
  163. // Logging
  164. Event::event_download($fileName.'.zip (folder)');
  165. //start download of created file
  166. $name = $fileName.'.zip';
  167. if (Security::check_abs_path($temp_zip_file, api_get_path(SYS_ARCHIVE_PATH))) {
  168. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  169. @unlink($temp_zip_file);
  170. exit;
  171. }
  172. } else {
  173. exit;
  174. }
  175. /* Extra function (only used here) */
  176. function my_pre_add_callback($p_event, &$p_header)
  177. {
  178. global $files;
  179. if (isset($files[basename($p_header['stored_filename'])])) {
  180. $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])];
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * Return the difference between two arrays, as an array of those key/values
  187. * Use this as array_diff doesn't give the.
  188. *
  189. * @param array $arr1 first array
  190. * @param array $arr2 second array
  191. *
  192. * @return array difference between the two arrays
  193. */
  194. function diff($arr1, $arr2)
  195. {
  196. $res = [];
  197. $r = 0;
  198. foreach ($arr1 as $av) {
  199. if (!in_array($av, $arr2)) {
  200. $res[$r] = $av;
  201. $r++;
  202. }
  203. }
  204. return $res;
  205. }