downloadfolder.inc.php 6.8 KB

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