downloadfolder.inc.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 FROM $tbl_student_publication AS work, $prop_table AS props
  42. WHERE props.c_id = $course_id AND
  43. work.c_id = $course_id AND
  44. props.tool='work' AND
  45. work.id=props.ref AND
  46. work.parent_id = $work_id AND
  47. work.filetype='file' AND props.visibility<>'2'";
  48. $query = Database::query($sql);
  49. //add tem to the zip file
  50. while ($not_deleted_file = Database::fetch_assoc($query)) {
  51. if (file_exists($sys_course_path.$_course['path'].'/'.$not_deleted_file['url'])) {
  52. $files[basename($not_deleted_file['url'])] = $not_deleted_file['title'];
  53. $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');
  54. }
  55. }
  56. } else {
  57. //for other users, we need to create a zipfile with only visible files and folders
  58. $sql = "SELECT url, title FROM $tbl_student_publication AS work, $prop_table AS props
  59. WHERE props.c_id = $course_id AND work.c_id = $course_id AND
  60. props.tool='work' AND
  61. work.accepted = 1 AND
  62. work.id=props.ref AND
  63. work.parent_id = $work_id AND
  64. work.filetype='file' AND
  65. props.visibility = '1' AND props.insert_user_id='".api_get_user_id()."' ";
  66. $query = Database::query($sql);
  67. //add tem to the zip file
  68. while ($not_deleted_file = Database::fetch_assoc($query)) {
  69. if (file_exists($sys_course_path.$_course['path'].'/'.$not_deleted_file['url'])) {
  70. $files[basename($not_deleted_file['url'])] = $not_deleted_file['title'];
  71. $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');
  72. }
  73. }
  74. }//end for other users
  75. if (!empty($files)) {
  76. //logging
  77. event_download(basename($work_data['title']).'.zip (folder)');
  78. //start download of created file
  79. $name = basename($work_data['title']).'.zip';
  80. if (Security::check_abs_path($temp_zip_file, api_get_path(SYS_ARCHIVE_PATH))) {
  81. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  82. @unlink($temp_zip_file);
  83. exit;
  84. }
  85. } else {
  86. exit;
  87. }
  88. /* Extra function (only used here) */
  89. function my_pre_add_callback($p_event, &$p_header) {
  90. global $files;
  91. if (isset($files[basename($p_header['stored_filename'])])) {
  92. $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])];
  93. return 1;
  94. }
  95. return 0;
  96. }
  97. /**
  98. * Return the difference between two arrays, as an array of those key/values
  99. * Use this as array_diff doesn't give the
  100. *
  101. * @param array $arr1 first array
  102. * @param array $arr2 second array
  103. * @return difference between the two arrays
  104. */
  105. function diff($arr1, $arr2) {
  106. $res = array();
  107. $r = 0;
  108. foreach ($arr1 as $av) {
  109. if (!in_array($av, $arr2)) {
  110. $res[$r] = $av;
  111. $r++;
  112. }
  113. }
  114. return $res;
  115. }