downloadfolder.inc.php 4.4 KB

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