downloadfolder.inc.php 6.5 KB

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