downloadfolder.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $_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. //zip library for creation of the zipfile
  29. require_once api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  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 = array();
  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. if (api_is_allowed_to_edit() || api_is_coach()) {
  49. //Search for all files that are not deleted => visibility != 2
  50. $sql = "SELECT DISTINCT
  51. url,
  52. title,
  53. description,
  54. insert_user_id,
  55. insert_date,
  56. contains_file
  57. $filenameCondition
  58. FROM $tbl_student_publication AS work
  59. INNER JOIN $prop_table AS props
  60. INNER JOIN $tableUser as u
  61. ON (
  62. props.c_id = $course_id AND
  63. work.c_id = $course_id AND
  64. work.id = props.ref AND
  65. props.tool='work' AND
  66. work.user_id = u.user_id
  67. )
  68. WHERE
  69. work.parent_id = $work_id AND
  70. work.filetype = 'file' AND
  71. props.visibility <> '2' AND
  72. work.active IN (0, 1) AND
  73. work.post_group_id = $groupId
  74. $sessionCondition
  75. ";
  76. } else {
  77. $courseInfo = api_get_course_info();
  78. protectWork($courseInfo, $work_id);
  79. $userCondition = null;
  80. // All users
  81. if ($courseInfo['show_score'] == 0) {
  82. // Do another filter
  83. } else {
  84. // Only teachers
  85. $userCondition = " AND props.insert_user_id = ".api_get_user_id();
  86. }
  87. //for other users, we need to create a zipfile with only visible files and folders
  88. $sql = "SELECT DISTINCT
  89. url,
  90. title,
  91. description,
  92. insert_user_id,
  93. insert_date,
  94. contains_file
  95. $filenameCondition
  96. FROM $tbl_student_publication AS work
  97. INNER JOIN $prop_table AS props
  98. ON (props.c_id = $course_id AND
  99. work.c_id = $course_id AND
  100. work.id = props.ref)
  101. WHERE
  102. props.tool='work' AND
  103. work.accepted = 1 AND
  104. work.active = 1 AND
  105. work.parent_id = $work_id AND
  106. work.filetype = 'file' AND
  107. props.visibility = '1' AND
  108. work.post_group_id = $groupId
  109. $userCondition
  110. ";
  111. }
  112. $query = Database::query($sql);
  113. //add tem to the zip file
  114. while ($not_deleted_file = Database::fetch_assoc($query)) {
  115. $user_info = api_get_user_info($not_deleted_file['insert_user_id']);
  116. $insert_date = api_get_local_time($not_deleted_file['insert_date']);
  117. $insert_date = str_replace(array(':', '-', ' '), '_', $insert_date);
  118. $title = basename($not_deleted_file['title']);
  119. if (!empty($filenameCondition)) {
  120. if (isset($not_deleted_file['filename']) && !empty($not_deleted_file['filename'])) {
  121. $title = $not_deleted_file['filename'];
  122. }
  123. }
  124. $filename = $insert_date.'_'.$user_info['username'].'_'.$title;
  125. // File exists
  126. if (file_exists($sys_course_path.$_course['path'].'/'.$not_deleted_file['url']) &&
  127. !empty($not_deleted_file['url'])
  128. ) {
  129. $files[basename($not_deleted_file['url'])] = $filename;
  130. $addStatus = $zip_folder->add(
  131. $sys_course_path.$_course['path'].'/'.$not_deleted_file['url'],
  132. PCLZIP_OPT_REMOVE_PATH,
  133. $sys_course_path.$_course['path'].'/work',
  134. PCLZIP_CB_PRE_ADD,
  135. 'my_pre_add_callback'
  136. );
  137. } else {
  138. // Convert texts in html files
  139. //if ($not_deleted_file['contains_file'] == 0) {
  140. $filename = trim($filename).".html";
  141. $work_temp = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().'_'.$filename;
  142. file_put_contents($work_temp, $not_deleted_file['description']);
  143. $files[basename($work_temp)] = $filename;
  144. $addStatus = $zip_folder->add(
  145. $work_temp,
  146. PCLZIP_OPT_REMOVE_PATH,
  147. api_get_path(SYS_ARCHIVE_PATH),
  148. PCLZIP_CB_PRE_ADD,
  149. 'my_pre_add_callback'
  150. );
  151. @unlink($work_temp);
  152. }
  153. }
  154. if (!empty($files)) {
  155. $fileName = api_replace_dangerous_char($work_data['title']);
  156. // Logging
  157. Event::event_download($fileName .'.zip (folder)');
  158. //start download of created file
  159. $name = $fileName .'.zip';
  160. if (Security::check_abs_path($temp_zip_file, api_get_path(SYS_ARCHIVE_PATH))) {
  161. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  162. @unlink($temp_zip_file);
  163. exit;
  164. }
  165. } else {
  166. exit;
  167. }
  168. /* Extra function (only used here) */
  169. function my_pre_add_callback($p_event, &$p_header)
  170. {
  171. global $files;
  172. if (isset($files[basename($p_header['stored_filename'])])) {
  173. $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])];
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. /**
  179. * Return the difference between two arrays, as an array of those key/values
  180. * Use this as array_diff doesn't give the
  181. *
  182. * @param array $arr1 first array
  183. * @param array $arr2 second array
  184. *
  185. * @return array difference between the two arrays
  186. */
  187. function diff($arr1, $arr2)
  188. {
  189. $res = array();
  190. $r = 0;
  191. foreach ($arr1 as $av) {
  192. if (!in_array($av, $arr2)) {
  193. $res[$r] = $av;
  194. $r++;
  195. }
  196. }
  197. return $res;
  198. }