downloadfolder.inc.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Functions and main code for the download folder feature
  5. *
  6. * @package chamilo.work
  7. */
  8. $path = $_GET['path'];
  9. //prevent some stuff
  10. if (empty($path)) {
  11. $path = '/';
  12. }
  13. //zip library for creation of the zipfile
  14. require api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  15. //we need this path to clean it out of the zip file
  16. //I'm not using dirname as it gives too much problems (cfr. \)
  17. $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
  18. //place to temporarily stash the zipfiles
  19. $temp_zip_dir = $sys_course_path.$_course['path'].'/temp';
  20. //create the temp dir if it doesn't exist
  21. //or do a cleanup befor creating the zipfile
  22. if (!is_dir($temp_zip_dir)) {
  23. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  24. } else {
  25. //cleanup: check the temp dir for old files and delete them
  26. $handle = opendir($temp_zip_dir);
  27. while (false !== ($file = readdir($handle))) {
  28. if ($file != '.' && $file != '..') {
  29. //the "age" of the file in hours
  30. $Diff = (time() - filemtime("$temp_zip_dir/$file"))/60/60;
  31. //delete files older than 4 hours
  32. if ($Diff > 4) {
  33. unlink("$temp_zip_dir/$file");
  34. }
  35. }
  36. }
  37. closedir($handle);
  38. }
  39. //create zipfile of given directory
  40. $temp_zip_file = $temp_zip_dir."/".md5(time()).".zip";
  41. $zip_folder = new PclZip($temp_zip_file);
  42. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  43. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  44. //Put the files in the zip
  45. //2 possibilities: admins get all files and folders in the selected folder (except for the deleted ones)
  46. //normal users get only visible files that are in visible folders
  47. //admins are allowed to download invisible files
  48. if (is_allowed_to_edit()) {
  49. //folder we want to zip --> no longer used, deleted files are included too like this
  50. //$what_to_zip = $sys_course_path.$_course['path']."/document".$path;
  51. //creation of the zipped folder
  52. //$zip_folder->create($what_to_zip ,PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/document".$remove_dir );
  53. //set the path that will be used in the query
  54. if ($path == '/') {
  55. $querypath = ''; // to prevent ...path LIKE '//%'... in query
  56. } else {
  57. $querypath = $path;
  58. }
  59. //search for all files that are not deleted => visibility != 2
  60. $query = Database::query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility<>'2'");
  61. //add tem to the zip file
  62. while ($not_deleted_file = Database::fetch_assoc($query)) { //var_dump($sys_course_path.$_course['path'].'/'.$not_deleted_file['url']);exit();
  63. $zip_folder->add($sys_course_path.$_course['path'].'/'.$not_deleted_file['url'], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/work'.$remove_dir);
  64. }
  65. }
  66. //for other users, we need to create a zipfile with only visible files and folders
  67. else {
  68. if ($path == '/') {
  69. $querypath = ''; // to prevent ...path LIKE '//%'... in query
  70. } else {
  71. $querypath = $path;
  72. }
  73. //big problem: visible files that are in a hidden folder are included when we do a query for visiblity='v'!!!
  74. //so... I do it in a couple of steps:
  75. //1st: get all files that are visible in the given path
  76. $query = Database::query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility='1' AND props.lastedit_user_id='".api_get_user_id()."'");
  77. //add them to an array
  78. $all_visible_files_path = array();
  79. while ($all_visible_files = Database::fetch_assoc($query)) {
  80. $all_visible_files_path[] = $all_visible_files['url'];
  81. }
  82. //2nd: get all folders that are invisible in the given path
  83. $query2 = Database::query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility<>'1' AND props.lastedit_user_id='".api_get_user_id()."'");
  84. //if we get invisible folders, we have to filter out these results from all visible files we found
  85. if (Database::num_rows($query2) > 0) {
  86. //add tem to an array
  87. while ($invisible_folders = Database::fetch_assoc($query2)) {
  88. //3rd: get all files that are in the found invisible folder (these are "invisible" too)
  89. $query3 = Database::query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$invisible_folders['path']."/%' AND work.filetype='file' AND props.visibility='1' AND props.lastedit_user_id='".api_get_user_id()."'");
  90. //add tem to an array
  91. while ($files_in_invisible_folder = Database::fetch_assoc($query3)) {
  92. $files_in_invisible_folder_path[] = $files_in_invisible_folder['url'];
  93. }
  94. }
  95. //compare the array with visible files and the array with files in invisible folders
  96. //and keep the difference (= all visible files that are not in an invisible folder)
  97. $files_for_zipfile = diff((array) $all_visible_files_path, (array) $files_in_invisible_folder_path);
  98. } else {
  99. //no invisible folders found, so all visible files can be added to the zipfile
  100. $files_for_zipfile = $all_visible_files_path;
  101. }
  102. //add all files in our final array to the zipfile
  103. for ($i=0;$i<count($files_for_zipfile);$i++) {
  104. $zip_folder->add($sys_course_path.$_course['path'].'/'.$files_for_zipfile[$i], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/work'.$remove_dir);
  105. }
  106. }//end for other users
  107. //logging
  108. // launch event
  109. event_download(basename($path).'.zip (folder)');
  110. //start download of created file
  111. $name = basename($path).'.zip';
  112. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  113. @unlink($temp_zip_file);
  114. exit;
  115. /* Extra function (only used here) */
  116. /**
  117. * Return the difference between two arrays, as an array of those key/values
  118. * Use this as array_diff doesn't give the
  119. *
  120. * @param array $arr1 first array
  121. * @param array $arr2 second array
  122. * @return difference between the two arrays
  123. */
  124. function diff($arr1, $arr2) {
  125. $res = array();
  126. $r = 0;
  127. foreach ($arr1 as $av) {
  128. if (!in_array($av, $arr2)) {
  129. $res[$r] = $av;
  130. $r++;
  131. }
  132. }
  133. return $res;
  134. }