downloadfolder.inc.php 7.4 KB

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