downloadfolder.inc.php 4.4 KB

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