downloadfolder.inc.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 __DIR__.'/../inc/global.inc.php';
  10. $current_course_tool = TOOL_STUDENTPUBLICATION;
  11. $_course = api_get_course_info();
  12. // Protection
  13. api_protect_course_script(true);
  14. require_once 'work.lib.php';
  15. $work_data = get_work_data_by_id($work_id);
  16. $groupId = api_get_group_id();
  17. if (empty($work_data)) {
  18. exit;
  19. }
  20. // Prevent some stuff.
  21. if (empty($path)) {
  22. $path = '/';
  23. }
  24. if (empty($_course) || empty($_course['path'])) {
  25. api_not_allowed();
  26. }
  27. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  28. // Creating a ZIP file
  29. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  30. $zip_folder = new PclZip($temp_zip_file);
  31. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  32. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  33. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  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. $sessionId = api_get_session_id();
  41. $sessionCondition = api_get_session_condition(
  42. $sessionId,
  43. true,
  44. false,
  45. 'props.session_id'
  46. );
  47. $filenameCondition = null;
  48. if (array_key_exists('filename', $work_data)) {
  49. $filenameCondition = ", filename";
  50. }
  51. $groupIid = 0;
  52. if ($groupId) {
  53. $groupInfo = GroupManager::get_group_properties($groupId);
  54. $groupIid = $groupInfo['iid'];
  55. }
  56. if (api_is_allowed_to_edit() || api_is_coach()) {
  57. //Search for all files that are not deleted => visibility != 2
  58. $sql = "SELECT DISTINCT
  59. url,
  60. title,
  61. description,
  62. insert_user_id,
  63. sent_date,
  64. contains_file
  65. $filenameCondition
  66. FROM $tbl_student_publication AS work
  67. INNER JOIN $prop_table AS props
  68. ON (work.id = props.ref AND props.c_id = work.c_id)
  69. INNER JOIN $tableUser as u
  70. ON (
  71. work.user_id = u.user_id
  72. )
  73. WHERE
  74. props.tool = 'work' AND
  75. props.c_id = $course_id AND
  76. work.c_id = $course_id AND
  77. work.parent_id = $work_id AND
  78. work.filetype = 'file' AND
  79. props.visibility <> '2' AND
  80. work.active IN (0, 1) AND
  81. work.post_group_id = $groupIid
  82. $sessionCondition
  83. ";
  84. } else {
  85. $courseInfo = api_get_course_info();
  86. protectWork($courseInfo, $work_id);
  87. $userCondition = null;
  88. // All users
  89. if ($courseInfo['show_score'] == 0) {
  90. // Do another filter
  91. } else {
  92. // Only teachers
  93. $userCondition = " AND props.insert_user_id = ".api_get_user_id();
  94. }
  95. //for other users, we need to create a zipfile with only visible files and folders
  96. $sql = "SELECT DISTINCT
  97. url,
  98. title,
  99. description,
  100. insert_user_id,
  101. sent_date,
  102. contains_file
  103. $filenameCondition
  104. FROM $tbl_student_publication AS work
  105. INNER JOIN $prop_table AS props
  106. ON (
  107. props.c_id = work.c_id AND
  108. work.id = props.ref
  109. )
  110. WHERE
  111. props.c_id = $course_id AND
  112. work.c_id = $course_id AND
  113. props.tool = 'work' AND
  114. work.accepted = 1 AND
  115. work.active = 1 AND
  116. work.parent_id = $work_id AND
  117. work.filetype = 'file' AND
  118. props.visibility = '1' AND
  119. work.post_group_id = $groupIid
  120. $userCondition
  121. ";
  122. }
  123. $query = Database::query($sql);
  124. //add tem to the zip file
  125. while ($not_deleted_file = Database::fetch_assoc($query)) {
  126. $user_info = api_get_user_info($not_deleted_file['insert_user_id']);
  127. $insert_date = api_get_local_time($not_deleted_file['sent_date']);
  128. $insert_date = str_replace(array(':', '-', ' '), '_', $insert_date);
  129. $title = basename($not_deleted_file['title']);
  130. if (!empty($filenameCondition)) {
  131. if (isset($not_deleted_file['filename']) && !empty($not_deleted_file['filename'])) {
  132. $title = $not_deleted_file['filename'];
  133. }
  134. }
  135. $filename = $insert_date.'_'.$user_info['username'].'_'.$title;
  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 = array();
  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. }