downloadfolder.inc.php 5.0 KB

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