downloadfolder.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. //protection
  12. api_protect_course_script(true);
  13. require_once 'work.lib.php';
  14. $work_data = get_work_data_by_id($work_id);
  15. if (empty($work_data)) {
  16. exit;
  17. }
  18. //prevent some stuff
  19. if (empty($path)) {
  20. $path = '/';
  21. }
  22. if (empty($_course) || empty($_course['path'])) {
  23. api_not_allowed();
  24. }
  25. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  26. //Creating a ZIP file
  27. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  28. $zip_folder = new PclZip($temp_zip_file);
  29. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  30. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  31. //Put the files in the zip
  32. //2 possibilities: admins get all files and folders in the selected folder (except for the deleted ones)
  33. //normal users get only visible files that are in visible folders
  34. //admins are allowed to download invisible files
  35. $files = array();
  36. $course_id = api_get_course_int_id();
  37. if (api_is_allowed_to_edit()) {
  38. //Search for all files that are not deleted => visibility != 2
  39. $sql = "SELECT url, title, description, insert_user_id, insert_date, contains_file
  40. FROM $tbl_student_publication AS work INNER JOIN $prop_table AS props
  41. ON (props.c_id = $course_id AND
  42. work.c_id = $course_id AND
  43. work.id = props.ref)
  44. WHERE props.tool='work' AND
  45. work.parent_id = $work_id AND
  46. work.filetype = 'file' AND
  47. props.visibility<>'2' ";
  48. } else {
  49. //for other users, we need to create a zipfile with only visible files and folders
  50. $sql = "SELECT url, title, description, insert_user_id, insert_date, contains_file
  51. FROM $tbl_student_publication AS work INNER JOIN $prop_table AS props
  52. ON (props.c_id = $course_id AND
  53. work.c_id = $course_id AND
  54. work.id = props.ref)
  55. WHERE
  56. props.tool='work' AND
  57. work.accepted = 1 AND
  58. work.parent_id = $work_id AND
  59. work.filetype='file' AND
  60. props.visibility = '1' AND
  61. props.insert_user_id='".api_get_user_id()."'
  62. ";
  63. }
  64. $query = Database::query($sql);
  65. //add tem to the zip file
  66. while ($not_deleted_file = Database::fetch_assoc($query)) {
  67. $user_info = api_get_user_info($not_deleted_file['insert_user_id']);
  68. $insert_date = api_get_local_time($not_deleted_file['insert_date']);
  69. $insert_date = str_replace(array(':','-', ' '), '_', $insert_date);
  70. $filename = $insert_date.'_'.$user_info['username'].'_'.basename($not_deleted_file['title']);
  71. if (file_exists($sys_course_path.$_course['path'].'/'.$not_deleted_file['url']) && !empty($not_deleted_file['url'])) {
  72. $files[basename($not_deleted_file['url'])] = $filename;
  73. $zip_folder->add($sys_course_path.$_course['path'].'/'.$not_deleted_file['url'], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/work', PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
  74. }
  75. //Convert texts in html files
  76. if ($not_deleted_file['contains_file'] == 0) {
  77. $filename = trim($filename).".html";
  78. $work_temp = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().'_'.$filename;
  79. file_put_contents($work_temp, $not_deleted_file['description']);
  80. $files[basename($work_temp)] = $filename;
  81. $zip_folder->add($work_temp, PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_ARCHIVE_PATH), PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
  82. @unlink($work_temp);
  83. }
  84. }
  85. if (!empty($files)) {
  86. //logging
  87. event_download(basename($work_data['title']).'.zip (folder)');
  88. //start download of created file
  89. $name = basename($work_data['title']).'.zip';
  90. if (Security::check_abs_path($temp_zip_file, api_get_path(SYS_ARCHIVE_PATH))) {
  91. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  92. @unlink($temp_zip_file);
  93. exit;
  94. }
  95. } else {
  96. exit;
  97. }
  98. /* Extra function (only used here) */
  99. function my_pre_add_callback($p_event, &$p_header) {
  100. global $files;
  101. if (isset($files[basename($p_header['stored_filename'])])) {
  102. $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])];
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. /**
  108. * Return the difference between two arrays, as an array of those key/values
  109. * Use this as array_diff doesn't give the
  110. *
  111. * @param array $arr1 first array
  112. * @param array $arr2 second array
  113. * @return difference between the two arrays
  114. */
  115. function diff($arr1, $arr2) {
  116. $res = array();
  117. $r = 0;
  118. foreach ($arr1 as $av) {
  119. if (!in_array($av, $arr2)) {
  120. $res[$r] = $av;
  121. $r++;
  122. }
  123. }
  124. return $res;
  125. }