downloadfolder.inc.php 7.0 KB

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