downloadfolder.inc.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Functions and main code for the download folder feature
  5. *
  6. * @package chamilo.document
  7. */
  8. set_time_limit(0);
  9. require_once '../inc/global.inc.php';
  10. api_protect_course_script();
  11. $sysCoursePath = api_get_path(SYS_COURSE_PATH);
  12. $courseInfo = api_get_course_info();
  13. $courseId = api_get_course_int_id();
  14. $sessionId = api_get_session_id();
  15. $groupId = api_get_group_id();
  16. $courseCode = api_get_course_id();
  17. // Check if folder exists in current course.
  18. $documentInfo = DocumentManager::get_document_data_by_id(
  19. $_GET['id'],
  20. $courseCode,
  21. false,
  22. 0
  23. );
  24. if (!empty($sessionId)) {
  25. /* If no data found and session id exists
  26. try to look the file inside the session */
  27. if (empty($documentInfo)) {
  28. $documentInfo = DocumentManager::get_document_data_by_id(
  29. $_GET['id'],
  30. $courseCode,
  31. false,
  32. $sessionId
  33. );
  34. }
  35. }
  36. $path = $documentInfo['path'];
  37. if (empty($path)) {
  38. $path = '/';
  39. }
  40. // A student should not be able to download a root shared directory
  41. if (($path == '/shared_folder' ||
  42. $path == '/shared_folder_session_' . api_get_session_id()) &&
  43. (!api_is_allowed_to_edit() || !api_is_platform_admin())
  44. ) {
  45. api_not_allowed(true);
  46. exit;
  47. }
  48. // Zip library for creation of the zip file.
  49. require api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  50. // Creating a ZIP file.
  51. $tempZipFile = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  52. $zip = new PclZip($tempZipFile);
  53. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  54. $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  55. // We need this path to clean it out of the zip file
  56. // I'm not using dir name as it gives too much problems (cfr.)
  57. $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
  58. // Put the files in the zip
  59. // 2 possibilities: Admins get all files and folders in the selected folder (except for the deleted ones)
  60. // Normal users get only visible files that are in visible folders
  61. // Admins are allowed to download invisible files
  62. if (api_is_allowed_to_edit()) {
  63. // Set the path that will be used in the query
  64. if ($path == '/') {
  65. $querypath = ''; // To prevent ...path LIKE '//%'... in query
  66. } else {
  67. $querypath = $path;
  68. }
  69. $querypath = Database::escape_string($querypath);
  70. // Search for all files that are not deleted => visibility != 2
  71. $sql = "SELECT path, id_session
  72. FROM $doc_table AS docs INNER JOIN $prop_table AS props
  73. ON
  74. docs.id = props.ref AND
  75. docs.c_id = props.c_id
  76. WHERE
  77. props.tool ='".TOOL_DOCUMENT."' AND
  78. docs.path LIKE '".$querypath."/%' AND
  79. docs.filetype = 'file' AND
  80. props.visibility <> '2' AND
  81. props.to_group_id = ".$groupId." AND
  82. props.id_session IN ('0', '$sessionId') AND
  83. docs.c_id = ".$courseId." ";
  84. $sql.= DocumentManager::getSessionFolderFilters($querypath, $sessionId);
  85. $query = Database::query($sql);
  86. // Add tem to the zip file
  87. while ($not_deleted_file = Database::fetch_assoc($query)) {
  88. // Filtering folders and
  89. if (strpos($not_deleted_file['path'], 'chat_files') > 0 ||
  90. strpos($not_deleted_file['path'], 'shared_folder') > 0
  91. ) {
  92. if (!empty($sessionId)) {
  93. if ($not_deleted_file['id_session'] != $sessionId) {
  94. continue;
  95. }
  96. }
  97. }
  98. $zip->add(
  99. $sysCoursePath.$courseInfo['path'].'/document'.$not_deleted_file['path'],
  100. PCLZIP_OPT_REMOVE_PATH,
  101. $sysCoursePath.$courseInfo['path'].'/document'.$remove_dir
  102. );
  103. }
  104. } else {
  105. // For other users, we need to create a zip file with only visible files and folders
  106. if ($path == '/') {
  107. $querypath = ''; // To prevent ...path LIKE '//%'... in query
  108. } else {
  109. $querypath = $path;
  110. }
  111. /* A big problem: Visible files that are in a hidden folder are
  112. included when we do a query for visiblity='v'
  113. So... I do it in a couple of steps:
  114. 1st: Get all files that are visible in the given path
  115. */
  116. $querypath = Database::escape_string($querypath);
  117. $sql = "SELECT path, id_session
  118. FROM $doc_table AS docs INNER JOIN $prop_table AS props
  119. ON
  120. docs.id = props.ref AND
  121. docs.c_id = props.c_id
  122. WHERE
  123. docs.c_id = $courseId AND
  124. props.tool = '".TOOL_DOCUMENT."' AND
  125. docs.path LIKE '".$querypath."/%' AND
  126. props.visibility = '1' AND
  127. docs.filetype = 'file' AND
  128. props.id_session IN ('0', '$sessionId') AND
  129. props.to_group_id = ".$groupId;
  130. $sql.= DocumentManager::getSessionFolderFilters($querypath, $sessionId);
  131. $query = Database::query($sql);
  132. // Add them to an array
  133. while ($all_visible_files = Database::fetch_assoc($query)) {
  134. if (strpos($all_visible_files['path'], 'chat_files') > 0 ||
  135. strpos($all_visible_files['path'], 'shared_folder') > 0
  136. ) {
  137. if (!empty($sessionId)) {
  138. if ($all_visible_files['id_session'] != $sessionId) {
  139. continue;
  140. }
  141. }
  142. }
  143. $all_visible_files_path[] = $all_visible_files['path'];
  144. }
  145. // 2nd: Get all folders that are invisible in the given path
  146. $sql = "SELECT path
  147. FROM $doc_table AS docs INNER JOIN $prop_table AS props
  148. ON
  149. docs.id = props.ref AND
  150. docs.c_id = props.c_id
  151. WHERE
  152. docs.c_id = $courseId AND
  153. props.tool = '".TOOL_DOCUMENT."' AND
  154. docs.path LIKE '".$querypath."/%' AND
  155. props.visibility <> '1' AND
  156. props.id_session IN ('0', '$sessionId') AND
  157. docs.filetype = 'folder'";
  158. $query2 = Database::query($sql);
  159. // If we get invisible folders, we have to filter out these results from all visible files we found
  160. if (Database::num_rows($query2) > 0) {
  161. // Add item to an array
  162. while ($invisible_folders = Database::fetch_assoc($query2)) {
  163. //3rd: Get all files that are in the found invisible folder (these are "invisible" too)
  164. $sql = "SELECT path
  165. FROM $doc_table AS docs INNER JOIN $prop_table AS props
  166. ON
  167. docs.id = props.ref AND
  168. docs.c_id = props.c_id
  169. WHERE
  170. docs.c_id = $courseId AND
  171. props.tool ='".TOOL_DOCUMENT."' AND
  172. docs.path LIKE '".$invisible_folders['path']."/%' AND
  173. docs.filetype ='file' AND
  174. props.id_session IN ('0', '$sessionId') AND
  175. props.visibility ='1'";
  176. $query3 = Database::query($sql);
  177. // Add tem to an array
  178. while ($files_in_invisible_folder = Database::fetch_assoc($query3)) {
  179. $files_in_invisible_folder_path[] = $files_in_invisible_folder['path'];
  180. }
  181. }
  182. // Compare the array with visible files and the array with files in invisible folders
  183. // and keep the difference (= all visible files that are not in an invisible folder)
  184. $files_for_zipfile = diff((array)$all_visible_files_path, (array)$files_in_invisible_folder_path);
  185. } else {
  186. // No invisible folders found, so all visible files can be added to the zipfile
  187. $files_for_zipfile = $all_visible_files_path;
  188. }
  189. // Add all files in our final array to the zipfile
  190. for ($i = 0; $i < count($files_for_zipfile); $i++) {
  191. $zip->add(
  192. $sysCoursePath . $courseInfo['path'] . '/document' . $files_for_zipfile[$i],
  193. PCLZIP_OPT_REMOVE_PATH,
  194. $sysCoursePath . $courseInfo['path'] . '/document' . $remove_dir
  195. );
  196. }
  197. } // end for other users
  198. // Launch event
  199. event_download(($path == '/') ? 'documents.zip (folder)' : basename($path).'.zip (folder)');
  200. // Start download of created file
  201. $name = ($path == '/') ? 'documents.zip' : $documentInfo['title'].'.zip';
  202. if (Security::check_abs_path($tempZipFile, api_get_path(SYS_ARCHIVE_PATH))) {
  203. DocumentManager::file_send_for_download($tempZipFile, true, $name);
  204. @unlink($tempZipFile);
  205. exit;
  206. }
  207. /**
  208. * Returns the difference between two arrays, as an array of those key/values
  209. * Use this as array_diff doesn't give the
  210. *
  211. * @param array $arr1 first array
  212. * @param array $arr2 second array
  213. *
  214. * @return array difference between the two arrays
  215. */
  216. function diff($arr1, $arr2) {
  217. $res = array();
  218. $r = 0;
  219. foreach ($arr1 as & $av) {
  220. if (!in_array($av, $arr2)) {
  221. $res[$r] = $av;
  222. $r++;
  223. }
  224. }
  225. return $res;
  226. }