downloadfolder.inc.php 11 KB

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